SQL考试常见题目

--1.学生表
Student(S,Sname,Sage,Ssex) --S 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别
--2.课程表 
Course(C,Cname,T) --C --课程编号,Cname 课程名称,T 教师编号
--3.教师表 
Teacher(T,Tname) --T 教师编号,Tname 教师姓名
--4.成绩表 
SC(S,C,score) --S 学生编号,C 课程编号,score 分数
*/
--创建测试数据
create table Student(S varchar(10),Sname varchar(10),Sage datetime,Ssex nvarchar(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(C varchar(10),Cname,varchar(10),T varchar(10))
insert into Course values('01' , '语文' , '02')
insert into Course values('02' , '数学' , '01')
insert into Course values('03' , '英语' , '03')
create table Teacher(T varchar(10),Tname,varchar(10))
insert into Teacher values('01' , '张三')
insert into Teacher values('02' , '李四')
insert into Teacher values('03' , '王五')
create table SC(S varchar(10),C varchar(10),score decimal(18,1))
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)

mysql...
--1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
SELECT a.*,b.score AS'01分数',c.score AS '02分数'
FROM student a
INNER JOIN sc b
ON a.s=b.s AND b.c='01'
INNER JOIN sc c
ON a.s=c.s AND c.c='02'
WHERE b.score>c.score
--2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
--3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
SELECT a.s
       ,a.sname
       ,AVG(b.score) AS avgnum
FROM student a
INNER JOIN sc b
ON a.s=b.s
GROUP BY a.s
       ,a.sname
HAVING AVG(b.score)>60
--4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩
--5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
SELECT a.s
       ,a.sname
       ,COUNT(b.c)
       ,SUM(b.score)
FROM student a
LEFT JOIN sc b
ON a.s=b.s
GROUP BY 1,2
--6、查询"李"姓老师的数量 
SELECT COUNT(1) FROM teacher WHERE tname LIKE '李%'
--7、查询学过"张三"老师授课的同学的信息 
SELECT a.*
FROM student a
INNER JOIN sc b
ON a.s=b.s
INNER JOIN course c
ON b.c=c.c
INNER JOIN teacher d
ON c.t=d.t
WHERE d.tname='张三'
GROUP BY 1,2,3,4
--方法2
SELECT a.*
FROM student a
LEFT JOIN (
	SELECT a.*
	FROM student a
	INNER JOIN sc b
	ON a.s=b.s
	INNER JOIN course c
	ON b.c=c.c
	INNER JOIN teacher d
	ON c.t=d.t
	WHERE d.tname='张三'
	GROUP BY 1,2,3,4
)b
ON a.s=b.s
WHERE b.s IS NULL

--8、查询没学过"张三"老师授课的同学的信息 
SELECT a.*
FROM student a
LEFT JOIN sc b
ON a.s=b.s						//找出所有学生学的课
WHERE NOT EXISTS(
		SELECT * 
		FROM course aa
		INNER JOIN teacher b
		ON aa.t=b.t
		INNER JOIN sc c
		ON aa.c=c.c					//找出所有张三老师教的课
		WHERE b.tname='张三'
		AND c.s=a.s
	)
GROUP BY 1,2,3,4


我的答案:SELECT
stu.*
FROM
student stu
WHERE stu.s NOT IN
(SELECT
s.s
FROM
teacher t INNER JOIN course c ON t.T=c.t_id INNER JOIN sc s ON c.id=s.c WHERE t.Tname='张三')
--9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
SELECT a.*
FROM student a
INNER JOIN sc b				//找出所有学过01课程的学生
ON a.s=b.s AND b.c='01'
INNER JOIN sc c
ON a.s=c.s AND c.c='02'		//在01的基础上找出所有学过02基础的学生
--10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
select *
from student a
left join sc b			//左链接的作用是使左表为一张大表,使得右表没匹配的字段为空
on a.s=b.s and b.c='01'   //学过01课程的学生
left join sc c
on a.s=c.s and c.c='02'   //学过02课程的学生
where b.c='01' and c.c is null    //因为左链接的缘故,没学过02的学生结果为空

方法二:
SELECT a1.*,b.c
FROM student a1
INNER JOIN sc b
ON a1.s=b.s AND b.c='01'    //所有学过01课程的学生
WHERE NOT EXISTS(
SELECT a2.*,b.c
FROM student a2
INNER JOIN sc b
ON a2.s=b.s AND b.c='01'
INNER JOIN sc c
ON a2.s=c.s AND c.c='02'		//学过01和02的学生
WHERE a1.s=a2.s)
--11、查询没有学全所有课程的同学的信息 
SELECT a.*
FROM student a
LEFT JOIN sc b
ON a.s=b.s
LEFT JOIN (SELECT COUNT(1) anum FROM course) c
ON 1=1
GROUP BY 1,2,3,4
HAVING MAX(c.anum)>COUNT(b.c)

方法二:
SELECT
(CASE WHEN (IFNULL(tab1.c1,0)+IFNULL(tab1.c2,0)+IFNULL(tab1.c3,0))!=06 THEN tab1.p END) qname,
(CASE WHEN (IFNULL(tab1.c1,0)+IFNULL(tab1.c2,0)+IFNULL(tab1.c3,0))!=06 THEN tab1.ename END) ss
FROM
(SELECT a.S p,a.Sname ename,b.c c1,c.c c2,d.c c3
FROM student a
LEFT JOIN sc b
ON a.s=b.s AND b.c='01'
LEFT JOIN sc c
ON a.s=c.s AND c.c='02'
LEFT JOIN sc d
ON a.s=d.s AND d.c='03') tab1
GROUP BY qname
HAVING IFNULL(qname,0)!=0

--12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息 
SELECT a.*
FROM student a
INNER JOIN sc b
ON a.s=b.s
WHERE EXISTS(
    SELECT 1 FROM sc WHERE s='01' AND c=b.c
)
GROUP BY 1,2,3,4
--13、查询和"01"号的同学学习的课程完全相同的其他同学的信息 
SELECT a.s,a.sname,a.sage,a.ssex
FROM (SELECT a.*,COUNT(b.c) AS snum
	FROM student a
	INNER JOIN sc b
	ON a.s=b.s
	WHERE EXISTS(
	    SELECT 1 FROM sc WHERE s='01' AND c=b.c
	)
	GROUP BY 1,2,3,4)a
INNER JOIN (SELECT a.*,COUNT(b.c) AS anum
	FROM student a
	INNER JOIN sc b
	ON a.s=b.s
	GROUP BY 1,2,3,4)b
ON a.s=b.s
INNER JOIN (SELECT COUNT(1) AS num1 FROM sc WHERE s='01')c
ON 1=1
WHERE a.snum=b.anum AND a.snum=c.num1
--方法二


SELECT a.*
	,COUNT(b.c) AS anum
	,SUM(CASE WHEN EXISTS(SELECT 1 FROM sc WHERE s='01' AND c=b.c) THEN 1 ELSE 0 END) AS snum
	,MAX(c.num1) AS num1
FROM student a
INNER JOIN sc b
ON a.s=b.s
INNER JOIN (SELECT COUNT(1) AS num1 FROM sc WHERE s='01')c
ON 1=1
GROUP BY 1,2,3,4
HAVING anum=snum AND anum=num1
--14、查询没学过"张三"老师讲授的任一门课程的学生姓名 
SELECT a.*
FROM student a
LEFT JOIN(
	SELECT a.s
	FROM student a
	LEFT JOIN sc b
	ON a.s=b.s
	LEFT JOIN course c
	ON b.c=c.c
	LEFT JOIN teacher d
	ON c.t=d.t AND d.tname='张三'
	WHERE d.t IS NOT NULL
	)b
ON a.s=b.s
WHERE b.s IS NULL
--15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 
SELECT a.s
       ,a.sname
       ,AVG(b.score)
FROM student a
INNER JOIN sc b
ON a.s=b.s
GROUP BY 1,2
HAVING SUM(CASE WHEN b.score>=60 THEN 0 ELSE 1 END)>=2
--16、检索"01"课程分数小于60,按分数降序排列的学生信息
SELECT a.*
       ,b.score
FROM student a
INNER JOIN sc b
ON a.s=b.s
WHERE 
  b.c='01'
  AND b.score<60
ORDER BY b.score DESC
--17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
SELECT a.*
       ,SUM(CASE WHEN b.c='01' THEN b.score ELSE 0 END) AS s01
       ,SUM(CASE WHEN b.c='02' THEN b.score ELSE 0 END) AS s02
       ,SUM(CASE WHEN b.c='03' THEN b.score ELSE 0 END) AS s03
       ,AVG(CASE WHEN b.score IS NULL THEN 0 ELSE b.score END) avs
FROM student a
LEFT JOIN sc b
ON a.s=b.s
GROUP BY 1,2,3,4
ORDER BY avs DESC

方法二:
SELECT
tab2.*,avge.av
FROM
(SELECT					//行列转换(没修的课程列为空,再相加)
tab1.s s,SUM(c1),SUM(c2),SUM(c3)
FROM
(SELECT
stu.s s,
(CASE WHEN s.c='01' THEN s.c END)c1,
(CASE WHEN s.c='02' THEN s.c END)c2,
(CASE WHEN s.c='03' THEN s.c END)c3
FROM
student stu
INNER JOIN 
sc s ON stu.s=s.s)tab1
GROUP BY tab1.s)tab2
INNER JOIN
(SELECT
AVG(score) av,s stu
FROM
sc
GROUP BY s) avge
ON avge.stu=tab2.s

--18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
--及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
SELECT a.c
       ,a.cname
       ,MAX(b.score)
       ,MIN(b.score)
       ,AVG(b.score)
       ,SUM(CASE WHEN b.score>=60 THEN 1 ELSE 0 END)/COUNT(1)
       ,SUM(CASE WHEN b.score>=70 AND b.score<80 THEN 1 ELSE 0 END)/COUNT(1)
       ,SUM(CASE WHEN b.score>=80 AND b.score<90 THEN 1 ELSE 0 END)/COUNT(1)
       ,SUM(CASE WHEN b.score>=90 THEN 1 ELSE 0 END)/COUNT(1)
FROM course a
INNER JOIN sc b
ON a.c=b.c
GROUP BY 1,2
--19、按各科成绩进行排序,并显示排名
SET @rn:=0;
SELECT a.*,@rn:=@rn+1
FROM(
    SELECT a.*,b.score
    FROM course a
    INNER JOIN sc b
    ON a.c=b.c
    WHERE a.c='01'
    ORDER BY b.c,score DESC
)a

方法二:
SELECT
tab1.*,(COUNT(*))rang
FROM
//排名,自连接,名次取决于分数大于自己的人数
(SELECT a.c c1,a.cname c2,b.s s1,b.score s2
    FROM course a
    INNER JOIN sc b
    ON a.c=b.c
    WHERE a.c='01'
    ORDER BY b.c,score DESC)tab1
    INNER JOIN
(SELECT a.c c1,a.cname c2,b.s s1,b.score s2
    FROM course a
    INNER JOIN sc b
    ON a.c=b.c
    WHERE a.c='01'
    ORDER BY b.c,score DESC)tab2
    ON tab1.s1!=tab2.s1  
    WHERE tab1.s2<=tab2.s2
    GROUP BY tab1.s1
ORDER BY rang



--20、查询学生的总成绩并进行排名
SELECT a.*
       ,COUNT(b.c)+1 asall
FROM sc a
LEFT JOIN sc b
ON a.c=b.c AND a.score85 THEN 1 ELSE 0 END) AS '[100-85]'
       ,SUM(CASE WHEN b.score<=85 AND b.score>70 THEN 1 ELSE 0 END) AS '[85-70]'
       ,SUM(CASE WHEN b.score<=70 AND b.score>60 THEN 1 ELSE 0 END) AS '[70-60]'
       ,SUM(CASE WHEN b.score<=60 AND b.score>0 THEN 1 ELSE 0 END) AS '[60-0]'

       ,SUM(CASE WHEN b.score<=100 AND b.score>85 THEN 1 ELSE 0 END)/COUNT(1) AS '[100-85]%'
       ,SUM(CASE WHEN b.score<=85 AND b.score>70 THEN 1 ELSE 0 END)/COUNT(1) AS '[85-70]%'
       ,SUM(CASE WHEN b.score<=70 AND b.score>60 THEN 1 ELSE 0 END)/COUNT(1) AS '[70-60]%'
       ,SUM(CASE WHEN b.score<=60 AND b.score>0 THEN 1 ELSE 0 END)/COUNT(1) AS '[60-0]%'
