sql新手練習 30題

sql 訓練 1 ~ 40 題

建表

首先,創建表咯。一共有4張表。分別是學生表課程表教師表成績表

  • 學生表Student

4個字段,SId(學生ID),Sname(學生姓名),Sage(學生年齡),Ssex(學生性別)

create table Student(SId varchar(10),Sname varchar(10),Sage datetime,Ssex varchar(10),PRIMARY key(SId));
insert into Student values('01' , '趙雷' , '1990-01-01' , '男');
insert into Student values('02' , '錢電' , '1990-12-21' , '男');
insert into Student values('03' , '孫風' , '1990-12-20' , '男');
insert into Student values('04' , '李雲' , '1990-12-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吳蘭' , '1992-01-01' , '女');
insert into Student values('07' , '鄭竹' , '1989-01-01' , '女');
insert into Student values('09' , '張三' , '2017-12-20' , '女');
insert into Student values('10' , '李四' , '2017-12-25' , '女');
insert into Student values('11' , '李四' , '2012-06-06' , '女');
insert into Student values('12' , '趙六' , '2013-06-13' , '女');
insert into Student values('13' , '孫七' , '2014-06-01' , '女');
  • 教師表Teacher

2個字段,TId(教師id),Tname(教師姓名)

create table Teacher(TId varchar(10),Tname varchar(10),PRIMARY key (Tid));
insert into Teacher values('01' , '張三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
  • 課程表

3個字段,CId(課程id),Cname(課程名稱),TId(教師id)

create table Course(CId varchar(10),Cname nvarchar(10),TId varchar(10),PRIMARY KEY(CId));
insert into Course values('01' , '語文' , '02');
insert into Course values('02' , '數學' , '01');
insert into Course values('03' , '英語' , '03');
  • 成績表

4個字段,skey(由於沒有不重複的字段,因此創建了主鍵字段),SId(學生id),CId(課程ID),score(學生成績)

create table SC(skey int ,SId varchar(10),CId varchar(10),score decimal(18,1),PRIMARY key(skey));
insert into SC values('1','01' , '01' , 80);
insert into SC values('2','01' , '02' , 90);
insert into SC values('3','01' , '03' , 99);
insert into SC values('4','02' , '01' , 70);
insert into SC values('5','02' , '02' , 60);
insert into SC values('6','02' , '03' , 80);
insert into SC values('7','03' , '01' , 80);
insert into SC values('8','03' , '02' , 80);
insert into SC values('9','03' , '03' , 80);
insert into SC values('10','04' , '01' , 50);
insert into SC values('11','04' , '02' , 30);
insert into SC values('12','04' , '03' , 20);
insert into SC values('13','05' , '01' , 76);
insert into SC values('14','05' , '02' , 87);
insert into SC values('15','06' , '01' , 31);
insert into SC values('16','06' , '03' , 34);
insert into SC values('17','07' , '02' , 89);
insert into SC values('18','07' , '03' , 98);

1 ~ 40 題

# 1.求每門課程的學生人數。
select Course.Cname,count(SId) 
from course, sc 
where course.CId = sc.CId 
group by course.Cid;

# 2.查詢課程編號爲 01 且課程成績在 80 分及以上的學生的學號和姓名

SELECT student.Sid,student.Sname,sc.score
FROM student, sc
where student.Sid = sc.SId
AND sc.Cid = '01' AND sc.score >= 80;

# 3.統計每門課程的學生選修人數(超過 5 人的課程才統計)

SELECT count(Sid) FROM sc
GROUP BY Cid HAVING count(Sid) > 5;


# 4.檢索至少選修兩門課程的學生學號

select sid from sc 
GROUP BY sid 
having count(cid) > 2

# 5.選修了全部課程的學生信息

SELECT a.Sid,a.Sname,a.Sage,a.Ssex FROM student a,sc b
WHERE a.Sid = b.Sid
GROUP BY Sid HAVING count(Cid) = (select count(*) from course);

# 6.查詢存在不及格的課程
SELECT c.Cname FROM Course c, sc s
where c.CId = s.CId
GROUP BY s.CId HAVING min(s.score) < 60;

select DISTINCT Course.Cname from sc ,Course  where sc.cid = Course.cid  
and sc.score < 60

# 7.查詢任何一門課程成績在 70 分以上的學生姓名、課程名稱和分數

SELECT a.Sname, b.Cname, c.score FROM student a, course b, sc c
where a.Sid = c.Sid AND c.Cid = b.Cid
AND c.score > 70;

# 8.查詢所有學生的課程及分數情況(存在學生沒成績,沒選課的情況)

select a.sname,b.cname,c.score from student a left join sc c 
on a.sid=c.sid
left join course b 
on b.cid = c.cid;

# 9.查詢課程名稱爲「數學」,且分數低於 60 的學生姓名和分數
SELECT c.Sname, b.score FROM Course a, sc b, student c 
WHERE a.CId = b.CId AND c.Sid = b.SId AND a.Cname = '數學' AND b.score < 60;

# 10.查詢平均成績大於等於 85 的所有學生的學號、姓名和平均成績
SELECT b.Sid, b.Sname, avg(a.score)'平均成績' FROM sc a, student b
where a.SId = b.Sid
GROUP BY a.SId HAVING avg(a.score) >= 85;


#11.查詢每門課程的平均成績,結果按平均成績降序排列,平均成績相同時,按課程編號升序排列
SELECT  avg(sc.score)'平均成績' FROM sc 
GROUP BY sc.CId order by avg(sc.score) desc, sc.CId;

# 12.查詢各科成績最高分、最低分和平均分
SELECT b.CId, b.Cname max(a.score), min(a.score),avg(a.score), count(a.SId),
sum(
		case when score < 60 then 1 else 0 end
)/ count(a.CId)
FROM sc a, course b where a.CId = b.CId GROUP BY a.CId;



select sc.cid,Course.Cname,max(sc.score),min(sc.score),AVG(sc.score),count(sc.SId),
sum(case
	when sc.score < 60 then 1 else 0
	end
)/count(sc.cid)'不及格率',
sum(case
	when sc.score > 60 and sc.score < 70 then 1 else 0
	end
)/count(sc.cid)'及格率',

sum(case
	when sc.score >= 70 AND sc.score < 80 then 1 else 0 
	end
)/count(sc.cid)'中等率',

sum(case
	when sc.score >= 80 AND sc.score < 90 then 1 else 0 
	end
)/count(sc.cid)'優良率',
sum(case
	when sc.score >= 90 AND sc.score <= 100 then 1 else 0 
	end
)/count(sc.cid)'優秀率'
from sc ,Course
where sc.cid =  Course.cid
GROUP BY sc.cid;


# 13.查詢男生、女生人數
SELECT Ssex,count(*) FROM student GROUP BY Ssex;


# 14.檢索" 01 "課程分數小於 60,按分數降序排列的學生信息
SELECT a.* from student a, sc b where a.Sid = b.SId
AND b.CId = '01' and b.score < 60 order by b.score desc;

# 15.按平均成績從高到低顯示所有學生的所有課程的成績以及平均成績
select a.SId,a.score,b.`平均成績` 
from sc a right join (select SId,avg(score)'平均成績' from sc GROUP BY SId) b 
on a.SId = b.SId ORDER BY b.平均成績 DESC;


# 16.查詢沒學過"張三"老師講授的任一門課程的學生姓名

SELECT student.Sname from student where student.Sid not in(
select a.sid from sc a, teacher b, course c
where a.CId = c.cid and b.tid = c.tid and b.Tname = '張三');


# 17.成績不重複,查詢選修「張三」老師所授課程的學生中,成績最高的學生信息及其成績
select b.* from sc a, student b, teacher c,course d 
where a.SId = b.Sid and a.CId = d.CId and c.Tid = d.TId
and c.Tname = '張三' order by a.score desc limit 1;

#18.成績有重複的情況下,查詢選修「張三」老師所授課程的學生中,成績最高的學生信息及其成績
-- 先修改數據庫,增加一個重複項
UPDATE sc SET score=90
where skey=17

-- 先查出最高的分數,然後再查分數等於最高分數的人
select b.*,a.score from sc a, student b
where a.SId = b.SId and a.score in(
select max(a.score) from sc a, student b, teacher c,course d 
where a.SId = b.Sid and a.CId = d.CId and c.Tid = d.TId
and c.Tname = '張三');

# 19 查詢不同課程成績相同的學生的學生編號、課程編號、學生成績
select a.cid, a.sid, any_value(a.score) from sc a,sc b 
where a.SId = b.Sid and a.score = b.score and a.CId != b.CId GROUP BY a.cid,b.SId;


# 20.查詢每門功課成績最好的前兩名
select any_value(a.sid),any_value(a.cid),any_value(a.score)  
from sc a left join sc b
on a.CId = b.CId and a.score < b.score
group by a.cid,a.sid
having count(b.score) < 2 order by a.cid;



# 21.查詢每門課程被選修的學生數
select count(*) from sc group by cid;


# 22.查詢出只選修兩門課程的學生學號和姓名
select b.sid,b.Sname from sc a, student b where a.SId = b.sid
group by b.sid having count(a.CId) = 2;

# 23查詢同名學生名單,並統計同名人數

select Sname, count(1) as number from student
group by Sname having number >= 2;


# 24.查詢 1990 年出生的學生名單
select * from Student where year(sage)= 1990;

# 25.查詢各學生的年齡.
select sid,sname,TIMESTAMPDIFF(year,sage,CURDATE())from student 

# 26.查詢本週過生日的學生
select * from student where 
week(curdate()) = week(Sage)

# 27.查詢本月過生日的學生
select * from student where MONTH(CURDATE()) = MONTH(sage);
# 28.查詢「李」姓老師的數量
select count(1) from teacher where Tname like '李%';

# 29.查有成績的學生信息

select * from Student where sid in (select sc.sid from sc)

# 30.查詢所有同學的學生編號、學生姓名、選課總數、所有課程的成績總和
select a.sid,a.Sname,count(1) as number, sum(b.score) as total 
from student a left join sc b 
on a.sid = b.sid
group by a.sid; 

# 31.查詢在 SC 表存在成績的學生信息
SELECT
	* 
FROM
	student 
WHERE
	sid IN ( SELECT DISTINCT sid FROM sc WHERE score IS NOT NULL );


# 32.查詢平均成績大於等於 60 分的同學的學生編號和學生姓名和平均成績
select a.Sid,a.Sname,AVG(b.score) from student a,sc b
where a.Sid = b.SId group by a.SId having AVG(b.score) >= 60;

# 33.查詢不存在" 01 "課程但存在" 02 "課程的情況

select * from sc where cid = '02' and sid not in (select  sid from sc  where cid='01')

# 34.查詢存在" 01 "課程但可能不存在" 02 "課程的情況

select  * from  sc where cid ='01';

# 35.按各科成績進行排序,並顯示排名 Score 重複時保留名次空缺
select a.CId,a.SId,any_value(a.score),count(b.score)+1 as '名次' from sc a left join sc b on a.CId = b.CId and a.score < b.score
group by a.CId,a.SId ORDER BY a.CId,count(b.sid)+1 ;

# 36.查詢" 01 "課程比" 02 "課程成績高的學生的信息及課程分數
select a.*,b.score,c.score from student a,
(select score, sid from sc where cid= '01')b,
(select score, sid from sc where cid= '02')c
where a.sid = b.sid and a.sid = c.sid and b.score > c.score;

# 37查詢學過「張三」老師授課的同學的信息

select * from student
where sid in (select sid from teacher a,course b, sc c 
where c.cid = b.cid and a.tid = b.tid and a.tname = '張三');

# 38查詢沒有學全所有課程的同學的信息

select a.* from student a left join sc b on a.Sid = b.SId
GROUP BY a.Sid HAVING count(b.cid) < (select count(cid) from course)


# 39.查詢至少有一門課與學號爲" 01 "的同學所學相同的同學的信息

select a.* from student a ,sc b where a.sid = b.sid and b.cid in 
(select cid from sc where sid ='01')
group by a.sid;

# 40.查詢和" 01 "號的同學學習的課程完全相同的其他同學的信息
select a.* from student a
where Sid in (select sid from sc where sid not in (select sid from sc where cid not in (select cid from sc where sid = '01')) 
GROUP BY sid having count(*) = (select count(cid) from sc where sid = '01') and sid != '01');

學習於:https://www.zhihu.com/collection/435712228

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章