sql查詢練習

# 創建數據庫school
CREATE DATABASE school;

# 創建表student
DROP TABLE IF EXISTS student;
CREATE TABLE student(
	`s_id` TINYINT(3) UNSIGNED NOT NULL AUTO_INCREMENT comment '學生編號' , 
	`s_name` VARCHAR(4) NOT NULL comment '學生姓名',
	`s_sex` VARCHAR(2) NOT NULL comment '學生性別', 
	`s_birthday` DATETIME comment '學生生日',
	`class` VARCHAR(5) comment '學生班級',
	PRIMARY KEY(`s_id`),
	KEY s_name(`s_name`)
)engine=innodb DEFAULT CHARSET=utf8 comment '學生表';

# 創建表course
CREATE TABLE course(
	c_id VARCHAR(5) NOT NULL comment '課程編號', 
	c_name VARCHAR(10) NOT NULL comment '課程名稱', 
	t_id VARCHAR(10) NOT NULL comment '授課老師'
)engine=innodb DEFAULT CHARSET=utf8 comment '課程表';

# 創建表score 
CREATE TABLE score (
	s_id VARCHAR(3) NOT NULL comment '學生編號', 
	c_id VARCHAR(5) NOT NULL comment '課程編號', 
	degree NUMERIC(10, 1) NOT NULL comment '學分'
)engine=innodb DEFAULT CHARSET=utf8 comment '分數表';

# 創建表teacher
CREATE TABLE teacher (
	t_id VARCHAR(3) NOT NULL comment '教師編號', 
	t_name VARCHAR(4) NOT NULL comment '教師名稱', 
	t_sex VARCHAR(2) NOT NULL comment '教師性別', 
	t_birthday DATETIME NOT NULL comment '教師生日',
	prof VARCHAR(6) comment '職稱', 
	depart VARCHAR(10) NOT NULL comment '院系'
)engine=innodb DEFAULT CHARSET=utf8 comment '教師表';

# 向各表導入數據
INSERT INTO student (s_id,s_name,s_sex,s_birthday,class) VALUES 
(108 ,'曾華' ,'男' ,'1977-09-01',95033),
(105 ,'匡明' ,'男' ,'1975-10-02',95031),
(107 ,'王麗' ,'女' ,'1976-01-23',95033),
(101 ,'李軍' ,'男' ,'1976-02-20',95033),
(109 ,'王芳' ,'女' ,'1975-02-10',95031),
(103 ,'陸君' ,'男' ,'1974-06-03',95031);


INSERT INTO course(c_id,c_name,t_id) VALUES 
('3-105' ,'計算機導論',825),
('3-245' ,'操作系統' ,804),
('6-166' ,'數據電路' ,856),
('9-888' ,'高等數學' ,100);

INSERT INTO score(s_id,c_id,degree) VALUES
(103,'3-245',86),
(105,'3-245',75),
(109,'3-245',68),
(103,'3-105',92),
(105,'3-105',88),
(109,'3-105',76),
(101,'3-105',64),
(107,'3-105',91),
(108,'3-105',78),
(101,'6-166',85),
(107,'6-106',79),
(108,'6-166',81);


INSERT INTO teacher(t_id,t_name,t_sex,t_birthday,prof,depart) VALUES 
(804,'李誠','男','1958-12-02','副教授','計算機系'),
(856,'張旭','男','1969-03-12','講師','電子工程系'),
(825,'王萍','女','1972-05-05','助教','計算機系'),
(831,'劉冰','女','1977-08-14','助教','電子工程系');






#sql題目(基礎)
1、 查詢student表中的所有記錄的s_name、s_sex和class列。 
	#無別名
	select s_name,s_sex,class from student;
	#有別名
	select s_name as 學生姓名, s_sex as 學生性別, class as 學生班級 from student;


2、 查詢教師所有的單位即不重複的depart列。 
	select distinct depart from teacher;
	

3、 查詢student表的所有記錄。 
	select * from student;
	

4、 查詢score表中成績在60到80之間的所有記錄。
	select * from score where degree>=60 and degree<=80;
	
	
5、 查詢score表中成績爲85,86或88的記錄。 
	#方法1
	select * from score where degree=85 or degree=86 or degree=88;
	
	#方法2
	select * from score where degree in (85,86,88);
	
	
