ろくろ猫のブログ

しがない会社員の備忘log

【SQL練習】SQL Bolt lesson8

lesson8の問題

f:id:rokuroneko:20210731221918j:plain


問題一覧はこちらです。
SQLBolt - Learn SQL - SQL Lesson 8: A short note on NULLs
f:id:rokuroneko:20200211123027p:plain

以下が解答となります。

1.解答

Find the name and role of all employees who have not been assigned to a building

建物に割り当てられていないすべての従業員の名前と役割を見つけてください

f:id:rokuroneko:20200211123041p:plain

SELECT * FROM employees WHERE building IS NULL;


2.解答

Find the names of the buildings that hold no employees

従業員がいない建物の名前を見つけてください

f:id:rokuroneko:20200211123054p:plain

SELECT * FROM buildings WHERE building_name NOT IN 
(SELECT DISTINCT building FROM employees WHERE building IS NOT NULL);

①:employeesテーブルからbuildingに値が存在しているものを取得する
②:①の結果からbuildingsテーブルのbuilding_nameと一致しないものを取得する