SQL Server的簡單查詢命令
阿新 • • 發佈:2018-11-10
SQL Server的簡單查詢命令
一、實驗目的
- 通過本次實驗使學生初步掌握資料庫中表的單表,多表查詢操作。
二、實驗內容
- 簡單無條件查詢
- 有條件查詢
- 分組查詢
- 模糊查詢
三、實驗環境
- Windows
- SQL Server
四、實驗步驟及結果
SQL Server中是不區分大小寫的
- 查詢全體學生的詳細記錄
select *
from Student
go
- 查詢全體學生的學號與姓名
select Sno,Sname
from Student
go
- 查詢全體學生的姓名、學號、所在系
select Sno,Sname,Sdept
from Student
go
- 查詢選修了課程的學生學號
select Distinct Sno
from SC
go
- 查全體學生的姓名及其出生年份
select Sno,SageAge
from Student
go
- 查詢學生總人數
select Count(*)人數
from Student
go
- 查詢選修了課程的學生人數
select Count(distinct Sno)人數
from SC
go
- 查詢學號是 201815121 的學生的總分、平均分、最高分和最低分。
select SUM(Grade)總分,
AVG(Grade)平均分,
MAX(Grade)最高分,
MIN(Grade)最低分
from SC
where Sno='201815121'
go
- 查詢計算機科學系全體學生的名單
select Sname
from Student
where Sdept='CS'
go
- 查詢所有年齡在 20 歲以下的學生姓名及其年齡
select Sname,SageBirthday
from Student
where Sage <20
go
- 檢索出年齡在 18-19 歲的學生姓名(兩種方法)
(1).select Sname
from Student
where Sage between 18 and 19
go
(2).select Sname
from Student
where Sage>=18 and
Sage<=19
go
- 查詢年齡不在 18-19歲之間的學生姓名 (兩種方法)
(1).select Sname,SageBirthday
from Student
where Sage<18 or
Sage>19
go
(2).select Sname,SageBirthday
from Student
where Sage not between 18 and 19
go
13 、 查詢年齡不是18和19的學生的姓名和年齡 (兩種方法)
(1).select Sname,SageBirthday
from Student
where Sage !=18 and Sage !=19
go
(2).select Sname,SageBirthday
from Student
where Sage <>18 and Sage <>19
go
14 、 查詢資訊系(IS)、數學系(MA)學生的姓名和性別
select Sname,Ssex
from Student
where Sdept IN('IS','MA')
go
15 、 查詢所有不姓劉的學生姓名。
select Sname
from Student
where Sname NOT LIKE '劉%'
go
16 、 查詢沒有先行課的課程號和課程名
select Cno,Cname
from Course
where Cpno is null
go
17 、 顯示年齡大於18歲的男女生人數,結果降序列
select Ssex, COUNT(Ssex)人數
from student
where Sage> 18
group by Ssex
order by COUNT(Ssex) desc
go
18 、 查詢選修了2門以上課程的學生學號
select Sno
from SC
group by Sno
Having COUNT(*)>2
go
19 、 查詢學生的學號、姓名、課程號和成績
select distinct Student.Sno,Sname,Cno,Grade
from Student,SC
where Student.Sno=SC.sno
go
20 、 查詢每一門課的間接先修課(即先修課的先修課)
select First.Cno,Second.Cpno
from Course First,Course Second
where First.Cpno = Second.Cno
go
21 、 查詢除劉晨外其他與“劉晨”同齡的學生。
select Sname
from Student
where Sage
in(
select Sage
from Student
where Sname='劉晨'
)
and Sname <>'劉晨'
go
22 、 找出每個學生超過他選修課程平均成績的課程號。
select Sno,Cno
from SC x
where
Grade>(
select AVG(Grade)
from SC y
where y.Sno=x.Sno
)
go
23 、 查詢其他系中比電腦科學某一學生年齡小的學生姓名和年齡
select sname, Sage
from student
where Sage
<Any(
select Sage
from student
where sdept='CS'
)
and sdept<>'CS'
go
24 、 查詢其他系中比計算機科學系所有學生年齡都小的學生姓名及年齡。
select student.sname, Sage
from student
where Sage
<(
select min (Sage)
from student
where sdept='CS'
)
and sdept<>'CS'