SQL基础三(例子)
生活随笔
收集整理的這篇文章主要介紹了
SQL基础三(例子)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
-----------聚合函數使用------------------------ --1、查詢student表中所有學生人數 select count(stuno) from student --2、查詢stucou表中選課的人次 select count(*)as 選課人數 from stucou --3、查詢stucou表中學生所選課程數量 select count(distinct couno) from stucou --4、查詢stucou表中選了001課程的人數 select count(*) from stucou where couno='001' --5、查詢stucou表中第2志愿(willorder)選了001課程的人數 select count(*) from stucou where willorder='2' and couno='001' --6、統計student2010表中籍貫與你相同(同一縣、區或市)的學生人數 select count(*) from student2010 where jtdz like '%汕頭%' --7、統計student2010表中與你同姓的學生人數 select * from student2010 where xm like '陳%' --8、查詢qypt08class表班級最多的人數 select max(rs) from qypt08class --9、查詢qypt08class表護理學院的班級最少人數 select min(rs) from qypt08class ---------分組統計(group by子句使用)-------------------- --1、統計student2010表中男、女生人數 select xb, count(xb) from student2010 group by xb --2、統計stucou表中各門課程的選修人數 select * from stucou select couno, count(*) from stucou group by couno --3、統計stucou表中每個學生選修的課程數量 select * from stucou select stuno,count(*) from stucou group by stuno --4、統計student2010表中每個院系的學生人數 select * from student2010 select xymc,count(*) from student2010 group by xymc --5、統計student2010表中每個班的學生人數,顯示yxmc,bj及對應的人數,并按人數由多到少排序 select * from student2010 select bjmc,xymc,count(*) as 人數 from student2010 group by bjmc,xymc order by 人數 desc --6、統計student2010表中各民族學生人數,并按人數由少到多排序 select mz,count(*) from student2010 group by mz order by count(*) --7、在student2010表分專業統計男、女生人數,按專業名稱排序 select zymc,xb,count(*) as 人數 from student2010 group by zymc,xb order by 人數 desc -------------------對分組統計的結果進一步篩選(having子句使用)------------------------------ --1、查詢qypt08class表中各院系的人數,只顯示人數多于400的記錄 select * from qypt08class select yx,sum(rs) from qypt08class group by yx having sum(rs)>400 --2、統計stucou表中各門課程的選修人數,只顯示人數少于30的記錄(顯示couno及對應的人數) select * from stucou select couno,count(*) from stucou group by couno having count(*)<30 --3、查詢student2010表中人數多于70人的班級的xymc、zymc、bjmc及rs(人數) select * from student2010 select xymc,zymc,bjmc,count(*) from student2010 group by xymc,zymc,bjmc having count(*)>20 --------------------coupute子句使用---------------------- --1、在qypt08class中統計每個院系人數多于60的班級數,并顯示統計的明確 -------------------------將查詢保存為新表(into)-------------------- --1、查詢student2010表的xymc、zymc、bjmc、xh、xm五個字段內容,并將查詢結果保存到新表student2010A中 --查詢表student2010A的內容,檢驗上題操作結果 --2、統計student2010表中每班的人數(rs),并將結果保存到新表class2010,新表包含xymc、zymc、bjmc、rs四個字段 --查詢表class2010的內容,檢驗上題操作結果 --3、查詢表student2011中所有女生的信息,并將結果保存到表girl2011中 ------使用嵌套子查詢完成1-7題---------- --1、在qypt08student表中查詢和“陳小梅”在同一班級的所有男同學的信息。 select * from qypt08student where bjmc in (select bjmc from qypt08student where xm='陳小梅') and xb='男' --2、在qypt08student表中查詢和“黃巧”在同一院系的所有女同學的信息。 select * from qypt08student where yx=(select yx from qypt08student where xm='黃巧' ) and xb='女' --3、在qypt08student表中查詢和“黃巧”在同一院系的所有陳姓女同學的信息。 select * from qypt08student where yx=(select yx from qypt08student where xm='黃巧' ) and xb='女' and xm like '陳%' --4、查詢course表中最多人選修的課程信息(willnum最大) select * from course where willnum in (select max(willnum) from course) --5、查詢course表中最少人選修的課程信息(willnum最小) select * from course where willnum=(select min(willnum) from course) --6、查詢course表中選修人數大于平均選修數的課程信息 select * from course select from course --7、查詢course表中選修人數少于平均選修數的課程信息 ------使用相關子查詢完成以下題目---------- --8、查詢所有有選修課的學生信息 --9、查詢沒有選修課程的學生信息 --10、查詢沒有人選修的課程信息 --11、查找選修了課程號為002的課程的學生信息 --12、查找20000001班沒有選修課程號為004的課程的學生信息 --13、查找選修了“智能建筑”課程的學生信息
------使用嵌套子查詢完成1-7題---------- --1、在qypt08student表中查詢和“陳小梅”在同一班級的所有男同學的信息。 select * from qypt08student where bjmc in (select bjmc from qypt08student where xm='陳小梅') and xb='男' --2、在qypt08student表中查詢和“黃巧”在同一院系的所有女同學的信息。 select * from qypt08student where yx=(select yx from qypt08student where xm='黃巧' ) and xb='女' --3、在qypt08student表中查詢和“黃巧”在同一院系的所有陳姓女同學的信息。 select * from qypt08student where yx=(select yx from qypt08student where xm='黃巧' ) and xb='女' and xm like '陳%' --4、查詢course表中最多人選修的課程信息(willnum最大) select * from course where willnum in (select max(willnum) from course) --5、查詢course表中最少人選修的課程信息(willnum最小) select * from course where willnum=(select min(willnum) from course) --6、查詢course表中選修人數大于平均選修數的課程信息 select * from course select from course --7、查詢course表中選修人數少于平均選修數的課程信息 ------使用相關子查詢完成以下題目---------- --8、查詢所有有選修課的學生信息 --9、查詢沒有選修課程的學生信息 --10、查詢沒有人選修的課程信息 --11、查找選修了課程號為002的課程的學生信息 --12、查找20000001班沒有選修課程號為004的課程的學生信息 --13、查找選修了“智能建筑”課程的學生信息
--1、在qypt08student表中查詢和“陳小梅”在同一班級的所有男同學的信息。 select * from qypt08student where bjmc in (select bjmc from qypt08student where xm='陳小梅') and xb='男' --2、在qypt08student表中查詢和“黃巧”在同一院系的所有女同學的信息。 select * from qypt08student where yx=(select yx from qypt08student where xm='黃巧' ) and xb='女' --3、在qypt08student表中查詢和“黃巧”在同一院系的所有陳姓女同學的信息。 select * from qypt08student where yx=(select yx from qypt08student where xm='黃巧' ) and xb='女' and xm like '陳%' --4、查詢course表中最多人選修的課程信息(willnum最大) select * from course where willnum in (select max(willnum) from course) --5、查詢course表中最少人選修的課程信息(willnum最小) select * from course where willnum=(select min(willnum) from course) --6、查詢course表中選修人數大于平均選修數的課程信息 select avg(willnum) from course select * from course where willnum > (select avg(willnum) from course ) --7、查詢course表中選修人數少于平均選修數的課程信息 select * from course where willnum < (select avg(willnum) from course ) --8、查詢所有有選修課的學生信息 select * from student select * from course select distinct stuno from stucou select * from student where stuno in (select distinct stuno from stucou) --9、查詢沒有選修課程的學生信息 select * from student where stuno not in (select distinct stuno from stucou) --10、查詢沒有人選修的課程信息 select * from course where willnum ='0' --11、查找選修了課程號為002的課程的學生信息 select stuno from stucou where couno ='002' select * from student where stuno in (select stuno from stucou where couno ='002') --12、查找20000001班沒有選修課程號為004的課程的學生信息 select * from class where classno ='20000001' select * from course where couno not ='004' --13、查找選修了“智能建筑”課程的學生信息 --14、查詢成績表中大于平均分的學生信息 --15、查詢已經選修了課程的學生信息 --視圖練習 -------------------------------------------------------------------------------- --第一題 --1、使用企業管理器創建視圖,要求數據表的來源為:department,class,student三個表 -----顯示學生每個學生所屬的院系名稱、班級名稱、學號及姓名,視圖保存為v_student --2、在查詢分析器中查看視圖V_student的數據 --3、使用T-SQL語句創建一視圖,要求數據表的來源為:department,class,student三個表 -----顯示學生每個學生所屬的院系名稱、班級名稱、學號及姓名,視圖保存為v_student2 --4、在查詢分析器中查看視圖V_student2的數據 --第二題 --1、使用企業管理器創建視圖,數據表的來源為:class,student,course,stucou四個表 -----顯示每個學生的班級名稱、學號、選修的課程名稱,視圖保存為v_cou --2、在查詢分析器中查看視圖V_cou的數據 --3、使用T-SQL語句創建一視圖,數據表的來源為:class,student,course,stucou四個表 -----顯示每個學生的班級名稱、學號、選修的課程名稱,視圖保存為v_cou2 --4、在查詢分析器中查看視圖V_cou2的數據 --第三題 --1、使用企業管理器創建視圖,數據表的來源為:department,class,student,course,stucou五個表 -----顯示每個學生所屬系部名稱,班級名稱、學號、姓名、選修的課程名稱,視圖保存為v_cou2A --2、在查詢分析器中查看視圖V_cou2A的數據 --3、使用T-SQL語句創建一視圖,數據表的來源為:department,class,student,course,stucou五個表 -----顯示每個學生所屬系部名稱,班級名稱、學號、姓名、選修的課程名稱,視圖保存為v_cou2B --4、在查詢分析器中查看視圖V_cou2B的數據 --第四題 --1、使用T-SQL語句創建一視圖,命名為V_stunocou。要求數據表的來源為stucou,course兩個表 -----顯示學生的學號及所選課程的名稱,并加密視圖的定義 --2、在查詢分析器中查看視圖V_stunocou的數據
--1、檢索student2010表中學制(XZ)為2年的學生信息 --2、檢索student2010表中班級名稱(BJMC)為“2010計算機網絡技術1班”的學生信息 --3、檢索student2010表中專業名稱(ZYMC)為“計算機網絡技術”的學生信息,按姓氏排序顯示 --4、檢索student2010表中專業名稱(ZYMC)為“計算機網絡技術”的學生的學號、姓名字段,字段名用中文顯示 --5、檢索stucou表中選修了004、009、010及015課程的記錄 --6、檢索student2010表中姓名最后一個字為“華”的女學生信息 --7、檢索student2010表中清新籍學生的信息 --8、顯示qypt08student表中的系部名稱(不重復顯示) --9、顯示stucou表中所有willorder為1的記錄 --10、顯示stucou表中所有couno為003的記錄 --11、顯示stucou表中所有willorder為1且couno為003的記錄 --12、顯示stucou表中所有willorder為2到4的記錄 --13、顯示qypt08class表中備注(bz)不為空的記錄 --14、顯示qypt08student表中所有學號末位為1的記錄 select * from qypt08student where xh like '%1' --15、顯示qypt08student表中每個班的學號為1號的記錄 select * from qypt08student where xh like '%01' --16、顯示qypt08student表中所有姓‘張’的記錄 select * from qypt08student where xm like '張%' --17、顯示qypt08student表中所有姓‘張’且姓名只包含兩個字的記錄 select * from qypt08student where xm like '張_' --18、顯示qypt08student表中所有姓‘張’且姓名只包含兩個字的女性記錄 select * from qypt08student where xm like '張_' and xb like '女' --19、顯示student表中Pwd的首末兩位均為7的記錄 select * from student where pwd like '7%7' --20、顯示qypt08student表中所有姓‘張’且姓名只包含三個字的記錄 select * from qypt08student where xm like '張%' and xm not like '張_' --21、顯示qypt08student表中所有姓‘張’且姓名只包含三個字的男性記錄 select * from qypt08student where xm like '張%' and xm not like '張_' and xb like '男' --22、顯示qypt08student表中所有姓張、李、劉的記錄 select * from qypt08student where xm like '[張,李,劉]%' --23、檢索student2010表中身份證號碼(SFZH)的最后一位為數字的學生信息 select * from student2010 where sfzh like '%[0-9]'
總結
以上是生活随笔為你收集整理的SQL基础三(例子)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ECMWF 和 GFS 模型
- 下一篇: Kubernetes 之 Nameser