MySQL查询练习题

MySQL练习题

  • 全部题目
  • 分类
      • 简单查询
      • where
      • 子查询
      • 聚合函数
      • 综合
  • 题目需要的数据

.

全部题目

# 查询
-- 1.查询student表中所有的记录

-- 2.查询student表中所有记录的s_name,s_sex和s_class列


-- 3.查询教师所有的单位但是不重复的t_depart列

-- 4.查询score表中成绩在60-80之间所有的记录(sc_degree)
-- 5.查询score表中成绩为85, 86, 或者88的记录(sc_degree)
-- 6.查询student表中'95031'班或者性别为'女'的同学记录
-- 7.以class降序查询student表中所有的记录
-- 8.以c_no升序.sc_degree降序插叙score表中所有的数据
-- 9.查询'95031'班的学生人数
-- 10.查询score表中的最高分数的学生号和课程号.(子查询或者排序)
-- 11.查询每门课的平均成绩
-- 12,查询score表中至少有2名学生选修的,并且以3开头的课程的平均分
-- 13.查询分数大于70但是小于90的s_no列:


-- 14.查询所有的学生 s_name , c_no, sc_degree列
--  15.查询所有学生的s_no, c_name, sc_degree列
-- 16.查询所有的学生 s_name , c_name, sc_degree列
-- 17.查询班级是'95031'班学生每门课的平均分
-- 18.查询选修"3-105"课程的成绩高于'109'号同学'3-105'成绩 的所有同学的记录

-- 19.查询成绩高于学号为'109',课程号为'3-105'的成绩的所有记录
--  20.查询所有学号为108.101的同学同年出生的所有学生的s_no,s_name和s_birthday
-- 21.查询 张旭 教师任课的学生的成绩
-- 22.查询选修课程的同学人数多余 5 人的教师姓名

-- 23.查询95033班和95031班全体学生的记录
-- 24.查询存在85分以上成绩的课程c_no

-- 25.查出所有'计算机系' 教师所教课程的成绩表
-- 26.查询'计算机系'与'电子工程系' 不同职称的教师的name和rof
-- 27,查询选修编号为"3-105"课程且成绩至少高于选修编号为'3-245'同学的c_no,s_no和sc_degree,并且按照sc_degree从高到地次序排序

-- 28.查询选修编号为"3-105"且成绩高于选修编号为"3-245"课程的同学c_no.s_no和sc_degree
-- 29. 查询所有教师和同学的 name ,sex, birthday

-- 30.查询所有'女'教师和'女'学生的name,sex,birthday
-- 31.查询成绩比该课程平均成绩低的同学的成绩表

-- 32.查询所有任课教师的t_name 和 t_depart(要在分数表中可以查得到)
-- 33.查出至少有2名男生的班号

-- 34.查询student 表中 不姓"王"的同学的记录
-- 35. 查询student 中每个学生的姓名和年龄(当前时间 - 出生年份)
-- 36. 查询student中最大和最小的 s_birthday的值

-- 37.以班级号和年龄从大到小的顺序查询student表中的全部记录
-- 38.查询"男"教师 及其所上的课
-- 39.查询最高分同学的s_no c_no 和 sc_degree;

-- 40. 查询和"李军"同性别的所有同学的s_name
-- 41.查询和"李军"同性别并且同班的所有同学的s_name
-- 42. 查询所有选修'计算机导论'课程的'男'同学的成绩表

分类

简单查询

