合肥工业大学—SQL Server数据库实验七:数据查询
數(shù)據(jù)查詢
- 1. 單表查詢
- 2. 多表連接查詢
1. 單表查詢
1. 查詢?nèi)w學(xué)生的信息;
-- 查詢?nèi)w學(xué)生的信息
select * from student
2. 根據(jù)專業(yè)編號(21)查詢學(xué)生的學(xué)號、性別和年齡;
-- 根據(jù)專業(yè)編號查詢學(xué)生的學(xué)號、性別和年齡
select st_id '學(xué)號',st_sex '性別',datediff(year,st_born,getdate()) '年齡' from student where tc_mj = 21
3. 查詢未設(shè)定先修課的所有課程的信息;
-- 查詢未設(shè)定先修課的所有課程的信息
select * from course where cs_prerequisite is null
4. 查詢選修了‘100’號課程,且成績達(dá)到80分的學(xué)號;
-- 查詢選修了‘100’號課程,且成績達(dá)到80分的學(xué)號;
select sc_id from select_course where sc_num = 100 and sc_grade > 80
2. 多表連接查詢
1. 查詢“計(jì)算機(jī)與信息系”(系號1)全體學(xué)生的學(xué)號、姓名、專業(yè)名稱;
-- 查詢“計(jì)算機(jī)與信息系”(系號1)全體學(xué)生的學(xué)號、姓名、專業(yè)名稱
select st_id,st_name,mj_name from student,major where student.tc_mj = major.mj_id and mj_dpt = '1'
2. 查詢非“人工智能”(專業(yè)號21)專業(yè),年齡小于20的學(xué)生信息;
-- 查詢非“人工智能”(專業(yè)號21)專業(yè),年齡小于20的學(xué)生信息
select * from student where tc_mj <> 23 and datediff(year,st_born,getdate())<20
3. 查詢先修課是“計(jì)算機(jī)基礎(chǔ)”(課程號94)的所有課程的信息;
-- 查詢先修課是“計(jì)算機(jī)基礎(chǔ)”(課程號94)的所有課程的信息
select * from course where cs_prerequisite = 94
4. 查詢至少選修了‘100’和‘96’課程的學(xué)生學(xué)號
-- 查詢至少選修了‘100’和‘96’課程的學(xué)生學(xué)號
select sc_id from select_course sc1 where sc_num = 100 and exists (select * from select_course sc2 where sc2.sc_num = 96 and sc1.sc_id = sc2.sc_id)
5. 查詢未選修“JAVA技術(shù)”(課程號100)課程的學(xué)生學(xué)號、姓名、性別和專業(yè)號
-- 查詢未選修“JAVA技術(shù)”課程的學(xué)生學(xué)號、姓名、性別和專業(yè)號
select distinct sc_id '學(xué)號',st_name '姓名',st_sex '性別',tc_mj '專業(yè)號' from select_course,student where sc_id = st_id and sc_id not in(select sc_id from select_course where sc_num = 100)
6. 查詢未選修任何課程的學(xué)生學(xué)號和姓名
-- 查詢未選修任何課程的學(xué)生學(xué)號和姓名
select sc_id,st_id from select_course,student where sc_id = st_id and sc_id not in (select sc_id from select_course where sc_num is not null)
7. 查詢未被學(xué)生選修的課程號、課程名、先修課
-- 查詢未被學(xué)生選修的課程號、課程名、先修課
select cs_id,cs_name,cs_prerequisite from course,select_course where sc_id not in (select distinct sc_id from select_course)
8. 用派生關(guān)系查詢平均成績達(dá)到90分的學(xué)生學(xué)號、姓名和平均成績
-- 用派生關(guān)系查詢平均成績達(dá)到90分的學(xué)生學(xué)號、姓名和平均成績
select st_id '學(xué)號',st_name '姓名',avg_grade '平均成績' from student,(select sc_id,avg(sc_grade) as avg_grade from select_course group by sc_id) as sc_avg where avg_grade>90 and sc_avg.sc_id = student.st_id
9. 查詢學(xué)生的學(xué)號,只要先修課是“90”的課程他們都選修了
-- 查詢學(xué)生的學(xué)號,只要先修課是“90”的課程他們都選修了
-- 查詢不存在先選課為90的課程沒有選修的學(xué)生學(xué)號
select sc_id
from
select_course sc1
where not exists (select * from course where cs_prerequisite = 90 and not exists (select * from select_course sc2 where sc2.sc_num = course.cs_id and sc2.sc_id = sc1.sc_id))
10. 查詢未參加課程“100”考試的學(xué)生名單(學(xué)號、姓名、專業(yè)號)
-- 查詢未參加課程“100”考試的學(xué)生名單(學(xué)號、姓名、專業(yè)號)
select distinct sc_id,st_name,tc_mj from select_course,student where sc_id = st_id and sc_id not in (select sc_id from select_course where sc_num = 100);
總結(jié)
以上是生活随笔為你收集整理的合肥工业大学—SQL Server数据库实验七:数据查询的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 合肥工业大学—SQL Server数据库
- 下一篇: 合肥工业大学—SQL Server数据库