适用于SQL各种数据库
适用于jwglxt数据库
use jwglxt
go
–1、求全体学生的学号、姓名、性别和年龄。
select sno,sname ,ssex,sage from Student
–2、求选修了课程的学生学号。
select distinct sno from sc
–3、求全体学生的详细信息。
select * from Student
–4、求全体学生的学号、姓名和出生年份。
select sno,sname,sbir from student
–5、求学生的学号和出生年份,显示时使用别名“学号”和“出生年份”。
select sno 学号,sbir 出生年份 from student
–6、求年龄大于19岁的学生的姓名和年龄。
select sname,sage from student
where sage > 19
–7、求年龄在19岁与22岁(含19岁和22岁)之间的学生的学号、姓名和年龄。
select sno,sname,sage from student
where sage >= 19 and sage <= 22
–8、求年龄不在19岁与22岁之间的学生的学号、姓名和年龄。
select sno,sname,sage from student
where not(sage >= 19 and sage <= 22)
–9、显示07网络、07电信的学生信息。
select * from class
where clname like ‘07网络’ or clname like ‘07电信’
–10、显示不属于07网络、07电信的学生信息。
select * from class
where clname not like ‘07网络’ and clname not like ‘07电信’
–11、求姓名是以“李”打头的学生。
select sname from student
where sname like ‘李%’
–12、求姓名中含有“良”的学生。
select sname from student
where sname like ‘%良%’
–13、求姓名长度至少是三个汉字且倒数第三个汉字必须是“马”的学生。
select sname from student
where sname like ‘%马__’ and len(sname) >= 3
–14、求选修课程01001或02002,成绩在80至95之间,学号为20080xxx的学生的学号、课程号和成绩。
select sno,cno,score from Sc
where sno like ‘20080%’
and (cno = ‘01001’ or cno = ‘02002’)
and (score >= 80 and score <= 95)
–15、查询Student表的前5条记录。
select top 5 * from student
–16、查询Student表的前10%的记录。
select top 10 percent * from student
–17、查询Student表中的年龄最大的一名学生信息。
select top 1 * from student
order by sage desc
–18、求缺少学习成绩的学生的学号和课程号。
select sno,cno from sc
where score is null
–19、求学生总人数。
select count(*) from student
–20、求选修了课程的学生人数。
select count(distinct sno) from sc
–21、求200801班的学生的平均年龄。
select avg(sage) from student
where clno = ‘200801’
–22、求选修了课程01001的最高、最低与平均成绩。
select max(score),min(score),avg(score) from sc
where cno = ‘01001’
–23、求各门课程的平均成绩与总成绩。
select avg(score),sum(score) from sc
group by cno
–24、 输入以下查询语句并执行,观察出现的其结果并分析其原因,并给出正确的查询语句。
–题目错误语句:
–use jwglxtYlxx
–go
–SELECT sno,cno,score,COUNT(*)FROM sc
– WHERE score>50 GROUP BY sno;
–答:
/*原因:在select子句中的列存在聚集函数时且需要和group by 配合使用时。
select子句中的列,除了聚集函数,都要在group by中出现。 */
–以下为正确的查询语句
use jwglxtYlxx
go
SELECT sno,cno,score,COUNT(*)FROM sc
WHERE score>50 GROUP BY sno,cno,score;
–25、求学生人数不足4人的班级及其相应的学生数。
select clno,count() from student
group by clno
having count() < 4
PS:有错误的地方还请大家指正,欢迎大家在评论区留言。
注意:
网络资源海量,本人无法有效辨认是否侵权,若有侵权,请联系作者删除内容。感谢您的访问。