+-------------+---------+ | Column Name | Type | +-------------+---------+ | PersonId | int | | FirstName | varchar | | LastName | varchar | +-------------+---------+
表Address, AddressId为主键
1 2 3 4 5 6 7 8
+-------------+---------+ | Column Name | Type | +-------------+---------+ | AddressId | int | | PersonId | int | | City | varchar | | State | varchar | +-------------+---------+
写sql查询FirstName, LastName, City, State
包含Person中每个人,无论他是否有对应地址
1 2
select p.FirstName,p.LastName,a.City,a.State from Person p leftjoin Address a on p.PersonId=a.PersonId
selectmax(Salary) from ( select t1.Salary from Employee t1 where (selectcount(distinct t2.Salary)=N-1from Employee t2 where t2.Salary>t1.Salary) ) t
Count+limit+DISTINCT:
1 2 3 4 5 6
SELECT e1.Salary FROM (SELECTDISTINCT Salary FROM Employee)e1 WHERE ( SELECTCOUNT(Salary) FROM (SELECTDISTINCT Salary FROM Employee)e2 WHERE e1.Salary<e2.Salary )=N-1 LIMIT1
select t.Score,t.Rank from ( select t1.Score,(selectcount(distinct t2.Score) from Scores t2 where t2.Score>=t1.Score) asRank from Scores t1 ) t orderby t.Score desc
Pre-uniqued:
1 2 3 4 5
SELECT Score, (SELECTcount(*) FROM (SELECTdistinct Score s FROM Scores) tmp WHERE s >= Score) Rank FROM Scores ORDERBY Score desc
+-----------+ | Customers | +-----------+ | Henry | | Max | +-----------+
优化版子查询:
1 2 3
SELECT A.Name as Customers from Customers A WHERENOTEXISTS (SELECT1FROM Orders B WHERE A.Id = B.CustomerId limit1)
各部门最高工资
表Employee
1 2 3 4 5 6 7 8
+----+-------+--------+--------------+ | Id | Name | Salary | DepartmentId | +----+-------+--------+--------------+ | 1 | Joe | 70000 | 1 | | 2 | Henry | 80000 | 2 | | 3 | Sam | 60000 | 2 | | 4 | Max | 90000 | 1 | +----+-------+--------+--------------+
表Department
1 2 3 4 5 6
+----+----------+ | Id | Name | +----+----------+ | 1 | IT | | 2 | Sales | +----+----------+
示例返回
1 2 3 4 5 6
+------------+----------+--------+ | Department | Employee | Salary | +------------+----------+--------+ | IT | Max | 90000 | | Sales | Henry | 80000 | +------------+----------+--------+
相关子查询:
1 2 3
select d.Name,t.Name,t.Salary from Department d,(select e1.Name,e1.Salary,e1.DepartmentId from Employee e1,(select DepartmentId,max(Salary) as Salary from Employee groupby DepartmentId) e2 where e1.Salary=e2.Salary and e1.DepartmentId=e2.DepartmentId) t where d.Id=t.DepartmentId
优化版子查询:
1 2 3 4 5 6 7
SELECT D.Name as Department,A.Name as Employee,A.Salary FROM Employee A, Department D WHERE A.DepartmentId = D.Id ANDNOTEXISTS (SELECT1FROM Employee B WHERE B.Salary > A.Salary AND A.DepartmentId = B.DepartmentId)
各部门前三高工资
表Employee
1 2 3 4 5 6 7 8 9 10
+----+-------+--------+--------------+ | Id | Name | Salary | DepartmentId | +----+-------+--------+--------------+ | 1 | Joe | 70000 | 1 | | 2 | Henry | 80000 | 2 | | 3 | Sam | 60000 | 2 | | 4 | Max | 90000 | 1 | | 5 | Janet | 69000 | 1 | | 6 | Randy | 85000 | 1 | +----+-------+--------+--------------+
表Department
1 2 3 4 5 6
+----+----------+ | Id | Name | +----+----------+ | 1 | IT | | 2 | Sales | +----+----------+
示例返回
1 2 3 4 5 6 7 8 9
+------------+----------+--------+ | Department | Employee | Salary | +------------+----------+--------+ | IT | Max | 90000 | | IT | Randy | 85000 | | IT | Joe | 70000 | | Sales | Henry | 80000 | | Sales | Sam | 60000 | +------------+----------+--------+
相关子查询:
1 2 3 4 5
select d.Name,e.Name,e.Salary from Department d,Employee e where d.Id=e.DepartmentId and (selectcount(distinct e1.Salary) from Employee e1 where e1.DepartmentId=e.DepartmentId and e1.Salary>e.Salary) < 3 orderby d.Id,e.Salary desc
不使用order by:
1 2 3 4 5 6 7 8 9
select d.Name Department, e1.Name Employee, e1.Salary from Employee e1 join Department d on e1.DepartmentId = d.Id where3 > (selectcount(distinct(e2.Salary)) from Employee e2 where e2.Salary > e1.Salary and e1.DepartmentId = e2.DepartmentId );