-- 1.查询student表中所有的记录
SELECT * FROM student;
+------+--------+-------+---------------------+---------+
| s_no | s_name | s_sex | s_birthday          | s_class |
+------+--------+-------+---------------------+---------+
| 101  | 曾华   || 1977-09-01 00:00:00 | 95033   |
| 102  | 匡明   || 1975-10-02 00:00:00 | 95031   |
| 103  | 王丽   || 1976-01-23 00:00:00 | 95033   |
| 104  | 李军   || 1976-02-20 00:00:00 | 95033   |
| 105  | 王芳   || 1975-02-10 00:00:00 | 95031   |
| 106  | 陆军   || 1974-06-03 00:00:00 | 95031   |
| 107  | 王尼玛 || 1976-02-20 00:00:00 | 95033   |
| 108  | 张全蛋 || 1975-02-10 00:00:00 | 95031   |
| 109  | 赵铁柱 || 1974-06-03 00:00:00 | 95031   |
+------+--------+-------+---------------------+---------+

-- 2.查询student表中所有记录的s_name,s_sex和s_class列

SELECT s_name,s_sex,s_class FROM student;
+------+--------+---------+
| s_no | s_name | s_class |
+------+--------+---------+
| 1    | admin  | 95033   |
| 101  | 曾华   | 95033   |
| 102  | 匡明   | 95031   |
| 103  | 王丽   | 95033   |
| 104  | 李军   | 95033   |
| 105  | 王芳   | 95031   |
| 106  | 陆军   | 95031   |
| 107  | 王尼玛 | 95033   |
| 108  | 张全蛋 | 95031   |
| 109  | 赵铁柱 | 95031   |
+------+--------+---------+

-- 3.查询教师所有的单位但是不重复的t_depart列
SELECT * FROM teacher;
SELECT DISTINCT t_depart FROM teacher;
+------------+
| t_depart   |
+------------+
| 计算机系   |
| 计算机机系 |
| 电子工程系 |
+------------+

-- 4.查询score表中成绩在60-80之间所有的记录(sc_degree)
SELECT * FROM score WHERE sc_degree <80 and sc_degree > 60;
# PS: BETWEEN AND 包含边界
+------+-------+-----------+
| s_no | c_no  | sc_degree |
+------+-------+-----------+
| 105  | 3-245 |        75 |
| 105  | 6-166 |        79 |
| 109  | 3-105 |        76 |
| 109  | 3-245 |        68 |
+------+-------+-----------+
-- 5.查询score表中成绩为85, 86, 或者88的记录(sc_degree)
SELECT * FROM score WHERE sc_degree=85 or sc_degree=86 OR sc_degree=88;
或者
SELECT * FROM score WHERE sc_degree IN(85, 86, 88);
+------+-------+-----------+
| s_no | c_no  | sc_degree |
+------+-------+-----------+
| 103  | 3-245 |        86 |
| 103  | 6-166 |        85 |
| 105  | 3-105 |        88 |
+------+-------+-----------+
-- 6.查询student表中'95031'班或者性别为'女'的同学记录
SELECT * FROM student;
SELECT * FROM student WHERE s_class=95033 or s_sex = '女';
+------+--------+-------+---------------------+---------+
| s_no | s_name | s_sex | s_birthday          | s_class |
+------+--------+-------+---------------------+---------+
| 102  | 匡明   || 1975-10-02 00:00:00 | 95031   |
| 103  | 王丽   || 1976-01-23 00:00:00 | 95033   |
| 105  | 王芳   || 1975-02-10 00:00:00 | 95031   |
| 106  | 陆军   || 1974-06-03 00:00:00 | 95031   |
| 108  | 张全蛋 || 1975-02-10 00:00:00 | 95031   |
| 109  | 赵铁柱 || 1974-06-03 00:00:00 | 95031   |
+------+--------+-------+---------------------+---------+
-- 7.以class降序查询student表中所有的记录
SELECT * FROM student 
ORDER BY s_class DESC;
+------+--------+-------+---------------------+---------+
| s_no | s_name | s_sex | s_birthday          | s_class |
+------+--------+-------+---------------------+---------+
| 1    | admin  || 1999-09-09 00:00:00 | 95033   |
| 101  | 曾华   || 1977-09-01 00:00:00 | 95033   |
| 103  | 王丽   || 1976-01-23 00:00:00 | 95033   |
| 104  | 李军   || 1976-02-20 00:00:00 | 95033   |
| 107  | 王尼玛 || 1976-02-20 00:00:00 | 95033   |
| 102  | 匡明   || 1975-10-02 00:00:00 | 95031   |
| 105  | 王芳   || 1975-02-10 00:00:00 | 95031   |
| 106  | 陆军   || 1974-06-03 00:00:00 | 95031   |
| 108  | 张全蛋 || 1975-02-10 00:00:00 | 95031   |
| 109  | 赵铁柱 || 1974-06-03 00:00:00 | 95031   |
+------+--------+-------+---------------------+---------+
-- 8.以c_no升序.sc_degree降序查询score表中所有的数据
SELECT * FROM score
ORDER BY c_no ASC, sc_degree DESC;

