数据库的嵌套查询和统计查询
生活随笔
收集整理的這篇文章主要介紹了
数据库的嵌套查询和统计查询
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
①求選修了數(shù)據(jù)結(jié)構(gòu)的學(xué)生學(xué)號(hào)和成績(jī)。
select s_no,score
from choice
where s_no in (select s_no from course where course_name='數(shù)據(jù)結(jié)構(gòu)')
②求01001課程的成績(jī)高于張彬的學(xué)生學(xué)號(hào)和成績(jī)。
select s_no,score
from choice
where course_no='01001'
and score>(select score from choice,studentwhere choice.s_no=student.s_no and s_name='張彬')
③求其他班中比js9901班某一學(xué)生年齡小的學(xué)生姓名和年齡。
select s_name,DATEDIFF(YEAR,s_birthday,GETDATE()) s_age
from student
where s_birthday>any(select s_birthday
from student
where class_no='js9901') and class_no<>'js9901'
④求其他班中比js9901班所有學(xué)生年齡都小的學(xué)生姓名和年齡。
select s_name,DATEDIFF(YEAR,s_birthday,GETDATE()) s_age
from student
where s_birthday>all(select s_birthday from student where class_no='js9901')
and class_no<>'js9901'
⑤求選修了02001課程的學(xué)生姓名。
select s_name from student
where exists(select * from choice where course_no='02001')
⑥求沒有選修了02001課程的學(xué)生姓名。
select s_name from student
where not exists(select * from choice where course_no='02001')
⑦查詢選修了全部課程的學(xué)生的姓名。
select s_name from student
where not exists(select * from choice where s_no=student.s_no)
⑧求至少選修了學(xué)號(hào)為991101的學(xué)生所選修的全部課程的學(xué)生學(xué)號(hào)和姓名。
select s_name from student
Where s_no in (select s_no from choice c1 where not exists(select choice c2
where c2.s_no='991101'
and not exists(select choice c3 where c3.s_no=c1.s_no and c3.s_no=c2.s_no)))
① 查詢學(xué)生總?cè)藬?shù)。
select count(*) from student
② 查詢選修了課程的學(xué)生人數(shù)。
select count(distinct s_no) from choice
③ 計(jì)算01001課程的學(xué)生平均成績(jī)。
select avg(score) from choice where course_no='01001’
④ 查詢選修01001課程的學(xué)生的最高分?jǐn)?shù)。
select max(score) from choice where course_no='01001'
⑤ 求學(xué)號(hào)為991101學(xué)生的總分和平均分。
select count(score),avg(score) from choice where course_no='991101'
⑥ 求各個(gè)課程號(hào)及相應(yīng)的選課人數(shù)。
select course_no,count(s_no) from choice group by course_no
⑦ 查詢選修了3門以上課程的學(xué)生學(xué)號(hào)。
select s_no from choice group by s_no having count(*)>3
⑧查詢選修了3門以上且各門課程均為及格的學(xué)生的學(xué)號(hào)及其總成績(jī),查詢結(jié)果按總成績(jī)降序列出。
select s_no,sum(score) as zongchengji from choice
where any(score)>=60
group by s_no
order by having(count(*)>3)
sum(score) desc
1. 集合查詢
在Transact-SQL中沒有直接提供集合的交操作INTERSECT和差MINUS操作,但可以使用其他方法實(shí)現(xiàn)。
①查詢js9902班的學(xué)生及年齡不大于19歲的學(xué)生。
select DATEDIFF(YEAR,s_birthday,GETDATE()) s_age
from student
where s_age<=19
union
select *
from student
where class_no='js9902'
②查詢選修了課程01001或者選修了01002的學(xué)生。
select s_no
from choice
where course_no='01001'
union
select s_no
from choice
where course_no='01002'
③查詢學(xué)號(hào)為991101和學(xué)號(hào)為991102的學(xué)生的學(xué)號(hào)和總分。
select s_no,sum(score)
from choice
where s_no='991101'
union
select s_no,sum(score)
from choice
where s_no='991102'
④查詢js9902班的學(xué)生與年齡不大于19歲的學(xué)生的交集。
select DATEDIFF(YEAR,s_birthday,GETDATE()) s_age
from student
where s_age<=19
intersect
select *
from student
where class_no='js9902'
⑤查詢js9902班的學(xué)生與年齡不大于19歲的學(xué)生的差集。select DATEDIFF(YEAR,s_birthday,GETDATE()) s_age
from student
where s_age<=19
except
select *
from student
where class_no='js9902'
總結(jié)
以上是生活随笔為你收集整理的数据库的嵌套查询和统计查询的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 集分宝怎么使用 集分宝的使用方法
- 下一篇: 关系数据库理论