FROM course a
INNER JOIN sc b
ON a.c=b.c
GROUP BY 1,2
--24、查询学生平均成绩及其名次 
SELECT a.*
       ,COUNT(b.s)+1
FROM (
	SELECT a.*,AVG(CASE WHEN b.score IS NULL THEN 0 ELSE b.score END) AS ascore
	FROM student a
	LEFT JOIN sc b
	ON a.s=b.s
	GROUP BY 1,2,3,4
      )a
LEFT JOIN(
	SELECT a.*,AVG(CASE WHEN b.score IS NULL THEN 0 ELSE b.score END) AS ascore
	FROM student a
	LEFT JOIN sc b
	ON a.s=b.s
	GROUP BY 1,2,3,4
      )b
ON a.ascore1
--31、查询1990年出生的学生名单(注:Student表中Sage列的类型是datetime) 
SELECT * FROM student WHERE YEAR(sage)=1990
--32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号
SELECT a.*
       ,AVG(b.score) ascore
FROM course a
LEFT JOIN sc b
ON a.c=b.c
GROUP BY 1,2,3
ORDER BY ascore DESC,a.c
--33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩 
SELECT a.*
       ,AVG(b.score) ascore
FROM student a
LEFT JOIN sc b
ON a.s=b.s
GROUP BY 1,2,3,4
HAVING ascore>=85
--34、查询课程名称为"数学",且分数低于60的学生姓名和分数 
SELECT c.*
FROM course a
LEFT JOIN sc b
ON a.c=b.c
LEFT JOIN student c
ON b.s=c.s
WHERE a.cname='数学'
AND b.score<60
--35、查询所有学生的课程及分数情况; 
SELECT *
FROM sc a
INNER JOIN student b
ON a.s=b.s
INNER JOIN course c
ON a.c=c.c
--36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数; 
SELECT b.sname
       ,c.cname
       ,a.score
