In any data-centric work, we have a deep understanding of SQL is the key to success, although this is not the most interesting part of the job. In fact, in addition to the SELECT FROM WHERE GROUP BY ORDER BY, SQL and many more methods. The more you know the function, the easier it is the content and operation of the desired query.
The authors wish to learn and communicate in this article the following two things:
SQL function other than 1) to study and teach some basic functions
2) explore some SQL practice interview questions
* Problem in this article only from Leetcode
Question 1: the second-highest salary
Write a SQL query used to obtain the second-highest salary from the Employee table. For example, the Employee table given below, the query should return 200 as the second high salary. If there is no second-highest salary, the query should return null.
+----+--------+
| Id | Salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+
1) Solution A : Use IFNULL , OFFSET
-
IFNULL (expression, alt): If null, then ifnull () Returns the specified value, otherwise return the expected value . If there is no second-highest salary, we'll use it returns null.
-
OFFSET: used in conjunction with the offset clause ignores the specified ORDERBY first n rows . This can be useful, because you want to get the second row (the second highest salary)
SELECT
IFNULL(
(SELECT DISTINCT Salary
FROM Employee
ORDER BY Salary DESC
LIMIT 1 OFFSET 1
), null) as SecondHighestSalary
FROM Employee
LIMIT 1
2) Solution B : using MAX ()
This query represents the selected MAX salary is not equal to the maximum salary, which is equal to select the second-highest salary.
SELECT MAX(salary) AS SecondHighestSalary
FROM Employee
WHERE salary != (SELECT MAX(salary) FROM Employee)
Question 2: duplicate e-mail
Writing SQL queries to find all duplicate e-mail named Person of the table.
+----+---------+
| Id | Email |
+----+---------+
| 1 | [email protected] |
| 2 | [email protected] |
| 3 | [email protected] |
+----+---------+
1) Solution A : subquery COUNT ()
First, create a sub-query that displays the number of times the frequency of each email. Then filtered subquery where the count is greater than 1.
SELECT Email
FROM (
SELECT Email, count(Email) AS count
FROM Person
GROUP BY Email
) as email_count
WHERE count > 1
2) Solution B: HAVING clause
-
HAVING clause is, in essence, you can WHERE statement and aggregation (GROUP BY) used in combination.
SELECT Email
FROM Person
GROUP BY Email
HAVING count(Email) > 1
Question 3: Temperature rise
Given a weather table below, write a SQL query to find before its higher temperatures compared to ID all dates (yesterday) date.
+---------+------------------+------------------+
| Id(INT) | RecordDate(DATE) | Temperature(INT) |
+---------+------------------+------------------+
| 1 | 2015-01-01 | 10 |
| 2 | 2015-01-02 | 25 |
| 3 | 2015-01-03 | 20 |
| 4 | 2015-01-04 | 30 |
+---------+------------------+------------------+
Solution: DATEDIFF ()
-
DATEDIFF is the difference between two dates, for today we will make sure that the temperature and the temperature yesterday was compared.
In simple terms, the query is to select the temperature at a given date is higher than the temperature of yesterday's ID.
SELECT DISTINCT a.Id
FROM Weather a, Weather b
WHERE a.Temperature > b.Temperature
AND DATEDIFF(a.Recorddate, b.Recorddate) = 1
Question 4: Sector highest salary
Employees The following table contains all employees. Each employee has an ID, a paycheck, there is a department ID column.
+----+-------+--------+--------------+
| Id | Name | Salary | DepartmentId |
+----+-------+--------+--------------+
| 1 | Joe | 70000 | 1 |
| 2 | Jim | 90000 | 1 |
| 3 | Henry | 80000 | 2 |
| 4 | Sam | 60000 | 2 |
| 5 | Max | 90000 | 1 |
+----+-------+--------+--------------+
The following table contains the departments in all sectors of the company.
+----+----------+
| Id | Name |
+----+----------+
| 1 | IT |
| 2 | Sales |
+----+----------+
Write SQL queries to find the highest salary in each department employees. For the above two tables, your SQL query should return (order of the rows does not matter) the following line.
+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT | Max | 90000 |
| IT | Jim |90000 |
| Sales | Henry | 80000 |
+------------+----------+--------+
Solution : the IN clause
-
IN clause allows you to use multiple OR clauses in the WHERE clause. For example, WHERE country = 'Canada' or country = 'USA' and WHERE country IN ( 'Canada', 'USA') the same.
-
In this case, we want to filter the department table to display only the maximum salary for each department (ie DepartmentId). Then, we can join two tables, wherein the DepartmentId filtered and Salary Department table.
SELECT
Department.name AS 'Department',
Employee.name AS 'Employee',
Salary
FROM Employee
INNER JOIN Department ON Employee.DepartmentId = Department.Id
WHERE (DepartmentId , Salary)
IN
( SELECT
DepartmentId, MAX(Salary)
FROM
Employee
GROUP BY DepartmentId
)
Question 5: swap seats
Mary is a high school teacher, she has a seating chart, above stores the student's name and the corresponding seat ID. Column ID is discrete increments, Mary wanted to swap seats adjacent to students.
You can write SQL queries to the output of Mary?
+---------+---------+
| id | student |
+---------+---------+
| 1 | Abbot |
| 2 | Doris |
| 3 | Emerson |
| 4 | Green |
| 5 | Jeames |
+---------+---------+
For the sample input, output:
+---------+---------+
| id | student |
+---------+---------+
| 1 | Doris |
| 2 | Abbot |
| 3 | Green |
| 4 | Emerson |
| 5 | Jeames |
+---------+---------+
Note: If the number of students is odd, there is no need to change the last seat.
Solution : the CASE the WHEN
-
CASE WHEN THEN statements can be regarded as an IF statement coding.
-
The first checks whether the number of rows WHEN statement is odd, if the number of lines is odd, make sure that the same ID number.
-
The second WHEN statements for each id is incremented (e.g., 1,3,5 becomes 2,4,6)
-
Similarly, the third statement WHEN decremented each id (2,4,6 becomes 1,3,5)
SELECT
CASE
WHEN((SELECT MAX(id) FROM seat)%2 = 1) AND id = (SELECT MAX(id) FROM seat) THENid
WHEN id%2 = 1 THEN id + 1
ELSE id - 1
END AS id, student
FROM seat
ORDER BY id
These are all solutions, if there are any questions or other comments, please comment!