N061-H2 Tier 5 · Expert · hard hr · Helix Systems

Return each qualifying department's name and its current average salary

Part of Query Structure Patterns for Performance in SQL

The problem

Scenario: Helix Systems' executive committee is identifying departments whose current average salary exceeds $100,000.

Task: Write a query to return each qualifying department's name and its current average salary.

Assumptions:

  • A current salary record has end_date recorded as a missing value.
  • A department's avg_salary is the average of every current salary record across its employees.
  • The result covers only departments whose avg_salary is strictly greater than 100000.

Output:

  • One row per qualifying department.
  • Columns in this order: department_name, avg_salary.
Schema · hr 4 tables
departments
id integer
name text
location text
budget numeric
salaries
id integer
employee_id integer
amount numeric
effective_date date
end_date? date
employees
id integer
name text
email text
department_id integer
manager_id? integer
hire_date date
title text
is_active boolean
job_history
id integer
employee_id integer
title text
department_id integer
start_date date
end_date? date

Run previews · Check grades

Write a query, then run it to see results here.

Worked solution Try it yourself first
Solution query
SELECT
  d.name AS department_name,
  dept_salaries.avg_salary
FROM
  departments d
  JOIN (
    SELECT
      e.department_id,
      AVG(s.amount) AS avg_salary
    FROM
      employees e
      JOIN salaries s ON s.employee_id = e.id
    WHERE
      s.end_date IS NULL
    GROUP BY
      e.department_id
  ) AS dept_salaries ON dept_salaries.department_id = d.id
WHERE
  dept_salaries.avg_salary > 100000

The shape

The threshold check happens at the department level, but the data lives at the salary-record level. A derived table aggregates current salary records up to the department level first, computing each department's average, and the outer query then attaches the department name and applies the > 100000 cut.

Clause by clause

  • The derived table:
SELECT e.department_id, AVG(s.amount) AS avg_salary
FROM employees e
JOIN salaries s ON s.employee_id = e.id
WHERE s.end_date IS NULL
GROUP BY e.department_id

The join pairs each employee with their salary records, WHERE s.end_date IS NULL narrows to current records, and the GROUP BY produces one row per department with the current average already computed. - SELECT d.name AS department_name, dept_salaries.avg_salary returns the department's display name alongside the precomputed average. - FROM departments d JOIN (...) AS dept_salaries ON dept_salaries.department_id = d.id attaches the department name to the aggregated row. Departments with no current salary records on file do not appear in the derived table, so the inner join silently drops them. - WHERE dept_salaries.avg_salary > 100000 applies the threshold after the per-department average has already been computed.

Why filter after aggregation and not before

The threshold is on the aggregate, not on individual salary records. Pushing > 100000 into the inner block would filter individual salaries, then average only the high-paid ones — a different, wrong number. The aggregation has to happen first so the per-department average reflects every current salary record in the department, and the threshold only enters once that average exists.

The trap

The current-salary filter is s.end_date IS NULL, not s.end_date = NULL. The equality form returns no rows silently — = NULL is never true in SQL, not even when the value is null, because null is not a value that equals anything. Writing the filter as = NULL would produce an empty derived table, which would then produce an empty final result, with no error message anywhere along the way. IS NULL is the only spelling that detects the missing-value case.

You practiced precomputing per-department averages in a derived table before threshold-checking against $100,000 — keeping the per-department calculation separate from the threshold cut.

How you actually get good at SQL

Reading explains SQL. Writing it, over and over with instant feedback, is what makes you fluent.

That's the whole SQLMaxx loop: 600+ real problems, instant AI feedback, mastery you can actually see, and spaced review that won't let you forget.

A stack of SQL practice problem cards, the top card showing an employees table.
615 problems · 66 concepts

Real problems. Not toy examples.

615 hand-built problems spanning all 66 concepts, from basic SELECTs to window functions, built on real schemas and real business questions, the kind you'll actually get asked on the job. Enough reps to make SQL automatic.

A retro computer showing a SQL query marked correct with a green checkmark.
Instant AI feedback

Write a query. Know if it's right in one second.

No copying an answer and hoping it clicked. The AI grader checks your real query against real data, catches exactly what's wrong, and explains the fix in plain English, like a senior analyst reading over your shoulder on every problem.

A circular mastery progress dial filling from blue to green, the SQLMaxx diamond at its center.
Mastery tracking

Stop guessing whether you actually know it.

SQLMaxx tracks every concept and shows you what you've mastered and what's still shaky. Your skills fill in one concept at a time, so 'I think I get joins' becomes something you can prove.

A SQL query editor circled by a blue return arrow with a clock, scheduled to come back for review.
Spaced review

Learn it once. Keep it for good.

Most of what you learn this week fades by next week. So when a concept comes due for review, SQLMaxx hands you a fresh problem to solve from a blank editor, not a flashcard to re-read. A research-backed spaced-repetition algorithm (FSRS) times each return for right before you'd forget, so your SQL is still there months later, when the interview or the job actually needs it.

Practice, feedback, mastery, review. That's the loop that turns reading into real skill.

Start free

No account, no credit card. Start solving in under a minute.