MySQL查詢 45道練習題


SQL查詢45道練習題

1.查詢Student表中的所有記錄的Sname、Ssex和Class列。
select sname,ssex,class from student
2.查詢教師所有的單位即不重復的Depart列。
select distinct depart from teacher
3.查詢Student表的所有記錄。
select * from student
4.查詢Score表中成績在60到80之間的所有記錄。
select degree from score where Degree between 60 and 80
或者
select degree from score where Degree>60 and degree<80
5.查詢Score表中成績為85,86或88的記錄。
select degree from score where degree=85 or degree=86 or degree=88
或者
select degree from score where degree in (85,86,88)
6.查詢Student表中“95031”班或性別為“女”的同學記錄。
select * from student where class="95031" or ssex='女'
7.以Class降序查詢Student表的所有記錄。
select * from student order by class desc
8.以Cno升序、Degree降序查詢Score表的所有記錄。
select * from score order by cno asc,degree desc
9. 查詢“95031”班的學生人數。
select count(*) from student where class="95031"
10.查詢Score表中的最高分的學生學號和課程號。(子查詢或者排序)
排序查詢:select * from score order by degree desc limit 0,1
子查詢:select sno,cno from score where degree=(select max(degree) from score)
11.查詢每門課的平均成績。
select cno,avg(degree) from score group by cno
12.查詢Score表中至少有5名學生選修的並以3開頭的課程的平均分數。
select avg(degree) from score where cno=( select cno from score group by cno having count(cno)>4 and cno like '3%')

select avg(degree) from score where cno like '3%' and cno in(select cno from score group by cno having count(cno)>4)

select avg(degree) from score group by cno having count(cno)>4 and cno like '3%'
13.查詢分數大於70,小於90的Sno列。
select sno from score where degree>70 and degree<90;
14.查詢所有學生的Sname、Cno和Degree列。
select student.Sname,score.Cno,score.Degree from student,score where student.Sno = score.Sno;
15.查詢所有學生的Sno、Cname和Degree列。
select course.Cname,score.sno,score.Degree from course,score where course.cno = score.cno;
16.查詢所有學生的Sname、Cname和Degree列。
select student.Sname,course.Cname,score.Degree from student,course,score where score.Cno=course.Cno and score.Sno=student.sno
17.查詢“95033”班學生的平均分。
select avg(degree) from score where sno in (select sno from student where class="95033");
18.假設使用如下命令建立了一個grade表:
create table grade(low int(3),upp int(3),rank char(1))
insert into grade values(90,100,’A’)
insert into grade values(80,89,’B’)
insert into grade values(70,79,’C’)
insert into grade values(60,69,’D’)
insert into grade values(0,59,’E’)
現查詢所有同學的Sno、Cno和rank列。
select score.Cno,score.Sno,grade.rank from score,grade where score.Degree < grade.upp and score.Degree>grade.low;

沒有重名的列可以不加表名,下面的寫法是不加表名的。

select sno,cno,rank from score,grade where degree between low and upp
19.查詢選修“3-105”課程的成績高於“109”號同學成績的所有同學的記錄。
select * from student where sno in(select sno from score where degree > (select degree from score where sno=109 and cno='3-105') and cno='3-105');

或者

select * from score shere cno ='3-105' and degree >(select max(degree) from score where sno='109')

或者

select * from score shere cno ='3-105' and degree >(select max(degree) from score where sno='109' and cno='3-105')


20.查詢score中選學多門課程的同學中分數為非最高分成績的記錄。

全部成績的最高分:
select student.sname,score.sno,score.cno,score.degree from student,score where student.sno=score.sno and score.sno in(select sno from score group by sno having count(cno)>1) and score.degree not in(select max(degree) from score)

每一門的最高分:

select * from score a where sno in(select sno from score group by sno having count(*)>1) and degree<(select max(degree) from score b where b.cno = a.cno)

21.查詢成績高於學號為“109”、課程號為“3-105”的成績的所有記錄。
select * from student where sno in(select sno from score where degree > (select degree from score where sno=109 and cno='3-105') and cno='3-105');
22.查詢和學號為108的同學同年出生的所有學生的Sno、Sname和Sbirthday列。
select sno,sname,sbirthday from student where year(sbirthday)= (select year(sbirthday) from student where sno=108);
23.查詢“張旭“教師任課的學生成績。
select degree from score where cno= (select cno from course where tno= (select tno from teacher where tname='張旭'));
24.查詢選修某課程的同學人數多於5人的教師姓名。
select teacher.tname, course.cname,score.cno from teacher,course,score where teacher.tno=course.tno and course.cno=score.cno and score.cno= (select cno from score group by cno having count(cno)>5)
25.查詢95033班和95031班全體學生的記錄。
select distinct student.*,score.* from student,score where student.sno=score.sno
26.查詢存在有85分以上成績的課程Cno.
select distinct cno from score where degree>85
27.查詢出“計算機系“教師所教課程的成績表。
select * from score where cno in(select cno from course where tno in(select tno from teacher where depart='計算機系'));
28.查詢“計算機系”與“電子工程系“不同職稱的教師的Tname和Prof

理解一:計算機系的教室和電子工程系的不同的。
select depart,tname,prof from teacher where prof not in(select prof from teacher where depart='電子工程系')

理解二:兩系互不相同的。

select tname,prof from teacher where depart='計算機系'  and prof not in(select prof from teacher where depart='電子工程系')

相關子查詢:

select Tname,Prof from Teacher a where Prof not in(select Prof from Teacher b where a.Depart!=b.Depart)


29.查詢選修編號為“3-105“課程且成績至少高於選修編號為“3-245”的同學的Cno、Sno和Degree,並按Degree從高到低次序排序。
select cno,sno,degree from score a where cno='3-105' and degree>any(select degree from score b where cno='3-245' and b.sno=a.sno) order by degree desc

