【SQL練習】SQL Bolt lesson7
lesson7の問題
問題一覧はこちらです。
SQLBolt - Learn SQL - SQL Lesson 7: OUTER JOINs
以下が解答となります。
1.解答
Find the list of all buildings that have employees
従業員がいるすべての建物を一覧化して見つけてください
SELECT * FROM buildings WHERE Building_name IN (SELECT building FROM employees GROUP BY building);
①:employeesテーブルのbuildingをグループ化して建物を特定する
②:①の結果(サブクエリ)とbuildingsテーブルのbuilding_nameが一致するものを取得する
2.解答
Find the list of all buildings and their capacity
すべての建物とその収容能力を一覧化して見つけてください
SELECT * FROM buildings;
3.解答
List all buildings and the distinct employee roles in each building (including empty buildings)
すべての建物と各建物(空の建物を含む)の従業員の役割を一覧化してください
SELECT DISTINCT building_name, capacity, role FROM buildings LEFT JOIN employees ON building_name = building;
①:buildingsテーブルのbuilding_nameとemployeesテーブルのbuildingが一致するものを外部結合する
②:①の結果から必要なカラムを取得し、重複するレコードをまとめる