超經典SQL基礎練習題,作完SQL及格(MYSQL)

SQL經典50題,這套題作的人不少,今天本身也寫了一下,互相學習!
引用數據
sql

四張測試表以下:
–1.學生表
Student(Sid,Sname,Sage,Ssex)
–Sid 學生編號,Sname 學生姓名,Sage 出生年月,Ssex 學生性別


學習

–2.課程表
Course(Cid,Cname,Tid)
–Cid --課程編號,Cname 課程名稱,Tid 教師編號

測試

–3.教師表
Teacher(Tid,Tname)
–Tid 教師編號,Tname 教師姓名

spa

–4.成績表
SC(Sid,Cid,score)
–Sid 學生編號,Cid 課程編號,score 分數

.net

--建表語句
--學生表 Student
create table Student(Sid varchar(10),Sname nvarchar(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' , '女');

--科目表 Course
create table Course(Cid varchar(10),Cname nvarchar(10),Tid varchar(10));
insert into Course values('01' , '語文' , '02');
insert into Course values('02' , '數學' , '01');
insert into Course values('03' , '英語' , '03');

--教師表 Teacher
create table Teacher(Tid varchar(10),Tname nvarchar(10));
insert into Teacher values('01' , '張三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');

--成績表 SC
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);
  1. 查詢" 01 「課程比」 02 "課程成績高的學生的信息及課程分數
--同一個學生科目一有成績可是科目二無成績的要注意
select
  s.*,
  c.score1,
  c.score2
from 
  student s join
  (SELECT
    a.sid as sid,
    a.score as score1,
    if(b.score is null,0,b.score) as score2
  from
    (SELECT
      sid,
      cid,
      score
    from sc 
      where cid='01') 
    a left join
    (SELECT
      sid,
      cid,
      score
    from sc 
      where cid='02') 
    b on 
      a.sid=b.sid where 
      a.score>if(b.score is null,0,b.score)) 
  c on 
    c.sid=s.sid

2 查詢同時存在" 01 「課程和」 02 "課程的狀況code

SELECT
  a.sid as sid,
  a.cid as cid1,
  a.score as score1,
  b.cid as cid2,
  b.score as score2
FROM
  (SELECT
    *
  FROM
    sc where cid='01') 
  a join
  (SELECT
    *
  FROM 
    sc where cid='02') 
  b on 
  a.sid=b.sid

3 查詢存在" 01 「課程但可能不存在」 02 "課程的狀況(不存在時顯示爲 null )blog

SELECT
  a.sid as sid,
  a.cid as cid1,
  a.score as score1,
  b.cid as cid2,
  b.score as score2
FROM
  (SELECT
    *
  FROM
    sc where cid='01') 
  a left join
  (SELECT
    *
  FROM 
    sc where cid='02') 
  b on 
  a.sid=b.sid

4 查詢不存在" 01 「課程但存在」 02 "課程的狀況排序

--答案1
--利用not in
select 
  sid,
  cid,
  score
FROM
  sc where 
  cid='02' and sid not in (select sid from sc where cid='01')

--答案2
--利用group_concat做爲分組後的過濾條件
SELECT
  *
FROM
  sc group by 
  sid having 
  GROUP_CONCAT(cid) not like '%01%'

5.查詢平均成績大於等於 60 分的同窗的學生編號和學生姓名和平均成績ci

--由於某些同窗只考了兩門,總共科目有三門,總成績除2或除3的寫法是徹底不一樣的
--除2 直接使用avg
select
  s.*,
  a.avg_score as avg_score
from 
  student s join
  (select
    sid,
    avg(score) as avg_score
  from 
    sc group by 
    sid having(avg(score)>60)) 
  a on 
  s.sid=a.sid

--除3 總分除以總門數
SELECT
  s.*,
  a.avg_score as avg_score
FROM 
  student s join
  (select
    sid,
    sum(score)/(select count(1) from course) as avg_score
  from
    sc group by 
    sid having sum(score)/(select count(1) from course)>60) 
  a on 
  s.sid=a.sid

6.查詢在 SC 表存在成績的學生信息get

select
  student.*
from
  sc JOIN 
  student on 
  sc.sid=student.sid group by 
  sid

7.查詢全部同窗的學生編號、學生姓名、選課總數、全部課程的總成績(沒成績的顯示爲 null )

SELECT
  s.*,
  a.count_course,
  a.sum_score
from 
  student s left join
  (SELECT
    sid,
    count(1) as count_course,
    sum(score) as sum_score
  FROM 
    sc group by 
    sid) 
  a on 
  s.sid=a.sid

8.查有成績的學生信息

SELECT
  s.*
from 
  student s join
  (SELECT
    sid
  FROM 
    sc group by 
    sid) 
  a on 
  s.sid=a.sid

9.查詢「李」姓老師的數量

SELECT
  count(1) as num
FROM
  teacher WHERE
  tname like '李%'

10.查詢學過「張三」老師授課的同窗的信息

SELECT
  st.*,
  t.tname as teacher
from 
  sc s join
  student st join 
  course c join
  teacher t on 
  c.tid=t.tid and 
  c.cid=s.cid and 
  s.sid=st.sid where 
  t.tname ='張三'

11.查詢沒有學全全部課程的同窗的信息

--答案1
--使用group_concat分組拼接課程,注意group_concat無序須要組內排序,還要判斷null的狀況
SELECT
  s.*,
  GROUP_CONCAT(cid) as concat_cid
from
  student s left JOIN
  sc on 
  s.sid=sc.sid group by 
  s.sid having 
  if(GROUP_CONCAT(cid order by cid) is null,'-1',GROUP_CONCAT(cid order by cid))!=(select GROUP_CONCAT(cid) from course)

--答案2
--多表join須要注意null值,若是三科並非語文數學英語,這樣寫並不合適
SELECT
  student.*
FROM
  student left join 
  sc on 
  sc.sid=student.sid group by 
  student.sid having count(1)<3

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

--答案1
--使用join結果集要排除學號01
SELECT
  DISTINCT student.* 
FROM
  sc join 
  student on 
  sc.sid=student.sid where 
  sc.sid<>'01' and sc.cid in 
  (SELECT
    cid
  FROM
	sc where 
	sid='01')

--答案2
--使用in
select 
  * 
from 
  student where 
  sid in 
  (select sid from sc where sid<>'01' and cid in 
  (select cid from sc where sid='01'))

12.查詢和" 01 "號的同窗學習的課程徹底相同的其餘同窗的信息

--答案1
--使用group_concat(),注意須要排序
SELECT
  student,*
FROM 
  sc join 
  student on 
  sc.sid=student.sid and 
  sc.sid<>'01' group by 
  sc.sid having 
  GROUP_CONCAT(cid order by cid)=
  (SELECT
    GROUP_CONCAT(cid)
  from
    sc where 
    sid='01')

--使用join有些繁瑣,要知足跟01的課程數量與課程名稱都同樣
SELECT
  student.*
from 
  sc join 
  student on 
  sc.sid=student.sid join
  (SELECT
    cid
  from
    sc where 
    sid='01'
  ) 
  a on 
  sc.cid=a.cid and 
  sc.sid<>'01' group by 
  sc.sid having 
  count(1)=(select count(1) from sc where sid='01')

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

--答案1 使用in和not in
select
  sname
from 
  student where 
  sid not in 
  (select sid from sc where cid in 
    (select cid from course where tid=
      (select tid from teacher where tname='張三')
    )
  )

--答案2
--使用join
SELECT
  sname
from 
  teacher t join 
  course c on 
  t.tid=c.tid and 
  tname='張三' join 
  sc on 
  c.cid=sc.cid 
  RIGHT JOIN 
  student s on 
  sc.sid=s.sid where 
  sc.sid is null
相關文章
相關標籤/搜索