+------+-------+-----------+
| s_no | c_no  | sc_degree |
+------+-------+-----------+
| 103  | 3-105 |        92 |
| 105  | 3-105 |        88 |
| 109  | 3-105 |        76 |
| 103  | 3-245 |        86 |
| 105  | 3-245 |        75 |
| 109  | 3-245 |        68 |
| 103  | 6-166 |        85 |
| 109  | 6-166 |        81 |
| 105  | 6-166 |        79 |
+------+-------+-----------+

where

-- 9.查询'95031'班的学生人数
SELECT COUNT(*)  FROM student WHERE s_class=95031;
+-------------+
| COUNT(*) |
+-------------+
|           5 |
+-------------+-- 
-- 13.查询分数大于70但是小于90的s_no列:
SELECT DISTINCT s_no 
FROM score 
WHERE sc_degree > 70 AND sc_degree < 90;

+------+
| s_no | 
+------+
| 103  |       
| 105  |     
| 109  |   
+------+



子查询

-- 10.查询score表中的最高分数的学生号和课程号.(子查询或者排序)

SELECT c_no,s_no FROM score WHERE sc_degree=(
	SELECT max(sc_degree) FROM score
);

+-------+------+
| c_no  | s_no |
+-------+------+
| 3-105 | 103  |
+-------+------+

聚合函数

-- 11.查询每门课的平均成绩
SELECT c_no,AVG(sc_degree) 
FROM SCORE 
GROUP BY c_no;
+-------+----------------+
| c_no  | AVG(sc_degree) |
+-------+----------------+
| 3-105 |        85.3333 |
| 3-245 |        76.3333 |
| 6-166 |        81.6667 |
+-------+----------------+
-- 12,查询score表中至少有2名学生选修的,并且以3开头的课程的平均分

SELECT avg(sc_degree),c_no
FROM score
GROUP BY c_no
HAVING count(s_no)>2  AND c_no LIKE '3%';
+----------------+-------+
| AVG(sc_degree) | c_no  |
+----------------+-------+
|        85.3333 | 3-105 |
|        76.3333 | 3-245 |
+----------------+-------+

综合

- 14.查询所有的学生 s_name , c_no, sc_degree列

SELECT s_name,c_no,sc_degree 
FROM score,student
WHERE student.s_no = score.s_no;

+--------+-------+-----------+
| s_name | c_no  | sc_degree |
+--------+-------+-----------+
| 王丽   | 3-105 |        92 |
| 王丽   | 3-245 |        86 |
| 王丽   | 6-166 |        85 |
| 王芳   | 3-105 |        88 |
| 王芳   | 3-245 |        75 |
| 王芳   | 6-166 |        79 |
| 赵铁柱 | 3-105 |        76 |
| 赵铁柱 | 3-245 |        68 |
| 赵铁柱 | 6-166 |        81 |
+--------+-------+-----------+
--  15.查询所有学生的s_no, c_name, sc_degree列
SELECT student.s_no,c_name,sc_degree
FROM student,course,score
WHERE student.s_no = score.s_no AND  score.c_no = course.c_no;

