SQL解锁常见和复杂查询的解决方案
如何查找数据库表中的重复记录?
mysql> select * from Contacts;
+——-+———-+
| name | phone |
+——-+———-+
| James | 80983243 |
| Johnny | 67543212 |
| Harry | 12341234 |
| Ron | 44446666 |
| James | 80983243 |
| Johnny | 67543212 |
| Harry | 12341234 |
| Ron | 44446666 |
| James | 80983243 |
| Johnny | 67543212 |
| Harry | 12341234 |
| Ron | 44446666 |
| James | 80983243 |
| Johnny | 67543212 |
| Harry | 12341234 |
| Ron | 44446666 |
| Ruby | 8965342 |
| Ruby | 6888342 |
+——-+———-+
18 rows in set (0.00 sec)
mysql> select name, count(name) from contacts group by name;
+——-+————-+
| name | count(name) |
+——-+————-+
| Harry | 4 |
| James | 4 |
| Johnny | 4 |
| Ron | 4 |
| Ruby | 2 |
+——-+————-+
5 rows in set (0.00 sec)
mysql> select name, count(name) from contacts group by name, phone;
+——-+————-+
| name | count(name) |
+——-+————-+
| Harry | 4 |
| James | 4 |
| Johnny | 4 |
| Ron | 4 |
| Ruby | 1 |
| Ruby | 1 |
+——-+————-+
mysql> select name, count(name) as times
from contacts
group by name, phone having times>1;
+——-+——-+
| name | times |
+——-+——-+
| Harry | 4 |
| James | 4 |
| Johnny | 4 |
| Ron | 4 |
+——-+——-+
4 rows in set (0.00 sec)
SQL中 Truncate 和 Delete 的区别?
候选键和主键的区别?
-
唯一标识:主键和候选键都可以唯一标识数据库中表中的记录。 -
约束:主键和候选键都有约束 UNIQUE 和 NOT NULL。 -
结构:主键或候选键可以是表中的单列或多列的组合。
如何查找员工的第二高或第二最高工资?
mysql> SELECT * FROM Employee;
+——–+———-+———+——–+
| emp_id | emp_name | dept_id | salary |
+——–+———-+———+——–+
| 1 | James | 10 | 2000 |
| 2 | Jack | 10 | 4000 |
| 3 | Henry | 11 | 6000 |
| 4 | Tom | 11 | 8000 |
+——–+———-+———+——–+
mysql> SELECT max(salary) FROM Employee WHERE salary NOT IN (SELECT max(salary) FROM Employee);
+————-+
| max(salary) |
+————-+
| 6000 |
+————-+
1 row IN SET (0.00 sec)
mysql> SELECT max(salary) FROM Employee WHERE salary < (SELECT max(salary) FROM Employee);
+————-+
| max(salary) |
+————-+
| 6000 |
+————-+
1 row IN SET (0.00 sec)
mysql> SELECT salary FROM (SELECT salary FROM Employee ORDER BY salary DESC LIMIT 2) AS emp ORDER BY salary LIMIT 1;
+——–+
| salary |
+——–+
| 6000 |
+——–+
1 row IN SET (0.00 sec)
12 个用于技术面试的 SQL 查询示例和练习题
1. 你能编写一个 SQL 查询来显示工资比经理高的员工(姓名)吗?
— 工资比经理高的员工(姓名)
SELECT a.emp_name FROM Employee a JOIN Employee b
ON a.mngr_id = b.emp_id
WHERE a.salary > b.salary;
2. 编写一个 SQL 查询来查找其部门中薪水最高的员工。
— 部门中薪资最高的员工
SELECT a.emp_name, a.dept_id
FROM Employee a JOIN
(SELECT a.dept_id, MAX(salary) as max_salary
FROM Employee a JOIN Department b ON a.dept_id = b.dept_id
GROUP BY a.dept_id) b
ON a.salary = b.max_salary AND a.dept_id = b.dept_id;
3. 编写一个 SQL 查询来列出少于 3 人的部门?
— 少于 3 人的部门
SELECT dept_id, COUNT(emp_name) as ‘Number of Employee’
FROM Employee
GROUP BY dept_id
HAVING COUNT(emp_name) < 3;
4. 编写一个 SQL 查询来显示所有部门及其人数。
— 所有部门及其人数
SELECT b.dept_name, COUNT(a.dept_id) as ‘Number of Employee’
FROM Employee a FULL OUTER JOIN Department b ON a.dept_id=b.dept_id
GROUP BY b.dept_name;
5. 你可以编写一个 SQL 查询来显示同一部门中没有经理的所有员工吗?
— 同一部门没有经理的员工
SELECT a.emp_name FROM Employee a JOIN Employee b
ON a.mngr_id = b.emp_id
WHERE a.dept_id != b.dept_id;
6. 你能编写一个 SQL 查询来列出所有部门以及那里的总工资吗?
— 所有部门以及总工资
SELECT b.dept_name, SUM(a.salary) as ‘Total Salary’
FROM Employee a FULL OUTER JOIN Department b ON a.dept_id = b.dept_id
GROUP BY b.dept_name;
微信赞赏支付宝扫码领红包