SQL語句大(bu)全

前言

近來感覺SQL語句有些生疏,于是,便有了這次的回憶

采用的是SQLServer 2008

最常見的三張表

Student(sno, name, sex, age, dept )
Course(cno, name, pno, teacher, credit)
SC(sno, cno, grade)

常見SQL語句

1.插入一名學(xué)生的信息'200215126', '張輝', '男', 20, 'IS'

insert into student(sno, name, sex, age, dept )
values( '200215126', '張輝', '男', 20, 'IS' )

2.更新學(xué)號為200215121的學(xué)生的年齡為20,學(xué)院為計算機(jī)院

update student 
set age=20,dept='CS' 
where sno='200215121'

3.檢索不學(xué)C2課的學(xué)生姓名與年齡

(思路:學(xué)生表里總體的姓名和年齡 “減去” 學(xué)C2的學(xué)生姓名和年齡 等于不學(xué)的)

集合的操作,見SQL語句對結(jié)果集操作

(select name ,age from student) 
Except
(select name ,age from student,sc where student.sno=sc.sno and sc.cno='2') 

4.檢索學(xué)習(xí)全部課程的學(xué)生學(xué)號

(思路:選課表中全部的學(xué)號和課程號 “除以” 課表中全部的課程號)

除法的操作,見SQL語句實現(xiàn)關(guān)系代數(shù)中的“除法”

select  distinct  sno  from  sc  sc1
where not exists
(
    select  cno from course
    where not exists
    (
        select * from sc sc2
        where sc2.sno=sc1.sno and sc2.cno=course.cno
    )
)

5.檢索所學(xué)課程包含學(xué)生95002所學(xué)課程的學(xué)生學(xué)號

(思路:選課表中全部的學(xué)號和課程號“除以”選課表中95002學(xué)生所學(xué)的課程號)

select  distinct  sno  from  sc  sc1
where not exists
(
    select  cno from sc sc2
    where sno='95002'
    and not exists
    (
        select * from sc sc3
        where sc3.sno=sc1.sno and sc3.cno=sc2.cno
    )
)

6.統(tǒng)計信息(總?cè)藬?shù),男生人數(shù),女生人數(shù),平均年齡,每個課程的平均分,最高分,95001的總學(xué)分)

詳情見 Sql Server 2008單個存儲過程統(tǒng)計多個信息

select
(select count(sno)from student) sumPersno,
(select count(sno)from student where sex='男') boynumber,
(select count(sno)from student where sex='女') girlnumber,
(select avg(age)from student) avgOfAge,
(select avg(grade)from sc where cno='1' ) avgOfCourse1,
(select avg(grade)from sc where cno='2' ) avgOfCourse2,
(select avg(grade)from sc where cno='3' ) avgOfCourse3,
(select max(grade)from sc where cno='1' ) maxOfCourse1,
(select max(grade)from sc where cno='2' ) maxOfCourse2,
(select max(grade)from sc where cno='3' ) maxOfCourse3,
(select sum(credit)from course, sc where course.cno=sc.cno and sc.sno='95001' ) sumCreditOf95001

7.找出不姓‘王’的學(xué)生記錄。

select sno, name, sex, age, dept 
from student 
where name not like'王%'

8.統(tǒng)計每個學(xué)生選修課程的個數(shù)

select student.sno, count(cno)SCnumber 
from student left join sc on sc.sno=student.sno  
group by student.sno

9.統(tǒng)計有學(xué)生選修的課程門數(shù)。

select count(distinct SC.cno)SCCourseNumber from SC;

10.求選修課程號為2的課程名稱,學(xué)生平均年齡,平均成績,均保留兩位小數(shù)

select course.name ,round(avg(age),2) avgOfAge ,round(avg(grade),2) avgGrade 
from student ,sc,course
where student.sno=sc.sno 
and course.cno=sc.cno
and sc.cno='2' 
group by course.name;

還可以用第6個的統(tǒng)計信息的方式來求(其中平均年齡可以換個方式)

select
    (select course.name from course where course.cno='2') courseName,
    (select round(avg(age),2)avgOfAge from student 
    where student.sno in(select sno from sc where cno='2'))avgOfAge,
    (select round(avg(grade),2) from sc where sc.cno='2')avgGrade

11.檢索學(xué)號比劉晨同學(xué)大,而年齡比他小的學(xué)生姓名

法1:暴力法

select student.name from student
where sno>(select sno from student where student.name='劉晨')
and age<(select age from student where student.name='劉晨')

法2:嵌套法

select name from student x 
where sno> some(select sno from student  y where y.name='劉晨' and x.age<y.age)

12.求年齡大于女同學(xué)平均年齡的男學(xué)生姓名和年齡。

select name ,age from student 
where sex='男' 
and age>(select round(avg(age),2) from student where sex='女')

13.求年齡大于所有女同學(xué)年齡的男學(xué)生姓名和年齡

select name ,age 
from student 
where sex='男' and age>(select max(age) from student where sex='女')

14.檢索每一門課程成績都大于等于90分的學(xué)生學(xué)號、姓名和性別

(思路:
1.在SC表中查出最小成績大于90的學(xué)生學(xué)號
按照學(xué)號進(jìn)行分組,然后求小組的最小成績大于90的學(xué)號
2.在Student表中根據(jù)sno查詳細(xì)信息

select sno,name,sex from student 
where sno in (select sno from sc group by sno having min(grade)>=90)

收獲:聚合函數(shù)只能用在Select后面和Having后面
group by 對數(shù)據(jù)進(jìn)行分組,然后 having對小組內(nèi)部的數(shù)據(jù)進(jìn)行操作

注意:操作的過程是:
先查,后對查詢結(jié)果分組,分組后,組內(nèi)調(diào)用函數(shù),最后根據(jù)Having后面的條件對其進(jìn)行篩選

注意:
Group By 后面出現(xiàn)的屬性,在Select后面一定要出現(xiàn)


15.把低于總平均成績的女同學(xué)成績提高5%

(思路:
1.首先求總平均成績
2.修改低于平均成績的女生的成績(此時的修改沒有改變已求出的平均成績)

update sc 
set grade=grade*1.05 
where sc.grade<(select avg(grade) from sc )
and sc.sno in(select sno from student where sex='女') 

16.檢索至少選修兩門課程的學(xué)生的學(xué)號

思路:
1.按照學(xué)號將學(xué)生分組
2.統(tǒng)計小組內(nèi)部選修課程的數(shù)量
3.根據(jù)條件檢索

select sc.sno 
from sc 
group by sc.sno
having count(sc.cno)>=2 

17.檢索所修課程平均成績大于等于90分的學(xué)生姓名及其平均成績

select sc.sno, student.name,avg(grade) 
from sc,student
where student.sno=sc.sno
group by sc.sno,student.name
having avg(grade)>90

收獲:
當(dāng)select里有聚合函數(shù)時,select的其他元素也必須是聚合函數(shù)或出現(xiàn)在group by后面


18.查詢最高分的學(xué)生學(xué)號和課程號

select sno,cno from sc
where grade=(select max(grade)from sc)

19.查詢存在有85分以上成績的課程Cno

不能用:select distinct cno from sc where grade>85,雖然能夠?qū)崿F(xiàn)

要用select distinct cno from sc where grade in(select grade from sc where grade>85)
或者
select cno from sc where grade>85 group by cno
再或者
select cno from sc group by cno having max(grade)>85


20.查詢所有選修“數(shù)學(xué)”課程的同學(xué)的學(xué)號和成績

方法一嵌套

select sno,grade from sc 
where cno=(select cno from course where course.name='數(shù)學(xué)' )

方法二鏈接

select sno,grade from course, sc 
where cno=(select cno from course where course.name='數(shù)學(xué)' )

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,825評論 6 546
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,814評論 3 429
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,980評論 0 384
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 64,064評論 1 319
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 72,779評論 6 414
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 56,109評論 1 330
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 44,099評論 3 450
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 43,287評論 0 291
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,799評論 1 338
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 41,515評論 3 361
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,750評論 1 375
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,221評論 5 365
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,933評論 3 351
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,327評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,667評論 1 296
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,492評論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 48,703評論 2 380

推薦閱讀更多精彩內(nèi)容