码迷,mamicode.com
首页 > 数据库 > 详细

sql查询

时间:2018-08-10 17:11:44      阅读:737      评论:0      收藏:0      [点我收藏+]

标签:any   sysdate   显示   电子工程   生成   语句   sele   计算机系   char   

 

 

 

 

 

                                              

课程:数据库

 

 设有一数据库,包括四个表:学生表(Student)、课程表(Course)、成绩表(Score)以及教师信息表(Teacher)。

1、用insert语句写上次所建的数据

INSERT INTO STUDENT (SNO,SNAME,SSEX)VALUES(‘12‘,‘大王‘,‘女‘);

2、查询“95031”班的学生人数。

SELECT SNO,SNAME,CLASS FROM STUDENT WHERE CLASS=‘95031‘;

3、查询Score表中成绩在60到80之间的所有记录。

select * from Score where Degree between 60 and 80

4、查询Score表中的最高分的学生学号和课程号。

SELECT * FROM SCORE WHERE DEGREE =(SELECT MAX(DEGREE) FROM SCORE )

5、查询‘3-105’号课程的平均分。

select avg(DEGREE) from Score where cno=‘3-105‘;

6、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。

SELECT AVG(DEGREE) FROM SCORE WHERE CNO LIKE ‘3%‘ GROUP BY CNO HAVING COUNT(CNO)>4

7、查询最低分大于70,最高分小于90的Sno列。

SELECT DEGREE FROM SCORE WHERE DEGREE BETWEEN 70 AND 90;

8、查询“95033”班每门课程的平均分。

SELECT AVG(DEGREE) FROM SCORE WHERE SNO IN( SELECT SNO FROM STUDENT WHERE CLASS=‘95033‘);

9、使用如下命令建了一个grade表:

create table grade(low   number(3,0),upp   number(3),rank  varchar(1));

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‘);

select sno,cno,(select rank from grade where degree between low and upp ) from score

现查询所有同学的Sno、Cno和rank列。

10、查询选修“3-105”课程中成绩高于“109”号同学”3-105”成绩的所有同学的记录。

11、查询score中选学一门以上课程的同学中分数为当门课程中非最高分成绩的学生及课程记录。

select sno,cno,degree from score s where sno in(select sno from score group by sno having count(*)>1)

and s.degree<(select max(degree) from score where cno=s.cno) order by cno

12、查询课程号为“3-105”的成绩高于学号为“109”学生所有课程成绩的学生记录。

SELECT * FROM SCORE WHERE DEGREE<(SELECT DEGREE FROM SCORE WHERE SNO=109 AND CNO=‘3-105‘);

13、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。

SELECT SNO,SNAME,SBIRTHDAY FROM STUDENT WHERE SBIRTHDAY=(SELE sbirthday from student where sno=108 )

 

14、查询“张旭“教师任课的学生成绩。

SELECT DEGREE FROM SCORE x,TEACHER y,COURSE z WHERE TNAME LIKE ‘张旭‘ AND x.CNO=z.CNO AND z.TNO=y.TNO;

15、查询选修某课程的同学人数多于5人的教师姓名。

SELECT TNAME FROM TEACHER WHERE TNO IN( select x.tno from course x,score y where x.cno=y.cno group by x.tno having count(*)>5 );

16、查询95033班和95031班全体学生的记录。

SELECT * FROM STUDENT WHERE CLASS=‘95033‘ OR CLASS=‘95031‘

17、查询存在有85分以上成绩的课程Cno.

select distinct cno from  score where degree>85;

不同的

17、查询出“计算机系“教师所教课程的成绩表。

select degree from score x,course y,teacher z WHERE z.depart=‘计算机系‘ AND  x.cno=y.cno and y.tno=z.tno;

19、查询“计算机系”与“电子工程系“中本系范围内无重复的职称的教师的Tname和Prof。

select tname,prof from teacher where DEPART=‘计算机系‘ and  prof not in(select prof from teacher where depart=‘电子工程系‘);

20、查询选修编号为“3-105“课程且该门成绩至少高于一位其他同学(其他同学的条件是必须同时选修“3-245”)的Cno、Sno和Degree,并按Degree从高到低次序排序。

SELECT * FROM SCORE WHERE CNO =‘3-105‘ AND DEGREE >ANY(SELECT DEGREE FROM SCORE WHERE CNO=‘3-245‘)

21、查询同时选修编号为“3-105”“3-245”且“3-105”成绩高于选修编号为“3-245”课程(自己与自己比对)的同学的Cno、Sno和Degree.

