Leetcode SQL Wrong Question Set

20200329

Question 1

Given a salary table, as shown below, there are values ​​for m = male and f = female. Swap all f and m values ​​(for example, change all f values ​​to m and vice versa). Only one Update statement is required, and there is no intermediate temporary table.

Note that you must only write one Update statement, please do not write any Select statement.

answer

update salary set sex = IF(sex = 'm', 'f', 'm')

Knowledge point

You can use the if function to determine the value of a column for data exchange. When there is no where condition, the entire table is updated.
IF function usage
IF (expr, v1, v2) If the expression expr holds, return the result v1; otherwise, return the result v2

Question 2

Write a SQL query to get the second highest salary (Salary) in the Employee table.

answer

Method 1: Sort Salary in descending order after deduplication, and then return the second record to get the second largest value.
Perhaps there is only one Salary value, which will return null

select (select distinct Salary from Employee limit 1,1) as secondhighestsalary;

Method 2: Find the maximum value smaller than the maximum value of the field, which is the second largest value

select MAX(Salary) as secondhighestsalary from Employee  
where Salary <(select MAX(Salary) from Employee );

Knowledge point

limit N     # 返回 N 条记录
offset M    # 跳过 M 条记录,M 默认为 0
limit M,N   # 相当于 limit N offset M,从第 M 条记录开始,返回 N 条记录

20200412

Question 3

Find employee number emp_no whose salary has increased by more than 15 times and the corresponding number of increases t

CREATE TABLE salaries (

emp_no int(11) NOT NULL,

salary int(11) NOT NULL,

from_date date NOT NULL,

to_date date NOT NULL,

PRIMARY KEY (emp_no,from_date));

answer:

select emp_no, count(emp_no)as t from salaries
group by emp_no having t>15;

Knowledge points:

This question should pay attention to the following four points:
1. The number of records of the same emp_no value can be counted using the COUNT () function and the GROUP BY statement
2. According to the meaning of the question, the number of output increases is t, so use the AS statement to count (emp_no) The value of is converted to t
3. Since the COUNT () function cannot be used in the WHERE statement, the HAVING statement is used to limit the condition of t> 15. 4. There is a misunderstanding in the end, and the increase is more than 15 times. The corresponding number of records in the salaries should be More than 16 (from the second record to count as the first increase), but for the sake of simplicity, the first record is regarded as the first increase, so let t> 15 to
pay attention: strictly speaking, the next The salary is higher than this article, but it is regarded as an increase, but as long as there is a record in this question, it is regarded as an increase.

doubt:

This question should assume that every change is increasing and not repeating.

Question 4

Find out the current (to_date = '9999-01-01') salary status of all employees. Only display the same salary once and display it in reverse order

CREATE TABLE salaries (

emp_no int(11) NOT NULL,

salary int(11) NOT NULL,

from_date date NOT NULL,

to_date date NOT NULL,

PRIMARY KEY (emp_no,from_date));

answer:

Method 1: Applicable small table

select distinct salary from salaries 
where to_date='9999-01-01' order by salary desc

Method 2: Apply to large tables

select  salary from salaries 
where to_date='9999-01-01' group by salary
order by salary desc

Knowledge point

1. Large tables are generally not efficient with distinct. When large amounts of data are prohibited, it is recommended to use group by to solve duplicate problems.
2. WHERE statement Before the GROUP BY statement, SQL will calculate the WHERE statement before grouping.
HAVING statement After GROUP BY statement, SQL will calculate HAVING statement after grouping
is filter group where filter rows

insufficient:

Order by statement needs to be enhanced

Question 5

Get the current salary of the current manager in all departments, given dept_no, emp_no and salary, currently means to_date = '9999-01-01'
CREATE TABLE dept_manager(
dept_nochar (4) NOT NULL,
emp_noint (11) NOT NULL,
from_datedate NOT NULL,
to_datedate NOT NULL,
PRIMARY KEY ( emp_no, dept_no));
CREATE TABLE salaries(
emp_noint (11) NOT NULL,
salaryint (11) NOT NULL,
from_datedate NOT NULL,
to_datedate NOT NULL,
PRIMARY KEY ( emp_no, from_date));

answer

select d.dept_no,d.emp_no,s.salary 
from salaries as s inner join dept_manager as d
on s.emp_no = d.emp_no
and s.to_date='9999-01-01' 
and d.to_date='9999-01-01'

Error solution

select dept_no,emp_no,salary from dept_manager 
where to_date='9999-01-01' 
from salaries inner join dept_manager
on salaries.emp_no = dept_manager.emp_no

Resolution:

Connect first then where

Question 6

Get all non-manager employees emp_no

CREATE TABLE dept_manager (

dept_no char(4) NOT NULL,

emp_no int(11) NOT NULL,

from_date date NOT NULL,

to_date date NOT NULL,

PRIMARY KEY (emp_no,dept_no));

CREATE TABLE employees (

emp_no int(11) NOT NULL,

birth_date date NOT NULL,

first_name varchar(14) NOT NULL,

last_name varchar(16) NOT NULL,

gender char(1) NOT NULL,

hire_date date NOT NULL,

PRIMARY KEY (emp_no));

