合肥工业大学—SQL Server数据库实验七:数据查询
生活随笔
收集整理的這篇文章主要介紹了
合肥工业大学—SQL Server数据库实验七:数据查询
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
數據查詢
- 1. 單表查詢
- 2. 多表連接查詢
1. 單表查詢
1. 查詢全體學生的信息;
-- 查詢全體學生的信息
select * from student
2. 根據專業編號(21)查詢學生的學號、性別和年齡;
-- 根據專業編號查詢學生的學號、性別和年齡
select st_id '學號',st_sex '性別',datediff(year,st_born,getdate()) '年齡' from student where tc_mj = 21
3. 查詢未設定先修課的所有課程的信息;
-- 查詢未設定先修課的所有課程的信息
select * from course where cs_prerequisite is null
4. 查詢選修了‘100’號課程,且成績達到80分的學號;
-- 查詢選修了‘100’號課程,且成績達到80分的學號;
select sc_id from select_course where sc_num = 100 and sc_grade > 80
2. 多表連接查詢
1. 查詢“計算機與信息系”(系號1)全體學生的學號、姓名、專業名稱;
-- 查詢“計算機與信息系”(系號1)全體學生的學號、姓名、專業名稱
select st_id,st_name,mj_name from student,major where student.tc_mj = major.mj_id and mj_dpt = '1'
2. 查詢非“人工智能”(專業號21)專業,年齡小于20的學生信息;
-- 查詢非“人工智能”(專業號21)專業,年齡小于20的學生信息
select * from student where tc_mj <> 23 and datediff(year,st_born,getdate())<20
3. 查詢先修課是“計算機基礎”(課程號94)的所有課程的信息;
-- 查詢先修課是“計算機基礎”(課程號94)的所有課程的信息
select * from course where cs_prerequisite = 94
4. 查詢至少選修了‘100’和‘96’課程的學生學號
-- 查詢至少選修了‘100’和‘96’課程的學生學號
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技術”(課程號100)課程的學生學號、姓名、性別和專業號
-- 查詢未選修“JAVA技術”課程的學生學號、姓名、性別和專業號
select distinct sc_id '學號',st_name '姓名',st_sex '性別',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)
6. 查詢未選修任何課程的學生學號和姓名
-- 查詢未選修任何課程的學生學號和姓名
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. 查詢未被學生選修的課程號、課程名、先修課
-- 查詢未被學生選修的課程號、課程名、先修課
select cs_id,cs_name,cs_prerequisite from course,select_course where sc_id not in (select distinct sc_id from select_course)
8. 用派生關系查詢平均成績達到90分的學生學號、姓名和平均成績
-- 用派生關系查詢平均成績達到90分的學生學號、姓名和平均成績
select st_id '學號',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. 查詢學生的學號,只要先修課是“90”的課程他們都選修了
-- 查詢學生的學號,只要先修課是“90”的課程他們都選修了
-- 查詢不存在先選課為90的課程沒有選修的學生學號
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”考試的學生名單(學號、姓名、專業號)
-- 查詢未參加課程“100”考試的學生名單(學號、姓名、專業號)
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);
總結
以上是生活随笔為你收集整理的合肥工业大学—SQL Server数据库实验七:数据查询的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 合肥工业大学—SQL Server数据库
- 下一篇: 合肥工业大学—SQL Server数据库