Use a derived table as a query source.
Section: Subqueries
Subquery in FROM
sql
sql
SELECT q.department_id, q.avg_salary
FROM (
SELECT department_id, AVG(salary) AS avg_salary
FROM employees
GROUP BY department_id
) q;Explanation
Derived tables let you treat nested query results as temporary relations.
Learn the surrounding workflow
Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.
Related commands
Same sheet · prioritizing Subqueries