student表3d
teacher表blog
course表it
score表io
對這四個表進行一些練習。class
1:查詢student表中全部記錄。select
select *from student;nio
2:查詢student表中name/sex/classid這些列。im
select name,sex,classid from student;數據
3:查詢教師中不重複的單位department。查詢
select distinct department from teacher;
distinct 列(一個或多個) 獲取不重複的記錄
4:查詢score表中成績再60至80之間的記錄。
select *from score where degree between 60 and 80;
5:查詢score表中成績爲85,86,88的記錄。
select *from score where degree in (86,85,88);
6:查詢student表中爲95031班的或性別爲女生的。
select *from student where classid=95031 or sex='女';
7:以classid列降序查詢student表的全部記錄。
select *from student order by desc classid ;
8:以degree降序courseid升序輸出score表中全部記錄。
select *from score order by courseid asc, degree desc;
9:查詢95031班級的人數。
select count(id) from student where classid=95031;
10:查詢score表中成績最高者的記錄。
select *from score where degree=(select max(degree) from score);
或: select id,courseid,degree order by degree desc limit 0,1;
11:查詢每門課的平均數。
select courseid,avg(degree) from score group by courseid;
12:查詢score表中至少2名學生選修以3開頭的課程的平均分數。
select courseid, avg(degree) as average from score group by courseid having count(courseid)>1 and courseid like '3%';
13:查詢score中分數大於70小於90的id列。
select *from score where degree between 70 and 90;
14:查詢學生的name列和degree列和classid列(多表查詢)。
select name,degree,classid from student,score where student.id=score.id
15:查詢學生的課程id課程名和degree列(多表查詢)。
select coursename,degree from score,course where course.courseid=score.courseid;
16:查詢學生的姓名成績和對應課程名。(三表查詢)。
select name,degree,coursename from student,course,score where score.id=student.id and score.courseid=course.courseid;
17:查詢95031班學生每門課的平均成績(子查詢)。
select avg(degree) from score where id in(select id from student where classid=95031) group by courseid;
18:查詢選修3105課程高於109好同窗3105課程成績的同窗記錄。
select *from score where courseid=3105 and degree>(select degree from score where id=109 and courseid=3105);
19:查詢成績高於學號109課程號爲3105的同窗的記錄。
select *from score where degree>(select degree from score where id=109 and courseid=3105);
20:查詢學號與101和108的同窗同年出生的學生的記錄。
select name, brithday,classid from student where year(brithday) in (select year(brithday) from student where id=108 or id=101);
21:查詢張旭老師教的課程學生的成績。
select *from score where courseid in (select courseid from course where teacherid=(select id from teacher where name='張旭'));
22:查詢選修課多餘5人的老師的記錄。
select *from teacher where id in (select teacherid from course where courseid in (select courseid from score group by courseid having count(courseid)>5));
23:查詢95033和95031班級同窗的記錄
select *from student where classid in (95033,95031);
24:查詢88分以上成績的課程name。
select coursename from course where courseid in(select courseid from score where degree>88);
25:查詢計算機系老師所教課程同窗的成績。
select *from score where courseid in(select courseid from course where teacherid in(select id from teacher where department='計算機系'));
26:查詢計算機系與電子工程系不一樣職稱的教師的記錄。
select *from teacher where department='計算機系'and professional not in (select professional from teacher where department='電子工程系' )
-> union
-> select *from teacher where department='電子工程系'and professional not in (select professional from teacher where department='計算機系' );
27:查詢選修編號爲3105的課程成績至少高於部分選修3245課程學生的成績的記錄。
select *from score where courseid=3105 and degree> any(select degree from score where courseid=3245);
28:查詢選修編號爲3105的課程成績高於全部選修3245課程學生的成績的記錄。
select *from score where courseid=3105 and degree> all(select degree from score where courseid=3245);
29:查詢全部教師和學生的name ,sex ,brithday。
select name,sex,brithday from student
-> union
-> select name,sex,brithday from teacher;
30:查詢女教師和女同窗的name sex brithday。
select name,sex,brithday from student where sex='女'
-> union
-> select name,sex,brithday from teacher where sex='女';
31:查詢成績比該課程平均分低的同窗的成績。
select *from score a where degree<(select avg(degree) from score b where a.courseid=b.courseid);
32:查詢任課老師的name和department。
select *from teacher where id in(select teacherid from course where courseid in(select courseid from score group by courseid));
33:查詢班級中至少有2名男生的班級。
select classid from student where sex='男' group by classid having count(classid)>1;
34:查詢班級中不姓王的同窗。
select *from student where name not like '王%';
35:查詢全部學生的姓名和年齡。
select name,year(now())-year(brithday) as age from student;
36:查詢學生中年齡最大和年齡最小的數據。
select max(year(now())-year(brithday)),min(year(now())-year(brithday)) from student;
37:以班號和年齡從大到小順序查詢student表中全部記錄。
select *from student order by classid,year(now())-year(brithday);
38:查詢男教師所上的課程。
select coursename from course where teacherid in (select id from teacher where sex='男');
39:查詢最高分同窗的信息。
select *from score where degree =(select max(degree) from score);
40:查詢和季軍同性別的全部同窗。
select *from student where sex=(select sex from student where name='季軍');
41:查詢和季軍同性別並同班的同窗的信息。
select *from student where sex=(select sex from student where name='季軍') and classid=(select classid from student where name='季軍');
42:查詢全部選修計算機導論課程的男同窗的成績。
select *from score where courseid=(select courseid from course where coursename='計算機導論') and id in(select id from student where sex='男' );