SQL練習(xí)_6 | 8 | SQLZOO_20191021

本系列刷題筆記主要用以記錄刷SQLZOO的過(guò)程中的思路缴啡、個(gè)人答案以及陌生的或者新的知識(shí)點(diǎn)阶冈。

題目來(lái)源 - SQLZOO
SQLZOO中題目中文版本與英文版本略有差異解藻,題目以英文版為準(zhǔn)

相關(guān)文章
SQL練習(xí)_1 | 0,1,2 | SQLZOO_20191002
SQL練習(xí)_2 | 3 | SQLZOO_20191008
SQL練習(xí)_3 | 4,5 | SQLZOO_20191010
SQL練習(xí)_4 | 6 | SQLZOO_20191012
SQL練習(xí)_5 | 7 | SQLZOO_20191015

目錄
8 Using Null
8+ Numeric Examples

8 Using Null

查詢表格

查詢表格_teacher, dept

1 List the teachers who have NULL for their department.

SELECT name
FROM teacher
WHERE dept IS NULL

2 Note the INNER JOIN misses the teachers with no department and the departments with no teacher.

SELECT teacher.name,
       dept.name
FROM teacher
  INNER JOIN dept ON (teacher.dept = dept.id)

3 Use a different JOIN so that all teachers are listed.

SELECT teacher.name,
       dept.name
FROM teacher
  LEFT JOIN dept ON (teacher.dept = dept.id)

4 Use a different JOIN so that all departments are listed.

SELECT teacher.name,
       dept.name
FROM teacher
  RIGHT JOIN dept ON (teacher.dept = dept.id)

5 Use COALESCE to print the mobile number. Use the number '07986 444 2266' if there is no number given. Show teacher name and mobile number or '07986 444 2266'

SELECT name,
       COALESCE(mobile,'07986 444 2266')
FROM teacher

6 Use the COALESCE function and a LEFT JOIN to print the teacher name and department name. Use the string 'None' where there is no department.

SELECT a.name,
       COALESCE(b.name,'None')
FROM teacher a
  LEFT JOIN dept b ON a.dept = b.id

7 Use COUNT to show the number of teachers and the number of mobile phones.

SELECT COUNT(DISTINCT name),
       COUNT(DISTINCT mobile)
FROM teacher

8 Use COUNT and GROUP BY dept.name to show each department and the number of staff. Use a RIGHT JOIN to ensure that the Engineering department is listed.

SELECT b.name,
       COUNT(DISTINCT a.name)
FROM teacher a
  RIGHT JOIN dept b ON a.dept = b.id
GROUP BY b.name

9 Use CASE to show the name of each teacher followed by 'Sci' if the teacher is in dept 1 or 2 and 'Art' otherwise.

SELECT name,
       CASE
         WHEN dept = '1' OR dept = '2' THEN 'Sci'
         ELSE 'Art'
       END 
FROM teacher

10 Use CASE to show the name of each teacher followed by 'Sci' if the teacher is in dept 1 or 2, show 'Art' if the teacher's dept is 3 and 'None' otherwise.

SELECT name,
       CASE
         WHEN dept = '1' OR dept = '2' THEN 'Sci'
         WHEN dept = '3' THEN 'Art'
         ELSE 'None'
       END 
FROM teacher

8+ Numeric Examples

查詢表格

查詢表格_nss(National Student Survey 2012)

1 Show the the percentage who STRONGLY AGREE

The example shows the number who responded for:
*question 1
*at 'Edinburgh Napier University'
*studying '(8) Computer Science'

SELECT A_STRONGLY_AGREE
FROM nss
WHERE question = 'Q01'
AND   institution = 'Edinburgh Napier University'
AND   subject = '(8) Computer Science'

2 Show the institution and subject where the score is at least 100 for question 15.

SELECT institution,
       subject
FROM nss
WHERE question = 'Q15'
AND   score >= 100

3 Show the institution and score where the score for '(8) Computer Science' is less than 50 for question 'Q15'

SELECT institution,
       score
FROM nss
WHERE question = 'Q15'
AND   score < 50
AND   subject = '(8) Computer Science'

4 Show the subject and total number of students who responded to question 22 for each of the subjects '(8) Computer Science' and '(H) Creative Arts and Design'.

SELECT subject,
       SUM(response)
FROM nss
WHERE question = 'Q22'
AND   (subject = '(H) Creative Arts and Design' OR subject = '(8) Computer Science')
GROUP BY subject

