数据库简单查询SqlServer学⽣表课程表选课表创建教材中的三张表格,并输⼊相应的数据
Create table student(
Sno char(9),
Same char(20),
Ssex char(2),
Sage smallint,
Sdept char(20)
);
Create table course(
Cno char(4),
Cname char(40),
Cpno char(4),
Ccredit smallint
);
Create table sc(
Sno char(9),
Cno char(4),
Grade smallint
);
Insert into student values(‘200215121’,’李勇’,’男’,20,’cs’);
Insert into student values(‘200215122’,’刘晨’,’⼥’,19,’cs’);
Insert into student values(‘200215123’,’王敏’,’⼥’,18,’ma’);
Insert into student values(‘200515124’,’张⽴’,’男’,19,’is’);
Insert into course values (‘1’,’数据库’,’5’,4);
Insert into course values (‘2’,’数学’,NULL,2);
Insert into course values(‘3’,’信息系统’,’1’,4);
Insert into course values(‘4’,’操作系统’,’6’,3);
Insert into course values(‘5’,’数据结构’,’7’,4);
Insert into course values(‘6’,’数据处理’,NULL,2);
Insert into course values(‘7’,’pascal语⾔’,’6’,4);
Insert into sc values(‘200215121’,’1’,92);
Insert into sc values(‘200215121’,’2’,85);
Insert into sc values(‘200215121’,’3’,88);
Insert into sc values(‘200215122’,’2’,90);
Insert into sc values(‘200215122’,’3’,80);
查询语句1:
1.  列出所有不姓李的所有学⽣;
select*FROM Student
where Sname NOTLIKE'李%'
2.  列出姓“张”且全名为2个汉字的学⽣
select*FROM Student
where Sname LIKE'张_'
3.  显⽰在1993年以后出⽣的学⽣的基本信息;
select*FROM Student
where Sage<2015-1993
4.  查询出课程名含有“数据”字串的所有课程基本信息;
select*From Course
where Cname LIKE'%数据%'
5.  列出选修了‘1’课程的学⽣学号,按成绩的降序排列;
select Sno FROM SC
where Cno='1'ORDERBY Grade DESC
6.  列出课程表中全部信息,按先修课的升序排列;
select*FROM Course ORDER BY Cpno ASC
7.  列出年龄超过平均值的所有学⽣名单,按年龄的降序显⽰;
select*FROM Student
where Sage>(SELECT AVG(Sage)
FROM Student)ORDERBY Sage DESC
8.  按照出⽣年份升序显⽰所有学⽣的学号、姓名、性别、出⽣年份及院系,在结果集中列标题分别指定为“学号,姓名,性别,出⽣年份,院系”;
select Sno学号,Sname姓名,Ssex性别,2014-Sage出⽣年⽉,Sdept院系
FROM Student ORDERBY 2014-Sage ASC
9.  按照课程号、成绩降序显⽰课程成绩在80-90之间的学⽣的学号、课程号及成绩
select Sno,Cno,Grade FROM SC
where Grade between 80AND 90ORDER BY Cno DESC,Grade DESC
10.显⽰学⽣信息表中的学⽣总⼈数及平均年龄,在结果集中列标题分别指定为“学⽣总⼈数,平均年龄”;
select COUNT(DISTINCT Sno)学⽣总⼈数,AVG(Sage)平均年龄
FROM Student
11.显⽰选修的课程数⼤于2的各个学⽣的选修课程数;
select Sno学号,COUNT(Sno)选修课程数
FROM SC GROUPBY Sno HAVING COUNT(*)>2
12.显⽰平均成绩⼤于“2002151022”学⽣平均成绩的各个学⽣的学号、平均成绩;
select Sno,AVG(Grade)
from SC
Group by Sno
having AVG(Grade)>(select AVG(Grade)from SC
where Sno='200215122')
查询语句2:
1、查询所有学⽣的Sname、Cname和Grade列。
SELECT Sname,Cname,Grade
FROM Student,Course,SC
where Student.Sno=SC.Sno and SC.Cno=Course.Cno;
2、查询所有选修“数据库”课程的同学的成绩。
SELECT Student.Sno,Sname,Cname,Grade
FROM Student,Course,SC
where Cname='数据库'AND Course.Cno=sc.Cno AND Student.Sno=sc.Sno;
3、查询和“李勇”同性别的同学Sname.
SELECT x.Sname FROM Studentx
where x.Ssex=(select Ssex FROM Student y WHERE y.Sname='李勇')AND x.Sname!='李勇'
4、查询所有同学的基本情况和选课情况,包括未选课的同学。
/* SELECTStudent.*,SC.* FROM Student,SC where Student.Sno=sc.Sno */
SELECT Student.*,SC.*
FROM Student LEFT OUTERJOIN SC ON(Student.Sno=SC.Sno)
5、查询选修1号课程且成绩⾼于90分的同学的名字。
SELECT Sname FROM Student,SC
WHERE Student.Sno=sc.Sno AND Cno='1'AND Grade>90
6、查询和学号为200215121的同学相同年龄的所有学⽣的Sno、Sname列。
由于该题学号为200215121时,没有其他同学与他年龄相同,故实验中把学号改为200215122。查询结果包括“刘晨”⾃⼰。
SELECT x.Sno,x.Sname FROM Student x
where x.Sage=(SELECT y.Sage FROM Student y WHERE y.Sno='200215122')
7、查询刘晨同学所有的成绩。
数据库简单吗SELECT Sname,Cno,Grade
FROM Student,SC
where Student.Sname='刘晨'AND Student.Sno=Sc.Sno
8、查询⾮计算机系的不超过计算机系所有学⽣的年龄的学⽣姓名。
SELECT x.Sname FROM Studentx
WHERE x.Sage<(SELECT MIN(y.Sage)
FROM Studenty WHERE y.Sdept='CS')AND x.Sdept!='CS'
查询语句3
1 、使⽤带IN谓词的⼦查询
(1)查询与刘晨在同⼀个系学习的学⽣的学号和性名。
select Sno,Sname FROM Student
WHERE Sdept IN(SELECT Sdept From Student WHERE Sname='刘晨') AND Sname!='刘晨'
(2)查询选修了数据库课程的学⽣的学号和姓名。
SELECT Sno,Sname From Student
WHERE Sno IN(SELECT Sno From SC WHERE Cno
IN(SELECT Cno FROM Course WHERE Cname IN('数据库')))
(3)查询选修了1号课程和2号课程的学⽣的学号和姓名。
SELECT Sno,Sname FROM Student
WHERE Sno IN(SELECT Sno FROM SC WHERE Cno IN('1','2'))
2 、使⽤带⽐较运算的⼦查询
(4)查询⽐男⽣平均年龄⼩的所有学⽣的信息。
SELECT*FROM Student/*因为AVG是采取舍去计数,故要加等号*/ WHERE Sage<=(SELECT AVG(Sage)FROM Student WHERE Ssex='男')
(5)查询⽐王敏年龄⼤的所有学⽣的信息。
SELECT*FROM Student
WHERE Sage>(SELECT Sage FROM Student WHERE Sname='王敏')
3 、使⽤带有ANY,ALL谓词的⼦查询
(6)查询其它系中⽐计算机系CS所有学⽣年龄都要⼩的学⽣姓名和年龄。SELECT Sname,Sage FROM Student
WHERE Sdept!='CS'
AND Sage<=ALL(SELECT Sage FROM Student WHERE Sdept='CS')
(7)查询与计算机系所有学⽣的年龄均不同的学⽣学号,姓名和年龄SELECT Sno,Sname,Sage FROM Student
WHERE Sage!=ALL(SELECT Sage FROM Student WHERE Sdept='CS')
4 、使⽤带Exists谓词的⼦查询和相关⼦查询
(8)查询选修了数据库的学⽣的姓名和学号
SELECT Sname,Sno FROM Student
WHERE EXISTS(SELECT*FROM SC
WHERE Student.Sno=Sno AND Cno
IN(SELECT Cno FROM Course WHERE Cname='数据库'))
(9)查询没有选修数据库的学⽣姓名和学号
SELECT Sname,Sno FROM Student
WHERE NOTEXISTS(SELECT*FROM SC
WHERE Student.Sno=Sno AND Cno
IN(SELECT Cno FROM Course WHERE Cname='数据库'))
(10)查询所有选修了1号课程的学⽣姓名。
SELECT Sname FROM Student
WHERE EXISTS(SELECT*FROM SC
WHERE Student.Sno=Sno AND Cno='1')
(11)查询没有⼈选修的课程的课程号和课程名。
SELECT Cno,Cname FROM Course
WHERE NOTEXISTS(SELECT*FROM SC WHERE Course.Cno=Cno)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。