sql语句练习50题(Mysql版)

数据来自@廖致君
参考@启明星的指引

数据字典

可以复制到电脑的记事本或者备忘录,方便及时查阅,加粗的是主键。
1、学生表
Student(Sid, Sname, Sage, Ssex)——学生表(学号,姓名,出生日期,性别)
2、课程表
Course(Cid, Cname, Tid)——课程表(课程编号,课程名称,教师编号)
3、教师表
Teacher(Tid, Tname)——教师表(教师编号,教师姓名)
4、成绩表
SC(Sid, Cid, score)——成绩表(学号,课程编号,成绩)

数据导入

创建表+插入数值

学生表

创建表

CREATE TABLE `Student`(
	`Sid` VARCHAR(6),
	`Sname` VARCHAR(10) NOT NULL DEFAULT '',
	`Sage` DATETIME NOT NULL DEFAULT '',
	`Ssex` VARCHAR(10) NOT NULL DEFAULT '',
	PRIMARY KEY(`Sid`)
);

插入数值

create table Student(Sid varchar(6), Sname varchar(10), Sage datetime, Ssex varchar(10));
insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
insert into Student values('02' , '钱电' , '1990-12-21' , '男');
insert into Student values('03' , '孙风' , '1990-05-20' , '男');
insert into Student values('04' , '李云' , '1990-08-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吴兰' , '1992-03-01' , '女');
insert into Student values('07' , '郑竹' , '1989-07-01' , '女');
insert into Student values('08' , '王菊' , '1990-01-20' , '女')

课程表

创建表

CREATE TABLE `Course`(
	`Cid`  VARCHAR(10),
	`Cname` VARCHAR(10) NOT NULL DEFAULT '',
	`Tid` VARCHAR(10) NOT NULL,
	PRIMARY KEY(`Cid`)
);

插入数值

insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03')

教师表

创建表

CREATE TABLE `Teacher`(
	`Tid` VARCHAR(10),
	`Tname` VARCHAR(10) NOT NULL DEFAULT '',
	PRIMARY KEY(`Tid`)
);

插入数值

insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五')

成绩表

创建表

CREATE TABLE `SC`(
	`Sid` VARCHAR(20),
	`Cid`  VARCHAR(20),
	`score` DECIMAL(18,1),
	PRIMARY KEY(`Sid`,`Cid`)
);

插入数值

insert into SC values('01' , '01' , 80);
insert into SC values('01' , '02' , 90);
insert into SC values('01' , '03' , 99);
insert into SC values('02' , '01' , 70);
insert into SC values('02' , '02' , 60);
insert into SC values('02' , '03' , 80);
insert into SC values('03' , '01' , 80);
insert into SC values('03' , '02' , 80);
insert into SC values('03' , '03' , 80);
insert into SC values('04' , '01' , 50);
insert into SC values('04' , '02' , 30);
insert into SC values('04' , '03' , 20);
insert into SC values('05' , '01' , 76);
insert into SC values('05' , '02' , 87);
insert into SC values('06' , '01' , 31);
insert into SC values('06' , '03' , 34);
insert into SC values('07' , '02' , 89);
insert into SC values('07' , '03' , 98)

题目

