SQL語句練習


前提條件

1.mysql環境,可以裝個phpstudy,簡單方便
2.建立數據表:
學生表:
Student(s_id,s_name,s_birth,s_sex) –學生編號,學生姓名, 出生年月,學生性別
課程表:
Course(c_id,c_name,t_id) – –課程編號, 課程名稱, 教師編號
教師表:
Teacher(t_id,t_name) –教師編號,教師姓名
成績表:
Score(s_id,c_id,s_s_score) –學生編號,課程編號,分數

--建表
--學生表
CREATE TABLE `Student`(
`s_id` VARCHAR(20),
`s_name` VARCHAR(20) NOT NULL DEFAULT '',
`s_birth` VARCHAR(20) NOT NULL DEFAULT '',
`s_sex` VARCHAR(10) NOT NULL DEFAULT '',
PRIMARY KEY(`s_id`)
);
--課程表
CREATE TABLE `Course`(
`c_id` VARCHAR(20),
`c_name` VARCHAR(20) NOT NULL DEFAULT '',
`t_id` VARCHAR(20) NOT NULL,
PRIMARY KEY(`c_id`)
);
--教師表
CREATE TABLE `Teacher`(
`t_id` VARCHAR(20),
`t_name` VARCHAR(20) NOT NULL DEFAULT '',
PRIMARY KEY(`t_id`)
);
--成績表
CREATE TABLE `Score`(
`s_id` VARCHAR(20),
`c_id` VARCHAR(20),
`s_score` INT(3),
PRIMARY KEY(`s_id`,`c_id`)
);

3.填充數據

--插入學生表測試數據
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' , '語文' , '02');
insert into Course values('02' , '數學' , '01');
insert into Course values('03' , '英語' , '03');

--教師表測試數據
insert into Teacher values('01' , '張三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');

--成績表測試數據
insert into Score values('01' , '01' , 80);
insert into Score values('01' , '02' , 90);
insert into Score values('01' , '03' , 99);
insert into Score values('02' , '01' , 70);
insert into Score values('02' , '02' , 60);
insert into Score values('02' , '03' , 80);
insert into Score values('03' , '01' , 80);
insert into Score values('03' , '02' , 80);
insert into Score values('03' , '03' , 80);
insert into Score values('04' , '01' , 50);
insert into Score values('04' , '02' , 30);
insert into Score values('04' , '03' , 20);
insert into Score values('05' , '01' , 76);
insert into Score values('05' , '02' , 87);
insert into Score values('06' , '01' , 31);
insert into Score values('06' , '03' , 34);
insert into Score values('07' , '02' , 89);
insert into Score values('07' , '03' , 98);

練習

1.查詢課程編號為“01”的課程比“02”的課程成績高的所有學生的學號

select a.*,b.s_score from
(select s_id,s_score from score where c_id='01') as a,
(select s_id,s_score from score where c_id = '02') as b
where a.s_score>b.s_score and a.s_id = b.s_id;

2.查詢平均成績大於60分的學生的學號和平均成績

select s_id, avg(s_score)
from  score
group by s_id
having avg(s_score)>60;

3.查詢所有學生的學號、姓名、選課數、總成績

select st.s_id,st.s_name,count(sc.c_id),avg(sc.s_score)
from student st,score sc
where st.s_id = sc.s_id
group by s_id,s_name;

或者使用:

select st.s_id,st.s_name,count(sc.c_id),avg(sc.s_score)
from student st join score sc
on st.s_id = sc.s_id
group by s_id,s_name;

4.查詢姓“張”的老師的個數

select count(distinct(t_name)) number from teacher where t_name like '張%';

5.查詢沒學過“張三”老師課的學生的學號、姓名

select distinct(st.s_id),st.s_name
from student st
where st.s_id not in
(select sc.s_id from score sc,teacher te,course co
where sc.c_id=co.c_id and te.t_id = co.t_id and t_name='張三');

6.查詢學過“張三”老師所教的所有課的同學的學號、姓名

select distinct(st.s_id),st.s_name
from student st
where st.s_id in
(select sc.s_id from score sc,teacher te,course co
where sc.c_id=co.c_id and te.t_id = co.t_id and t_name='張三');

7.查詢學過編號為“01”的課程並且也學過編號為“02”的課程的學生的學號、姓名

select distinct(st.s_id),st.s_name
from student st
where st.s_id in
(select a.s_id from (select s_id,c_id from score where c_id='01') a
inner join (select s_id,c_id  from score where c_id='02') b
on a.s_id=b.s_id
);

或者(下面這個可以很好地改為學過“01”沒學過“02”等情況)

select distinct(st.s_id),st.s_name
from student st
where st.s_id in
(select s_id from score where c_id='01')
and s_id in
(select s_id  from score where c_id='02')

8.查詢課程編號為“02”的總成績

