SQL数据查询语句:1: (选择表中的若干列) 求全体学生的学号、姓名、性别和年龄。
2: (不选择重复行) 求选修了课程的学生学号。
3: (选择表中的所有列) 求全体学生的详细信息。
4: (使用表达式) 求全体学生的学号、姓名和出生年份。
5: (使用列的别名) 求学生的学号和出生年份,显示时使用别名“学号”和“出生年份”。
6: (比较大小条件) 求年龄大于19岁的学生的姓名和年龄。
7: (比较大小条件) 求注册B1班或注册B2班年龄大于18岁的学生的姓名、班级号和年龄。
8: (确定范围条件) 求年龄在19岁与22岁(含20岁和22岁)之间的学生的学号和年龄。
9: (确定范围条件) 求年龄不在19岁与22岁之间的学生的学号和年龄。
10:(确定集合条件) 求在下列各班的学生信息:注册B1班、注册B2班。
11:(确定集合条件) 求不是注册B1班、注册B2班的学生信息。
12:(匹配查询) 求姓名是以“李”打头的学生。
13:(匹配查询) 求姓名中含有“志”的学生。
14:(匹配查询) 求姓名长度至少是三个汉字且倒数第三个汉字必须是“马”的学生。
15:(匹配查询) 求选修课程JC001或JC003,成绩在80至90之间,学号为2007xxx的学生的学号、课程号和成绩。
16:(涉及空值查询) 求缺少学习成绩的学生的学号和课程号。
17:(控制行的显示顺序) 求选修JC003课程或JC004课程的学生的学号、课程号和分数。
18:(组函数) 求学生总人数。
19:(组函数) 求选修了课程的学生人数。
20:(组函数) 求注册B1班学生的平均年龄。
21:(组函数) 求选修了课程JC001的最高、最低与平均成绩以及课程的名称。
22:(分组查询) 求各门课程的平均成绩与总成绩。
23:(分组查询) 求各班级的人数和平均年龄。
24:(分组查询) 输入以下查询语句并执行,观察出现的其结果并分析其原因。
SELECT SNAME,SDEPT,COUNT(*)FROM STUDENT WHERE SDEPT=’CS’GROUP BY SDEPT;25:(分组查询) 分析以下语句为什么会出现错误。
并给出正确的查询语句。
SELECT SAGE FROM STUDENT GROUP BY SNO;26:(分组查询) 求学生人数不足90人的班级及其相应的学生数。
27:(分组查询) 求各班的学生人数。
28:(涉及空值的查询) 分别观察各组函数、行的显示顺序以及分组查询与空值的关系。
29:(自然连接查询) 求学生号以及其选修课程的课程名称和成绩,但查询结果中只能有一个CNO字段。
30:(连接查询) 求选修了课程JC001且成绩在70分以下或成绩在90分以上的学生的姓名、课程名称和成绩。
31:(连接查询与表的别名) 求选修了课程的学生的学生姓名、课程号和成绩。
select sname,cno,gradefrom student inner join sc on student.sno=sc.snowhere student.sno in (select sno from sc group by sno having count(*)>0)32:(自身连接查询) 求年龄大于’李边’的所有学生的姓名、班级号和年龄。
select a.sname,a.classno,a.sagefrom student a inner join student b on a.sage>b.sage and b.sname='李边'33:(连接查询) 求选修了课程JC002或JC003的学生的学号、课程号、课程名和成绩。
select a.sno,o,cname,gradefrom student a,course b,sc cwhere a.sno=c.sno and o=o and o in ('JC002','JC003')34:(子查询) 求与‘李边’年龄相同的学生的姓名和班级。
select a.sname,a.classnofrom student a inner join student b on a.sage=b.sage andb.sname='李边' (自身连接)实现或select sname,classnofrom studentwhere sage=(select sage from student where sname='李边')(子查询实现)35:(子查询) 求选修了课程名为’计算机基础’的学生的学号和姓名。
select sno,sname from studentwhere sno in (select sno from scwhere cno in(select cno from coursewhere cname='计算机基础')) 或select a.sno,snamefrom student a,course b,sc cwhere a.sno=c.sno and o=o and ame='计算机基础'(连接查询实现)36:(子查询ANY) 求比注册B1班中某一学生年龄大的学生的姓名和班级号。
select sname,classnofrom studentwhere sage>ANY(select sage from studentwhere classno='注册B1')and classno!='注册B1'或select sname,classnofrom studentwhere sage>(select min(sage) from studentwhere classno='注册B1')and classno!='注册B1'37:(子查询ALL) 求比注册B1班中全体学生年龄大的学生的姓名和班级号。
select sname,classnofrom studentwhere sage>ALL(select sage from studentwhere classno='注册B1')and classno!='注册B1'或select sname,classnofrom studentwhere sage>(select max(sage) from studentwhere classno='注册B1')and classno!='注册B1'38:(子查询EXISTS) 求选修了课程JC004的学生的姓名和班级。
select sno,classnofrom studentwhere EXISTS(select sno from scwhere sno=student.sno and cno='JC004')或select a.sno,classnofrom student a,sc bwhere a.sno=b.sno and o='JC004'39:(返回多列的子查询) 求与‘李边’同班且同龄的学生的姓名和班级。
select sname,classnofrom studentwhere sno in(select a.sno from student a inner join studentb on a.sage=b.sage and a.classno=b.classno andb.sname='李边')或select a.sname,a.classnofrom student a inner join student b on a.sage=b.sage anda.classno=b.classno and b.sname='李边' (自身连接实现) 40:(多个子查询) 求与‘李边’同班,且年龄大于‘韩晶’的学生的信息。
select * from studentwhere classno=(select classno from studentwhere sname='李边')and sage>(select sage from studentwhere sname='韩晶')41:(子查询中使用表连接) 求注册B2班中年龄相同的学生的姓名和年龄。
select sname,sagefrom studentwhere sno in(select distinct a.sno from student a inner join student b on a.sage=b.sage )and classno='注册B2'或select distinct a.sname,a.sage from student a inner join student b on a.sage=b.sagewhere b.classno=’注册B2’42:(嵌套与分组查询) 检索选修某课程的学生人数多于3人的课程的名称。
select cname from coursewhere EXISTS(select count(*) from scwhere cno=ogroup by cnohaving count(*)>3)43:(相关子查询) 求未选修课程JC004的学生的姓名。
select sname from studentwhere NOT EXISTS(select * from scwhere sno=student.sno and cno='JC004')44:(相关子查询) 求选修了全部课程的学生的姓名。
select sname from studentwhere sno in(select sno from scgroup by snohaving count(*)=(select count(*) from course)) 45:(相关子查询) 求至少选修了学生‘2007532110’所选修的全部课程的学生的学号。
select distinct a.sno from sc a left join sc b on o=o and b.sno='2007532110'group by a.snohaving count(*)>=(select count(*) from sc c wherec.sno='2007532110')46:(相关子查询) 求成绩比所选修课程平均成绩高的学生的学号、课程号、和成绩。