mirror of https://github.com/dunwu/db-tutorial.git
leecode 数据库题
parent
32317e98f1
commit
c6e96ce3ef
|
@ -0,0 +1,30 @@
|
|||
-- 有一个courses 表 ,有: student (学生) 和 class (课程)。
|
||||
--
|
||||
-- 请列出所有超过或等于5名学生的课。
|
||||
--
|
||||
-- 例如,表:
|
||||
--
|
||||
-- +---------+------------+
|
||||
-- | student | class |
|
||||
-- +---------+------------+
|
||||
-- | A | Math |
|
||||
-- | B | English |
|
||||
-- | C | Math |
|
||||
-- | D | Biology |
|
||||
-- | E | Math |
|
||||
-- | F | Computer |
|
||||
-- | G | Math |
|
||||
-- | H | Math |
|
||||
-- | I | Math |
|
||||
-- +---------+------------+
|
||||
-- 应该输出:
|
||||
--
|
||||
-- +---------+
|
||||
-- | class |
|
||||
-- +---------+
|
||||
-- | Math |
|
||||
-- +---------+
|
||||
-- Note:
|
||||
-- 学生在每个课中不应被重复计算。
|
||||
|
||||
SELECT class FROM courses GROUP BY class HAVING COUNT(DISTINCT student)>4;
|
|
@ -0,0 +1,33 @@
|
|||
-- 表1: Person
|
||||
--
|
||||
-- +-------------+---------+
|
||||
-- | 列名 | 类型 |
|
||||
-- +-------------+---------+
|
||||
-- | PersonId | int |
|
||||
-- | FirstName | varchar |
|
||||
-- | LastName | varchar |
|
||||
-- +-------------+---------+
|
||||
-- PersonId 是上表主键
|
||||
-- 表2: Address
|
||||
--
|
||||
-- +-------------+---------+
|
||||
-- | 列名 | 类型 |
|
||||
-- +-------------+---------+
|
||||
-- | AddressId | int |
|
||||
-- | PersonId | int |
|
||||
-- | City | varchar |
|
||||
-- | State | varchar |
|
||||
-- +-------------+---------+
|
||||
-- AddressId 是上表主键
|
||||
--
|
||||
--
|
||||
-- 编写一个 SQL 查询,满足条件:无论 person 是否有地址信息,都需要基于上述两表提供 person 的以下信息:
|
||||
--
|
||||
--
|
||||
--
|
||||
-- FirstName, LastName, City, State
|
||||
|
||||
SELECT Person.FirstName, Person.LastName, Address.City, Address.State
|
||||
FROM Person
|
||||
LEFT JOIN Address
|
||||
ON Person.PersonId = Address.PersonId;
|
|
@ -0,0 +1,36 @@
|
|||
-- 某网站包含两个表,Customers 表和 Orders 表。编写一个 SQL 查询,找出所有从不订购任何东西的客户。
|
||||
--
|
||||
-- Customers 表:
|
||||
--
|
||||
-- +----+-------+
|
||||
-- | Id | Name |
|
||||
-- +----+-------+
|
||||
-- | 1 | Joe |
|
||||
-- | 2 | Henry |
|
||||
-- | 3 | Sam |
|
||||
-- | 4 | Max |
|
||||
-- +----+-------+
|
||||
-- Orders 表:
|
||||
--
|
||||
-- +----+------------+
|
||||
-- | Id | CustomerId |
|
||||
-- +----+------------+
|
||||
-- | 1 | 3 |
|
||||
-- | 2 | 1 |
|
||||
-- +----+------------+
|
||||
-- 例如给定上述表格,你的查询应返回:
|
||||
--
|
||||
-- +-----------+
|
||||
-- | Customers |
|
||||
-- +-----------+
|
||||
-- | Henry |
|
||||
-- | Max |
|
||||
-- +-----------+
|
||||
|
||||
SELECT Name AS Customers FROM Customers c
|
||||
WHERE c.Id NOT IN (SELECT DISTINCT CustomerId FROM Orders);
|
||||
|
||||
SELECT Name AS Customers
|
||||
FROM Customers
|
||||
INNER JOIN Orders
|
||||
ON Customers.Id != Orders.CustomerId;
|
|
@ -0,0 +1,33 @@
|
|||
-- Employee 表包含所有员工,他们的经理也属于员工。每个员工都有一个 Id,此外还有一列对应员工的经理的 Id。
|
||||
--
|
||||
-- +----+-------+--------+-----------+
|
||||
-- | Id | Name | Salary | ManagerId |
|
||||
-- +----+-------+--------+-----------+
|
||||
-- | 1 | Joe | 70000 | 3 |
|
||||
-- | 2 | Henry | 80000 | 4 |
|
||||
-- | 3 | Sam | 60000 | NULL |
|
||||
-- | 4 | Max | 90000 | NULL |
|
||||
-- +----+-------+--------+-----------+
|
||||
-- 给定 Employee 表,编写一个 SQL 查询,该查询可以获取收入超过他们经理的员工的姓名。
|
||||
-- 在上面的表格中,Joe 是唯一一个收入超过他的经理的员工。
|
||||
--
|
||||
-- +----------+
|
||||
-- | Employee |
|
||||
-- +----------+
|
||||
-- | Joe |
|
||||
-- +----------+
|
||||
|
||||
-- 以下 3 种解法,由上至下,处理速度越来越慢:
|
||||
-- 第 1 种查询
|
||||
SELECT e1.Name AS Employee
|
||||
FROM Employee e1
|
||||
INNER JOIN Employee e2
|
||||
ON e1.ManagerId = e2.Id AND e1.Salary > e2.Salary;
|
||||
|
||||
-- 第 2 种解法
|
||||
SELECT e1.Name AS Employee FROM Employee e1, Employee e2
|
||||
WHERE e1.ManagerId = e2.Id AND e1.Salary > e2.Salary;
|
||||
|
||||
-- 第 3 种查询
|
||||
SELECT e1.Name AS Employee FROM Employee e1 WHERE
|
||||
e1.Salary > (SELECT e2.Salary FROM Employee e2 WHERE e1.ManagerId = e2.Id);
|
|
@ -0,0 +1,22 @@
|
|||
-- 给定一个 Weather 表,编写一个 SQL 查询,来查找与之前(昨天的)日期相比温度更高的所有日期的 Id。
|
||||
--
|
||||
-- +---------+------------------+------------------+
|
||||
-- | 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 |
|
||||
-- +---------+------------------+------------------+
|
||||
-- 例如,根据上述给定的 Weather 表格,返回如下 Id:
|
||||
--
|
||||
-- +----+
|
||||
-- | Id |
|
||||
-- +----+
|
||||
-- | 2 |
|
||||
-- | 4 |
|
||||
-- +----+
|
||||
|
||||
SELECT w1.Id FROM Weather w1, Weather w2
|
||||
WHERE w1.RecordDate = DATE_ADD(w2.RecordDate,interval 1 DAY )
|
||||
AND w1.Temperature > w2.Temperature;
|
|
@ -0,0 +1,21 @@
|
|||
-- 编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) 。
|
||||
--
|
||||
-- +----+--------+
|
||||
-- | Id | Salary |
|
||||
-- +----+--------+
|
||||
-- | 1 | 100 |
|
||||
-- | 2 | 200 |
|
||||
-- | 3 | 300 |
|
||||
-- +----+--------+
|
||||
-- 例如上述 Employee 表,SQL查询应该返回 200 作为第二高的薪水。如果不存在第二高的薪水,那么查询应返回 null。
|
||||
--
|
||||
-- +---------------------+
|
||||
-- | SecondHighestSalary |
|
||||
-- +---------------------+
|
||||
-- | 200 |
|
||||
-- +---------------------+
|
||||
|
||||
|
||||
SELECT (SELECT DISTINCT salary FROM Employee
|
||||
ORDER BY salary DESC LIMIT 1,1)
|
||||
AS SecondHighestSalary;
|
Loading…
Reference in New Issue