sql50道題及答案詳解
建立表語句
create table Student(sid 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(cid varchar(10),cname varchar(10),tid varchar(10));
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));
insert into Teacher values('01' , '張三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
create table SC(sid varchar(10),cid 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);
12345678910111213141516171819202122232425262728293031323334353637
這裡是資料庫欄位的結構圖
1.查詢“某1”課程比“某2”課程成績高的所有學生的學號;
select a.sid from (select sid,score from SC where cid=1) a,(select sid,score from SC where cid=2) b where a.score>b.score and a.sid=b.sid;
2.查詢平均成績大於60分的同學的學號和平均成績;
select sid,AVG(score) from SC group by sid having avg(score) > 60;
3.查詢所有同學的學號、姓名、選課數、總成績
select Student.sid,Student.sname,count(SC.cid),sum(SC.score) from Student left join SC on Student.sid=SC.sid group by sid,sname;
4.查詢姓“李”的老師的個數;
select count(Teacher.tid) from Teacher where Teacher.tname like '李%';
5.查詢沒學過“張三”老師課的同學的學號、姓名;
select s.sid,s.sname from Student as s where sid not in(select DISTINCT(s.sid) from Course as c,SC as s,Teacher as t where c.cid=s.cid AND c.tid=t.tid AND t.tname='張三');
6.查詢學過“```”並且也學過編號“```”課程的同學的學號、姓名;
select a.sid,a.sname from
(select s.sid,s.sname from Student as s,Course as c,SC as sc where c.cname='數學' AND s.sid=sc.sid AND c.cid=sc.cid) a,
(select s.sid,s.sname from Student as s,Course as c,SC as sc where c.cname='語文' AND s.sid=sc.sid AND c.cid=sc.cid) b
WHERE a.sid=b.sid;
7.查詢學過“張三”老師所教的所有課的同學的學號、姓名;
SELECT a.sid,a.sname from (select s.sid,s.sname from Student as s,Course as c,SC as sc ,Teacher as t where t.tname='王五' AND s.sid=sc.sid AND c.cid=sc.cid AND c.tid=t.tid) a;
8.查詢課程編號“01”的成績比課程編號“02”課程低的所有同學的學號、姓名;
SELECT DISTINCT(a.sid),a.sname from (select sc.score,s.sid,s.sname from Student as s,SC as sc where sc.cid=01) a, (select sc.score,s.sid,s.sname from Student as s,SC as sc where sc.cid=02) b where a.score<b.score AND a.sid=b.sid;
9.查詢所有課程成績小於60分的同學的學號、姓名;
select s.sid,s.sname,sc.cid from Student as s,SC as sc where sc.score<60 AND sc.sid=s.sid;
10.查詢沒有學全所有課的同學的學號、姓名;
select s.sid,s.sname from Student as s,SC as sc where s.sid=sc.sid GROUP BY s.sid,s.sname HAVING count(sc.cid)<(select count(cid) from Course);
11.查詢至少有一門課與學號為“01”的同學所學相同的同學的學號和姓名;
SELECT Student.sid,Student.sname FROM SC,Student WHERE EXISTS (
SELECT cid FROM SC WHERE sid=01 AND SC.cid=cid
) AND Student.sid=SC.sid GROUP BY Student.sid,Student.sname;
12.查詢和"01"號的同學學習的課程完全相同的其他同學的學號和姓名
select * from Student where sid in (
SELECT sid FROM SC WHERE
cid in (SELECT cid FROM SC WHERE sid=01) AND sid!=01 GROUP BY sid HAVING COUNT(*)=(SELECT COUNT(*) FROM SC WHERE sid=01)
)
13.把“SC”表中“張三”老師教的課的成績都更改為此課程的平均成績;
update SC,(
SELECT t.tid,avg(score) as ascore
from Course as c,SC as sc,Teacher as t
WHERE t.tname='張三' AND c.tid=t.tid AND c.cid=sc.cid
)a set score=a.ascore
;
14、查詢沒學過"張三"老師講授的任一門課程的學生姓名
SELECT * FROM Student WHERE sid in(
SELECT sid FROM SC WHERE cid NOT in(
SELECT cid
FROM Teacher,Course WHERE Teacher.tname='張三' AND Teacher.tid=Course.tid
)
)
15、查詢兩門及其以上不及格課程的同學的學號,姓名及其平均成績
SELECT s.sid,s.sname,AVG(score)
FROM SC sc JOIN Student s on sc.sid=s.sid WHERE
sc.score<60 GROUP BY s.sid,s.sname HAVING COUNT(sc.score)>=2
16、檢索"01"課程分數小於60,按分數降序排列的學生資訊
SELECT Student.*,SC.score from Student,SC WHERE EXISTS (
SELECT sid,score FROM SC WHERE cid=01 AND score<60 AND sid=Student.sid
) AND Student.sid=SC.sid AND SC.cid=01 ORDER BY SC.score DESC;
17、按平均成績從高到低顯示所有學生的所有課程的成績以及平均成績
SELECT a.*
,SUM(CASE WHEN b.cid='01' THEN b.score ELSE 0 END) AS s01
,SUM(CASE WHEN b.cid='02' THEN b.score ELSE 0 END) AS s02
,SUM(CASE WHEN b.cid='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.sid=b.sid
GROUP BY 1,2,3,4
ORDER BY avs DESC;
18.查詢各科成績最高分、最低分和平均分:以如下形式顯示:課程ID,課程name,最高分,最低分,平均分,及格率,中等率,優良率,優秀率
SELECT a.cid
,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.cid=b.cid
GROUP BY 1,2;
19.按各科平均成績從低到高和及格率的百分數從高到低順序
SELECT cid,avg_score,CONCAT(de,'%') 及格率
FROM(
SELECT sc0.cid,
AVG(sc0.score) avg_score,
CAST((
SELECT COUNT(1) FROM SC WHERE cid=sc0.cid AND score>=60
)*1.0/(
SELECT COUNT(1) FROM SC WHERE cid=sc0.cid
)*100 AS DECIMAL(10,2)) de
FROM SC sc0
GROUP BY sc0.cid
ORDER BY avg_score,de DESC)a;
20、查詢學生的總成績並進行排名
select a.sid,a.sname,a.sage,a.sum,count(b.sum) rank
from
(select a.sid,
a.sname,
a.sage,
a.ssex,
sum(b.score) sum
from student a inner join sc b
on a.sid=b.sid
group by a.sid,
a.sname,
a.sage,
a.ssex) a left join
(select a.sid,
a.sname,
a.sage,
a.ssex,
sum(b.score) sum
from student a inner join sc b
on a.sid=b.sid
group by a.sid,
a.sname,
a.sage,
a.ssex) b
on 1=1
where b.sum>a.sum
group by a.sid,a.sname,a.sage,a.sum order by rank;
21、查詢不同老師所教不同課程平均分從高到低顯示
SELECT Course.tid,Teacher.tname,a.cid,a.avg_score
FROM (SELECT cid,avg(score) avg_score FROM SC GROUP BY cid ) a,Course,Teacher
WHERE a.cid=Course.cid AND Teacher.tid=Course.tid ORDER BY a.avg_score DESC;
22、查詢所有課程的成績第2名到第3名的學生資訊及該課程成績
SELECT s.*,sc0.*
FROM SC sc0 INNER JOIN Student s ON s.sid=sc0.sid
WHERE (SELECT COUNT(DISTINCT score) FROM SC WHERE sc0.cid=SC.cid AND sc0.score>SC.score) BETWEEN 2 AND 3
ORDER BY sc0.cid;
23、統計各科成績各分數段人數:課程編號,課程名稱,[100-85],[85-70],[70-60],[0-60]及所佔百分比
這道題 太複雜了懶的寫
SELECT cid,count(SELECT score FROM SC WHERE score BETWEEN 85 AND 100) a,count(SELECT score FROM SC WHERE score BETWEEN 85 AND 100)/count(score),count(score) FROM SC GROUP BY cid;
SELECT count(1) FROM (SELECT score FROM SC WHERE score BETWEEN 85 AND 100)a;
24、查詢學生平均成績及其名次
SELECT a.sid,
a.avg_score,
sum(case when a.avg_score<b.avg_score then 1 else 0 end)+1 scc
FROM (SELECT sid,avg(score) avg_score FROM SC GROUP BY sid) a INNER JOIN
(SELECT sid,avg(score) avg_score FROM SC GROUP BY sid) b
GROUP BY a.sid,
a.avg_score
ORDER BY scc;
25、查詢各科成績前三名的記錄
SELECT sc0.*,
(SELECT count(1) FROM SC WHERE SC.cid=sc0.cid AND SC.score>sc0.score)+1 rank
FROM SC sc0
GROUP BY 2,1,3
HAVING rank<=3
ORDER BY sc0.cid,rank;
26.查詢每門課程被選修的學生數
SELECT sc.cid,count(sc.sid) from Course c,SC sc WHERE c.cid=sc.cid GROUP BY sc.cid;
27.查詢出只選修了一門課程的全部學生的學號和姓名
SELECT s.sid,s.sname from SC as sc,Student AS s WHERE sc.sid=s.sid GROUP BY sc.sid,s.sname HAVING COUNT(cid)=2;
28、查詢男生、女生人數
SELECT count(ssex='女'),count(ssex='男') FROM Student;
29、查詢名字中含有"風"字的學生資訊
SELECT Student.* FROM Student WHERE sname like '%風%';
30、查詢同名同性學生名單,並統計同名人數
SELECT sname,ssex,count(1)
FROM Student GROUP BY 1,2
HAVING count(1)>1;
31、查詢1990年出生的學生名單(注:Student表中Sage列的型別是datetime)
SELECT Student.* FROM Student WHERE YEAR(sage)=1990;
32.查詢每門課程的平均成績,結果按平均成績升序排列,平均成績相同時,按課程號降序排列
SELECT cid,AVG(score) from SC GROUP BY cid order by avg(score),cid DESC;
37.查詢不及格的課程,並按課程號從大到小排列
SELECT cid,score FROM SC WHERE score<60 order by cid DESC;
38.查詢課程編號為"01"且課程成績在60分以上的學生的學號和姓名;
SELECT s.sid,s.sname,score,sc.cid
FROM SC as sc,Student as s
WHERE sc.sid=s.sid AND sc.cid=01 AND score>60;
40.查詢選修“張三”老師所授課程的學生中,成績最高的學生姓名及其成績
這道題寫的時候想出了兩種方達,都能實現
SELECT *
FROM (SELECT s.sid as aid,s.sname as aname,sc.score as ascore,t.tname FROM SC as sc,Student s,Course as c,Teacher as t
WHERE sc.sid=s.sid AND c.cid=sc.cid AND c.tid=t.tid AND t.tname="張三" ORDER BY score) a LIMIT 0,1;
SELECT s.sid,s.sname,a.ascore
FROM SC as sc,Student s,Course as c,Teacher as t ,
(SELECT MAX(score) ascore FROM SC as sc,Student s,Course as c,Teacher as t
WHERE sc.sid=s.sid AND c.cid=sc.cid AND c.tid=t.tid AND t.tname="張三" ) a
WHERE sc.sid=s.sid AND c.cid=sc.cid AND c.tid=t.tid AND t.tname="張三" AND sc.score=a.ascore;
42、查詢每門功課成績最好的前兩名
SELECT sc.* FROM SC sc WHERE (SELECT COUNT(1) FROM SC WHERE SC.cid=sc.cid AND score>sc.score)<2 ORDER BY sc.cid,sc.score;
43、統計每門課程的學生選修人數(超過5人的課程才統計)。要求輸出課程號和選修人數,查詢結果按人數降序排列,若人數相同,按課程號升序排列
SELECT cid,count(1) count
FROM SC GROUP BY cid HAVING count(1)>=5 ORDER BY count desc,cid ;
44、檢索至少選修兩門課程的學生學號
SELECT a.*,b.count
FROM Student a INNER JOIN (SELECT sid,count(cid) count FROM SC GROUP BY sid HAVING count>1)b
ON a.sid=b.sid GROUP BY 1,2,3,4;
45、查詢選修了全部課程的學生資訊
SELECT sid,count(cid) count_c FROM SC
GROUP BY sid
HAVING count_c=(SELECT COUNT(1) FROM Course);
46、查詢各學生的年齡
SELECT Student.*,YEAR(CURDATE())-YEAR(Student.sage) FROM Student;
47、查詢本週過生日的學生
SELECT * FROM Student WHERE WEEKOFYEAR(NOW())-WEEKOFYEAR(sage)=0;
48、查詢下週過生日的學生
SELECT * FROM Student WHERE WEEKOFYEAR(NOW())-WEEKOFYEAR(sage)=-1;
49、查詢本月過生日的學生
SELECT * FROM Student WHERE MONTH(NOW())-MONTH(sage)=0;
50、查詢下月過生日的學生
SELECT * FROM Student WHERE MONTH(NOW())-MONTH(sage)=-1;