any 代表括號內的任意一個
30.查詢選修編號為“3-105”且成績高於選修編號為“3-245”課程的同學的Cno、Sno和Degree.

select cno,sno,degree from score a where cno='3-105' and degree>all(select degree from score b where cno='3-245' and b.sno=a.sno) order by degree desc
all代表所有的


31.查詢所有教師和同學的name、sex和birthday.
select teacher.Tname as name,teacher.Tsex as sex,teacher.Tbirthday as birthday from teacher
union
select student.Sname as name,student.Ssex as sex,student.Sbirthday as birthday from student
32.查詢所有“女”教師和“女”同學的name、sex和birthday.
select teacher.Tname as name,teacher.Tsex as sex,teacher.Tbirthday as birthday from teacher where tsex='女'
union
select student.Sname as name,student.Ssex as sex,student.Sbirthday as birthday from student where ssex='女'
33.查詢成績比該課程平均成績低的同學的成績表。
select * from score a where degree<(select avg(degree) from score b where b.cno =a.cno)
34.查詢所有任課教師的Tname和Depart.
select tname,depart from teacher where tno in(select tno from course where cno in(select cno from score))
35.查詢所有未講課的教師的Tname和Depart.
select tname,depart from teacher where tno not in (select tno from course where cno in (select distinct cno from score))
36.查詢至少有2名男生的班號。
select distinct class from student a where (select count(ssex) from student b where ssex ='男' and a.class=b.class )>1
37.查詢Student表中不姓“王”的同學記錄。
select * from student where sname not like '王%'
38.查詢Student表中每個學生的姓名和年齡。
select student.sname,(2016-year(sbirthday)) as sage from student where true
網上查詢的year(curdate())意思是當前年
select student.sname,(year(curdate())-year(sbirthday)) as sage from student where true
39.查詢Student表中最大和最小的Sbirthday日期值。
select min(sbirthday),max(sbirthday) from student
40.以班號和年齡從大到小的順序查詢Student表中的全部記錄。
select * from student order by class desc,sbirthday asc
41.查詢“男”教師及其所上的課程。
select teacher.Tname,course.Cname from teacher,course where course.Tno=teacher.Tno and tsex='男'
42.查詢最高分同學的Sno、Cno和Degree列。
select sno,cno,degree from score where degree=(select max(degree) from score)
43.查詢和“李軍”同性別的所有同學的Sname.
select sname from student where ssex=(select ssex from student where sname='李軍')
44.查詢和“李軍”同性別並同班的同學Sname.
select sname from student a where ssex=(select ssex from student b where sname='李軍' and a.class=b.class)
45.查詢所有選修“計算機導論”課程的“男”同學的成績表。
select student.sno,student.sname,score.degree from student,course,score where student.ssex='男' and course.cname='計算機導論' and student.sno=score.sno and course.cno=score.cno

 

做題經驗總結:

1.做題過程中感覺比較難的是12、20、24、45題。12、20、24題能想出邏輯,后面的條件的寫法比較不好寫,試過很多遍才試寫出來。45題直接想不出來寫法。

2.對12題的理解

  查詢Score表中至少有5名學生選修的並以3開頭的課程的平均分數。
select avg(degree) from score where cno=( select cno from score group by cno having count(cno)>5 and cno like '3%')

  先查出至少有5名學生選修的並以3開頭的課程,用到的是分組查詢select 列 from 表 group by 列 having 條件,后面有2個條件,用and連接。

3.對20題的理解(20題和24題解法相近)

  查詢score中選學多門課程的同學中分數為非最高分成績的記錄。
select student.sname,score.sno,score.cno,score.degree from student,score where student.sno=score.sno and score.sno in(select sno from score group by sno having count(cno)>1) and score.degree not in(select max(degree) from score)

涉及到多個表時,可以采取這種寫法。先寫出想查詢的列,然后寫關系。注意關系的寫法,后面2個關系為選修課程大於1門的和分數不屬於最高分的,and后面不是直接寫關系,而是先寫需要限制關系的列,括號里寫限制的條件。關系的寫法可以認為是固定寫法。

4.對45題的理解

  查詢所有選修“計算機導論”課程的“男”同學的成績表。
select student.sno,student.sname,score.degree from student,course,score where student.ssex='男' and course.cname='計算機導論' and student.sno=score.sno and course.cno=score.cno

   這道題涉及到了3張表,其關系是student表和course表都和score表有關系,但是彼此之間沒有聯系。最初的想法是先寫出score表的degree列,再去找限制關系,但是2個沒有關系的表的限制關系無法寫在一個表中。正確的查詢方法是先寫出這3個表的存在關系的列,讓把3張表有了聯系,再去寫3張表的關系。

  查詢最主要的是兩個方面,一個是找要查詢的列與表或者表與表的關系,個人感覺稍微復雜點的(2個表或者2個以上的表)就可以用45題的解法,;二是寫條件,條件的寫法注意以下題中的寫法:

6題:class=95031 or ssex='女'  字符串要加‘’。

10題:limit0,1跳過0條數據取1條,意思就是取第一條數據。

12題:where cno=( select cno from score group by cno having count(cno)>5 and cno like '3%')分組查詢后又加一個條件,用and連接。

18題:where score.Degree < grade.upp and score.Degree>grade.low 分數可以直接比較

20題:and score.sno in(select sno from score group by sno having count(cno)>1) and score.degree not in(select max(degree) from score)   and后面加上score.sno in再寫條件,否則沒法加限制條件。

22題:year(sbirthday) 生日取年,例如2016-07-12,取出來的是2016。

28題:!=表示不等於

38題:year(curdate())意思是當前年


免責聲明!

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



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