SQL課上作業(yè)與答案(1)_第1頁
SQL課上作業(yè)與答案(1)_第2頁
SQL課上作業(yè)與答案(1)_第3頁
SQL課上作業(yè)與答案(1)_第4頁
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、資料收集于網(wǎng)絡(luò),如有侵權(quán) 請聯(lián)系網(wǎng)站刪除 1、 查詢 xs 表中計算機(jī)專業(yè)同學(xué)的學(xué)號、姓名和總學(xué)分,結(jié)果中各列的標(biāo)題分別指定為 number、 name 和 mark。 Select 學(xué)號 as number姓名 as name總學(xué)分 as mark from xs where 專業(yè)名 = 計算機(jī); 2、 查詢 xs 表中計算機(jī)專業(yè)各同學(xué)的學(xué)號、姓名和總學(xué)分,對總學(xué)分按如下規(guī)則進(jìn)行 替換: 若總學(xué)分為空值,替換為“尚未選課” ;若總學(xué)分小于 50,替換為“不及格” ;若總學(xué) 分在5052之間,替換為“合格”;若總學(xué)分大于52,替換為“優(yōu)秀”??倢W(xué)分列的標(biāo) 題更改為“等級” 。 select 學(xué)

2、號,姓名 , case when 總學(xué)分 is null then 尚未選課 when 總學(xué)分 =50 and 總學(xué)分 50; 1 row in set 7、求選修 101 課程的學(xué)生的最高分和最低分。 select max(成績)as 最高分,min(成績)as 最低分 from xs_kc where 課程號=101; 1 row in set 8、求學(xué)號 081101 的學(xué)生所學(xué)課程的總成績。 select sum(成績)as 總成績 from xs_kc where 學(xué)號=81101; 9、求選修 101 課程的學(xué)生的平均成績。 select 學(xué)號,avg(成績)from xs_kc

3、where 課程號=101; 10、求選修 101 課程的成績的方差。 select variance(成績)from xs_kc where 課程號=101; 11、求選修 101 課程的成績的標(biāo)準(zhǔn)差。 select stddev( 成績 ) from xs_kc where 課程號 =101; 12、求選修了 206 課程的學(xué)生的學(xué)號。 select group_concat( 學(xué)號 ) from xs_kc where 課程號 =206; 13、從 xs 表中檢索出所有學(xué)生的信息,并使用表別名student。 select * from xs as student; 14、查找 xscj

4、 數(shù)據(jù)庫中所有學(xué)生選過的課程名和課程號 select distinct kc.課程名,xs_kc.課程號 from kc,xs_kc where kc.課程號=xs_kc課程號; 3 rows in set 15、用 FROM 子句的 JOIN 關(guān)鍵字表達(dá)下列查詢:查找選修了206 課程且成績在 80 分 以上的學(xué)生姓名及成績。 select 姓名,成績 from xs inner join xs_kc on xs.學(xué)號=xs_kc.學(xué)號where 課程號 =206 and 成績 80; 3 rows in set 16、用 FROM 的 JOIN 關(guān)鍵字表達(dá)下列查詢:查找選修了“計算機(jī)基礎(chǔ)”

5、課程且成績在 80 分以上的學(xué)生學(xué)號、姓名、課程名及成績。 select xs學(xué)號,姓名,課程名,成績 from xs join xs_kc on xs.學(xué)號=xs_kc.學(xué)號 join kc on xs_kc.課程號=kc.課程號 where課程名=計算機(jī)基礎(chǔ)and成績 80; 8 rows in set 17、查找 xscj 數(shù)據(jù)庫中課程不同、成績相同的學(xué)生的學(xué)號、課程號和成績。 select a.學(xué)號,a.課程號,b.課程號,a.成績 from xs_kc as a join xs_kc as b on a.成績=b. 成績and a.學(xué)號=b.學(xué)號and a.課程號!= b.課程號;