FROM sc a
INNER JOIN student b
ON a.s=b.s
INNER JOIN course c
ON a.c=c.c
WHERE a.score>70
--37、查询不及格的课程
--38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名; 
SELECT a.*
FROM student a
INNER JOIN sc b
ON a.s=b.s
WHERE b.c='01' AND b.score>80
--39、求每门课程的学生人数 
--40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
SELECT a.*,b.score
FROM student a
INNER JOIN sc b
ON a.s=b.s
INNER JOIN(
	SELECT c.c
	       ,MAX(c.score) AS maxscore
	FROM teacher a
	INNER JOIN course b
	ON a.t=b.t
	INNER JOIN sc c
	ON b.c=c.c
	WHERE a.tname='张三'
	GROUP BY c)c
ON b.c=c.c AND b.score=c.maxscore
--41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 
SELECT a.s
       ,a.c
       ,a.score
FROM sc a
INNER JOIN (
	SELECT a.score
	       ,b.s
	       ,COUNT(1)
	FROM sc a
	INNER JOIN student b
	ON a.s=b.s
	GROUP BY a.score,b.s
	HAVING COUNT(1)>1
)b
ON a.s=b.s AND a.score=b.score

方法二
SELECT a.*
FROM sc a
LEFT JOIN sc b
ON a.s=b.s AND a.score=b.score AND a.c<>b.c
GROUP BY 1,2,3
HAVING COUNT(b.s)>0
--42、查询每门功成绩最好的前两名 
--43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列  
SELECT a.c
       ,COUNT(1) AS pnum
