Scottish Parliament/zh

1.一個成員被工黨逐出黨,現沒屬任何黨。找出他。

select name from msp
where party is null

2.列出每個黨及其領導人。

select name,leader from party

3.列出每個黨及其領導人,這些黨其實是沒有領導人的。

select name,leader from party
where leader is not null

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

select party.name from party join msp on (msp.party=party.code)
 group by party.name having count(msp.name)>0

5.列出議會成員的名單,如有所屬政黨,一同列出。確保 Canavan MSP, Dennis 是在名單中。 按msp.name順序排列。

select msp.name , party.name from msp left join party on (msp.party=party.code)
order by msp.name

6.列出議會中每一政黨的黨員人數。

select party.name,count(msp.name) from msp join party on (msp.party=party.code) 
group by party.name

7.列出每一政黨的議會中黨員人數,包括沒有黨員在議會中的政黨。

select party.name,count(msp.name) from msp right join party on (msp.party=party.code) 
group by party.name

你可能感兴趣的:(sql,SQL)