6、2 rows in set 18、查找所有學(xué)生情況及他們選修的課程號,若學(xué)生未選修任何課,也要包括其情況。 select xs_kc.*,課程 號 from xs left outer join xs_kc on xs.學(xué)號=xs_kc.學(xué)號; 19、查找被選修了的課程的選修情況和所有開設(shè)的課程名。 select xs_kc.*,課程名 from xs_kc right join kc on xs_kc.課程號=kc.課程號; 20、列出學(xué)生所有可能的選課情況。 mysql select 學(xué)號,姓名,課程號,課程名 - from xs cross join kc; 21、查詢xscj數(shù)據(jù)庫xs

7、表中學(xué)號為81101的學(xué)生的情況。 select 學(xué)號 ,姓名,總學(xué)分 from xs where 學(xué)號=81101; 1 row in set 22、查詢xs表中總學(xué)分大于 50分的學(xué)生的情況。 select * from xs where 總學(xué)分 50; 23、查詢xs表中備注為空的同學(xué)的情況。 select * from xs where 備注 is null; mysql select * from xs where 備注 null; 24、查詢xs表中專業(yè)為計算機(jī),性別為女(0)的同學(xué)的情況。 select * from xs where 專業(yè)名 =計算機(jī) and 性別 =0; 25

8、、查詢xscj數(shù)據(jù)庫xs表中姓“王”的學(xué)生學(xué)號、姓名及性別。 select 學(xué)號,姓名,性別 from xs where 姓名 like 王%; 26、查詢xscj數(shù)據(jù)庫xs表中,學(xué)號倒數(shù)第二個數(shù)字為0的學(xué)生的學(xué)號、姓名及專業(yè)名。 select 學(xué)號,姓名 ,專業(yè)名 from xs where 學(xué)號 like %0_; |查詢xs表中名字包含下畫線的學(xué)生學(xué)號和姓名。 select 學(xué)號 ,姓名 from xs where 學(xué)號 like %#_% escape #; 28、查詢 xscj 數(shù)據(jù)庫 xs 表中不在 1993 年出生的學(xué)生情況。 select * from xs where 出生時

9、間 1993; 29、 查詢xs表中專業(yè)名為“計算機(jī)”、“通信工程”或“無線電”的學(xué)生的情況。 select * from xs where 專業(yè)名 =計算機(jī) or 專業(yè)名 =通信工程 or 專業(yè)名 =無線電 ; 30、查詢xscj數(shù)據(jù)庫中總學(xué)分尚不定的學(xué)生情況。 select * from xs where 總學(xué)分 is null; 31、 查找在xscj數(shù)據(jù)庫中選修了課程號為206的課程的學(xué)生的姓名、學(xué)號。 select 姓名 ,學(xué)號 from xs where 學(xué)號 in(select 學(xué)號 from xs_kc where 課程號 =206); 32、查找未選修離散數(shù)學(xué)的學(xué)生的姓名、學(xué)