select sum(s_score)
from score
where c_id='02';

9.查詢存在課程成績小於60分的學生的學號、姓名

select distinct(st.s_id), st.s_name
from student st,score sc
where st.s_id=sc.s_id and sc.s_score<60;

如果是所有課程小於60分,則為

select distinct(st.s_id), st.s_name
from student st
where st.s_id not in
(select s_id from score where s_score>60)

10.查詢沒有學全所有課的學生的學號、姓名

select st.s_id,st.s_name
from student st inner join score sc on st.s_id=sc.s_id
group by st.s_id,st.s_name
having count(c_id)<(select count(distinct(c_id)) from course);

11.查詢至少有一門課與學號為“01”的學生所學課程相同的學生的學號和姓名

select distinct(st.s_id),st.s_name
from student st inner join score sc on st.s_id=sc.s_id
where sc.c_id in
(select c_id from score where s_id='01') and st.s_id !='01';

12.查詢和“01”號同學所學課程完全相同的其他同學的學號

select sc.s_id, st.s_name
from score sc inner join student st on sc.s_id=st.s_id
group by sc.s_id,st.s_name
having group_concat(c_id order by c_id)
=(select group_concat(c_id order by c_id) from score where s_id='01')
and sc.s_id!='01'

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

select st.s_id,st.s_name
from student st
where s_id not in
(select sc.s_id
from score sc
inner join course co on sc.c_id=co.c_id
inner join teacher te on co.t_id=co.t_id
where te.t_name='張三');

或者

select distinct st.s_id,st.s_name
from student st
where st.s_id not in
(select sc.s_id from score sc,teacher te,course co
where co.t_id = te.t_id and sc.c_id=co.c_id and te.t_name='張三');

14.查詢兩門及其以上不及格課程的同學的學號,姓名及其平均成績

select distinct st.s_id,st.s_name ,avg(sc.s_score)
from student st inner join score sc on st.s_id=sc.s_id
where not sc.s_score >= 60
group by st.s_id,st.s_name
having count(c_id)>=2;

SELECT student.s_id,student.s_name,avg(score.s_score)
From student inner join score on student.s_id = score.s_id
Where student.s_id in
(select tem.s_id from (select * from score sc where sc.s_score<60) tem
group by tem.s_id
having count(tem.c_id)>=2)
group by student.s_id,student.s_name

SELECT student.s_id,student.s_name,avg(score.s_score)
From student inner join score on student.s_id = score.s_id
Where student.s_id in
(select sc.s_id from score sc where sc.s_score<60
group by sc.s_id
having count(sc.c_id)>=2)
group by student.s_id,student.s_name

15.檢索"01"課程分數小於60,按分數降序排列的學生信息

select *
from student st inner join score sc
on st.s_id=sc.s_id
where sc.s_score<60 and sc.c_id='01'
order by sc.s_score desc;

16.按平均成績從高到低顯示所有學生的所有課程的成績以及平均成績

select s_id,
max(case when c_id='01' then s_score else NUll end) '01',
max(case when c_id='02' then s_score else NULL end) '02',
max(case when c_id='03' then s_score else NUll end) '03',
avg(s_score)
from score
group by s_id
order by avg(s_score) desc

17.查詢各科成績最高分、最低分和平均分:以如下形式顯示:課程ID,課程name,最高分,最低分,平均分,及格率,中等率,優良率,優秀率

及格為>=60,中等為:70-80,優良為:80-90,優秀為:>=90

select sc.c_id, co.c_name, max(s_score) max, min(s_score) min, avg(s_score) avg,
avg(case when s_score>=60 then 1.0 else 0.0 end) '及格率',
avg(case when s_score>=70 and s_score<80 then 1.0 else 0.0 end) '中等率',
avg(case when s_score>=80 and s_score<90 then 1.0 else 0.0 end) '優良率',
avg(case when s_score>=90 then 1.0 else 0.0 end) '優秀率'
from score sc inner join course co on sc.c_id=co.c_id
group by sc.c_id;

18.查詢每門課程的平均成績,結果按平均成績升序排序,平均成績相同時,按課程號降序排列

select c_id, avg(s_score)
from score
group by c_id
order by avg(s_score) asc,c_id desc;

19.查詢學生的總成績並進行排名

select s_id,sum(s_score)
from score sc
group by s_id
order by sum(s_score) desc;

20.查詢不同老師所教不同課程平均分從高到低顯示

select te.t_id, te.t_name, sc.c_id,avg(sc.s_score)
from score sc inner join course co on sc.c_id=co.c_id
inner join teacher te on co.t_id=te.t_id
group by te.t_id,te.t_name,sc.c_id
order by avg(sc.s_score) desc;

參考:

1.sql面試題(學生表_課程表_成績表_教師表)
2.SQL面試必會50題


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM