SQL語句中的查詢操作

單表查詢

1.查詢全體學生地 學號和姓名

select Sno,Sname
from Student;

2.查詢全體學生的詳細記錄

select *
from Student;

3.查詢經過計算的值(查詢全體學生的姓名及出生年份)

select Sname, 'Year of Birth;',2004-Sage,LOWER(Sdept)
from Student;

選擇表中的若干元祖

1.消除取消重複的行

select distinct Sno
from SC;

2.查詢滿足條件的元祖

比較運算符  =,>, <, >=, <=, !=, <>, !>, !<, NOT +

確定範圍 between and, not between and

確定集合 in,not in

字符匹配  like, not like

空值 is null, is not null

多重運算符(邏輯運算符) and ,or, not

比較大小

查詢計算機科學系全體學生的名單

select Sname
from Student
where Sdept='CS'

注意該查詢的運算過程 :對Student表進行全表掃描,取出一個元組,檢查該元組在Sdept列的值是否等於‘CS’。如果相等,取出Sname列的值形成一個新的元組輸出,否則跳出該元組,取下一個元組。(from->where->select)面試可能會問。。。。

查詢所有年齡在20歲以下的學生姓名及年齡

select Sname,Sage
from Student
where Sage<20;

查詢考試成績不及格的學生的學號

select distinct Sno
from SC
where Grade<60;

確定範圍

查詢年齡在20-23歲之間的學生姓名、系別和年齡

select Sname,Sdept,Sage
from Student
where Sage between 20 and 23;

查詢年齡不在20-23歲之間的學生姓名、系別和年齡

select Sname,Sdept,Sage
from Student
where Sage not between 20 and 23;

確定集合

查詢計算機科學系(CS)、數學系(MA)和信息系(IS)學生的姓名和性別

select Sname,Ssex
from Student
where Sdept in ('CS','MA','IS');

查詢既不是計算機系,數學系,也不是信息系的學生姓名和性別

select Sname,Ssex
from Student
where Sdept not in ('CS','MA','IS');


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章