练习sql
-- 1. 查询「李」姓老师的数量-- 2. 查询名字中含有「风」字的学生信息-- 3. 查询男生、女生人数-- 4. 查询课程编号为02的总成绩-- 5. 查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列-- 6. 求每门课程的学生人数-- 7. 统计每门课程的学生选修人数(超过 5 人的课程才统计)-- 8. 检索至少选修两门课程的学生学号-- 9. 查询在 SC 表存在成绩的学生信息-- 10. 查询不存在" 01 "课程但存在" 02 "课程的情况-- 11. 查询同时存在" 01 "课程和" 02 "课程的情况-- 12. 查询出只选修两门课程的学生学号和姓名-- 13. 查询没有学全所有课程的同学的信息
-- 注意:用子查询求所有课程数而不是直接写3;student表中有没选课的学生-- 14. 查询选修了全部课程的学生信息-- 15. 查询所有课程成绩均小于60分的学号、姓名-- 16. 查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名-- 17. 查询学过「张三」老师授课的同学的信息-- 18. 查询没学过"张三"老师讲授的任一门课程的学生姓名-- 19. 查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息-- 20. 查询和" 01 "号的同学学习的课程完全相同的其他同学的信息-- 21. 查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩-- 22. 检索" 01 "课程分数小于 60,按分数降序排列的学生信息-- 23. 查询不及格的课程及学生名,学号,按课程号从大到小排列-- 24. 查询课程名称为「数学」,且分数低于 60 的学生姓名和分数-- 25. 查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩-- 26. 查询不同老师所教不同课程平均分从高到低显示-- 27. 查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩-- 28.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩-- 29. 查询同名同性学生名单,并统计同名同性人数-- 30. 查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况)-- 31. 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )-- 32. 查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显示为 null )-- 33. 查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数-- 34. 查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数-- 35. 成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩-- 36. 成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩-- 37. 按各科成绩进行排序,并显示排名,Score 重复时保留名次空缺-- 38. 按各科成绩进行排序,并显示排名,Score 重复时合并名次-- 39. 查询学生的总成绩,并进行排名,总分重复时保留名次空缺-- 40. 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺-- 41. 查询每门功成绩最好的前两名-- 42. 查询各科成绩前三名的记录-- 43. 查询所有课程成绩第2名到第3名的学生信息及该课程成绩-- 44.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩-- 45. 使用分段[100-85],[85-70],[70-60],[<60]来统计各科成绩,分别统计各分数段人数:课程ID和课程名称-- 46. 查询各科成绩最高分、最低分和平均分:以如下形式显示:课程 ID ,课程 name ,最高分,最低分,平均分,及格率,中等率,优良率,优秀率。及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90-- 47. 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比-- 48. 查询各学生的年龄,只按年份来算-- 49. 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一-- 50. 查询本月过生日的学生-- 51. 查询下月过生日的学生-- 52. 查询本周过生日的学生-- 53. 查询下周过生日的学生
答案
select date_format(date_sub(curdate(), INTERVAL WEEKDAY(curdate()) - 7 DAY), '%m-%d');
select date_format(date_sub(curdate(), INTERVAL WEEKDAY(curdate()) - 13 DAY), '%m-%d');
select *
from student
where date_format(Sage, '%m-%d') >(select date_format(date_sub(curdate(), INTERVAL WEEKDAY(curdate()) - 7 DAY), '%m-%d')) anddate_format(Sage, '%m-%d') <(select date_format(date_sub(curdate(), INTERVAL WEEKDAY(curdate()) - 13 DAY), '%m-%d'));
select sno, sname, sage from student
where week(replace(sage, year(sage), year(current_date())), 1) = week(current_date(),1)+1;
select *
from student
where date_format(Sage, '%m-%d') >=(select date_format(date_sub(curdate(), INTERVAL WEEKDAY(curdate()) - 0 DAY), '%m-%d')) anddate_format(Sage, '%m-%d') <(select date_format(date_sub(curdate(), INTERVAL WEEKDAY(curdate()) - 7 DAY), '%m-%d'));
select sno, sname, sage from student
where month(sage)=month(current_date())+1;
select sno, sname, sage from student
where month(sage)=month(current_date());
select timestampdiff(year,Sage,curdate())-1 from student;
select year(curdate())-year(Sage) from student;
select c.cno,c.Cname,sum(case when score >= 85 and score < 100 then 1 else 0 end) as fir,sum(case when score >= 70 and score < 85 then 1 else 0 end) as sec,sum(case when score >= 60 and score < 70 then 1 else 0 end) as thir,sum(case when score >= 0 and score < 60 then 1 else 0 end) as four,sum(case when a.score >= 85 then 1 else 0 end) / count(a.sno) as '[100-85]百分比',sum(case when a.score >= 70 and a.score < 85 then 1 else 0 end) / count(a.sno) as '[85-70]百分比',sum(case when a.score >= 60 and a.score < 70 then 1 else 0 end) / count(a.sno) as '[70-60]百分比',sum(case when a.score < 60 then 1 else 0 end) / count(a.sno) as '[60-0]百分比'
from sc aleft join course c on a.Cno = c.Cno
group by c.cno;
select a.Cno,c.Cname,max(score),min(score),avg(score),sum(case when score > 60 then 1 else 0 end) / count(a.Cno) as '及格率',sum(case when score >= 70 and score<80 then 1 else 0 end) / count(a.Cno) as '中等率',sum(case when score >= 80 and score<90 then 1 else 0 end) / count(a.Cno) as '优良率',sum(case when score >= 90 then 1 else 0 end) / count(a.Cno) as '优秀率'
from sc aleft join course c on a.Cno = c.Cno
group by a.Cno, c.Cname;
select a.Cno, c.Cname,sum(case when a.score>=85 and a.score<100 then 1 else 0 end) as fir,sum(case when a.score>=70 and a.score<85 then 1 else 0 end) as sec,sum(case when a.score>=60 and a.score<70 then 1 else 0 end) as thi,sum(case when a.score<60 then 1 else 0 end) as four
from sc aleft join course c on a.Cno = c.Cno
group by a.Cno, c.Cname;
select *
from student sleft join (select avg(score) as avg,sno,min(case when cno = '01' then score else null end) as '01',min(case when cno = '02' then score else null end) as '02',min(case when cno = '03' then score else null end) as '03'from scgroup by sno) a on s.Sno = a.Sno
order by a.avg desc;
(select sum(score), sno from sc group by Sno order by sum(score) desc limit 1,1)
union all
(select sum(score), sno from sc group by Sno order by sum(score) desc limit 2,1);
select *
from student sleft join (select sno, score, rank() over (order by score desc) as rankingfrom scgroup by sno, score) a on s.Sno = a.Sno
where ranking>=2 and ranking<=3;
select cno,score
from sc s1
where (select count(s2.Sno) from sc s2 where s2.cno = s1.Cno and s2.score <= s1.score) > 3
order by cno,score desc;
select * from (select *, dense_rank() over (partition by cno order by score desc) as rankifrom sc) a where a.ranki<=3;
select *
from (select *, rank() over (partition by cno order by score desc) as rankifrom sc) a
where a.ranki <= 2;
select sno,sum(score),dense_rank() over (order by sum(score) desc)from sc group by sno;
select sno,sum(score),rank() over (order by sum(score) desc) as runk from sc group by sno;
select *,dense_rank() over (partition by cno order by score desc) as runk
from sc order by Cno,score desc ;
select *,rank() over (partition by cno order by score desc) as runk
from sc order by Cno,score desc ;
select *
from sc scleft join student s on sc.Sno = s.Snoleft join course c on sc.Cno = c.Cnoleft join teacher t on c.Tno = t.Tno
where Tname='张三' order by score desc limit 0,1;
select *
from student stleft join (select sno, score from sc where Cno = '01') sc1 on st.Sno = sc1.Snoleft join (select sno, score from sc where Cno = '02') sc2 on sc1.Sno = sc2.Sno
where sc1.score > sc2.score;
select *
from scleft join course c on sc.Cno = c.Cnoleft join student s on sc.Sno = s.Sno
where s.sno not in (select distinct Snofrom scwhere score < 70group by sno);select a.sno, sc.Cno, sc.score
from (select *from scwhere cno = '01') aleft join scon sc.Sno = a.Sno;select st.sno, st.Sname, count(s.Cno), sum(s.score)
from student stleft join sc s on st.Sno = s.Sno
group by st.sno, st.Sname;
select s1.*
from student s1 left join student s2 on s1.Sname=s2.Sname and s1.Ssex=s2.Ssex
where s1.Sno!=s2.Sno;
select *
from (select count(cno), sno,avg(score)from scwhere score < 60group by snohaving count(score) >= 2) aleft join student s on a.Sno = s.Sno;
select s.Sno, s.Sname, avg(score)
from scleft join student s on sc.Sno = s.Sno
where 1=1
group by s.Sno, s.Sname having avg(score)>60;
select sc.cno,avg(score),c.Tno
from sc left join course c on sc.Cno = c.Cno
group by Cno order by avg(score) desc;select s.sno,s.Sname, avg(score)
from scleft join student s on sc.Sno = s.Sno
group by sno having avg(score)>=85;select s.Sname,sc.score
from sc scleft join course c on sc.Cno = c.Cnoleft join student s on sc.Sno = s.Sno
where c.Cname = '数学' and sc.score<60;
select c.Cname,c.Cno,s.Sname,s.Sno
from sc scleft join student s on sc.Sno = s.Snoleft join course c on sc.Cno = c.Cno
where sc.score<60 order by c.Cno desc ;select s.* from scleft join student s on sc.Sno = s.Sno
where Cno='01' and score<60 order by score desc;
select distinct s.Sno,s1.Cno,s1.score
from sc s1left join sc s2 on s1.Sno = s2.Snoleft join student s on s1.Sno = s.Sno
where s1.score = s2.scoreand s1.Cno != s2.Cno;
select s.*
from sc s1left join student s on s1.Sno = s.Sno
where s1.cno in (select Cno from sc where Sno = '01')and s1.Sno!='01'
group by Sno
having count(s1.Cno)=(select count(Cno) from sc where Sno = '01');
select distinct s.*
from scleft join student s on sc.Sno = s.Sno
where Cno in (select Cno from sc where Sno = '01')and sc.Sno!='01';
select * from student where sno not in(select sc.Snofrom scleft join course c on sc.Cno = c.Cnoleft join teacher t on c.Tno = t.Tnowhere Tname = '张三');
select *
from student
where sno in (select sc.Snofrom scleft join course c on sc.Cno = c.Cnoleft join teacher t on c.Tno = t.Tnowhere Tname = '张三');
select s.Sno, Sname, Sage, Ssex,sc.score
from sc scleft join student s on sc.Sno = s.Sno
where Cno='01' and score>=80;
select *
from student
where Sno in (select Sno from sc where score < 60 group by Sno having count(score) = count(Cno));
select s.*
from scleft join student s on sc.Sno = s.Sno
group by sc.Sno
having count(score) = (select count(1)from course);select sc.*
from student scleft join sc s on sc.Sno = s.Sno
group by sc.Sno
having count(s.score) != (select count(1)from course);select student.*
from studentleft join sc s on student.Sno = s.Sno
group by student.Sno having count(score)=2;select * from student
where Sno in(select sno from sc where Cno='01')and sno in (select sno from sc where Cno='02');select * from student
where Sno not in(select sno from sc where Cno ='01')and sno in (select sno from sc where Cno='02');
select * from student where Sno in (select distinct sno from sc);select Sno from sc group by Sno having count(Cno)>=2;select Cno,count(Sno) from sc group by Cno having count(Sno)>5;
select count(Sno) from sc group by Cno;select avg(score) from sc group by Cno order by avg(score) desc;select sum(score) from sc where Cno='02';
select count(Ssex),Ssex from student group by Ssex;
select * from student where Sname like '%风%';
select * from teacher where Tname like '李%';
增删改sql练习题,
CREATE DATABASE IF NOT EXISTS test01_library CHARACTER SET 'utf8';
USE test01_library;
CREATE TABLE books(
id INT,
name VARCHAR(50),
`authors` VARCHAR(100) ,
price FLOAT,
pubdate YEAR ,
note VARCHAR(100),
num INT
);
INSERT INTO books
VALUES(1,'Tal of AAA','Dickes',23,1995,'novel',11);
INSERT INTO books (id,name,`authors`,price,pubdate,note,num)
VALUES(2,'EmmaT','Jane lura',35,1993,'Joke',22);
INSERT INTO books (id,name,`authors`,price,pubdate,note,num) VALUES
(3,'Story of Jane','Jane Tim',40,2001,'novel',0),
(4,'Lovey Day','George Byron',20,2005,'novel',30),
(5,'Old land','Honore Blade',30,2010,'Law',0),
(6,'The Battle','Upton Sara',30,1999,'medicine',40),
(7,'Rose Hood','Richard haggard',28,2008,'cartoon',28);
UPDATE books SET price=price+5 WHERE note = 'novel';
UPDATE books SET price=40,note='drama' WHERE name='EmmaT';
DELETE FROM books WHERE num=0;
SELECT * FROM books WHERE name LIKE '%a%';
SELECT COUNT(*),SUM(num) FROM books WHERE name LIKE '%a%';
SELECT * FROM books WHERE note = 'novel' ORDER BY price DESC;
SELECT * FROM books ORDER BY num DESC,note ASC;
SELECT note,COUNT(*) FROM books GROUP BY note;
SELECT note,SUM(num) FROM books GROUP BY note HAVING SUM(num)>30;
SELECT * FROM books LIMIT 5,5;
SELECT note,SUM(num) sum_num FROM books GROUP BY note ORDER BY sum_num DESC LIMIT 0,1;
SELECT * FROM books WHERE CHAR_LENGTH(REPLACE(name,' ',''))>=10;
SELECT name AS "书名" ,note, CASE note
WHEN 'novel' THEN '小说'
WHEN 'law' THEN '法律'
WHEN 'medicine' THEN '医药'
WHEN 'cartoon' THEN '卡通'
WHEN 'joke' THEN '笑话'
END AS "类型"
FROM books;
SELECT name,num,CASE
WHEN num>30 THEN '滞销'
WHEN num>0 AND num<10 THEN '畅销'
WHEN num=0 THEN '无货'
ELSE '正常'
END AS "库存状态"
FROM books;
SELECT IFNULL(note,'合计总库存量') AS note,SUM(num) FROM books GROUP BY note WITH
ROLLUP;
SELECT IFNULL(note,'合计总数') AS note,COUNT(*) FROM books GROUP BY note WITH ROLLUP;
SELECT * FROM books ORDER BY num DESC LIMIT 0,3;
SELECT * FROM books ORDER BY pubdate ASC LIMIT 0,1;
SELECT * FROM books WHERE note = 'novel' ORDER BY price DESC LIMIT 0,1;
SELECT * FROM books ORDER BY CHAR_LENGTH(REPLACE(name,' ','')) DESC LIMIT 0,1;