Skip to content

Instantly share code, notes, and snippets.

@JunichiIto
Last active February 6, 2017 22:32
Show Gist options
  • Save JunichiIto/690e2a146b7bc1cfd5840a0739525c5d to your computer and use it in GitHub Desktop.
Save JunichiIto/690e2a146b7bc1cfd5840a0739525c5d to your computer and use it in GitHub Desktop.
【SQL腕試し問題】現在の部署に所属する社員の一覧を取得するSQLを書いて下さいの解答例
-- http://qiita.com/jnchito/items/29e22cc5a73da29f65a3
SELECT
e.id
,e.name
,sh.section_name
,to_char(sh.start_date, 'yyyy/mm/dd') as start_date
FROM
employees e
INNER JOIN
section_histories sh
ON sh.employee_id = e.id
WHERE
sh.section_name = 'IT'
AND sh.start_date =
(SELECT MAX(sh2.start_date)
FROM section_histories sh2
WHERE sh.employee_id = sh2.employee_id)
ORDER BY
e.id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment