創建四張數據表
學生表student:學號,姓名,性別,出生年月日,所在班級
教師表teacher:教師編號,教師名,教師性別,出生年月日,職稱,所在部門
課程表course:課程號,課程名,教師編號
成績表score:學號,課程號,成績
添加信息:
學生表:
insert into student values ('1001','老大','男','1999-08-01','2000');
insert into student values ('1002','老二','女','1999-08-02','2000');
insert into student values ('1003','老三','男','1999-08-03','2001');
insert into student values ('1004','老四','女','1999-08-04','2001');
insert into student values ('1005','老五','男','1999-08-05','2002');
insert into student values ('1006','老六','男','1999-08-06','2002');
insert into student values ('1007','老七','男','1999-08-07','2003');
insert into student values ('1008','老八','女','1999-08-08','2004');
教師表:
insert into teacher values('201','王菊','女','1890-10-11','副教授','計算機系');
insert into teacher values('202','李平','男','1890-10-11','教授','電子工程系');
insert into teacher values('203','張利','男','1890-10-11','副教授','自動化系');
insert into teacher values('204','王霞','女','1890-10-11','講師','電子工程系');
insert into teacher values('205','劉傑','男','1890-10-11','助教','計算機系');
課程表:
insert into course values ('3-101','計算機基礎','201');
insert into course values ('3-102','高等數學','202');
insert into course values ('3-103','操作系統','203');
insert into course values ('3-104','概率論','204');
insert into course values ('3-105','概率論','205');
成績表:
insert into score values ('1001','3-101','98');
insert into score values ('1001','3-103','97');
insert into score values ('1002','3-101','88');
insert into score values ('1003','3-104','78');
insert into score values ('1004','3-103','98');
insert into score values ('1006','3-101','77');
insert into score values ('1008','3-104','55');
insert into score values ('1001','3-102','98');
insert into score values ('1007','3-105','67')
查詢練習:
1.查詢student表的所有記錄;
select * from student
2.查詢student表的sname,ssex,class列;
select sname,ssex,class from student
3.查詢教師所有的部門,不重復顯示
select distinct depart from teacher
4.查詢成績表中成績在77-88之間的所有記錄
select * from score where degree between '77' and '88'
由查詢的數據可知,between...and查詢的結果包括兩端的值
select * from score where degree>77 and degree<88
由查詢數據可知,直接使用運算符比較不包含兩端數據
5.查詢成績為98,88,或77的記錄
select * from score where degree = '77' or degree = '88' or degree = '98'
select * from score where degree in (77,88,98) (in表示或者的關系)
6.查詢學生表中1001班或性別為女的學生記錄
select * from student where class='2000' or ssex = '女'
7.以class降序查詢學生表的所有記錄
select * from student order by class desc
8.以cno升序,degree降序查詢成績表的所有記錄
select * from score order by cno asc , degree desc
9.查詢2000班的所有學生人數
select count(*) from student where class = 2000
10.查詢成績表中的最高分的學生號和課程號(子查詢)
select sno,cno from score where degree=(select max(degree) from score)
11.查詢每門課的平均成績(分組查詢group by)
select cno,avg(degree)from score group by cno
12.查詢score 表中至少有2名學生選修的並以3開頭的課程的平均成績(具有條件的分組查詢group by....having,模糊查詢)
select cno,count(cno),avg(degree) from score group by cno having count(cno)>=2 and cno like'3%'
13.查詢分數大於70,小於90的sno列
select sno,degree from score where degree between 70 and 90
select sno,degree from score where degree>70 and degree<90
14.查詢所有學生的sname,cno,degree列(多表查詢)
select sname,cno,degree from student,score where student.sno = score.sno
15.查詢所有學生的sno,cname,degree的列(多表聯查)
select sno,cname,degree from course,score where course.cno = score.cno
16.查詢所有學生的sname,cname,degree(三表聯查,找到一個中間表,另兩個表與中間表產生聯系)
select sname,cname,degree from student,course,score where student.sno= score.sno and course.cno = score.cno
17.查詢“2000”班學生每門課的平均成績
(1)2000班的學生的學號 select * from student where class='2000' select sno from student where class='2000' (2)根據(1)學生號查詢2000班的學生成績表 select * from score where sno in (select sno from student where class='2000') select cno,degree from score where sno in (select sno from student where class='2000') (3)根據(2)中的cno對學科進行分組求平均分 select cno,avg(degree) from score where sno in (select sno from student where class='2000') group by cno
18.查詢選修“3-101”課程的成績高於“1006”號同學選修“3-101”成績的所有同學的記錄
(1)查選“1006”號同學選修3-101課的成績 select * from score where cno='3-101' and sno='1006' (2)查詢選修“3-101”課程的成績高於“1006”號同學選修“3-101”成績的所有同學的記錄 select * from score where degree>(select degree from score where cno='3-101' and sno='1006')and cno='3-101'
19.查詢成績高於學號“1006”、課程號為“3-101”的成績的所有記錄
(1)查選“1006”號同學選修3-101課的成績 select * from score where cno='3-101' and sno='1006' (2)查詢成績高於學號“1006”、課程號為“3-101”的成績的所有記錄 select * from score where degree>(select degree from score where cno='3-101' and sno='1006')
20.查詢學號為1008、1001的同學的同年出生的所有學生的sno\sname\sbrithday列
(1)查詢學號為1008、1001的同學的同年出生 select year(sbrithday) from student where sno in ('1001','1008') (2)查詢學號為1008、1001的同學的同年出生的所有學生的sno\sname\sbrithday列 select sno,sname,sbrithday from student where year(sbrithday) =(select year(sbrithday) from student where sno in ('1001','1008'))
21.查詢王菊老師任課的學生成績
(1)查詢王菊老師的課程號 select cno from teacher as t,course as c where t.tno=c.tno (2)查詢王菊老師任課的學生成績 select sno,degree from score where cno=(select cno from teacher as t,course as c where t.tno=c.tno and tname='王菊')
22.查詢選修某課程的學生人數多於2人的教師姓名
(1)根據課程號查詢選修人數大於2人的課程號(group by的條件分組查詢) select cno from score group by cno having count(cno)>2 (2)根據(1)查詢出課程表中的教師號 select tno from course where cno =(select cno from score group by cno having count(cno)>2) (3)根據教師號查詢教師名字 select tname from teacher where tno=(select tno from course where cno =(select cno from score group by cno having count(cno)>2))
23.查詢2000班和2001班的學生記錄(或的使用,In,or)
select * from student where class in('2000','2001')
select * from student where class ='2000' or class='2001'
24.查詢計算機系老師所教課程的成績
(1)查詢計算機系老師的教師號 select tno from teacher where depart='計算機系' (2)根據教師號查詢老師對應的課程號 select cno from course where tno in (select tno from teacher where depart='計算機系') (3)根據課程號查詢成績 select cno,degree from score where cno in (select cno from course where tno in (select tno from teacher where depart='計算機系'))
25.查詢計算機系和電子工程系不同職稱的教師姓名和職稱prof(這兩個系中職稱相同的舍去)
(1)查詢電子工程系的所有老師的職稱 select prof from teacher where depart = '計算機系' 查詢計算機系的所有老師的職稱 select prof from teacher where depart = '電子工程系' (2)查詢計算機系中與電子工程系中不同的職稱 select prof from teacher where depart = '計算機系' and prof not in (select prof from teacher where depart = '電子工程系') 查詢電子工程系中與計算機系中不同的職稱 select prof from teacher where depart = '電子工程系' and prof not in (select prof from teacher where depart = '計算機系') (3)使用union將(2)中的語句進行聯合(求並集),得出兩個系中不同的職稱 select prof from teacher where depart = '電子工程系' and prof not in (select prof from teacher where depart = '計算機系') union select prof from teacher where depart = '計算機系' and prof not in (select prof from teacher where depart = '電子工程系')
26.查詢選修編號為“3-101”課程且成績至少高於選修編號為“3-102”的同學的cno,sno,degree,並按成績由高到底排列
(1)查詢出選修“3-102”學生的成績 select degree from score where cno='3-102' (2)查詢出選修3-101課程的成績至少大於3-102的學生cno,sno,degree的信息(至少any:只需要滿足3-101中的成績大於3-102中的最低成績即可) select cno,sno,degree from score where degree>any(select degree from score where cno='3-102') and cno ='3-101' (3)根據(2)的成績降序排列(order by ... desc) select cno,sno,degree from score where degree>any(select degree from score where cno='3-102') and cno ='3-101' order by degree desc
27.查詢選修編號為“3-101”課程且成績高於選修編號為“3-102”的同學的cno,sno,degree
(1)查詢出選修“3-102”學生的成績 select degree from score where cno='3-102' (2)查詢出選修3-101課程的成績大於3-102的學生cno,sno,degree的信息(且all:高於3-102班的最高成績才滿足) select cno,sno,degree from score where degree>all(select degree from score where cno='3-102') and cno ='3-101'
28.查詢所有教師和學生的name,sex,brithday(求並集union)
select sname as name,ssex as sex,sbrithday as brithday from student
union
select tname as name,tsex as sex,tbrithday as brithday from teacher
29.查詢成績比該成績平均分低的同學的成績表
(1)查詢每一門課的平均成績 select avg(degree) from score b group by cno (2)通過采用復制表結構的方法做條件查詢(保證使用的cno是相同的) select * from score a where degree<(select avg(degree) from score b where a.cno=b.cno)
30.查詢至少有兩名男生的班級
select count(ssex),class from student where ssex='男' group by class having count(ssex)>=2
31.查詢student表中不含“老”字的同學記錄
select * from student where sname not like'老%'
32.查詢student表中每個學生的姓名和年齡
年齡=當前年份-出生年份 (1)當前年份 select year(now()) (2)學生出生年份 select year(sbrithday) from student (3)每個學生的姓名和年齡 select sname,year(now())-year(sbrithday) as age from student
33.以班號和年齡從大到小查詢student表中所有同學的信息
select * from student order by class desc,sbrithday
34.按等級查詢
創建一個等級表 create table grade(low int(3),upp int(3),grade char(4)) 插入數據 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,grade select sno,cno,grade from score,grade where score.degree between grade.low and grade.upp
35.sql的四種連接查詢
內連接:inner join 或 join
外連接:
左外連接:left join ...on或 left outer join ...on
右外連接:right join...on 或 right outer join...on
完全外連接:full join...on 或 full outer join...on
創建兩張表:
person 表:id,name,cardId
card 表 :id,name
create table person (id int (10) primary key ,name varchar(20),cardid int(10) );
create table card (id int(10) primary key ,name varchar (20) );
向兩張表中分別添加數據
insert into card values(1,'飯卡');
insert into card values(2,'地鐵卡');
insert into card values(3,'工商卡');
insert into card values(4,'郵政卡');
insert into card values(5,'農行卡');
insert into person values(1,'老大',1);
insert into person values(2,'老二',4);
insert into person values(3,'老三',5);
insert into person values(4,'老四',2);
insert into person values(5,'老五',2);
insert into person values(6,'老六',6);
注意到person表中並沒有創建外鍵
(1)內連接(jion ...on),查詢出兩張表有關聯的信息(即兩表共有的部分)
select * from person join card on person.cardId=card.id
select * from person inner join card on person.cardId=card.id
(2)左外連接:會把左邊表里的所有數據取出來,而右邊表中的數據,如果有相等的就顯示出來,若沒有就顯示為空
select * from person left join card on person.cardId=card.id
(3)右外連接:會把右邊表里的所有數據取出來,而左邊表中的數據,如果有相等的就顯示出來,若沒有就顯示為空
select * from person right join card on person.cardId=card.id
(4)全外連接(full join ),注:Mysql 默認不支持此種寫法 Oracle支持(select * from person full join card on person.cardId=card.id) ,可以使用將左連接與右連接結合起來作為全連接
select * from person right join card on person.cardId=card.id
union
select * from person left join card on person.cardId=card.id