6、 查詢student表中“95031”班或性別爲“女”的同學記錄。 
	select * from student where class=95031 or s_sex='女';
	

7、 以class降序查詢student表的所有記錄。 
	select * from student order by class desc;
	
	
8、 以c_id升序、degree降序查詢score表的所有記錄。
	select * from score order by c_id asc,degree desc;
 
9、 查詢“95031”班的學生人數。 
	select count(*) as 95031班學生人數 from student where class=95031;
	

10、查詢score表中的最高分的學生學號和課程號。 
	select s_id,c_id from score where degree=(select MAX(degree) from score);
	

11、查詢‘3-105’號課程的平均分。 
	select AVG(degree) from score where c_id='3-105';
	

12、查詢score表中至少有5名學生選修的並以3開頭的課程的平均分數。
	select AVG(degree) as 平均分,c_id as 課程 from score where c_id like '3%' group by c_id having count(s_id)>=5;
 
 
13、查詢最低分大於70,最高分小於90的s_id列。 
	select s_id from score where degree>70 and degree <90;


14、查詢所有學生的s_name、c_id和degree列。 
	select student.s_name,score.c_id,score.degree from student left join score on student.s_id=score.s_id;
	

15、查詢所有學生的s_id、c_name和degree列。 
	select student.s_id,course.c_name,score.degree from student
	join score  on student.s_id=score.s_id
	left join course on score.c_id=course.c_id;


16、查詢所有學生的s_name、c_name和degree列。 
	select student.s_name,course.c_name,score.degree from student
	join score  on student.s_id=score.s_id
	left join course on score.c_id=course.c_id;


17、查詢“95033”班所選課程的平均分。 
	select AVG(degree) from score 
	join student on score.s_id=student.s_id where class=95033;

18、假設使用如下命令建立了一個grade表: 
	create table grade(
		low varchar(3) not null comment 'low',
		upp varchar(3) not null comment 'upp',
		rank varchar(3) not null comment 'rank'
    )engine=innodb DEFAULT CHARSET=utf8 comment '等級表';
	
	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');
	
	現查詢所有同學的s_id、c_id和rank列?
	
	select score.s_id,score.c_id,grade.rank from score,grade 
	where score.degree>=grade.low and score.degree<=grade.upp;
	
	
19、查詢選修“3-105”課程的成績高於“109”號同學成績的所有同學的記錄。
	#**all的意思是“對於子查詢返回的列中的所有值,如果比較結果爲true,則返回true”
	select * from score where c_id='3-105' and degree>all(select degree from score where s_id=109 and c_id='3-105');


20、查詢score中選學一門以上課程的同學中分數爲非最高分成績的記錄。 
	select * from score group by degree having count(c_id)>1;
	select * from score 
	where degree not in 
	(select MAX(degree) from score group by s_id)
	and s_id in
	(select s_id from score group by s_id having count(s_id)>1);
	


21、查詢成績高於學號爲“109”、課程號爲“3-105”的成績的所有記錄。
	select * from score 
	where degree>(select degree from score where s_id=109 and c_id='3-105');
	
 
22、查詢和學號爲108的同學同年出生的所有學生的s_id、s_name和s_birthday列。 
	select s_id,s_name,s_birthday from student 
	where YEAR(s_birthday)=(select YEAR(s_birthday) from student where s_id=108);
	

23、查詢“張旭“教師任課的學生成績。 
	#方法1
	select s.s_id,s.degree from score s 
	join course c on s.c_id=c.c_id
	left join teacher t on c.t_id=t.t_id
	where t.t_name='張旭';
	
	#方法2
	select s.s_id,s.degree from score s
	join (teacher t,course c) on (s.c_id=c.c_id and c.t_id=t.t_id)
	where t.t_name='張旭';
	
	

24、查詢選修某課程的同學人數多於5人的教師姓名。
	#方法1--通過子查詢
	select teacher.t_name from teacher where t_id in 
	(select t_id from course where c_id in 
	(select c_id from score group by c_id having count(s_id)>5));
	
	#方法2--通過連接查詢(推薦)
	select t.t_name from teacher t join 
	(course c,score s) on (t.t_id=c.t_id and c.c_id=s.c_id)
	group by s.c_id having count(s.s_id)>5;
 