1、查询“01”课程比“02”课程成绩高的学生的信息及课程分数
2、查询“01”课程比“02”课程成绩低的学生的信息及课程分数
3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩
5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
6、查询“李”姓老师的数量
7、查询学过“张三”老师授课的同学的信息
8、查询没学过“张三”老师授课的同学的信息
9、查询学过编号为“01”并且也学过编号为“02”的课程的同学的信息
10、查询学过编号为“01”但是没有学过编号为“02”的课程的同学的信息
11、查询没有学全所有课程的同学的信息
12、查询至少有一门课与学号为“01”的同学所学相同的同学的信息
13、查询和“01”号的同学学习的课程完全相同的其他同学的信息
14、查询没学过“张三”老师讲授的任一门课程的学生姓名
15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
16、检索“01”课程分数小于60,按分数降序排列的学生信息
17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率(及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90)
19、按各科成绩进行排序,并显示排名
20、查询学生的总成绩并进行排名
21、查询不同老师所教不同课程平均分从高到低显示
22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
24、查询学生平均成绩及其名次
25、查询各科成绩前三名的记录
26、查询每门课程被选修的学生数
27、查询出只选两门课程的全部学生的学号和姓名
28、查询男生、女生人数
29、查询名字中含有“风”字的学生信息
30、查询同名同性学生名单,并统计同名人数
31、查询1990年出生的学生名单
32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
34、查询课程名称为“数学”,且分数低于60的学生姓名和分数
35、查询所有学生的课程及分数情况
36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数
37、查询不及格的课程
38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名
39、求每门课程的学生人数
40、查询选修“张三”老师所授课程的学生中,成绩最高的学生信息及其成绩
41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
42、查询每门功成绩最好的前两名
43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
44、检索至少选修两门课程的学生学号
45、查询选修了全部课程的学生信息
46、查询各学生的年龄
47、查询本周过生日的学生
48、查询下周过生日的学生
49、查询本月过生日的学生
50、查询下月过生日的学生

运行结果

1、查询“01”课程比“02”课程成绩高的学生的信息及课程分数

SELECT Student.Sid AS 学生编号, Sname AS 学生姓名, 01_score AS 课程1, 02_score AS 课程2 FROM Student,
	(SELECT * FROM
		(select Sid AS 01_Sid, Cid AS 01_Cid, score AS 01_score FROM SC WHERE Cid='01')AS A,
		(select Sid AS 02_Sid, Cid AS 02_Cid, score AS 02_score FROM SC WHERE Cid='02')AS B
	WHERE A.01_Sid=B.02_Sid
	AND A.01_score>B.02_score )AS C 
WHERE Student.Sid=C.01_Sid;

运行结果:
sql语句练习50题(Mysql版)_第1张图片
2、查询“01”课程比“02”课程成绩低的学生的信息及课程分数

SELECT Student.Sid AS 学生编号, Sname AS 学生姓名, 01_score AS 课程1, 02_score AS 课程2 FROM Student,
	(SELECT * FROM
		(select Sid AS 01_Sid, Cid AS 01_Cid, score AS 01_score FROM SC WHERE Cid='01')AS A,
		(select Sid AS 02_Sid, Cid AS 02_Cid, score AS 02_score FROM SC WHERE Cid='02')AS B
	WHERE A.01_Sid=B.02_Sid
	AND A.01_score<B.02_score )AS C 
WHERE Student.Sid=C.01_Sid;

运行结果:
sql语句练习50题(Mysql版)_第2张图片

3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩

SELECT Student.Sid AS 学生编号, Sname AS 学生姓名, avg_score AS 平均成绩 
FROM Student,
(
	SELECT Sid, ROUND(AVG(score),2) AS avg_score FROM SC 
	GROUP BY Sid
	HAVING AVG(score)>=60) AS A 
WHERE Student.Sid=A.Sid

运行结果:
sql语句练习50题(Mysql版)_第3张图片
4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩

SELECT Student.Sid AS 学生编号, Sname AS 学生姓名, avg_score AS 平均成绩 
FROM Student,
(
	SELECT Sid, ROUND(AVG(score),2) AS avg_score FROM SC 
	GROUP BY Sid
	HAVING AVG(score)<60) AS A 
WHERE Student.Sid=A.Sid

运行结果:
sql语句练习50题(Mysql版)_第4张图片
5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩

SELECT SC.Sid AS 学生编号, Sname AS 学生姓名, COUNT(Cid) AS 选课数,SUM(score) AS 总成绩
FROM SC, Student
WHERE SC.Sid=Student.Sid
GROUP BY SC.Sid;

运行结果:
sql语句练习50题(Mysql版)_第5张图片
6、查询“李”姓老师的数量

SELECT count(*) AS 姓李
FROM Teacher
WHERE Tname LIKE '李%';

运行结果:
sql语句练习50题(Mysql版)_第6张图片

7、查询学过“张三”老师授课的同学的信息