+------+------------+-----------+
| s_no | c_name     | sc_degree |
+------+------------+-----------+
| 103  | 计算机导论 |        92 |
| 103  | 操作系统   |        86 |
| 103  | 数字电路   |        85 |
| 105  | 计算机导论 |        88 |
| 105  | 操作系统   |        75 |
| 105  | 数字电路   |        79 |
| 109  | 计算机导论 |        76 |
| 109  | 操作系统   |        68 |
| 109  | 数字电路   |        81 |
+------+------------+-----------+
-- 16.查询所有的学生 s_name , c_name, sc_degree列


SELECT s_name,c_name,sc_degree
FROM student,course,score
WHERE student.s_no = score.s_no AND score.c_no = course.c_no;

+--------+------------+-----------+
| s_name | c_name     | sc_degree |
+--------+------------+-----------+
| 王丽   | 计算机导论 |        92 |
| 王丽   | 操作系统   |        86 |
| 王丽   | 数字电路   |        85 |
| 王芳   | 计算机导论 |        88 |
| 王芳   | 操作系统   |        75 |
| 王芳   | 数字电路   |        79 |
| 赵铁柱 | 计算机导论 |        76 |
| 赵铁柱 | 操作系统   |        68 |
| 赵铁柱 | 数字电路   |        81 |
-- 17.查询班级是'95031'班学生每门课的平均分

-- 首先查询出这个班级的学生的学号
SELECT s_no
FROM student
WHERE s_class = 95031;
-- 然后根据学号选择对应的课程并且根据c_no分组
SELECT c_no,AVG(sc_degree)
FROM score
WHERE s_no IN (
	SELECT s_no
	FROM student
	WHERE s_class = 95031
)
GROUP BY c_no;

+-------+-------------------+
| c_no  | AVG(sc.sc_degree) |
+-------+-------------------+
| 3-105 |           82.0000 |
| 3-245 |           71.5000 |
| 6-166 |           80.0000 |
+-------+-------------------+
-- 综合
-- 18.查询选修"3-105"课程的成绩高于'109'号同学'3-105'成绩 的所有同学的记录

SELECT *
FROM student,score
WHERE c_no = '3-105' AND sc_degree > (
	SELECT sc_degree
	FROM score 
	WHERE s_no=109 AND c_no = '3-105'
) AND score.s_no = student.s_no;


+------+--------+-------+---------------------+---------+------+-------+-----------+
| s_no | s_name | s_sex | s_birthday          | s_class | s_no | c_no  | sc_degree |
+------+--------+-------+---------------------+---------+------+-------+-----------+
| 103  | 王丽   || 1976-01-23 00:00:00 | 95033   | 103  | 3-105 |        92 |
| 105  | 王芳   || 1975-02-10 00:00:00 | 95031   | 105  | 3-105 |        88 |
+------+--------+-------+---------------------+---------+------+-------+-----------+



-- 19.查询成绩高于学号为'109',课程号为'3-105'的成绩的所有记录
SELECT * 
FROM score
WHERE sc_degree > (
	SELECT sc_degree
	FROM score
	WHERE s_no= 109 and c_no =  '3-105'
);
--  20.查询所有学号为108.101的同学同年出生的所有学生的s_no,s_name和s_birthday


SELECT * 
FROM student 
WHERE YEAR(s_birthday) IN (
	SELECT YEAR(s_birthday) 
	FROM student WHERE s_no 
	IN('108','101')
	);
	
	
	+------+--------+-------+---------------------+---------+
| s_no | s_name | s_sex | s_birthday          | s_class |
+------+--------+-------+---------------------+---------+
| 101  | 曾华   || 1977-09-01 00:00:00 | 95033   |
| 102  | 匡明   || 1975-10-02 00:00:00 | 95031   |
| 105  | 王芳   || 1975-02-10 00:00:00 | 95031   |
| 108  | 张全蛋 || 1975-02-10 00:00:00 | 95031   |
+------+--------+-------+---------------------+---------+
-- 21.查询 张旭 教师任课的学生的成绩