10、號、專業(yè)名。 select 姓名 ,學(xué)號 ,專業(yè)名 from xs where 學(xué)號 not in (select 學(xué)號 from xs_kc where 課程號 in (select 課程號 from xs where 課程號 =離散數(shù)學(xué) ); 33、查找選修了離散數(shù)學(xué)的學(xué)生學(xué)號。 select 學(xué)號 from xs_kc where 課程號 = (select 課程號 from kc where 課程名 =離散數(shù)學(xué) ); 34、查找xs表中比所有計算機(jī)系的學(xué)生年齡都大的學(xué)生學(xué)號、姓名、專業(yè)名、出生日期。 select 學(xué)號 ,姓名 ,專業(yè)名 ,出生時間 from xs where 出生時間

11、 any(select 成績 from xs_kc where 課 程號 =101); 36、查找選修 206 號課程的學(xué)生姓名。 select 姓名 from xs where exists (select * from xs_kc where 課程號=206 and 學(xué)號=xs學(xué)號); 37、查找選修了全部課程的同學(xué)的姓名。 select 姓名 from xs where not exists (select * from kc where not exists (select * from xs_kc where學(xué)號=xs學(xué)號and課程號=kc課程號); 38、 從xs表中查找總學(xué)分大于

12、50分的男同學(xué)的姓名和學(xué)號。 select 姓名 ,學(xué)號 from xs where 總學(xué)分 50 and 性別 =1; select 姓名 ,學(xué)號 from xs where 學(xué)號 in (select 學(xué)號 from xs where 總學(xué)分 50 and 性別 =1); select 姓名,學(xué)號,總學(xué)分 from (select 姓名,學(xué)號,性別,總學(xué)分 from xs where 總學(xué)分 50) as student where 性別 =1; 39、 從xs表中查找所有女學(xué)生的姓名、學(xué)號,以及與81101號學(xué)生的年齡差距。 select 姓名,學(xué)號,year(出生時間)-year(se

13、lect 出生時間 from xs where 學(xué)號=81101 ) ) as 年齡差距 from xs where 性別 =0; 40、查找與 81101 號學(xué)生性別相同、總學(xué)分相同的學(xué)生學(xué)號和姓名。 select 學(xué)號 ,姓名 from xs where (性別 ,總學(xué)分 )=(select 性別 ,總學(xué)分 from xs where 學(xué)號 =81101); 41、將xscj數(shù)據(jù)庫中各專業(yè)名輸出。 select 專業(yè)名 from xs group by 專業(yè)名 ; 42、求xscj數(shù)據(jù)庫中各專業(yè)的學(xué)生數(shù)。 select 專業(yè)名 ,count(*) as 學(xué)生數(shù) from xs group

14、by 專業(yè)名 ; 43、求被選修的各門課程的平均成績和選修該課程的人數(shù)。 select 課程號,avg(成績)as 平均成績,count(*) as 總?cè)藬?shù) ” from xs_kc group by 課程號; 44、在 xscj 數(shù)據(jù)庫上產(chǎn)生一個結(jié)果集,包括每個專業(yè)的男生人數(shù)、女生人數(shù)、總?cè)藬?shù),以 及學(xué)生總?cè)藬?shù)。 select 專業(yè)名 ,性別 ,count(*) as 總?cè)藬?shù) from xs group by 專業(yè)名 ,性別 with rollup; 45、在 xscj 數(shù)據(jù)庫上產(chǎn)生一個結(jié)果集,包括每門課程各專業(yè)的平均成績、每門課程的總平 均成績和所有課程的總平均成績。 select課程名,

15、專業(yè)名,avg(成績)as平均成績 from xs_kc,kc,xs where xs_kc課程號 =kc課程 號 and xs_kc.學(xué)號=xs.學(xué)號 group by 課程名,專業(yè)名 with rollup; 46、查找xscj數(shù)據(jù)庫中平均成績在 85分以上的學(xué)生的學(xué)號和平均成績。 select 學(xué)號,avg(成績)as 平均成績from xs_kc group by 學(xué)號 having avg(成績)=85; 47、 查找選修課程超過2門且成績都在 80 分以上的學(xué)生的學(xué)號 。 select 學(xué)號 from xs_kc group by 學(xué)號 having count(*) 2; 48、

16、 查找通信工程專業(yè)平均成績在85 分以上的學(xué)生的學(xué)號和平均成績。 select 學(xué)號,avg(成績)as 平均成績from xs_kc where 學(xué)號 in (select 學(xué)號 from xs where 專業(yè)名=通信工程)group by 學(xué)號 having avg(成績)=85; 49、將通信工程專業(yè)的學(xué)生按出生日期先后排序。 select 學(xué)號,姓名 ,專業(yè)名 ,出生時間 from xs where 專業(yè)名 =通信工程 order by 出生時間 ; 50、將計算機(jī)專業(yè)學(xué)生按其平均成績排列。 select 學(xué)號,姓名,專業(yè)名 from xs where 專業(yè)名=計算機(jī) ” order by (select avg(成績)from xs_kc group by xs_kc.學(xué)號 having xs.學(xué)號=xs_kc.學(xué)號); 51、 查找xs表中學(xué)號最靠前的5位學(xué)生的信息。 selec

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論