Mysql編程練習之二

Mysql編程練習之二

2.查詢教師全部的單位,即不重複的depart列
select depart
from teacher
group by depart;sql

注://下面這個SQL是錯誤的
select *
from teacher
group by depart;
1.去除重複列
select distinct depart
from teacher;
5.查詢student表中95031班或者性別爲女的同窗記錄
select *
from student
where class=95031 or ssex = ‘女’;編程

7.以Cno升序、Degree降序查詢Score表的全部記錄。
錯誤的寫法
select *
from score
order by cno asc and degree desc;(and多餘)ide

正確的寫法
select *
from score
order by cno asc , degree desc;排序

8.查詢「95031」班的學生人數。
這裏須要注意的是,count(*)是對單一字段的count,而非全部字段的count。
select count(*)
from student
where class = ‘95031’;it

10.查詢Score表中的最高分的學生學號和課程號。(子查詢或者排序)
1.使用子查詢
select sno,cno
from score
where degree in(
select max(degree) min(degreee)
from score);table

2.使用排序class

十一、 查詢每門課的平均成績。
select cno,avg(degree)
from score
group by cno;select

十二、查詢Score表中至少有5名學生選修的並以3開頭的課程的平均分數。
select avg(degree)
from score
where 4<(sleect count(*) from score group by cno)
and cno like ‘%3’;語法

select cno,avg(degree)
from score
where cno like ‘%3’ and cno in
(select cno
from score
group by cno
having count(*)>=3);查詢

select cno,avg(Degree)
from score
where Cno like ‘3%’ and Cno in
(select Cno from score group by Cno having count(*)>=5);
用in 不用= 是由於可能會有多個

having的使用!!
1三、查詢分數大於70,小於90的Sno列。
select sno,degree
from score
where grade > 70 and score<90;

1四、查詢全部學生的Sname、Cno和Degree列。
select sname,cno,degree
from score,student
where student.sno = score.sno;
但是上面的運行結果以下:
就是按照cno來分組了,而非是按照是sname分組了

1五、 查詢全部學生的Sno、Cname和Degree列。
select Sno,Cname,Degree
from Score,Course
where Score.Cno=Course.Cno;

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

select Sname,Cname,Degree
from student,course,score
where student.Sno=score.Sno and course.Cno=score.Cno;
如何理解where語句?

select Sname,Cname,Degree
from student
join score on student.Sno=score.Sno
join course on course.Cno=score.Cno;

1七、查詢「95033」班學生的平均分。
select sno,avg(degree)
from score
where sno in (
select sno
from student
where class = ‘95031’);

上下兩個都正確

select avg(degree) as ‘class=95031’
from Score
where Sno in (select Sno from Student where Class=’95031’ );

1八、 假設使用以下命令創建了一個grade表:

create table grade(low int(3),upp int(3),rank char(1));
insert into grade values(90,100,’A’),
(80,89,’B’),
(70,79,’C’),
(60,69,’D’),
(0,59,’E’);

現查詢全部同窗的Sno、Cno和rank列。
select Sno,Cno,rank from Score,grade
where degree between low and upp;

不理解爲何直接能夠between low and upp?
這裏sno和cno,degree均是score中的表
rank 是grade中的表

1九、查詢選修「3-105」課程的成績高於「109」號同窗成績的全部同窗的記錄。
select student.sno,sname
from student,course,score
where score.cno=’3-105’ and score.degree >(
select max(degree)
from score
where score.cno = ‘3-105’ and score.sno = 109);

1.Unknown column ‘cno’ in ‘where clause’的錯誤是由於:在student表中,沒有
cno字段
2.寫完以後,應該思考一下語法結構是否正確,是否有冗餘等

select * from score
where Cno=’3-105’ and degree>
(select max(degree )
from Score
where Sno=’109’ and Cno=’3-105’ );

20.選了多門課程而且是這個課程下不是最高分的

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

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

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

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

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

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

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

2八、查詢「計算 機系」與「電子工程系「不一樣職稱的教師的Tname和Prof。

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

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

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

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

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

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

35 、 查詢全部未講課的教師的Tname和Depart.

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

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

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

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

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

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

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

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

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

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

相關文章
相關標籤/搜索