1. 兩表合併查詢(Combine Two Tables)
題目描述:
Table: Person
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| PersonId | int |
| FirstName | varchar |
| LastName | varchar |
+-------------+---------+
PersonId is the primary key column for this table.
Table: Address
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| AddressId | int |
| PersonId | int |
| City | varchar |
| State | varchar |
+-------------+---------+
AddressId is the primary key column for this table.
Write a SQL query for a report that provides the following information for each person in the Person table, regardless if there is an address for each of those people:
FirstName, LastName, City, State
這個還是挺簡單的,直接上:
select Person.FirstName,Person.LastName,Address.City,Address.State from Person left join Address on Person.PersonId= Address.PersonId;
描述:
SQL中常見的JOIN
left join(左聯接) 返回包括左表中的所有記錄和右表中聯結字段相等的記錄
right join(右聯接) 返回包括右表中的所有記錄和左表中聯結字段相等的記錄
inner join(等值連接) 只返回兩個表中聯結字段相等的行
上圖:
2. 第二高薪(Second Highest Salary)
Write a SQL query to get the second highest salary from the Employee table.
+----+--------+
| Id | Salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+
For example, given the above Employee table, the query should return 200 as the second highest salary. If there is no second highest salary, then the query should return null.
+---------------------+
| SecondHighestSalary |
+---------------------+
| 200 |
+---------------------+
找出第二高的,也就是在除去最高的剩餘裏面找出最高的;
select max(Salary) as SecondHighestSalary from Employee where Salary not in (select max(Salary) from Employee);
3. 掙錢比經理多(Employees Earning More Than Their Managers)
題目描述:
The Employee table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id.
+----+-------+--------+-----------+
| Id | Name | Salary | ManagerId |
+----+-------+--------+-----------+
| 1 | Joe | 70000 | 3 |
| 2 | Henry | 80000 | 4 |
| 3 | Sam | 60000 | NULL |
| 4 | Max | 90000 | NULL |
+----+-------+--------+-----------+
Given the Employee table, write a SQL query that finds out employees who earn more than their managers. For the above table, Joe is the only employee who earns more than his manager.
+----------+
| Employee |
+----------+
| Joe |
+----------+
對於本題,Joe的經理是編號爲3的Sam,Henry的經理爲編號爲4的Max,按照題目要求,Joe的工資是高於他的經理Sam的(7K>6K),所以本例的目的是返回Joe:
類似於兩表查詢:
select name as Employee from Employee e1 where Salary > (select Salary from Employee e2 where e1.ManagerId=e2.Id)
或者:
select name as Employee from Employee e1
where exists (select * from Employee e2 where e2.Id=e1.ManagerId and e1.Salary>e2.Salary )
建議寫exists語句時,子查詢中直接用*,而不用對列進行任何函數操作,避免碰到官方bug,
4. 重複值查詢
題目描述:
Write a SQL query to find all duplicate emails in a table named Person.
+----+---------+
| Id | Email |
+----+---------+
| 1 | [email protected] |
| 2 | [email protected] |
| 3 | [email protected] |
+----+---------+
For example, your query should return the following for the above table:
+---------+
| Email |
+---------+
| [email protected] |
+---------+
Note: All emails are in lowercase.
這個比較簡單:
Select Email from Person group by Email having count(Email)>1;
這裏注意三點:
- group by:常規用法是配合聚合函數,利用分組信息進行統計,常見的是配合max等聚合函數篩選數據後分析,以及配合having進行篩選後過濾。
- having:having字句可以讓我們篩選成組後的各種數據,where字句在聚合前先篩選記錄,也就是說作用在group by和having字句前。
- count():它返回檢索行的數目, 不論其是否包含 NULL值。