實(shí)驗(yàn)名稱 ?????????SQL語(yǔ)言進(jìn)行簡(jiǎn)單查詢 | |||
注意:原版word在下載資源里面(免費(fèi)下載) | |||
實(shí)驗(yàn)?zāi)康募耙螅?/p>
| |||
實(shí)驗(yàn)內(nèi)容及步驟: 注:所有程序第一行均在XSGL數(shù)據(jù)庫(kù)中進(jìn)行,即每次運(yùn)行程序第一行均為use XSGL
select count(distinct sno) 學(xué)生人數(shù)--distinct表示去掉重復(fù)行 from sc
select sum(credit) from course,sc where sno = '200515001' and sc.cno = course.cno and grade>=60; --成績(jī)超過(guò)60才有學(xué)分,并連接兩個(gè)表
select sno,cno from sc where grade is not null; --排除空值
select sname,sdept,sage from student where sage>=20and sage<=23; --范圍條件語(yǔ)句? 也可以用between…and…
select sno,grade from sc where cno=3 order by grade desc;--降序排列;
select *--查詢所有表的所有信息 from student where sdept=' 'or sdept is null;--sdept 為空或者為空格
select sname,sage from student where ssex='男'and sdept='CS';
select cname from course--like語(yǔ)句表示求相似的, where cname like '數(shù)據(jù)%';--數(shù)據(jù)%表示以數(shù)據(jù)開(kāi)頭,后面0到無(wú)窮多個(gè)字符
select sname from student where sname like '_向%';--_表示一個(gè)字符
select sno,cno from sc where grade>90;--以90分以上為優(yōu)秀
select sname from student where (sdept<>'cs'and sdept<>'ma')and sage>=20;--注意優(yōu)先級(jí),最好加括號(hào)
select ssex,count(ssex) 人數(shù)--也可以用*代替ssex from student where sdept='cs' group by ssex--分別輸出男女需要分組
select count(sno) 人數(shù),sdept,sage from student group by sdept,sage—group by 語(yǔ)句后面的詞數(shù)與 select后單個(gè)詞數(shù)一致 order by sdept desc,sage ;--降序?yàn)閐esc 升序?yàn)閍sc可省略
select count(sno) 人數(shù),sdept from student group by sdept order by count(sno) desc;--降序?yàn)閐esc 升序?yàn)閍sc可省略
select sdept,ssex,count(sno) 人數(shù) from student group by sdept,ssex order by sdept,ssex desc;--女生在前,按照第二字母順序降序?yàn)閐esc
select student.sno,sname--必須加上student.或者sc.因?yàn)閮蓚€(gè)表都有sno from sc,student where student.sno=sc.sno--連接兩個(gè)表 group by student.sno,sname having count(*)>3--分組后顯示選了三門(mén)以上的,而不是直接顯示整個(gè)表三門(mén)以上的
select sno,avg(grade) 平均分,max(grade) 最高分, min(grade) 最低分,count(cno) 選課門(mén)數(shù) from sc group by sno
select sno,avg(grade) 平均分 from sc group by sno--查詢的是選修至少兩門(mén)的每個(gè)學(xué)生,然后計(jì)算每個(gè)學(xué)生的課程的信息,則對(duì)學(xué)生分組,每一個(gè)學(xué)生為一組 having? count(cno)>=2; --having后面可以使用聚合函數(shù)。聚合函數(shù)就是對(duì)一組值進(jìn)行計(jì)算并且返回單一值的函數(shù):sum---求和,count---計(jì)數(shù),max---最大值,avg---平均值等。所以不用where。
select sno,avg(grade) 平均分 from sc group by sno having? avg(grade)>=80; 比較:?求各學(xué)生的60分以上課程的平均分. select sno,avg(grade) 平均分 from sc where grade>60 group by sno--having既然是對(duì)查出來(lái)的結(jié)果進(jìn)行過(guò)濾,那么就不能對(duì)沒(méi)有查出來(lái)的值使用having。
select student.sno from sc,student where sdept='is'and student.sno=sc.sno--一定要記得連接兩個(gè)表 group by student.sno having count(cno)>2;
select cname,grade from sc,student,course--需要連接三個(gè)表 where course.cno=sc.cno and student.sno=sc.sno and sname='李勇 ';
select sdept,student.sno,sname,cname from student,course,sc --sno是student表的主碼,cno是course表的主碼,需要用主碼與sc表中的外碼連接 where student.sno=sc.sno and course.cno=sc.cno and (grade<60 or grade is null) --不用and因?yàn)槌煽?jī)小于六十與成績(jī)?yōu)榭詹荒芡瑫r(shí)發(fā)生 --有or出現(xiàn)注意通過(guò)題目判斷是否加括號(hào) order by sdept ,student.sno--先按系排序,則系別在前面
select student.sno,sname,cname from student,course,sc where student.sno=sc.sno and course.cno=sc.cno and (grade<60 or grade is null) and sdept='cs' order by student.sno
select student.sno,sname,cname from student, course, sc where student.sno=sc.sno and course.cno=sc.cno and grade is null and sdept not in('cs','ma')--與sdept<>'cs'and sdept<>'ma'一致
select avg(grade) from course,sc where course.cno=sc.cno and cname='DB'
select cname, avg(grade) --成績(jī)?cè)趕c表,課程名在course表 from course, sc where course.cno=sc.cno group by cname--求每一門(mén)課程,所以分組 order by avg(grade) desc--從大到小即為倒序
select sum(credit) 總學(xué)分 from student,course,sc--成績(jī)?cè)趕c表,學(xué)分在course表,李勇在student表 where student.sno=sc.sno and course.cno=sc.cno文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-432649.html and sname='李勇' and grade>=60文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-432649.html |
到了這里,關(guān)于數(shù)據(jù)庫(kù)SQL Server實(shí)驗(yàn)報(bào)告 之 SQL語(yǔ)言進(jìn)行簡(jiǎn)單查詢(3/8)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!