25、查詢95033班和95031班全體學生的記錄。 
	select * from student where class in (95033,95031);
	

26、查詢存在有85分以上成績的課程c_id. 
	#方法1
	select distinct c_id from score where degree>85;
	
	#方法2
	select c_id from score group by c_id having MAX(degree)>85;

27、查詢出“計算機系“教師所教課程的成績表。
	select s.degree,t.depart from score s join 
	(course c,teacher t) on (s.c_id=c.c_id and c.t_id=t.t_id)
	where t.depart='計算機系';
 
 
28、查詢“計算機系”與“電子工程系“不同職稱的教師的t_name和prof。 
	select t_name,prof from teacher 
	where depart='計算機系' 
	and 
	prof not in (select prof from teacher where depart='電子工程系');

	
29、查詢選修編號爲“3-105“課程且成績至少高於選修編號爲“3-245”的同學的c_id、s_id和degree,並按degree從高到低次序排序。 
	select * from score 
	where c_id='3-105'
	and 
	degree>any(select degree from score where c_id='3-245')
	order by degree desc;


30、查詢選修編號爲“3-105”且成績高於選修編號爲“3-245”課程的同學的c_id、s_id和degree. 
	select * from score 
	where c_id='3-105'
	and 
	degree>all(select degree from score where c_id='3-245');


31、查詢所有教師和同學的name、sex和birthday. 
	select s.s_name 名字,s.s_sex 性別,s.s_birthday 年齡 from student s
	union
	select t.t_name 名字,t.t_sex 性別,t.t_birthday 年齡 from teacher t;

	
32、查詢所有“女”教師和“女”同學的name、sex和birthday. 
	select s.s_name 名字,s.s_sex 性別,s.s_birthday 年齡 from student s where s.s_sex='女'
	union
	select t.t_name 名字,t.t_sex 性別,t.t_birthday 年齡 from teacher t where t.t_sex='女';
	
	

33、查詢成績比該課程平均成績低的同學的成績表。 
	select * from score a 
	join (select c_id,AVG(degree) average from score group by c_id) b
	on a.c_id=b.c_id
	where a.degree<b.average;
	

34、查詢所有任課教師的t_name和depart. (不是所有老師都教課)
	select t.t_name,t.depart from teacher t join course c on t.t_id=c.t_id;


35、查詢所有未講課的教師的t_name和depart. 
	#方法1--子查詢
	select t_name,depart from teacher where t_id not in (select t_id from course);
	
	#方法2--exists
	select t_name,depart from teacher where not exists (select * from course where course.t_id=teacher.t_id);

	
36、查詢至少有2名男生的班號。 
	select class from student where s_sex='男' group by class having count(s_sex)>1;
	

37、查詢student表中不姓“王”的同學記錄。 
	select * from student where s_name not like '王%';
	

38、查詢student表中每個學生的姓名和年齡。 
	select s_name as 姓名,(YEAR(NOW())-YEAR(s_birthday)) as 年齡 from student;
	

39、查詢student表中最大和最小的s_birthday日期值。 
	select DATE_FORMAT(MAX(s_birthday),'%Y-%d-%m') as 最大日期,DATE_FORMAT(MIN(s_birthday),'%Y-%d-%m') as 最小日期 from student;


40、以班號和年齡從大到小的順序查詢student表中的全部記錄。 
	select * from student order by class desc,s_birthday desc;
	

41、查詢“男”教師及其所上的課程。 
	select * from course c
	join teacher t on c.t_id=t.t_id
	where t.t_sex='男';


42、查詢最高分同學的s_id、c_id和degree列。 
	select * from score order by degree desc limit 1;


	
43、查詢和“李軍”同性別的所有同學的s_name. 
	select s_name from student where s_sex=(select s_sex from student where s_name='李軍');


	
44、查詢和“李軍”同性別並同班的同學s_name. 
	select s_name from student where (s_sex,class)=(select s_sex,class from student where s_name='李軍');

	
	
45、查詢所有選修“計算機導論”課程的“男”同學的成績表。
	select sc.* from score sc
	join (student st,course c) on (sc.s_id=st.s_id and sc.c_id=c.c_id)
	where st.s_sex='男' and c.c_name='計算機導論';







 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章