--  首先查询出张旭老师教的课程号
SELECT c_no
FROM teacher,course
WHERE t_name = '张旭' and teacher.t_no = course.tno;
-- 然后出结果
SELECT * 
FROM score
WHERE c_no = (
	SELECT c_no
	FROM teacher,course
	WHERE t_name = '张旭' and teacher.t_no = course.tno	
);

+------+--------+-------+------
| s_no | c_no   | sc_degree	  | 
+------+--------+--------------
| 103  | 6-166  | 85    			| 
| 105  | 6-166  | 79   			  | 
| 109  | 6-166  | 81      	  | 

-- 22.查询选修课程的同学人数多于 3 人的教师姓名


SELECT *
FROM teacher
WHERE t_no IN(
	SELECT tno
	FROM course
	WHERE c_no IN (
		SELECT c_no
		FROM score
		GROUP BY c_no
		HAVING COUNT(c_no)
	)	
);

+------+--------+-------+---------------------+--------+------------+
| t_no | t_name | t_sex | t_birthday          | t_rof  | t_depart   |
+------+--------+-------+---------------------+--------+------------+
| 804  | 李诚   || 1958-12-02 00:00:00 | 副教授 | 计算机系   |
| 825  | 王萍   || 1972-05-05 00:00:00 | 助教   | 计算机系   |
| 856  | 张旭   || 1969-03-12 00:00:00 | 讲师   | 电子工程系 |
+------+--------+-------+---------------------+--------+------------+
-- 23.查询95033班和95031班全体学生的记录


SELECT * FROM student WHERE s_class IN('95031','95033') ORDER BY s_class ;
+------+--------+-------+---------------------+---------+
| s_no | s_name | s_sex | s_birthday          | s_class |
+------+--------+-------+---------------------+---------+
| 102  | 匡明   || 1975-10-02 00:00:00 | 95031   |
| 105  | 王芳   || 1975-02-10 00:00:00 | 95031   |
| 106  | 陆军   || 1974-06-03 00:00:00 | 95031   |
| 108  | 张全蛋 || 1975-02-10 00:00:00 | 95031   |
| 109  | 赵铁柱 || 1974-06-03 00:00:00 | 95031   |
| 101  | 曾华   || 1977-09-01 00:00:00 | 95033   |
| 103  | 王丽   || 1976-01-23 00:00:00 | 95033   |
| 104  | 李军   || 1976-02-20 00:00:00 | 95033   |
| 107  | 王尼玛 || 1976-02-20 00:00:00 | 95033   |
+------+--------+-------+---------------------+---------+

-- 24.查询存在85分以上成绩的课程c_no

SELECT c_no
FROM score
WHERE sc_degree >= 85;

+-------+
| c_no  |
+-------+
| 3-105 |
| 3-245 |
| 6-166 |
| 3-105 |
+-------+

-- 25.查出所有'计算机系' 教师所教课程的成绩表

SELECT * 
FROM score
WHERE c_no IN (
	SELECT c_no
	FROM course
	WHERE tno IN(
		SELECT t_no
		FROM teacher
		WHERE t_depart = '计算机系'
	)
);
+------+-------+-----------+
| s_no | c_no  | sc_degree |
+------+-------+-----------+
| 103  | 3-245 |        86 |
| 105  | 3-245 |        75 |
| 109  | 3-245 |        68 |
| 103  | 3-105 |        92 |
| 105  | 3-105 |        88 |
| 109  | 3-105 |        76 |
+------+-------+-----------+

-- UNION
-- 26.查询'计算机系'与'电子工程系' 不同职称的教师的name和rof

SELECT * 
FROM teacher 
WHERE t_depart = '计算机系' AND t_rof 
NOT IN (SELECT t_rof FROM teacher WHERE t_depart = '电子工程系')
UNION
SELECT * 
FROM teacher
WHERE t_depart = '电子工程系' AND t_rof 
NOT IN (SELECT t_rof FROM teacher WHERE t_depart = '计算机系')


