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