MySql練習題

一、學生表                                                                                            二、教師表mysql

            

 

三、課程表                                                                   四、分數表sql

                      

 

2、練習題spa

一、查詢Student表中的全部記錄的Sname、Ssex和Class列。
mysql> select Sname,Ssex,Class from Student;
二、 查詢教師全部的單位即不重複的Depart列 
mysql> Select Depart from teacher group by Depart;
 
mysql> select distinct depart from teacher;
 
三、 查詢Student表的全部記錄。
mysql> select * from student;
四、 查詢Score表中成績在60到80之間的全部記錄
mysql> select * from Score where Degree > 60 and Degree<80;
 
mysql> select * from score where degree between 60 and 80;
五、 查詢Score表中成績爲85,86或88的記錄。
mysql> select * from Score where Degree = 85 or Degree=86 or Degree = 88;
 
mysql> select * from Score where Degree in (85,86,88);
 

 六、 查詢Student表中「95031」班或性別爲「女」的同窗記錄。code

mysql> Select * from Student where Class=95031 or Ssex = '';
七、 以Class降序查詢Student表的全部記錄。 
 
mysql> Select * from Student order by Class desc;
 

   八、 以Cno升序、Degree降序查詢Score表的全部記錄。 blog

 
mysql> Select * from (Select * from Score order by Cno) as s order by Degree desc;
 
mysql> select * from score ORDER BY cno asc,degree desc;
九、 查詢「95031」班的學生人數。
mysql> Select count(1) from Student where Class = 95031;

十、 查詢Score表中的最高分的學生學號和課程號。(子查詢或者排序)
mysql> Select Sno,Cno from Score where Degree = (Select max(Degree) from Score);
 

10.1查詢Score表中除了每門課程最高分的學生學號和課程號。(子查詢或者排序)排序

mysql> Select Sno,Cno from Score where Degree not in (Select max(Degree) from Score group by Cno);

十一、 查詢每門課的平均成績。io

mysql> Select Cno,avg(Degree) from Score group by Cno;

十二、查詢Score表中至少有5名學生選修的並以3開頭的課程的平均分數。class

mysql> Select Cno,avg(Degree) from group by Cno having count(1)>=5 and Cno like "3%";
mysql> Select Cno,avg(Degree) from Score group by Cno having count(1)>=5 and left(Cno,1)= "3";
  • 增長: 查詢Score表中至少有3名學生選修的不以3開頭的課程的平均分數
mysql> Select Cno,avg(Degree) from Score group by Cno having count(Sno)>=3 and left(Cno,1)<>"3";


1三、查詢分數大於等於70,小於等於90的Sno列。
select

  • 注意:between and包含邊界值
mysql> Select Sno from Score where Degree between 70 and 90;
  • 增長:查詢分數小於70,大於90的Sno列
  • not between and不包含邊界值
mysql> Select Sno from Score where Degree not between 70 and 90;

1四、查詢全部學生的Sname、Cno和Degree列。
nio

mysql> Select Sname,Cno,Degree from Student join Score on Student.Sno=Score.Sno;

1五、查詢全部學生的Sno、Cname和Degree列。

mysql> Select Sno,Cname,Degree from Score join Course on Course.Cno=Score.Cno;

 1六、查詢全部學生的Sname、Cname和Degree列。

mysql> Select Sname,Cname,Degree from Course join (Select Sname,Cno,Degree from Student join Score on Student.Sno=Score.Sno) as s on Course.Cno=s.Cno;

1七、 查詢「95033」班學生的平均分。

  • mysql> Select avg(Degree) from (Select Degree from Score join (Select Sno from Student where Class=95033) as s on Score.Sno = s.Sno) as d;
  • mysql> Select avg(Degree) from Score where Sno in (Select Sno from Student where Class = 95033);

1八、現查詢全部同窗的Sno、Cno和rank列

1九、查詢選修「3-105」課程的,而且成績高於「109」號同窗成績的全部同窗的記錄。

mysql> Select * from Score where Degree > (Select Degree from Score where Cno = "3-105" and Sno = 109) and Cno ="3-105";

2一、 查詢成績高於學號爲「109」、課程號爲「3-105」的成績的全部記錄。

mysql> Select * from Score where Degree > (Select Degree from Score where Cno = "3-105" and Sno = 109);


2二、查詢和學號爲108的同窗同年出生的全部學生的Sno、Sname和Sbirthday列。

mysql> Select Sno,Sname,Sbirthday from Student where year(Sbirthday) = (Select year(Sbirthday) from Student where Sno = 108);