+------+--------+-------+---------------------+--------+------------+
| t_no | t_name | t_sex | t_birthday          | t_rof  | t_depart   |
+------+--------+-------+---------------------+--------+------------+
| 804  | 李诚   || 1958-12-02 00:00:00 | 副教授 | 计算机系   |
| 856  | 张旭   || 1969-03-12 00:00:00 | 讲师   | 电子工程系 |
+------+--------+-------+---------------------+--------+------------+
-- 27,查询选修编号为"3-105"课程且成绩至少高于选修编号为'3-245'同学的c_no,s_no和sc_degree,并且按照sc_degree从高到地次序排序

SELECT * 
FROM score
WHERE sc_degree > ANY(
SELECT sc_degree
FROM score
WHERE c_no = '3-245'
) AND c_no = '3-105'
ORDER BY sc_degree DESC;

+------+-------+-----------+
| s_no | c_no  | sc_degree |
+------+-------+-----------+
| 103  | 3-105 |        92 |
| 105  | 3-105 |        88 |
| 109  | 3-105 |        76 |
+------+-------+-----------
-- 28.查询选修编号为"3-105"且成绩高于选修编号为"3-245"课程的同学c_no.s_no和sc_degree


SELECT *
FROM score
WHERE c_no = '3-105' AND sc_degree > ALL(
	SELECT sc_degree 
	FROM score
	WHERE c_no = '3-245'
);
+------+-------+-----------+
| s_no | c_no  | sc_degree |
+------+-------+-----------+
| 103  | 3-105 |        92 |
| 105  | 3-105 |        88 |
+------+-------+-----------+


-- 29. 查询所有教师和同学的 name ,sex, birthday
SELECT s_name AS name, s_sex AS sex, s_birthday AS birthday  FROM student
UNION
SELECT t_name AS name, t_sex AS sex, t_birthday AS birthday FROM teacher;

+--------+-----+---------------------+
| name   | sex | birthday            |
+--------+-----+---------------------+
| 曾华   || 1977-09-01 00:00:00 |
| 匡明   || 1975-10-02 00:00:00 |
| 王丽   || 1976-01-23 00:00:00 |
| 李军   || 1976-02-20 00:00:00 |
| 王芳   || 1975-02-10 00:00:00 |
| 陆军   || 1974-06-03 00:00:00 |
| 王尼玛 || 1976-02-20 00:00:00 |
| 张全蛋 || 1975-02-10 00:00:00 |
| 赵铁柱 || 1974-06-03 00:00:00 |
| 李诚   || 1958-12-02 00:00:00 |
| 王萍   || 1972-05-05 00:00:00 |
| 刘冰   || 1977-08-14 00:00:00 |
| 张旭   || 1969-03-12 00:00:00 |
+--------+-----+---------------------+
-- 30.查询所有'女'教师和'女'学生的name,sex,birthday

SELECT s_name AS name , s_sex AS sex, s_birthday AS birthday 
FROM student
WHERE s_sex = '女'
UNION
SELECT t_name AS name, t_sex AS sex, t_birthday AS birthday
FROM teacher 
WHERE t_sex = '女';
+------+-----+---------------------+
| name | sex | birthday            |
+------+-----+---------------------+
| 王丽 || 1976-01-23 00:00:00 |
| 王芳 || 1975-02-10 00:00:00 |
| 王萍 || 1972-05-05 00:00:00 |
| 刘冰 || 1977-08-14 00:00:00 |
+------+-----+---------------------+

-- 
-- 31.查询成绩比该课程平均成绩低的同学的成绩表
SELECT * 
FROM score AS sc1
WHERE sc1.sc_degree < (
	SELECT AVG(sc_degree)
	FROM score AS sc2
	WHERE sc2.c_no = sc1.c_no
	
);