FROM sc a
GROUP BY 1
HAVING pnum>5
ORDER BY pnum DESC,a.c
--44、检索至少选修两门课程的学生学号 
--45、查询选修了全部课程的学生信息 


--46、查询各学生的年龄
SELECT a.*,YEAR(CURDATE())-YEAR(a.sage)
FROM student a
--47、查询本周过生日的学生
SELECT a.*
FROM student a
WHERE CONCAT(MONTH(a.sage),DAY(a.sage))>=CONCAT(MONTH(SUBDATE(CURDATE(),WEEKDAY(CURDATE()))),DAY(SUBDATE(CURDATE(),WEEKDAY(CURDATE()))))
AND CONCAT(MONTH(a.sage),DAY(a.sage))<=CONCAT(MONTH(SUBDATE(CURDATE(),WEEKDAY(CURDATE())-6)),DAY(SUBDATE(CURDATE(),WEEKDAY(CURDATE())-6)))

--48、查询下周过生日的学生

--49、查询本月过生日的学生
SELECT a.* FROM student a WHERE MONTH(a.sage)=MONTH(CURDATE())
--50、查询下月过生日的学生
SELECT a.* FROM student a WHERE MONTH(a.sage)=MONTH(CURDATE())+1

 

你可能感兴趣的:(SQL考试常见题目)