经典51道SQL查询练习题
时间: 2023-09-12 14:03:36 浏览: 190
好的,以下是经典的51道SQL查询练习题:
1. 查询表中所有记录
```
SELECT * FROM table_name;
```
2. 查询表中的某些列
```
SELECT column_name1, column_name2 FROM table_name;
```
3. 查询表中不重复的某些列
```
SELECT DISTINCT column_name1, column_name2 FROM table_name;
```
4. 按条件查询表中的记录
```
SELECT * FROM table_name WHERE condition;
```
5. 对查询结果进行排序
```
SELECT * FROM table_name ORDER BY column_name;
```
6. 对查询结果进行倒序排序
```
SELECT * FROM table_name ORDER BY column_name DESC;
```
7. 查询表中前 n 条记录
```
SELECT * FROM table_name LIMIT n;
```
8. 查询表中第 m 条到第 n 条记录
```
SELECT * FROM table_name LIMIT m, n-m+1;
```
9. 对查询结果进行计数
```
SELECT COUNT(*) FROM table_name;
```
10. 对查询结果进行求和
```
SELECT SUM(column_name) FROM table_name;
```
11. 对查询结果进行平均值计算
```
SELECT AVG(column_name) FROM table_name;
```
12. 对查询结果进行最大值计算
```
SELECT MAX(column_name) FROM table_name;
```
13. 对查询结果进行最小值计算
```
SELECT MIN(column_name) FROM table_name;
```
14. 对查询结果进行分组计算
```
SELECT column_name, COUNT(*) FROM table_name GROUP BY column_name;
```
15. 对查询结果进行分组计算并进行排序
```
SELECT column_name, COUNT(*) FROM table_name GROUP BY column_name ORDER BY COUNT(*) DESC;
```
16. 对查询结果进行多条件查询
```
SELECT * FROM table_name WHERE condition1 AND condition2;
```
17. 对查询结果进行模糊查询
```
SELECT * FROM table_name WHERE column_name LIKE '%keyword%';
```
18. 对查询结果进行通配符查询
```
SELECT * FROM table_name WHERE column_name LIKE '_keyword';
```
19. 对查询结果进行多表查询
```
SELECT * FROM table_name1 JOIN table_name2 ON table_name1.column_name=table_name2.column_name;
```
20. 对查询结果进行多表联合查询
```
SELECT * FROM table_name1 UNION SELECT * FROM table_name2;
```
21. 对查询结果进行多表交集查询
```
SELECT * FROM table_name1 INTERSECT SELECT * FROM table_name2;
```
22. 对查询结果进行多表差集查询
```
SELECT * FROM table_name1 EXCEPT SELECT * FROM table_name2;
```
23. 对查询结果进行子查询
```
SELECT * FROM table_name WHERE column_name IN (SELECT column_name FROM another_table WHERE condition);
```
24. 对查询结果进行连接查询
```
SELECT * FROM table_name1 LEFT JOIN table_name2 ON table_name1.column_name=table_name2.column_name;
```
25. 对查询结果进行左连接查询
```
SELECT * FROM table_name1 LEFT OUTER JOIN table_name2 ON table_name1.column_name=table_name2.column_name;
```
26. 对查询结果进行右连接查询
```
SELECT * FROM table_name1 RIGHT OUTER JOIN table_name2 ON table_name1.column_name=table_name2.column_name;
```
27. 对查询结果进行全连接查询
```
SELECT * FROM table_name1 FULL OUTER JOIN table_name2 ON table_name1.column_name=table_name2.column_name;
```
28. 对查询结果进行自连接查询
```
SELECT * FROM table_name t1, table_name t2 WHERE t1.column_name=t2.column_name;
```
29. 对查询结果进行临时表查询
```
CREATE TEMPORARY TABLE temp_table_name AS SELECT * FROM table_name WHERE condition;
SELECT * FROM temp_table_name;
```
30. 对查询结果进行自定义列名
```
SELECT column_name1 AS name1, column_name2 AS name2 FROM table_name;
```
31. 对查询结果进行多条件排序
```
SELECT * FROM table_name ORDER BY column_name1 ASC, column_name2 DESC;
```
32. 对查询结果进行多表连接查询并进行分组计算
```
SELECT table_name1.column_name1, COUNT(*) FROM table_name1 JOIN table_name2 ON table_name1.column_name=table_name2.column_name GROUP BY table_name1.column_name1;
```
33. 对查询结果进行多表连接查询并进行聚合计算
```
SELECT table_name1.column_name1, SUM(table_name2.column_name2) FROM table_name1 JOIN table_name2 ON table_name1.column_name=table_name2.column_name GROUP BY table_name1.column_name1;
```
34. 对查询结果进行多表连接查询并进行统计计算
```
SELECT table_name1.column_name1, COUNT(*) FROM table_name1 JOIN table_name2 ON table_name1.column_name=table_name2.column_name WHERE condition GROUP BY table_name1.column_name1;
```
35. 对查询结果进行分页查询
```
SELECT * FROM table_name LIMIT m, n-m+1;
```
36. 对查询结果进行日期计算
```
SELECT DATE_ADD(date_column, INTERVAL 1 MONTH) FROM table_name;
```
37. 对查询结果进行日期格式化
```
SELECT DATE_FORMAT(date_column, '%Y-%m-%d') FROM table_name;
```
38. 对查询结果进行日期比较
```
SELECT * FROM table_name WHERE DATEDIFF(date_column1, date_column2) > 30;
```
39. 对查询结果进行日期范围查询
```
SELECT * FROM table_name WHERE date_column BETWEEN 'start_date' AND 'end_date';
```
40. 对查询结果进行日期部分提取
```
SELECT YEAR(date_column), MONTH(date_column), DAY(date_column) FROM table_name;
```
41. 对查询结果进行多表连接查询并进行分组计算和排序
```
SELECT table_name1.column_name1, COUNT(*) FROM table_name1 JOIN table_name2 ON table_name1.column_name=table_name2.column_name GROUP BY table_name1.column_name1 ORDER BY COUNT(*) DESC;
```
42. 对查询结果进行多表连接查询并进行多条件排序
```
SELECT * FROM table_name1 JOIN table_name2 ON table_name1.column_name=table_name2.column_name ORDER BY table_name1.column_name1 ASC, table_name2.column_name2 DESC;
```
43. 对查询结果进行多表连接查询并进行多条件分组计算和排序
```
SELECT table_name1.column_name1, COUNT(*) FROM table_name1 JOIN table_name2 ON table_name1.column_name=table_name2.column_name WHERE condition GROUP BY table_name1.column_name1 ORDER BY COUNT(*) DESC;
```
44. 对查询结果进行多表连接查询并进行多条件聚合计算和排序
```
SELECT table_name1.column_name1, SUM(table_name2.column_name2) FROM table_name1 JOIN table_name2 ON table_name1.column_name=table_name2.column_name WHERE condition GROUP BY table_name1.column_name1 ORDER BY SUM(table_name2.column_name2) DESC;
```
45. 对查询结果进行多表连接查询并进行多条件统计计算和排序
```
SELECT table_name1.column_name1, COUNT(*) FROM table_name1 JOIN table_name2 ON table_name1.column_name=table_name2.column_name WHERE condition GROUP BY table_name1.column_name1 ORDER BY COUNT(*) ASC;
```
46. 对查询结果进行多表连接查询并进行多条件分组计算、聚合计算和排序
```
SELECT table_name1.column_name1, COUNT(*), SUM(table_name2.column_name2) FROM table_name1 JOIN table_name2 ON table_name1.column_name=table_name2.column_name WHERE condition GROUP BY table_name1.column_name1 ORDER BY COUNT(*) DESC, SUM(table_name2.column_name2) ASC;
```
47. 对查询结果进行多表连接查询并进行多条件统计计算、聚合计算和排序
```
SELECT table_name1.column_name1, COUNT(*), SUM(table_name2.column_name2) FROM table_name1 JOIN table_name2 ON table_name1.column_name=table_name2.column_name WHERE condition GROUP BY table_name1.column_name1 ORDER BY COUNT(*) ASC, SUM(table_name2.column_name2) DESC;
```
48. 对查询结果进行多表连接查询并进行多条件分组计算、聚合计算、统计计算和排序
```
SELECT table_name1.column_name1, COUNT(*), SUM(table_name2.column_name2), AVG(table_name2.column_name2) FROM table_name1 JOIN table_name2 ON table_name1.column_name=table_name2.column_name WHERE condition GROUP BY table_name1.column_name1 ORDER BY COUNT(*) DESC, SUM(table_name2.column_name2) ASC;
```
49. 对查询结果进行多表连接查询并进行多条件分组计算、聚合计算、统计计算、排序和分页查询
```
SELECT table_name1.column_name1, COUNT(*), SUM(table_name2.column_name2), AVG(table_name2.column_name2) FROM table_name1 JOIN table_name2 ON table_name1.column_name=table_name2.column_name WHERE condition GROUP BY table_name1.column_name1 ORDER BY COUNT(*) DESC, SUM(table_name2.column_name2) ASC LIMIT m, n-m+1;
```
50. 对查询结果进行多表连接查询并进行多条件分组计算、聚合计算、统计计算、排序、分页查询和列过滤
```
SELECT table_name1.column_name1, COUNT(*), SUM(table_name2.column_name2), AVG(table_name2.column_name2) FROM table_name1 JOIN table_name2 ON table_name1.column_name=table_name2.column_name WHERE condition GROUP BY table_name1.column_name1 ORDER BY COUNT(*) DESC, SUM(table_name2.column_name2) ASC LIMIT m, n-m+1 OFFSET k ROWS FETCH NEXT l ROWS ONLY;
```
51. 对查询结果进行多表连接查询并进行多条件分组计算、聚合计算、统计计算、排序、分页查询、列过滤和条件过滤
```
SELECT table_name1.column_name1, COUNT(*), SUM(table_name2.column_name2), AVG(table_name2.column_name2) FROM table_name1 JOIN table_name2 ON table_name1.column_name=table_name2.column_name WHERE condition GROUP BY table_name1.column_name1 HAVING COUNT(*) > 10 ORDER BY COUNT(*) DESC, SUM(table_name2.column_name2) ASC LIMIT m, n-m+1 OFFSET k ROWS FETCH NEXT l ROWS ONLY;
```
阅读全文