2三、查詢「張旭「教師任課的學生成績。

mysql> Select Sno,Degree,Cno from Score where Cno in (Select Cno from Course where Tno in (Select Tno from Teacher where Tname = "張旭"));


2四、查詢選修某課程的同窗人數多於5人的教師姓名。

mysql> Select Tname from Teacher where Tno in (Select Tno from Course where Cno in (Select Cno from Score group by Cno having count(1) > 5));


 2五、查詢95033班和95031班全體學生的記錄。

mysql> Select * from Student where Class in (95033,95031);

2六、  查詢存在有85分以上成績的課程Cno.

mysql> Select distinct Cno from Score where Degree > 85;

2七、查詢出「計算機系「教師所教課程的成績表。

mysql> select * from score where cno in (select cno from course where Tno in (select tno from teacher where depart = "計算機系"));

2九、查詢選修編號爲「3-105「課程且成績至少高於一個選修編號爲「3-245」的同窗的Cno、Sno和Degree,並按Degree從高到低次序排序。

mysql> Select Sno,Cno,Degree from Score where Cno = "3-105" and Degree > any(Select Degree from Score where Cno="3-245");

30、查詢選修編號爲「3-105」且成績高於選修編號爲「3-245」課程的同窗的Cno、Sno和Degree.

mysql> Select Sno,Cno,Degree from Score where Cno = "3-105" and Degree > all(Select Degree from Score where Cno="3-245");

3一、 查詢全部教師和同窗的name、sex和birthday.

select sname,ssex,sbirthday from student union all select tname,tsex,tbirthday from teacher;

3二、查詢全部「女」教師和「女」同窗的name、sex和birthday.

mysql> Select s.Sname,s.ssex,s.sbirthday from (select sname,ssex,sbirthday from student union all select tname,tsex,tbirthday from teacher) as s where s.Ssex = "女";
(select sname,ssex,sbirthday from student where ssex = "女") union all (select tname,tsex,tbirthday from teacher where tsex = "女");

3三、 查詢成績比該課程平均成績低的同窗的成績表

 

Select * from Score s1 where Degree < (Select avg(degree) from score s2 group by cno having s1.cno=s2.cno);

 

3四、 查詢全部任課教師的Tname和Depart

mysql> Select tname,depart from teacher where tno in (select distinct tno from course where cno in (select distinct cno from score group by cno));

3六、查詢至少有2名男生的班號 

錯誤:

 

mysql> select s.class from (select * from student where ssex = "男") as s group by s.class having count(1);

 

正確:

 

select class from student where ssex='' group by class having count(1) >=2

 

3七、查詢Student表中不姓「王」的同窗記錄

mysql> select * from student where left(sname,1) <>"王";
mysql> select * from student where sname not like '王%';

3八、查詢Student表中每一個學生的姓名和年齡

mysql> select sname,year(now())-year(Sbirthday) as age from student;

3九、查詢Student表中最大和最小的Sbirthday日期值。

mysql> select max(Sbirthday),min(Sbirthday) from student;

40、以班號和年齡從大到小的順序查詢Student表中的所有記錄。

mysql> select * from student order by class desc,Sbirthday;
mysql> select * from student ORDER BY class,year(now())-year(sbirthday);

4一、查詢「男」教師及其所上的課程。

mysql> select * from course where tno in (select tno from teacher where tsex = "男");

4二、查詢最高分同窗的Sno、Cno和Degree列。

mysql> select sno,cno,degree from score where degree = (select max(degree) from score);

4三、查詢和「李軍」同性別的全部同窗的Sname.

mysql> Select sname from student where ssex = (select ssex from student where sname ="李軍");

4四、查詢和「李軍」同性別並同班的同窗Sname.

mysql> Select sname from student where (ssex,class) in  (select ssex,class from student where sname ="李軍");

4五、查詢全部選修「計算機導論」課程的「男」同窗的成績表.

 

mysql> select * from score where cno = (select cno from course where cname = "計算機導論") and sno in (select sno from student where ssex = "男")

 

 

4七、查詢各科選課總人數和及格人數,以以下形式顯示:課程編號,課程名稱,總人數,及格人數,按課程編號升序排列

 

select s.cno,cname,allnum,passnum from (select s1.cno,allnum,passnum from (select Cno,count(*) as allnum from score group by cno) as s1 join (select Cno,count(*) as passnum from score where degree >= 80 group by cno) as s2 on s1.cno=s2.cno) as s join course on s.cno=course.cno order by s.cno;
相關文章
相關標籤/搜索