合肥工业大学—SQL Server数据库实验八:使用聚集函数的SELECT语句
生活随笔
收集整理的這篇文章主要介紹了
合肥工业大学—SQL Server数据库实验八:使用聚集函数的SELECT语句
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在數據庫EDUC中用SQL語句實現如下查詢:
1) 查詢學生的總人數
-- 查詢學生的總人數
select count(*) from student
select count(1) from student
2) 查詢每個專業的專業編號、專業名稱、學生人數、學生的平均年齡
-- 查詢每個專業的專業編號、專業名稱、學生人數、學生的平均年齡
select mj_id '專業編號', mj_name '專業名稱', count(*) '學生人數', avg(datediff(year,st_born,getdate())) '學生的平均年齡'
from major,student
where student.tc_mj = major.mj_id
group by mj_id,mj_name
3) 查詢選修了課程的學生人數
-- 查詢選修了課程的學生人數
select count(distinct sc_id) from select_course
4) 查詢選修了課程“100”,且成績超過該課程平均成績的學生學號、姓名
-- 查詢選修了課程“100”,且成績超過該課程平均成績的學生學號、姓名
select st_id,st_name
from select_course,student
where sc_id in (select sc_id from select_course where sc_num = 100)
and sc_id = st_id
and sc_num = 100
and sc_grade>(select avg(sc_grade) from select_course where sc_num = 100)
5) 查詢每門課的課程號、選課人數、平均成績、最高分和最低分。
-- 查詢每門課的課程號、選課人數、平均成績、最高分和最低分
select cs_id, count(sc_num) '選課人數',avg(sc_grade) '平均分', max(sc_grade) '最高分', min(sc_grade) '最低分'
from course,select_course
where cs_id = sc_num
group by cs_id
6) 查詢選修課程超過2 門課的學生學號和姓名。
-- 查詢選修課程超過2 門課的學生學號和姓名
select distinct st_id,st_name
from student,select_course sc
where sc_id = st_id
and exists (select sc_id,count(sc_num) from select_course sc1where sc.sc_id = sc1.sc_idgroup by sc_id having count(*) > 2)
總結
以上是生活随笔為你收集整理的合肥工业大学—SQL Server数据库实验八:使用聚集函数的SELECT语句的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 合肥工业大学—SQL Server数据库
- 下一篇: 合肥工业大学—SQL Server数据库