虽然网上已经有很多相关资源,但是正确解答的优质资源并不太多,因此本篇文章将这50道题重新整理了一下,希望能给刚学习sql的童鞋一点帮助。
Student(S,Sname,Sage,Ssex) 学生表
Course(C,Cname,T) 课程表
SC(S,C,score) 成绩表
Teacher(T,Tname) 教师
CREATE TABLE `student` (
`S` varchar(10) NOT NULL,
`Sname` varchar(10) DEFAULT NULL,
`Sage` varchar(10) DEFAULT NULL,
`Ssex` varchar(15) DEFAULT NULL,
PRIMARY KEY (`S`)
);
CREATE TABLE `course` (
`C` varchar(20) NOT NULL,
`Cname` varchar(20) DEFAULT NULL,
`T` varchar(10) DEFAULT NULL,
PRIMARY KEY (`C`)
);
CREATE TABLE `teacher` (
`T` varchar(10) NOT NULL,
`Tname` varchar(20) DEFAULT NULL,
PRIMARY KEY (`T`)
);
CREATE TABLE `sc` (
`s` int NOT NULL,
`C` varchar(10) NOT NULL,
`score` varchar(10) DEFAULT NULL,
PRIMARY KEY (`s`,`C`)
);
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' , '女');
insert into Course values('01','CHINESE','02');
insert into Course values('02','MATH','01');
insert into Course values('03','ENGLISH','03');
insert into Teacher values('01','Li Pengfei');
insert into Teacher values('02','Wang Wen');
insert into Teacher values('03','Zhang Zhichao');
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);
SELECT a.s
FROM ( SELECT s,score FROM sc WHERE c='01') AS a,
( SELECT s,score FROM sc WHERE c='02') AS b
WHERE a.score>b.score AND a.s=b.s;
SELECT s,ROUND(AVG(score)) avg_score
FROM sc
GROUP BY s
HAVING avg_score>60;
SELECT s '学号',
ROUND(AVG(score),2) '平均成绩',
COUNT(c) '课程总数',
SUM(score) '总分'
FROM SC
GROUP BY s
HAVING 平均成绩>60;
SELECT count(Tname)
FROM teacher
WHERE Tname like '%li%';
SELECT s,sname
FROM student
WHERE sname NOT IN (SELECT DISTINCT(sname)
FROM student AS st
INNER JOIN sc
ON st.s=sc.s
INNER JOIN course AS co
ON sc.C=co.C
INNER JOIN teacher AS te
ON co.T=te.T
WHERE tname = 'Li Pengfei');
SELECT student.s,Sname
FROM sc,student
WHERE sc.s=student.s AND c='01'AND Sname IN(
SELECT sname
FROM sc,student
WHERE sc.s=student.s AND c='02');
SELECT s,sname
FROM student
WHERE s in (
SELECT sc.s
FROM sc
INNER JOIN course AS co
ON sc.C=co.C
INNER JOIN teacher AS te
ON co.T=te.T
WHERE tname = 'Li Pengfei'
GROUP BY sc.s
HAVING COUNT(1)= (SELECT COUNT(1) FROM course,teacher
WHERE course.t=teacher.t AND Tname='Li Pengfei'));
SELECT student.s,Sname
FROM student,
(SELECT s,score FROM sc WHERE c='01') AS t1,
(SELECT s,score FROM sc WHERE c='02') AS t2
WHERE student.s=t1.S AND t1.s=t2.s AND t1.score>t2.score;
SELECT student.s,sname
FROM student,sc
WHERE student.s=sc.s
GROUP BY student.s
HAVING SUM(CASE WHEN IFNULL(score,0)<80 THEN 1 ELSE 0 END)=COUNT(C);
SELECT student.s,Sname
FROM sc,student
WHERE sc.s=student.s
GROUP BY s
HAVING COUNT(c)!=(SELECT COUNT(c) FROM course );
SELECT DISTINCT student.s,sname
FROM student
INNER JOIN sc
ON student.s=sc.s
WHERE c in (SELECT c
FROM sc
WHERE s='01')
AND SC.S!='01'
SELECT s,Sname
FROM student
WHERE s IN (SELECT DISTINCT s
FROM sc
WHERE c in (SELECT DISTINCT c FROM sc WHERE s='07')
AND s!='07'
GROUP BY s
HAVING count(*)=(SELECT count(1) FROM sc WHERE s='07'));
UPDATE sc a JOIN
(SELECT ROUND(AVG(score)) t , sc.c from sc
JOIN course ON sc.c=course.c
JOIN teacher ON course.T = teacher.t
WHERE Tname='Li Pengfei'
GROUP BY c ) b
ON a.c=b.c
SET a.score= b.t;
SELECT student.s, Sname
FROM sc,student
WHERE sc.s=student.s AND sc.s!='07' AND
sc.s NOT IN ( SELECT s FROM sc
WHERE c NOT IN (SELECT c FROM sc WHERE s='07'))
GROUP BY sc.s
HAVING COUNT(*) = (SELECT COUNT(*) FROM SC WHERE s='07');
DELETE FROM sc WHERE c IN (
SELECT course.c FROM course,teacher
WHERE course.t=teacher.t AND Tname='Li Pengfei');
INSERT INTO sc
(SELECT s,02,(SELECT ROUNd(AVG(score),2) FROM sc WHERE c='02' )
FROM student
WHERE s NOT IN (SELECT s FROM sc WHERE c='03' ));
SELECT s,
IFNULL ((SELECT score FROM sc,course WHERE t.s=sc.s AND sc.C=course.C AND cNAME='MATH'),0) "数学",
IFNULL ((SELECT score FROM sc,course WHERE t.s=sc.s AND sc.C=course.C AND cNAME='CHINESE'),0) "语文",
IFNULL ((SELECT score FROM sc,course WHERE t.s=sc.s AND sc.C=course.C AND cNAME='ENGLISH'),0) "英语" ,
COUNT(c),
ROUND(AVG(score),2) avg_score
FROM sc t
GROUP BY S
ORDER BY avg_score DESC;
SELECT c course_id,
max(score) max,
min(score) min
FROM sc
GROUP BY c;
SELECT c course_id,
ROUND(AVG(score)) avg_score,
ROUND(SUM(CASE WHEN IFNULL(score,0)>=60 THEN 1 ELSE 0 END)/COUNT(c)*100,2) "RATE(%)"
FROM sc
GROUP BY c
ORDER BY avg_score ,"RATE(%)" DESC;
SELECT
round(sum(CASE WHEN Cname='MATH' THEN score ELSE 0 END)/sum(CASE WHEN Cname='MATH' THEN 1 ELSE 0 END),2 ) avg_score_math,
round(sum(CASE WHEN Cname='MATH' AND ifnull(score,0)>=60 THEN 1 ELSE 0 END)/sum(CASE WHEN Cname='MATH' THEN 1 ELSE 0 END)*100,2 ) 'math(%)',
round(sum(CASE WHEN Cname='chinese' THEN score ELSE 0 END)/sum(CASE WHEN Cname='chinese' THEN 1 ELSE 0 END),2 ) avg_score_chinese,
round(sum(CASE WHEN Cname='chinese' AND ifnull(score,0)>=60 THEN 1 ELSE 0 END)/sum(CASE WHEN Cname='chinese' THEN 1 ELSE 0 END)*100,2 ) 'chinese(%)',
round(sum(CASE WHEN Cname='english' THEN score ELSE 0 END)/sum(CASE WHEN Cname='english' THEN 1 ELSE 0 END),2 ) avg_score_english,
round(sum(CASE WHEN Cname='english' AND ifnull(score,0)>=60 THEN 1 ELSE 0 END)/sum(CASE WHEN Cname='english' THEN 1 ELSE 0 END)*100,2 ) 'english(%)'
FROM sc,course
WHERE sc.c=course.c;
SELECT teacher.t,Tname,course.cname,ROUND(AVG(score),2) avg_score
FROM sc,teacher,course
WHERE sc.c=course.c AND course.t=teacher.t
GROUP BY t,course.c
ORDER BY avg_score DESC;
SELECT * FROM
(SELECT c.*,ROW_NUMBER() OVER(ORDER BY c.avg_score DESC) AS rn
FROM
(SELECT student.s,sname,
SUM(CASE WHEN cname='math' then score ELSE 0 END) 'math',
SUM(CASE WHEN cname='chinese' then score ELSE 0 END) 'chinese',
SUM(CASE WHEN cname='english' then score ELSE 0 END) 'english',
ROUND(AVG(sc.score),2) avg_score
FROM student ,sc ,course
WHERE student.s=sc.s AND sc.c=course.c
GROUP BY student.s) c
)d
LIMIT 2,4;
SELECT course.c,cname,
SUM( CASE WHEN score<=100 AND score>85 THEN 1 ELSE 0 END) AS "100-85",
SUM(CASE WHEN score<=85 AND score>70 THEN 1 ELSE 0 END) AS "85-70",
SUM(CASE WHEN score<=70 AND score>60 THEN 1 ELSE 0 END) AS "70-60",
SUM(CASE WHEN score<=60 THEN 1 ELSE 0 END) AS "-60"
FROM sc,course
WHERE sc.c=course.c
GROUP BY course.c,cname;
SELECT s,Sname,平均成绩,
(SELECT 1+COUNT(平均成绩)
FROM (SELECT sc.s,Sname,ROUND(AVG(score),2) "平均成绩"
FROM sc,student
WHERE sc.s=student.s
GROUP BY sc.s) AS t1
WHERE t1.平均成绩 > t2.平均成绩) AS "名次"
FROM (SELECT sc.s,Sname,ROUND(AVG(score),2) "平均成绩"
FROM sc,student
WHERE sc.s=student.s
GROUP BY sc.s) AS t2
order by 平均成绩 DESC;
SELECT s,c,Sname,score,排名
FROM(SELECT sc.s,c,Sname,score,
ROW_NUMBER() over (PARTITION BY c order by score DESC) as "排名"
FROM sc,student
WHERE sc.s=student.s) t1
WHERE 排名<=3;
SELECT c,COUNT(s)
FROM sc
GROUP BY c;
SELECT sc.s,Sname,COUNT(c)
FROM sc
INNER JOIN student
ON sc.s=student.s
GROUP BY sc.s
HAVING COUNT(c)=2;
SELECT Ssex,COUNT(Ssex)
FROM student
GROUP BY Ssex;
SELECT Sname
FROM student
WHERE Sname like "张%";
SELECT Sname,Ssex,COUNT(*)
FROM student
GROUP BY Sname,Ssex
HAVING COUNT(*)>1;
SELECT Sname,Sage
FROM student
WHERE YEAR(Sage)='1990';
SELECT s,ROUND(AVG(score),2) avg_score
FROM sc
GROUP BY s
ORDER BY avg_score,C DESC;
SELECT sc.s,Sname,ROUND(AVG(score),2) avg_score
FROM SC
INNER JOIN student
ON sc.s=student.s
GROUP BY sc.s
HAVING avg_score>85;
SELECT student.s,Sname,score
FROM sc,student,course
WHERE sc.s=student.s AND course.c=sc.c AND Cname='MATH' AND score<60;
SELECT sc.s,sname,course.c,cname
FROM sc,student,course
WHERE sc.s=student.s AND sc.c=course.c;
SELECT sc.s,Sname,Cname,score
FROM sc
INNER JOIN student st
ON sc.s=st.s
INNER JOIN course co
ON sc.c=co.c
WHERE sc.s NOT IN (SELECT sc.s FROM sc WHERE score<70 )
ORDER BY sc.s;
SELECT sc.s,Sname,Cname,score
FROM sc
INNER JOIN student st
ON sc.s=st.s
INNER JOIN course co
ON sc.c=co.c
WHERE score<60;
SELECT sc.s,Sname,Cname,score
FROM sc
INNER JOIN student st
ON sc.s=st.s
INNER JOIN course co
ON sc.c=co.c
WHERE co.c='03' AND score>80;
SELECT count(DISTINCT s)
FROM sc;
SELECT sname,sc.c,score
FROM sc
INNER JOIN student
ON sc.s=student.s
INNER JOIN course
ON course.c=sc.c
INNER JOIN teacher
ON teacher.t=course.t
WHERE Tname="Li Pengfei"
LIMIT 1;
SELECT cname,count(DISTINCT(s))
FROM sc,course
GROUP BY Cname;
SELECT DISTINCT t1.s,t1.c,t1.score
FROM sc t1,sc t2
WHERE t1.s!=t2.s AND t1.score=t2.score
ORDER BY t1.s;
SELECT s,c,rn
FROM (SELECT s,c,ROW_NUMBER() over(PARTITION BY c order by score DESC) as rn
FROM sc) AS t1
WHERE rn<3;
SELECT c,count(DISTINCT s)
FROM sc
GROUP BY C
HAVING count(DISTINCT s)>5
ORDER BY count(DISTINCT s) DESC,c;
SELECT s, count(c)
FROM sc
GROUP BY s
HAVING COUNT(c)>=3;
select course.c,Cname
from SC,course
WHERE sc.c=course.c
group by sc.c
having count(*) = (select count(DISTINCT s) from sc);
SELECT sname
FROM student
WHERE s NOT IN (SELECT s FROM course,teacher,sc
WHERE course.T=teacher.T AND sc.c=course.C AND Tname="Li Pengfei");
SELECT s,ROUND(AVG(score),2)
FROM sc
WHERE score<60
GROUP BY S
HAVING SUM(CASE WHEN IFNULL(score,0)<60 THEN 1 ELSE 0 END)>2;
SELECT s,c,score
FROM sc
WHERE c='03' AND score<60
order by score desc;
DELETE FROM sc WHERE s="02" AND c="01";