1. 程式人生 > >MYSQL數據庫45道練習題

MYSQL數據庫45道練習題

練習題 l數據庫 tin 子查詢 teacher int mysql 職稱 values

--第一題查詢Student表中的所有記錄的Sname、Ssex和Class列。
select Sname,Ssex,Class from Student;
--第二題查詢教師所有的單位即不重復的Depart列。
select distinct Depart from Teacher;
--第三題、 查詢Student表的所有記錄
select * from Student;
--第四題、 查詢Score表中成績在60到80之間的所有記錄。
select * from Score where Degree between 60 and 80;
--第五題、 查詢Score表中成績為85,86或88的記錄。
select * from Score where Degree = 85 or Degree = 86 or Degree = 88;
--第六題、 查詢Student表中“95031”班或性別為“女”的同學記錄。
select * from Student where Class = 95031 or Ssex = ‘女‘;
--第七題、 以Class降序查詢Student表的所有記錄。
select * from Student order by Class asc;
--第八題、 以Cno升序、Degree降序查詢Score表的所有記錄。
select * from Score order by Cno asc , Degree desc;
--第九題、 查詢“95031”班的學生人數。
select count(*) from Student where Class = 95031;
--第十題、 查詢Score表中的最高分的學生學號和課程號。(子查詢或者排序)
select Sno,Cno from Score where Degree = (select max(Degree) from Score);
--第十一題、 查詢每門課的平均成績。
select avg(Degree) from Score where Cno=‘3-245‘;
select avg(Degree) from Score where Cno = ‘3-105‘;
select avg(Degree) from Score where Cno = ‘6-166‘;
select distinct Cno from Score;
select Cno,avg(Degree) from Score where select distinct Cno from Score;
--上面是錯誤歷程-------------------------------------------------------------------------------------
select Cno,avg(Degree) from Score group by Cno;
--第十二題、查詢Score表中至少有5名學生選修的並以3開頭的課程的平均分數。
select CNO,avg(Degree) from Score where Cno like ‘3%‘ group by Cno having count(*)>4;
--先取首字母為3的 在分組 分組 完成後篩選條件為 Cno數量大於等於5;
--where 後面不能加聚合函數-----------------------------------------------------------------------------------------
--第十三題、查詢分數大於70,小於90的Sno列。
select Sno,Degree from Score where Degree>70 and Degree<90;
--第十四題、查詢所有學生的Sname、Cno和Degree列。
select Sname,Cno,Degree from Student join Score on Student.Sno = Score.Sno;
--方法二
select Student.Sname,Score.Cno,Score.Degree from Student,Score where Student.Sno = Score.Sno;
--第十五題、查詢所有學生的Sno、Cname和Degree列。
select Sno,Cname,Degree from Course join Score on (Score.Cno = Course.Cno);
--第十六題、查詢所有學生的Sname、Cname和Degree列。
select Sname,Cname,Degree from Student join Score on (Student.Sno = Score.Sno) join Course on (Course.Cno = Score.Cno);
--第十七題、 查詢“95033”班學生的平均分。
select Class,avg(Degree) from Score join Student on(Student.Sno = Score.Sno) where Class = 95033;
--第十八題、 假設使用如下命令建立了一個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 Sno,Cno,rank from grade join Score;
select Sno,Cno,rank from grade join Score on (Degree between low and upp);
--第十九題、 查詢選修“3-105”課程的成績高於“109”號同學成績的所有同學的記錄。
select Degree from Score where Sno = 109 and Cno = ‘3-105‘;

select * from Score where Degree > (select Degree from Score where Sno = 109 and Cno = ‘3-105‘) group by Sno having Cno = ‘3-105‘;

select Sno from Score where Cno = ‘3-105‘ and Degree > (select max(Degree) from Score where Sno = 109 and Cno = ‘3-105‘);

--第二十題、查詢score中選學多門課程的同學中分數為非最高分成績的記錄。
select Sno from Score group by Sno having count(Cno)>1--選多門課同學的學號
select max(Degree) from Score--最高分
select * from Score where Degree < (select max(Degree) from Score)--小於最高分的學生信息
--選取選修多門課程且非最高成績的學生學號
select sno from Score where Degree < (select max(Degree) from Score) group by Sno having count(Cno)>1;
--該學號所有信息
select * from Score where sno in (select sno from Score where Degree < (select max(Degree) from Score) group by Sno having count(Cno)>1)
--第二十一題、 查詢成績高於學號為“109”、課程號為“3-105”的成績的所有記錄。
select * from Score where Degree > (select max(Degree) from Score where Sno = 109) and Cno = ‘3-105‘;
--第二十二題、查詢和學號為108的同學同年出生的所有學生的Sno、Sname和Sbirthday列。


select Sno,Sname,Sbirthday from Student where year(Sbirthday) = (select year(Sbirthday) from Student where Sno = 107);

--第二十三題、查詢“張旭“教師任課的學生成績。
select Tno from Teacher where Tname = ‘張旭‘
select Cno from Course where Tno = (select Tno from Teacher where Tname = ‘張旭‘)
select Degree from Score where Cno = (select Cno from Course where Tno = (select Tno from Teacher where Tname = ‘張旭‘))
--第二十四題、查詢選修某課程的同學人數多於5人的教師姓名。
select Cno from Score group by Cno having count(Cno)>5
select Tno from Course where Cno in (select Cno from Score group by Cno having count(Cno)>5)
select Tname from Teacher where Tno in (select Tno from Course where Cno in (select Cno from Score group by Cno having count(Cno)>5))

