ろくろ猫のブログ

しがない会社員の備忘log

【SQL練習】SQL Bolt lesson11

lesson11の問題

f:id:rokuroneko:20210731221918j:plain

問題一覧はこちらです。
SQLBolt - Learn SQL - SQL Lesson 11: Queries with aggregates (Pt. 2)
f:id:rokuroneko:20200226222254p:plain


以下が解答となります。

1.解答

Find the number of Artists in the studio (without a HAVING clause)

スタジオ内のアーティストの数を見つけてください(HAVING句なし)

f:id:rokuroneko:20200226222333p:plain

SELECT count(*) FROM employees WHERE role = 'Artist';

①:役割を条件に検索する
②:①の検索結果のレコード数を取得する

2.解答

Find the number of Employees of each role in the studio

スタジオの各役割の従業員数を見つけてください

f:id:rokuroneko:20200226222347p:plain

SELECT role, count(*) FROM employees GROUP BY role;


3.解答

Find the total number of years employed by all Engineers

すべてのエンジニアが雇用合計年数を調べてください

f:id:rokuroneko:20200226222359p:plain

SELECT building, SUM(years_employed) FROM employees GROUP BY building;

①:役割で集計する
②:①の集計結果から抽出条件を指定する
③:②の取得結果を合算する