http://kuwalab.hatenablog.jp/entry/20120223/p1
WITH句
from句サブクエリのより良い書き方。
withで定義したサブクエリ名を、withの中で使うことが出来るため、サブクエリの中でサブクエリを使うSQLをネストを深くせずに書くことができる。
WITH dept_costs AS ( SELECT department_name, SUM(salary) dept_total FROM employees e, departments d WHERE e.department_id = d.department_id GROUP BY department_name), avg_cost AS ( SELECT SUM(dept_total)/COUNT(*) avg FROM dept_costs) SELECT * FROM dept_costs WHERE dept_total > (SELECT avg FROM avg_cost) ORDER BY department_name;