SELECT * FROM Student 
WHERE Sid IN
	(SELECT Sid FROM SC,
		(SELECT Cid FROM Teacher, Course
		WHERE Teacher.Tid=Course.Tid
		AND Tname='张三') AS A 
	WHERE SC.Cid=A.Cid);

运行结果:
sql语句练习50题(Mysql版)_第7张图片
8、查询没学过“张三”老师授课的同学的信息

SELECT * FROM Student 
WHERE Sid NOT IN
	(SELECT Sid FROM SC,
		(SELECT Cid FROM Teacher, Course
		WHERE Teacher.Tid=Course.Tid
		AND Tname='张三') AS A 
	WHERE SC.Cid=A.Cid);

运行结果:
sql语句练习50题(Mysql版)_第8张图片
9、查询学过编号为“01”并且也学过编号为“02”的课程的同学的信息

SELECT Sid, Sname, Sage, Ssex FROM Student,
	(SELECT * FROM
	(SELECT Sid AS A_Sid FROM SC WHERE Cid='01') AS A,
	(SELECT Sid AS B_Sid FROM SC WHERE Cid='02') AS B
	WHERE A.A_Sid=B.B_Sid) AS C
WHERE Student.Sid=C.A_Sid;

运行结果:
sql语句练习50题(Mysql版)_第9张图片
10、查询学过编号为“01”但是没有学过编号为“02”的课程的同学的信息

SELECT * FROM Student
WHERE Sid IN
(SELECT Sid FROM SC
WHERE Cid='01') 
AND Sid NOT IN
(SELECT Sid FROM SC
WHERE Cid='02') 

运行结果:sql语句练习50题(Mysql版)_第10张图片
11、查询没有学全所有课程的同学的信息

SELECT * FROM Student WHERE Sid IN
	(SELECT Sid FROM SC
	GROUP BY Sid
	HAVING COUNT(Cid)< (SELECT COUNT(*) FROM Course));

运行结果:
sql语句练习50题(Mysql版)_第11张图片
12、查询至少有一门课与学号为“01”的同学所学相同的同学的信息

SELECT * FROM Student WHERE Sid IN
(SELECT DISTINCT Sid from SC WHERE Cid in (
SELECT Cid FROM SC WHERE Sid='01') AND Sid <> '01') ;

运行结果:

sql语句练习50题(Mysql版)_第12张图片
13、查询和“01”号的同学学习的课程完全相同的其他同学的信息

SELECT * FROM Student WHERE Sid in ( 
SELECT Sid FROM
(SELECT GROUP_CONCAT(Cid ORDER BY Cid)AS group1 FROM SC 
WHERE Sid='01')AS A ,
(SELECT Sid, GROUP_CONCAT(Cid ORDER BY Cid)AS group2 FROM SC 
WHERE Sid!='01' GROUP BY Sid)AS B
WHERE A.group1=B.group2)

运行结果:
sql语句练习50题(Mysql版)_第13张图片
14、查询没学过“张三”老师讲授的任一门课程的学生姓名

SELECT Sname FROM Student, 
	(SELECT Sid FROM SC WHERE Cid IN
		(SELECT Cid FROM Course, Teacher
		WHERE Course.Tid=Teacher.Tid
		AND Teacher.Tname='张三'))AS A 
WHERE Student.Sid=A.Sid

运行结果:sql语句练习50题(Mysql版)_第14张图片

15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩

SELECT Student.Sid AS 学号, Student.Sname AS 姓名, AVG(score) AS 平均成绩
FROM Student, SC
WHERE Student.Sid=SC.Sid
AND score < 60
GROUP BY Student.Sid 
HAVING COUNT(score)>=2

运行结果:sql语句练习50题(Mysql版)_第15张图片
16、检索“01”课程分数小于60,按分数降序排列的学生信息

SELECT Student.Sid AS 学号, Sname AS 姓名, score AS 成绩 FROM SC, Student
WHERE Cid='01' AND score<60 AND SC.Sid=Student.Sid
ORDER BY score DESC