5 Show the subject and total number of students who A_STRONGLY_AGREE to question 22 for each of the subjects '(8) Computer Science' and '(H) Creative Arts and Design'.

SELECT subject,sum(response*(A_STRONGLY_AGREE/100))
  FROM nss
 WHERE question='Q22'
AND (subject='(H) Creative Arts and Design'
   or subject='(8) Computer Science')
group by subject

6 Show the percentage of students who A_STRONGLY_AGREE to question 22 for the subject '(8) Computer Science' show the same figure for the subject '(H) Creative Arts and Design'.

Use the ROUND function to show the percentage without decimal places.

SELECT subject,
       ROUND(SUM(response*A_STRONGLY_AGREE) / SUM(response))
FROM nss
WHERE question = 'Q22'
AND   (subject = '(H) Creative Arts and Design' OR subject = '(8) Computer Science')
GROUP BY subject

7 Show the average scores for question 'Q22' for each institution that include 'Manchester' in the name.

The column score is a percentage - you must use the method outlined above to multiply the percentage by the response and divide by the total response. Give your answer rounded to the nearest whole number.

SELECT institution,
       ROUND(SUM(score*response) / SUM(response))
FROM nss
WHERE question = 'Q22'
AND   institution LIKE '%Manchester%'
GROUP BY institution
ORDER BY institution

8 Show the institution, the total sample size and the number of computing students for institutions in Manchester for 'Q01'.

在where后加 subject = '(8) Computer Science' 約束或者使用子查詢均無(wú)法實(shí)現(xiàn)查處計(jì)算機(jī)學(xué)生數(shù)的要求

SELECT institution,
       SUM(sample),
       SUM(CASE WHEN subject = '(8) Computer Science' THEN sample ELSE NULL END)
FROM nss
WHERE question = 'Q01'
AND   (institution LIKE '%Manchester%')
GROUP BY institution
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末孝治,一起剝皮案震驚了整個(gè)濱河市损痰,隨后出現(xiàn)的幾起案子福侈,更是在濱河造成了極大的恐慌,老刑警劉巖卢未,帶你破解...
    沈念sama閱讀 216,372評(píng)論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件肪凛,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡辽社,警方通過(guò)查閱死者的電腦和手機(jī)伟墙,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)滴铅,“玉大人戳葵,你說(shuō)我怎么就攤上這事『撼祝” “怎么了拱烁?”我有些...
    開(kāi)封第一講書(shū)人閱讀 162,415評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)噩翠。 經(jīng)常有香客問(wèn)我戏自,道長(zhǎng),這世上最難降的妖魔是什么伤锚? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,157評(píng)論 1 292
  • 正文 為了忘掉前任擅笔,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘猛们。我一直安慰自己念脯,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,171評(píng)論 6 388
  • 文/花漫 我一把揭開(kāi)白布阅懦。 她就那樣靜靜地躺著,像睡著了一般徘铝。 火紅的嫁衣襯著肌膚如雪耳胎。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,125評(píng)論 1 297
  • 那天惕它,我揣著相機(jī)與錄音怕午,去河邊找鬼。 笑死淹魄,一個(gè)胖子當(dāng)著我的面吹牛郁惜,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播甲锡,決...
    沈念sama閱讀 40,028評(píng)論 3 417
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼兆蕉,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了缤沦?” 一聲冷哼從身側(cè)響起虎韵,我...
    開(kāi)封第一講書(shū)人閱讀 38,887評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎缸废,沒(méi)想到半個(gè)月后包蓝,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,310評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡企量,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,533評(píng)論 2 332
  • 正文 我和宋清朗相戀三年测萎,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片届巩。...
    茶點(diǎn)故事閱讀 39,690評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡硅瞧,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出恕汇,到底是詐尸還是另有隱情零酪,我是刑警寧澤,帶...
    沈念sama閱讀 35,411評(píng)論 5 343
  • 正文 年R本政府宣布拇勃,位于F島的核電站四苇,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏方咆。R本人自食惡果不足惜月腋,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,004評(píng)論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧榆骚,春花似錦片拍、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,659評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至碉钠,卻和暖如春纲缓,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背喊废。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,812評(píng)論 1 268
  • 我被黑心中介騙來(lái)泰國(guó)打工祝高, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人污筷。 一個(gè)月前我還...
    沈念sama閱讀 47,693評(píng)論 2 368
  • 正文 我出身青樓工闺,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親瓣蛀。 傳聞我的和親對(duì)象是個(gè)殘疾皇子陆蟆,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,577評(píng)論 2 353

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