select Cno,Sno,Degree from Score a where (select Degree from Score b where Cno=‘3-105‘ and b.Sno=a.Sno)>(select Degree from Score c where Cno=‘3-245‘ and c.Sno=a.Sno)

21、查询所有教师和同学的name、sex和birthday.

SELECT SNO AS ID, SNAME AS NAME,SSEX AS sex,SBIRTHDAY AS birthday ,‘学生‘TYPE FROM STUDENT UNION SELECT TNO AS ID,TNAME AS NAME,TSEX AS sex,TBIRTHDAY AS birthday,‘老师‘ TYPE FROM TEACHER

23、查询所有“女”教师和“女”同学的name、sex和birthday,增加伪列列出他们的身份(教师还是同学)

SELECT SNO AS ID, SNAME AS NAME,SSEX AS sex,SBIRTHDAY AS birthday ,‘学生‘TYPE FROM STUDENT WHERE SSEX=‘女‘

UNION SELECT TNO AS ID,TNAME AS NAME,TSEX AS sex,TBIRTHDAY AS birthday,‘老师‘ TYPE FROM TEACHER WHERE TSEX=‘女‘

---

24、查询成绩比该课程平均成绩低的同学的成绩表(使用相关子查询来做)。

SELECT * FROM SCORE t1 WHERE t1.DEGREE <(SELECT AVG(t2.DEGREE) FROM SCORE t2 WHERE t2.CNO=t1.CNO)

25、查询所有任课教师的Tname和Depart.(使用exists)

SELECT TNAME,DEPART FROM TEACHER t1 WHERE EXISTS( SELECT t2.TNAME  FROM TEACHER t2 WHERE t2.TNAME=t1.TNAME AND t2.DEPART=t1.DEPART)

 

26  查询所有未讲课的教师的Tname和Depart.

select Tname,Depart from TEACHER WHERE  TNAME NOT IN(SELECT TNAME  TNO FROM TEACHER,COURSE,Score where Teacher.Tno=Course.Tno and Course.Cno=Score.CNO)

27、查询至少有2名男生的班号。

SELECT CLASS FROM STUDENT WHERE SSEX=‘男‘ GROUP BY CLASS HAVING COUNT(*)>1

28、查询Student表中不姓“王”的同学记录。

SELECT * FROM STUDENT WHERE SNAME NOT LIKE (‘王%‘)

29、查询Student表中每个学生的姓名和年龄。

select sname,to_char(sysdate, ‘yyyy‘)-to_char(sbirthday, ‘yyyy‘) as age from "STUDENT";

30、查询Student表中最大和最小的Sbirthday日期值的学生。

select max(to_char(sbirthday, ‘yyyy‘)) from "STUDENT" union

select min(to_char(sbirthday, ‘yyyy‘)) from "STUDENT";

31、以班号和年龄从大到小的顺序查询Student表中的全部记录。

select * from student order by class desc,sbirthday desc

32、查询“男”教师及其所上的课程。

SELECT TNAME,CNAME FROM  TEACHER,COURSE WHERE TSEX=‘男‘ AND Teacher.Tno=Course.TNO

 

33、查询每门课程中最高分同学的Sno、Cno和Degree列。

select * from score s where s.degree=(

select max(degree) from score where cno=s.cno

)

35、查询和“李军”同性别并同班的同学Sname.

SELECT SNAME FROM STUDENT WHERE SSEX=(SELECT SSEX FROM STUDENT WHERE SNAME=‘李军‘ ) AND SNAME not IN(‘李军‘) AND CLASS=(SELECT CLASS FROM STUDENT

WHERE SNAME=‘李军‘)

36、查询所有选修“计算机导论”课程的“男”同学的成绩表

SELECT SNO,DEGREE FROM SCORE WHERE SNO IN (SELECT SNO FROM STUDENT WHERE SSEX=‘男‘) AND CNO IN (select Cno from Course where Cname=‘计算机导论‘)

 

37、 查询教师所有的Depart列(重复的只要列出一项)。

select distinct Depart from Teacher

37、显示每门课程的名称及有多少人修

SELECT COUNT(sno),(SELECT CNAME from COURSE b where a.CNO=b.cno) from SCORE a GROUP BY CNO

 

sql查询

标签:any   sysdate   显示   电子工程   生成   语句   sele   计算机系   char   

原文地址:https://www.cnblogs.com/lyxjava/p/9455718.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!