+------+-------+-----------+
| s_no | c_no  | sc_degree |
+------+-------+-----------+
| 105  | 3-245 |        75 |
| 105  | 6-166 |        79 |
| 109  | 3-105 |        76 |
| 109  | 3-245 |        68 |
| 109  | 6-166 |        81 |
+------+-------+-----------+


-- 32.查询所有任课教师的t_name 和 t_depart(要在分数表中可以查得到)
SELECT t_name,t_depart
FROM teacher
WHERE t_no IN (
SELECT t_no 
FROM course
);
+--------+------------+
| t_name | t_depart   |
+--------+------------+
| 李诚   | 计算机系   |
| 王萍   | 计算机系   |
| 刘冰   | 电子工程系 |
| 张旭   | 电子工程系 |

-- 33.查出至少有4名男生的班号

SELECT s_class
FROM student 
WHERE s_sex = '男'
GROUP BY s_class
HAVING COUNT(s_no) > 3;

+---------+
| s_class |
+---------+
| 95031   |
+---------+

-- 34.查询student 表中 不姓"王"的同学的记录
SELECT * 
FROM student
WHERE s_name NOT LIKE '王%';

+------+--------+-------+---------------------+---------+
| s_no | s_name | s_sex | s_birthday          | s_class |
+------+--------+-------+---------------------+---------+
| 101  | 曾华   || 1977-09-01 00:00:00 | 95033   |
| 102  | 匡明   || 1975-10-02 00:00:00 | 95031   |
| 104  | 李军   || 1976-02-20 00:00:00 | 95033   |
| 106  | 陆军   || 1974-06-03 00:00:00 | 95031   |
| 108  | 张全蛋 || 1975-02-10 00:00:00 | 95031   |
| 109  | 赵铁柱 || 1974-06-03 00:00:00 | 95031   |
+------+--------+-------+---------------------+---------+


-- 35. 查询student 中每个学生的姓名和年龄(当前时间 - 出生年份)
SELECT s_name , YEAR(NOW()) - YEAR(s_birthday) AS age
FROM student;
+--------+------+
| s_name | age  |
+--------+------+
| 曾华   |   43 |
| 匡明   |   45 |
| 王丽   |   44 |
| 李军   |   44 |
| 王芳   |   45 |
| 陆军   |   46 |
| 王尼玛 |   44 |
| 张全蛋 |   45 |
| 赵铁柱 |   46 |
+--------+------+

-- 36. 查询student中最大和最小的 s_birthday的值

SELECT MAX(s_birthday),MIN(s_birthday) FROM student;

+---------------------+---------------------+
| MAX(s_birthday)     | MIN(s_birthday)     |
+---------------------+---------------------+
| 1977-09-01 00:00:00 | 1974-06-03 00:00:00 |
+---------------------+---------------------+

-- 37.以班级号和年龄从大到小的顺序查询student表中的全部记录
SELECT *
FROM student
ORDER BY s_class DESC ,s_birthday ASC;

+------+--------+-------+---------------------+---------+
| s_no | s_name | s_sex | s_birthday          | s_class |
+------+--------+-------+---------------------+---------+
| 103  | 王丽   || 1976-01-23 00:00:00 | 95033   |
| 104  | 李军   || 1976-02-20 00:00:00 | 95033   |
| 107  | 王尼玛 || 1976-02-20 00:00:00 | 95033   |
| 101  | 曾华   || 1977-09-01 00:00:00 | 95033   |
| 106  | 陆军   || 1974-06-03 00:00:00 | 95031   |
| 109  | 赵铁柱 || 1974-06-03 00:00:00 | 95031   |
| 105  | 王芳   || 1975-02-10 00:00:00 | 95031   |
| 108  | 张全蛋 || 1975-02-10 00:00:00 | 95031   |
| 102  | 匡明   || 1975-10-02 00:00:00 | 95031   |
+------+--------+-------+---------------------+---------+


-- 38.查询"男"教师 及其所上的课