运行结果:
sql语句练习50题(Mysql版)_第16张图片
17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩

SELECT Sid AS 学号, 01_score AS '课程1成绩', 02_score AS '课程2成绩', 03_score AS '课程3成绩', avg_score AS 平均成绩 from 
(	SELECT Sid,
	SUM(case when Cid='01' THEN score ELSE null END) AS 01_score,
	SUM(case when Cid='02' THEN score ELSE null END) AS 02_score,
	SUM(case when Cid='03' THEN score ELSE null END) AS 03_score,
	AVG(score) AS avg_score
	FROM SC
	GROUP BY Sid) AS A 
ORDER BY avg_score;

运行结果:
sql语句练习50题(Mysql版)_第17张图片

18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率(及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90)

SELECT SC.Cid AS 课程编号, Cname AS 课程名称, MAX(score) AS 最大值, MIN(score) AS 最小值, ROUND(AVG(score),2) AS 平均成绩, 
CONCAT(ROUND(SUM(CASE WHEN score>= 60 THEN 1 ELSE 0 END)/COUNT(*)*100,2),"%") AS 及格率,
CONCAT(ROUND(SUM(CASE WHEN score>= 70 AND score < 80 THEN 1 ELSE 0 END)/COUNT(*)*100,2),"%") AS 中等率,
CONCAT(ROUND(SUM(CASE WHEN score>= 80 AND score < 90 THEN 1 ELSE 0 END)/COUNT(*)*100,2),"%") AS 优良率,
CONCAT(ROUND(SUM(CASE WHEN score>= 90 AND score < 100 THEN 1 ELSE 0 END)/COUNT(*)*100,2),"%") AS 优秀率
FROM SC, Course
WHERE SC.Cid = Course.Cid
GROUP BY SC.Cid;

运行结果:sql语句练习50题(Mysql版)_第18张图片

19、按各科成绩进行排序,并显示排名

SELECT Cid, score, DENSE_RANK()over(PARTITION BY Cid ORDER BY score DESC)AS rank_score
FROM SC

运行结果:
sql语句练习50题(Mysql版)_第19张图片

20、查询学生的总成绩并进行排名

SELECT *, DENSE_RANK() over(ORDER BY sum_score DESC) AS rank_score
FROM
(SELECT Sid, SUM(score) AS sum_score FROM SC
GROUP BY Sid) AS A

运行结果:
sql语句练习50题(Mysql版)_第20张图片

21、查询不同老师所教不同课程平均分从高到低显示

SELECT Course.Cid, Teacher.Tname, Teacher.Tid, DENSE_RANK()over(ORDER BY '平均成绩') AS rank_score 
FROM Teacher,Course,
(SELECT Cid, avg(score) AS '平均成绩' FROM SC
GROUP BY Cid) AS A 
WHERE Teacher.Tid = Course.Tid
AND Course.Cid = A.Cid 

运行结果:
sql语句练习50题(Mysql版)_第21张图片

22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩

SELECT Student.Sid, Sname, Cid, score, rank_score FROM Student 
JOIN
(SELECT Sid, Cid, score, rank_score from 
(SELECT *, DENSE_RANK()over(PARTITION BY Cid ORDER BY score DESC) AS rank_score
FROM SC) AS A 
WHERE A.rank_score>=2 AND A.rank_score<=3) AS B 
ON Student.Sid=B.Sid

运行结果:
sql语句练习50题(Mysql版)_第22张图片

23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比

SELECT Course.Cid AS 课程编号, Cname AS 课程名称, 100_85,85_70 ,70_60,60_0
FROM Course
LEFT JOIN (SELECT Cid, 
CONCAT(ROUND(SUM(CASE WHEN score >= 85 AND score <= 100 THEN 1 ELSE 0 END)/ count(*) * 100, 2),"%") AS 100_85 ,
CONCAT(ROUND(SUM(CASE WHEN score >= 70 AND score <= 85 THEN 1 ELSE 0 END)/ count(*) * 100, 2),"%") AS 85_70 ,
CONCAT(ROUND(SUM(CASE WHEN score >= 60 AND score < 70 THEN 1 ELSE 0 END)/ count(*) * 100, 2),"%") AS 70_60,
CONCAT(ROUND(SUM(CASE WHEN score >= 0 AND score < 60 THEN 1 ELSE 0 END)/ count(*) * 100, 2),"%") AS 60_0
FROM SC GROUP BY Cid) A
ON A.Cid=Course.Cid

