25道Mysql SQL练习题,让你轻松掌握数据库查询技能(mysqlsql练习题)

MySQL是一款广泛使用的开源关系型数据库。在日常工作中,查询数据库是必不可少的一项技能。然而,对于初学者来说,SQL查询的思路与语法是相对较为复杂的。为了帮助大家更好地掌握MySQL数据库查询技能,本文为大家提供了25道MySQL SQL练习题,希望能够帮助大家更快地熟悉SQL语言。

1. 查询所有学生的信息。

SELECT * FROM students;

2. 查询所有学生的姓名和学号。

SELECT `name`, `id` FROM students;

3. 查询年龄大于等于18岁的学生的学号、姓名、年龄。

SELECT `id`, `name`, `age` FROM students WHERE age >= 18;

4. 查询班级为3班的所有学生的学号、姓名。

SELECT `id`, `name` FROM students WHERE class = 3;

5. 查询所有男生的姓名、年龄。

SELECT `name`, `age` FROM students WHERE gender = '男';

6. 查询姓“李”的学生的学号、姓名。

SELECT `id`, `name` FROM students WHERE name LIKE '李%';

7. 查询平均成绩大于等于80分的学生的学号和平均成绩。

SELECT `id`, AVG(score) AS avg_score FROM scores GROUP BY `id` HAVING avg_score >= 80;

8. 查询所有学生的学号、姓名、选修科目的数目。

SELECT `id`, `name`, COUNT(*) AS count FROM scores GROUP BY `id`;

9. 查询选修科目数目最多的学生的姓名和选修科目数目。

SELECT `name`, COUNT(*) AS count FROM scores JOIN students ON scores.`id` = students.`id` GROUP BY students.`id` ORDER BY count DESC LIMIT 1;

10. 查询所有学生的学号、姓名、选修科目的平均分,按平均分从高到低排序。

SELECT scores.`id`, students.`name`, AVG(score) AS average_score FROM scores JOIN students ON scores.`id` = students.`id` GROUP BY scores.`id`, students.`name` ORDER BY average_score DESC;

11. 查询所有选修了大于等于3门科目的学生的姓名。

SELECT students.`name` FROM scores JOIN students ON scores.`id` = students.`id` GROUP BY scores.`id` HAVING COUNT(*) >= 3;

12. 查询所有学生的学号、姓名,按照姓名的长度升序排序。

SELECT `id`, `name` FROM students ORDER BY LENGTH(`name`) ASC;

13. 查询成绩在80~90分之间的学生的学号、姓名、成绩。

SELECT scores.`id`, students.`name`, score FROM scores JOIN students ON scores.`id` = students.`id` WHERE score BETWEEN 80 AND 90;

14. 查询每个班级的平均成绩,并按照班级升序排序。

SELECT class, AVG(score) AS average_score FROM scores JOIN students ON scores.`id` = students.`id` GROUP BY class ORDER BY class ASC;

15. 查询成绩排名在前10名的学生的学号、姓名、成绩。

SELECT scores.`id`, students.`name`, score FROM scores JOIN students ON scores.`id` = students.`id` ORDER BY score DESC LIMIT 10;

16. 查询没有选修任何科目的学生的姓名。

SELECT `name` FROM students WHERE `id` NOT IN (SELECT DISTINCT `id` FROM scores);

17. 查询至少选修了两门以上不同科目的学生的学号和姓名。

SELECT `id`, `name` FROM students WHERE `id` IN (SELECT `id` FROM scores GROUP BY `id` HAVING COUNT(DISTINCT subject) >= 2);

18. 查询选修了所有科目的学生的学号和姓名。

SELECT `id`, `name` FROM students WHERE `id` IN (SELECT `id` FROM scores GROUP BY `id` HAVING COUNT(DISTINCT subject) = (SELECT COUNT(DISTINCT subject) FROM scores));

19. 查询成绩最低的学生的学号、姓名、成绩。

SELECT `id`, `name`, score FROM scores JOIN students ON scores.`id` = students.`id` ORDER BY score ASC LIMIT 1;

20. 查询成绩最高的学生的学号、姓名、成绩。

SELECT `id`, `name`, score FROM scores JOIN students ON scores.`id` = students.`id` ORDER BY score DESC LIMIT 1;

21. 查询没有重名的学生的姓名和数目。

SELECT `name`, COUNT(*) AS count FROM students GROUP BY `name` HAVING count = 1;

22. 查询学生平均成绩超过90分的教师的姓名。

SELECT teachers.`name` FROM teachers JOIN courses ON teachers.`id` = courses.`tid` JOIN scores ON courses.`id` = scores.`cid` GROUP BY teachers.`name` HAVING AVG(score) > 90;

23. 查询所有学生的姓名和生日。

SELECT `name`, `birth` FROM students;

24. 查询所有学生的姓名和年龄。

SELECT `name`, YEAR(CURDATE()) - YEAR(`birthdate`) - (RIGHT(CURDATE(), 5) 

25. 查询没有选修科目、或者选修科目成绩都小于60的学生的姓名。

SELECT students.`name` FROM students LEFT JOIN scores ON students.`id` = scores.`id` GROUP BY students.`id` HAVING COUNT(scores.`cid`) = 0 OR MAX(scores.`score`) 

以上就是25道MySQL SQL练习题,希望这些练习题能够帮助大家更好地掌握MySQL数据库查询技能。若有需要,可以根据实际情况进行修改和补充,以拓展自己的MySQL数据库查询技能。


数据运维技术 » 25道Mysql SQL练习题,让你轻松掌握数据库查询技能(mysqlsql练习题)