Helix Systems' compensation team wants to flag every salary record that exceeds the company-wide average salary.
Write a query to return the employee ID and salary amount for every above-average salary record.
Assumptions:
- The
salariestable contains every salary record (current and historical). - The company-wide average is computed across every row in the
salariestable. - An employee with multiple salary records may have some above and some below the average.
Output:
- One row per above-average salary record, with columns
employee_idandamount.
Schema · hr 4 tables
Run previews · Check grades
Write a query, then run it to see results here.
Worked solution Try it yourself first
SELECT
employee_id,
amount
FROM
salaries
WHERE
amount > (
SELECT
AVG(amount)
FROM
salaries
) The shape
Same structural pattern as the product-average filter, applied to Helix's salary records. (SELECT AVG(amount) FROM salaries) resolves to the company-wide average, and the outer WHERE keeps only the rows whose amount is strictly above that number.
Clause by clause
FROM salariesis the source: every salary record, including historical ones and multiple records per employee.WHERE amount > (SELECT AVG(amount) FROM salaries)filters row by row. The subquery returns a single number — the average across every row insalaries. Each outer row'samountis compared against that number; rows at or below the average drop.SELECT employee_id, amountreturns the two identifying fields per qualifying record. An employee can appear multiple times in the result if more than one of their salary records sits above the company average — and disappear from rows where an earlier salary was below it. The filter operates per record, not per employee.
Why this and not a literal
A hardcoded threshold would go stale every time a new salary is recorded. The subquery recomputes the average on every run, so "above average" tracks the table as it changes — which is what the compensation team's flagging report needs.
You practiced the same scalar-subquery-in-WHERE shape against a different table. The pattern doesn't care about the domain — wherever the threshold is "the average / max / min of the column being filtered," the scalar subquery is the natural expression.