运行结果:
sql语句练习50题(Mysql版)_第23张图片
24、查询学生平均成绩及其名次

SELECT *, DENSE_RANK()over(ORDER BY avg_score) AS rank_score
FROM
(SELECT Sid, AVG(score)AS avg_score FROM SC
GROUP BY Sid) AS A

运行结果:sql语句练习50题(Mysql版)_第24张图片

25、查询各科成绩前三名的记录

SELECT * FROM
(SELECT *, DENSE_RANK() over(PARTITION BY Cid ORDER BY score) AS rank_score 
FROM SC) AS A 
WHERE rank_score <=3

运行结果:sql语句练习50题(Mysql版)_第25张图片

26、查询每门课程被选修的学生数

SELECT Cid AS 课程编号, COUNT(Sid) AS 选课人数 
FROM SC 
GROUP BY Cid;

运行结果:
sql语句练习50题(Mysql版)_第26张图片
27、查询出只选两门课程的全部学生的学号和姓名

 SELECT Student.Sid AS 学生编号, Sname AS 学生姓名 
FROM Student,
(SELECT Sid FROM SC
GROUP BY Sid
HAVING COUNT(Cid)=2) AS A 
WHERE Student.Sid=A.Sid

运行结果:sql语句练习50题(Mysql版)_第27张图片
28、查询男生、女生人数

SELECT Ssex AS 性别, COUNT(*) AS 数量 
FROM Student
GROUP BY Ssex;

运行结果:
sql语句练习50题(Mysql版)_第28张图片

29、查询名字中含有“风”字的学生信息

SELECT * FROM Student
WHERE Sname LIKE '%风%';

运行结果:sql语句练习50题(Mysql版)_第29张图片
30、查询同名同性学生名单,并统计同名人数

SELECT Student.Sname, Student.Ssex, COUNT(*) FROM Student
JOIN Student A 
ON A.Sid!=Student.Sid AND Student.Sname= A.Sname AND Student.Ssex= A.Ssex
GROUP BY Student.Sname, Student.Ssex;

运行结果:sql语句练习50题(Mysql版)_第30张图片
31、查询1990年出生的学生名单

SELECT * FROM Student
WHERE YEAR(Sage) = 1990;

运行结果:sql语句练习50题(Mysql版)_第31张图片

32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列

SELECT Cid AS 课程编号, ROUND(AVG(score),2) AS 平均成绩
FROM SC
GROUP BY Cid
ORDER BY 平均成绩 DESC, Cid

运行结果:
sql语句练习50题(Mysql版)_第32张图片
33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩

SELECT Student.Sid, Sname, avg_score
FROM Student,
(SELECT Sid, AVG(score) AS avg_score
FROM SC
GROUP BY Sid) AS A 
WHERE Student.Sid = A.Sid
AND avg_score >= 85

运行结果:
sql语句练习50题(Mysql版)_第33张图片
34、查询课程名称为“数学”,且分数低于60的学生姓名和分数

SELECT Sname, score FROM SC, Student, Course
WHERE SC.Sid = Student.Sid
AND SC.Cid = Course.Cid
AND Course.Cname='数学'
AND score < 60;

运行结果:
sql语句练习50题(Mysql版)_第34张图片
35、查询所有学生的课程及分数情况

SUM(CASE WHEN Cid='01' THEN score ELSE null END) AS 01_score,
SUM(CASE WHEN Cid='02' THEN score ELSE null END) AS 02_score,
SUM(CASE WHEN Cid='03' THEN score ELSE null END) AS 03_score
FROM SC
GROUP BY Sid

运行结果:
sql语句练习50题(Mysql版)_第35张图片
36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数