answer:

Method 1: Use LEFT JOIN to connect two tables, then select the emp_no record corresponding to dept_no value of NULL from this table

select employees.emp_no from employees
left join dept_manager
on employees.emp_no = dept_manager.emp_no
where dept_no is null;

Method 2: Use NOT IN to select emp_no records in employees but not in dept_manager

select emp_no from employees
where emp_no not in (
select emp_no from dept_manager)

insufficient

The statement for finding null values ​​is: where dept_no is null

Question 7

Get the current manager of all employees. If the current manager is its own, the result is not displayed. The current representation is to_date = '9999-01-01'.

The first column gives the current employee's emp_no, and the second column gives the manager_no corresponding to its manager.

CREATE TABLE dept_emp (

emp_no int(11) NOT NULL,

dept_no char(4) NOT NULL,

from_date date NOT NULL,

to_date date NOT NULL,

PRIMARY KEY (emp_no,dept_no));

CREATE TABLE dept_manager (

dept_no char(4) NOT NULL,

emp_no int(11) NOT NULL,

from_date date NOT NULL,

to_date date NOT NULL,

PRIMARY KEY (emp_no,dept_no));

answer

select de.emp_no,dm.emp_no AS manager_no 
from dept_manager AS dm,dept_emp AS de
where de.emp_no <> dm.emp_no
and de.dept_no = dm.dept_no
and dm.to_date='9999-01-01';

Parsing

1. Because you want to export your own manager and know that you and the manager ’s department should be the same, there are restrictions de.dept_no = dm.dept_no
2.select… from… where… can be directly connected to the table
3. In order to enhance the code readability , Replace dept_emp with the alias de, and dept_manager with dm

Question 8

Corresponding salary

CREATE TABLE dept_emp (

emp_no int(11) NOT NULL,

dept_no char(4) NOT NULL,

from_date date NOT NULL,

to_date date NOT NULL,

PRIMARY KEY (emp_no,dept_no));

CREATE TABLE salaries (

emp_no int(11) NOT NULL,

salary int(11) NOT NULL,

from_date date NOT NULL,

to_date date NOT NULL,

PRIMARY KEY (emp_no,from_date));

answer

select d.dept_no, s.emp_no, max(s.salary)
from dept_emp as d,salaries as s
where d.emp_no = s.emp_no
and d.to_date = '9999-01-01' 
and s.to_date = '9999-01-01' 
group by dept_no

Parsing

1. Connect the two tables with INNER JOIN first, the restriction is that the emp_no of the two tables is the same, that is d.emp_no = s.emp_no;
2. Use GROUP BY d.dept_no to divide each department into a group, use MAX ( ) Function selects the highest paid person in each group

20200413

Question 9

Title description

Get grouped by title from the titles table, the number of each group is greater than or equal to 2, give the title and the corresponding number t.
Note that duplicate emp_no is ignored.
CREATE TABLE IF NOT EXISTS titles(
emp_noint (11) NOT NULL,
titlevarchar (50) NOT NULL,
from_datedate NOT NULL,
to_datedate DEFAULT NULL);

answer

method one

select title ,count(distinct emp_no) as t
from titles
group by title
having t>=2

Method Two

select title,count(title) as tfrom (select distinct emp_no,title,from_date,to_date     from titles )group by title having t>=2;

doubt:

In this question, is the number of titles = emp_no = number of records?

Question 10

Title description

Find all employee information in the employees table where emp_no is odd and last_name is not Mary, and sort them in reverse order of hire_date

CREATE TABLE employees (

emp_no int(11) NOT NULL,

birth_date date NOT NULL,

first_name varchar(14) NOT NULL,

last_name varchar(16) NOT NULL,

gender char(1) NOT NULL,

hire_date date NOT NULL,

PRIMARY KEY (emp_no))

answer

select * from employees
where emp_no % 2 =1
and last_name != 'Mary'
order by hire_date desc

Resolution:

There are three points to note:
1. The employee number is an odd number, then the emp_no balance (%) should be 1
2. The last_name is not Mary, and Mary should use '', use '! = 'Or' <> 'means
3. Arrange in reverse order according to hire_date, use desc

Question 11

Title description

Calculate the average salary corresponding to the current (to_date = '9999-01-01') salary of employees corresponding to each title type. The result gives the title and the average salary avg.
CREATE TABLE salaries(
emp_noint (11) NOT NULL,
salaryint (11) NOT NULL,
from_datedate NOT NULL,
to_datedate NOT NULL,
PRIMARY KEY ( emp_no, from_date));
CREATE TABLE IF NOT EXISTS “titles” (
emp_noint (11) NOT NULL,
titlevarchar ( 50) NOT NULL,
from_datedate NOT NULL,
to_datedate DEFAULT NULL);

answer

select title ,avg(salary) as avg
from titles a inner join salaries b
on a.emp_no= b.emp_no and a.to_date= '9999-01-01'  and b.to_date = '9999-01-01'
group by a.title 
Published 6 original articles · liked 0 · visits 331

Guess you like

Origin blog.csdn.net/linph174/article/details/105176849