天天看點

SQL筆試 I 經典20題及答案解析(上)

SQL筆試 I 經典20題及答案解析(上)

面試經常碰到SQL面試題,好久沒實操已生疏?今天給大家簡單準備20道SQL經典面試題,還有下篇;喜歡可關注作者,靜候分享,溫故而知新,可以為師矣.

01 建表語句

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);      

02 表結構預覽

--學生表

Student(SId,Sname,Sage,Ssex)

--SId 學生編号,Sname 學生姓名,Sage 出生年月,Ssex 學生性别

--課程表

Course(CId,Cname,TId)

--CId 課程編号,Cname 課程名稱,TId 教師編号

--教師表

Teacher(TId,Tname)

--TId 教師編号,Tname 教師姓名

--成績表

SC(SId,CId,score)

--SId 學生編号,CId 課程編号,score 分數

1. 查詢“01”課程比“02”課程成績高的所有學生的學号;

select distinct t1.sid as sidfrom 
    (select * from sc where cid='01')t1
left join 
    (select * from sc where cid='02')t2
on t1.sid=t2.sid
where t1.score>t2.score      

2. 查詢平均成績大于60分的同學的學号和平均成績;

select 
    sid
    ,avg(score)
from sc
group by sid
having avg(score)>60      

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

select
    student.sid as sid
    ,sname
    ,count(distinct cid) course_cnt
    ,sum(score) as total_score
from student
left join sc
on student.sid=sc.sid
group by sid,sname      

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

select
    count(distinct tid) as teacher_cnt
from teacher
where tname like '李%'      

5. 查詢沒學過“張三”老師課的同學的學号、姓名;

select
    sid,sname
from student
where sid not in 
    (
        select
            sc.sid
        from teacher
        left join course
            on teacher.tid=course.tid
        left join sc
            on course.cid=sc.cid
        where teacher.tname='張三'
    )      

6. 查詢學過“01”并且也學過編号“02”課程的同學的學号、姓名;

select
    t.sid as sid
    ,sname
from 
    (
        select
            sid
            ,count(if(cid='01',score,null)) as count1
            ,count(if(cid='02',score,null)) as count2
        from sc
        group by sid
        having count(if(cid='01',score,null))>0 and count(if(cid='02',score,null))>0
    )t
left join student
    on t.sid=student.sid      

7. 查詢學過“張三”老師所教的課的同學的學号、姓名;

select
    student.sid
    ,sname
from 
    (
        select
            distinct cid 
        from course
        left join teacher 
        on course.tid=teacher.tid
        where teacher.tname='張三'
    )course
left join sc 
    on course.cid=sc.cid
left join student
    on sc.sid=student.sid
group by student.sid,sname      

8. 查詢課程編号“01”的成績比課程編号“02”課程低的所有同學的學号、姓名;

select
    t1.sid,sname
from 
    (
        select distinct t1.sid as sid
        from 
            (select * from sc where cid='01')t1
        left join 
            (select * from sc where cid='02')t2
        on t1.sid=t2.sid
        where t1.score>t2.score
    )t1
left join student
    on t1.sid=student.sid      

9. 查詢所有課程成績小于60分的同學的學号、姓名;

select
    t1.sid,sname
from 
    (
        select
            sid,max(score)
        from sc
        group by sid
        having max(score<60)
    )t1
left join student
    on t1.sid=student.sid      

10. 查詢沒有學全所有課的同學的學号、姓名;

select
    t1.sid,sname
from 
    (
        select
            count(cid),sid
        from sc
        group by sid
        having count(cid) < (select count(distinct cid) from course)
    )t1
left join student
    on t1.sid=student.sid      

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

select
    distinct sc.sid
from 
    (
        select
            cid
        from sc
        where sid='01'
    )t1
left join sc
    on t1.cid=sc.cid      

12. 查詢和"01"号的同學學習的課程完全相同的其他同學的學号和姓名

#注意是和'01'号同學課程完全相同但非學習課程數相同的,這裡我用左連接配接解決這個問題select
    t1.sid,sname
from
    (
        select
            sc.sid
            ,count(distinct sc.cid)
        from 
            (
                select
                    cid
                from sc
                where sid='01'
            )t1 #選出01的同學所學的課程
        left join sc
            on t1.cid=sc.cid
        group by sc.sid
        having count(distinct sc.cid)= (select count(distinct cid) from sc where sid = '01')
    )t1
left join student
    on t1.sid=student.sid
where t1.sid!='01'      

13. 把“SC”表中“張三”老師教的課的成績都更改為此課程的平均成績;

#暫跳過update題目      

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

select 
    sname
from student
where sid not in
    (
        select
            distinct sid
        from sc
        left join course
            on sc.cid=course.cid
        left join teacher
            on course.tid=teacher.tid 
        where tname='張三'
    )      

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

select
    t1.sid,sname,avg_score
from 
    (
        select
            sid,count(if(score<60,cid,null)),avg(score) as avg_score
        from sc
        group by sid
        having count(if(score<60,cid,null)) >=2
    )t1
left join student
    on t1.sid=student.sid
      

16. 檢索"01"課程分數小于60,按分數降序排列的學生資訊

select 
    sid,if(cid='01',score,100)from sc
where if(cid='01',score,100)<60
order by if(cid='01',score,100) desc      

17. 按平均成績從高到低顯示所有學生的平均成績

select sid,avg(score)
from sc
group by sid
order by avg(score) desc      

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

select
    sc.cid
    ,cname
    ,max(score) as max_score
    ,min(score) as min_score
    ,avg(score) as avg_score
    ,count(if(score>=60,sid,null))/count(sid) as pass_rate
 from sc 
 left join course
    on sc.cid=course.cid
 group by sc.cid      

19. 按各科平均成績從低到高和及格率的百分數從高到低順序

#這裡先按照平均成績排序,再按照及格百分數排序,
select 
    cid
    ,avg(score) as avg_score
    ,count(if(score>=60,sid,null))/count(sid) as pass_rate
from sc
group by cid
order by avg_score,pass_rate desc      

20. 查詢學生的總成績并進行排名

select
    sid
    ,sum(score) as sum_score
from sc
group by sid
order by sum_score desc      

作者:tomocat,如果侵權請聯系删除