SELECT *
FROM course
WHERE tno IN(
	SELECT t_no
	FROM teacher
	WHERE t_sex = '男'
);	
+-------+----------+------+
| c_no  | c_name   | t_no |
+-------+----------+------+
| 3-245 | 操作系统 | 804  |
| 6-166 | 数字电路 | 856  |
+-------+----------+------+

-- 39.查询最高分同学的s_no c_no 和 sc_degree;


SELECT * 
FROM score 
WHERE sc_degree = (select MAX(sc_degree) AS sc_degree FROM score);
+------+-------+-----------+
| s_no | c_no  | sc_degree |
+------+-------+-----------+
| 103  | 3-105 |        92 |
+------+-------+-----------+

-- 40. 查询和"李军"同性别的所有同学的s_name

SELECT * FROM student;
SELECT * FROM teacher;
SELECT * FROM course;
SELECT * FROM score;

SELECT s_name
FROM student
WHERE s_sex = (
 SELECT s_sex
 FROM student
 WHERE s_name = '李军'
);

+--------+
| s_name |
+--------+
| 曾华   |
| 匡明   |
| 李军   |
| 陆军   |
| 王尼玛 |
| 张全蛋 |
| 赵铁柱 |
+--------+


-- 
-- 41.查询和"李军"同性别并且同班的所有同学的s_name
SELECT s_name
FROM student
WHERE s_sex = (
 SELECT s_sex
 FROM student
 WHERE s_name = '李军'
	) 
AND  s_class = (
	SELECT s_class 
	FROM student 
	WHERE s_name = '李军'
);
+--------+
| s_name |
+--------+
| 曾华   |
| 李军   |
| 王尼玛 |
+--------+


-- - 42. 查询所有选修'计算机导论'课程的'男'同学的成绩表

SELECT * FROM score WHERE c_no = (SELECT c_no FROM course WHERE c_name = '计算机导论' ) AND s_no IN(SELECT s_no FROM student WHERE s_sex = '男');

+------+-------+-----------+
| s_no | c_no  | sc_degree |
+------+-------+-----------+
| 109  | 3-105 |        76 |
+------+-------+-----------+

-- 

题目需要的数据

本文需要四个数据表

-- 查看学生表结构
DESCRIBE student;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| s_no       | varchar(20) | NO   | PRI | NULL    |       |
| s_name     | varchar(20) | NO   |     | NULL    |       |
| s_sex      | varchar(10) | NO   |     | NULL    |       |
| s_birthday | datetime    | YES  |     | NULL    |       |
| s_class    | varchar(20) | YES  |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+

--查看teacher表结构    DESCRIBE teacher;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| t_no       | varchar(20) | NO   | PRI | NULL    |       |
| t_name     | varchar(20) | NO   |     | NULL    |       |
| t_sex      | varchar(20) | NO   |     | NULL    |       |
| t_birthday | datetime    | YES  |     | NULL    |       |
| t_rof      | varchar(20) | NO   |     | NULL    |       |
| t_depart   | varchar(20) | NO   |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+

--查看course表结构    DESCRIBE course;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| c_no   | varchar(20) | NO   | PRI | NULL    |       |
| c_name | varchar(20) | NO   |     | NULL    |       |
| t_no   | varchar(20) | NO   | MUL | NULL    |       |
+--------+-------------+------+-----+---------+-------+

--查看score表结构    DESCRIBE score;
+-----------+---------------+------+-----+---------+-------+
| Field     | Type          | Null | Key | Default | Extra |
+-----------+---------------+------+-----+---------+-------+
| s_no      | varchar(20)   | NO   | PRI | NULL    |       |
| c_no      | varchar(20)   | NO   | MUL | NULL    |       |
| sc_degree | decimal(10,0) | YES  |     | NULL    |       |
+-----------+---------------+------+-----+---------+-------+

插入该数据见之前的文章 link

你可能感兴趣的:(MySQL,数据库,学习)