SELECT SC.Sid, Sname, Cname, score 
FROM SC, Student, Course
WHERE SC.Sid = Student.Sid 
AND Course.Cid= SC.Cid
AND score>70;

运行结果:
sql语句练习50题(Mysql版)_第36张图片

37、查询不及格的课程

SELECT Sid, SC.Cid, Cname FROM SC, Course
WHERE SC.Cid=Course.Cid
AND score<60

运行结果:
sql语句练习50题(Mysql版)_第37张图片
38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名

SELECT Sid, Sname FROM Student 
WHERE Sid IN(
SELECT Sid FROM SC
WHERE Cid='01' AND score > 80)

运行结果:
sql语句练习50题(Mysql版)_第38张图片
39、求每门课程的学生人数

SELECT SC.Cid AS 课程编号, Cname AS 课程名称, COUNT(Sid) AS 选课人数 
FROM SC, Course
WHERE SC.Cid=Course.Cid
GROUP BY SC.Cid

运行结果:sql语句练习50题(Mysql版)_第39张图片
40、查询选修“张三”老师所授课程的学生中,成绩最高的学生信息及其成绩

SELECT * FROM
	(SELECT Sid, Cid, score, DENSE_RANK()over(ORDER BY score DESC) AS rank_score
	FROM SC WHERE Cid IN
		(SELECT Cid FROM Course, Teacher
		WHERE Course.Tid = Teacher.Tid AND Tname='张三')) AS A 
WHERE rank_score=1

运行结果:
sql语句练习50题(Mysql版)_第40张图片

41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩

select DISTINCT b.Sid,b.Cid,b.score from SC A,SC B 
	WHERE  A.Cid!=B.Cid AND A.score=B.score

运行结果:sql语句练习50题(Mysql版)_第41张图片

42、查询每门功成绩最好的前两名

SELECT * FROM
	(SELECT *, DENSE_RANK()over(PARTITION BY Cid ORDER BY score DESC) AS rank_score 
	FROM SC) AS A 
WHERE rank_score <=2

运行结果:
sql语句练习50题(Mysql版)_第42张图片

43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列

SELECT Cid AS 课程编号, (CASE WHEN COUNT(Sid)>5 THEN COUNT(Sid) ELSE '少于5人' END) AS '选修人数' 
FROM SC
GROUP BY Cid
ORDER BY 选修人数 DESC, Cid

运行结果:
sql语句练习50题(Mysql版)_第43张图片
44、检索至少选修两门课程的学生学号

SELECT Sid FROM SC
GROUP BY Sid
HAVING COUNT(Cid)>=2

运行结果:sql语句练习50题(Mysql版)_第44张图片

45、查询选修了全部课程的学生信息

SELECT * FROM Student WHERE Sid IN(
SELECT Sid FROM SC
GROUP BY Sid
HAVING COUNT(Cid)=(SELECT COUNT(*) FROM Course))

运行结果:
sql语句练习50题(Mysql版)_第45张图片
46、查询各学生的年龄

SELECT Sid AS 学生编号, Sname AS 姓名, TIMESTAMPDIFF(YEAR,Sage,NOW())AS 年龄
FROM Student

运行结果:
sql语句练习50题(Mysql版)_第46张图片
47、查询本周过生日的学生

SELECT * FROM Student
WHERE WEEK(Sage)=WEEK(NOW());

运行结果:
sql语句练习50题(Mysql版)_第47张图片
48、查询下周过生日的学生

SELECT * FROM Student
WHERE WEEK(Sage)=WEEK(NOW())+1;

运行结果:
sql语句练习50题(Mysql版)_第48张图片
49、查询本月过生日的学生

SELECT * FROM Student
WHERE MONTH(Sage)=MONTH(NOW());

运行结果:
sql语句练习50题(Mysql版)_第49张图片
50、查询下月过生日的学生

SELECT * FROM Student
WHERE MONTH(Sage)=MONTH(NOW())+1;

运行结果:sql语句练习50题(Mysql版)_第50张图片

你可能感兴趣的:(sql语句练习50题(Mysql版))