--第二十五題、查詢95033班和95031班全體學生的記錄。

select * from Student where class = 95033 or class = 95031

--第二十六題、 查詢存在有85分以上成績的課程Cno.

select distinct Cno from Score where Degree > 85

--第二十七題、查詢出“計算機系“教師所教課程的成績表。

select Tno from Teacher where Depart = ‘計算機系‘
select Cno from Course where Tno in (select Tno from Teacher where Depart = ‘計算機系‘)
select * from Score where Cno in (select Cno from Course where Tno in (select Tno from Teacher where Depart = ‘計算機系‘))

--第二十八題、查詢“計算 機系”與“電子工程系“不同職稱的教師的Tname和Prof。

select Tname,Prof from Teacher where Prof in (select Prof from Teacher where Depart = ‘計算機系‘ or Depart = ‘電子工程系‘ group by Prof having count(Prof) = 1)
--select Prof from Teacher where Depart = ‘計算機系‘ or Depart = ‘電子工程系‘ group by Prof having count(Prof) = 1

--第二十九題、查詢選修編號為“3-105“課程且成績至少高於選修編號為“3-245”的同學的Cno、Sno和Degree,並按Degree從高到低次序排序。

select Cno,Sno,Degree from Score where Cno = ‘3-105‘ and Degree >= (select max(Degree) from Score where Cno = ‘3-245‘)
order by Degree asc

--第三十題、查詢選修編號為“3-105”且成績高於選修編號為“3-245”課程的同學的Cno、Sno和Degree.
select Cno,Sno,Degree from Score where Cno = ‘3-105‘ and Degree >= (select max(Degree) from Score where Cno = ‘3-245‘)

--第三十一題、 查詢所有教師和同學的name、sex和birthday.

select Tname,Tsex,Tbirthday from Teacher union select Sname,Ssex,Sbirthday from Student

--第三十二題、查詢所有“女”教師和“女”同學的name、sex和birthday.

select Tname,Tsex,Tbirthday from Teacher where Tsex = ‘女‘ union select Sname,Ssex,Sbirthday from Student where Ssex = ‘女‘

--第三十三題、 查詢成績比該課程平均成績低的同學的成績表。

select Cno,avg(Degree) from Score group by Cno;
select * from Score join (select Cno as Pno,avg(Degree) as pj from Score group by Pno) as pjb on (Score.Cno = pjb.Pno)
select * from Score join (select Cno as Pno,avg(Degree) as pj from Score group by Pno) as pjb on (Score.Cno = pjb.Pno)
where Degree < pj;

--第三十四題、 查詢所有任課教師的Tname和Depart.


select * from Score group by Cno

select distinct Tname,Depart from Score join Course on Course.Cno = Score.Cno join Teacher on Teacher.Tno = Course.Tno where Course.Cno in
(select Cno from Score group by Cno)

--第三十五題、 查詢所有未講課的教師的Tname和Depart.
select Cno from Score group by Cno
select Tno from Course where Cno not in (select Cno from Score group by Cno)
select Tname,Depart from Teacher where Tno in (select Tno from Course where Cno not in (select Cno from Score group by Cno))


--第三十六題、查詢至少有2名男生的班號。

select * from Student group by Class having count(Ssex) > 1 and Ssex = ‘男‘
select Class from Student where Sno in (select Sno from Student group by Class having count(Ssex) > 1 and Ssex = ‘男‘)

--第三十七題、查詢Student表中不姓“王”的同學記錄。

select * from Student where Sname not in (select Sname from Student where Sname like ‘李%‘)

--第三十八題、查詢Student表中不姓“王”的同學記錄。

select sname from Student where Sname not like ‘王%‘;

-- 第三十九題、查詢Student表中最大和最小的Sbirthday日期值。

select MAX(sbirthday) as ‘最大值‘,MIN(sbirthday)‘最小值‘ from Student

-- 第四十題、以班號和年齡從大到小的順序查詢Student表中的全部記錄。

select class,sbirthday from Student order by Class desc,Sbirthday asc

-- 第四十一題、查詢“男”教師及其所上的課程。

select cname from course where tno in(select tno from teacher where tsex=’男’)

select tname,cname from teacher ,course where teacher.tno=course.tno and tsex=‘男‘

-- 第四十二題、查詢最高分同學的Sno、Cno和Degree列。

(1)select * from score where Degree = (select max(Degree) from score)

(2)select top 1 * from score order by degree desc

-- 第四十三題、查詢和“李軍”同性別的所有同學的Sname.

select sname from Student where Ssex=(select Ssex from Student where Sname=‘李軍‘)

-- 第四十四題、查詢和“李軍”同性別並同班的同學Sname.

select sname from Student where Ssex=(select Ssex from Student where Sname=‘李軍‘)and class=(select Class from Student where Sname=‘李軍‘);

-- 第四十五題、查詢所有選修“計算機導論”課程的“男”同學的成績表。

select Degree from score where Sno in(select Sno from student where Ssex=‘1‘) and Cno in (select Cno from course where Cname = ‘計算機導論‘);

MYSQL數據庫45道練習題