1.列出學系department是NULL值的老師。 selectnamefrom teacher where dept isnull
1 2 3 4
2.注意INNERJOIN 不理會沒有學系的老師及沒有老師的學系。 SELECT teacher.name, dept.name FROM teacher INNERJOIN dept ON (teacher.dept=dept.id)
1 2 3 4 5
3.使用不同的JOIN(外連接),來列出全部老師。 select teacher.name, casewhen dept.name isnullthennull else dept.name end from teacher left join dept on (teacher.dept=dept.id)
1 2 3
4.使用不同的JOIN(外連接),來列出全部學系。 selectcasewhen teacher.name isnullthennullelse teacher.name end,dept.name from dept left join teacher on (teacher.dept=dept.id)
1 2 3 4
5.Use COALESCE to print the mobile number. Use the number '07986 444 2266'if there isno number given. Show teacher nameand mobile number or'07986 444 2266'
selectname,coalesce(mobile,'07986 444 2266') from teacher
1 2 3 4
6.Use the COALESCE functionand a LEFT JOINto print the teacher nameand department name. Use the string 'None'where there isno department.
select teacher.name,coalesce(dept.name,'None') from teacher left join dept on (teacher.dept=dept.id)
1 2
7.使用COUNT來數算老師和流動電話數目。 selectcount(name),count(mobile) from teacher
1 2 3 4
8.使用COUNT 和 GROUP BY dept.name來顯示每一學系的老師數目。 使用 RIGHT JOIN 以確保工程系Engineering 是在當中。 select dept.name,count(teacher.id) from teacher right join dept on (teacher.dept=dept.id) group by dept.name
1 2 3 4 5 6 7
9.Use CASEtoshow the nameofeach teacher followed by'Sci'if the teacher isin dept 1or2and'Art' otherwise.
select teacher.name, casewhen dept.id = 1or dept.id = 2then'Sci' else'Art' end from teacher left join dept on (teacher.dept=dept.id)
1 2 3 4 5 6 7 8
10.Use CASEtoshow the nameofeach teacher followed by'Sci'if the teacher isin dept 1or2, show'Art'if the teacher's dept is 3 and 'None' otherwise. select teacher.name, case when dept.id = 1 or dept.id = 2 then 'Sci' when dept.id = 3 then 'Art' else 'None' end from teacher left join dept on (teacher.dept=dept.id)
Scottish Parliament/zh
1 2 3
1.一個成員被工黨逐出黨,現沒屬任何黨。找出他。 selectnamefrom msp where party isnull
1 2
2.列出每個黨及其領導人。 selectname,leaderfrom party
1 2 3
3.列出每個黨及其領導人,這些黨其實是沒有領導人的。 selectname,leader from party where leader isnotnull
1 2 3 4 5
4.列出政黨名單,當中最少有一名黨員在議會內。 select party.name from party left join msp on (msp.party=party.code) group by party.code,party.name having count(msp.name) >= 1
1 2 3 4
5.列出議會成員的名單,如有所屬政黨,一同列出。確保 Canavan MSP, Dennis 是在名單中。 按msp.name順序排列。 select msp.name,party.name from msp left join party on (msp.party=party.code) orderby msp.name
1 2 3 4
6.列出議會中每一政黨的黨員人數。 select party.name,count(msp.name) from party inner join msp on (msp.party=party.code) group by party.code,party.name
1 2 3 4
7.列出每一政黨的議會中黨員人數,包括沒有黨員在議會中的政黨。 select party.name,count(msp.name) from party left join msp on (msp.party=party.code) group by party.code,party.name
Quiz
1.Select the code which uses an outer join correctly. Answer:
1
SELECT teacher.name, dept.name FROM teacher LEFTOUTERJOIN dept ON (teacher.dept = dept.id)
2.Select the correct statement that shows the name of department which employs Cutflower Answer:
1
SELECT dept.name FROM teacher JOIN dept ON (dept.id = teacher.dept) WHERE teacher.name = 'Cutflower'
3.Select out of following the code which uses a JOIN to show a list of all the departments and number of employed teachers Answer:
1
SELECT dept.name, COUNT(teacher.name) FROM teacher RIGHT JOIN dept ON dept.id = teacher.dept GROUP BY dept.name
4.Using SELECT name, dept, COALESCE(dept, 0) AS result FROM teacher on teacher table will: Answer:
1
display 0in result columnforall teachers without department
5.Query:
1 2 3 4 5 6
SELECTname, CASEWHEN phone = 2752THEN'two' WHEN phone = 2753THEN'three' WHEN phone = 2754THEN'four' ENDAS digit FROM teacher
shows following ‘digit’: Answer:
1
'four' for Throd
6.Select the result that would be obtained from the following code:
1 2 3 4 5 6 7 8
SELECTname, CASE WHEN dept IN (1) THEN'Computing' ELSE'Other' END FROM teacher