SQLZOO: Scottish Parliament/zh & NSS Tutorial

4. 列出政黨名單,當中最少有一名黨員在議會內。

select party.name
from msp join party on msp.party=party.code
group by party.name

因为 msp 是按党员统计的表,所以直接将 msp 和 party 内联接,返回的一定是有党员的政党


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='(8) Computer Science' OR subject= '(H) Creative Arts and Design')
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( A_STRONGLY_AGREE/100*response)
FROM nss
WHERE question='Q22'
AND (subject='(8) Computer Science'
OR subject= '(H) Creative Arts and Design')
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'.

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

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

SELECT institution,round(sum(score/100*response)/sum(response)*100,0)
FROM nss
WHERE question='Q22'
AND (institution LIKE '%Manchester%')
GROUP BY institution

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

SELECT institution,sum(sample),
SUM(CASE WHEN subject='(8) Computer Science' THEN sample ELSE 0 END)
FROM nss
WHERE question='Q01'
AND (institution LIKE '%Manchester%')
GROUP BY institution

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值