SQL Subqueries SQL Topic exerciseHardVerified answerSQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

Find Salaries Higher Than Any HR Salary

Apply ANY semantics to the HR salary subquery and return the qualifying employees.

  • Subqueries
  • Filtering
  • Sorting

Exercise brief

Understand the request

Compensation benchmarking analyst A benchmark report needs employees paid above at least one salary in HR.

Find every employee (in any department) whose salary exceeds at least one HR (department_id = 20) salary. `> ANY` means `> MIN(...)`. Return employee_id, first_name, last_name, salary — ordered by salary DESC, employee_id.

Return

  • Return employee_id, first_name, last_name, salary in this exact left-to-right order.

Constraints

  • Use > ANY (SELECT ...) on engines that support quantified predicates.
  • On Core SQL, use the equivalent > (SELECT MIN(...)) form.

Data you will use

Review the relevant tables before deciding how to join, filter, or aggregate them.

employees

  • employee_idINTEGER
  • first_nameVARCHAR(50)
  • last_nameVARCHAR(50)
  • salaryINTEGER
  • department_idINTEGER

Hints, when you need them

Open one clue at a time so you still do the reasoning.

Hint 1

`> ANY (subquery)` ≡ `> MIN(subquery)`. The condition holds when ONE element is beaten.

Hint 2

SOME is a synonym for ANY in standard SQL (PostgreSQL, MySQL, SQL Server). SQLite does NOT support ANY/SOME/ALL.

Hint 3

Empty subquery: `> ANY (∅)` is FALSE — no rows qualify.

Verified SQL answer

Attempt the problem first, then compare structure and reasoning—not just syntax.

Reveal solution and explanation
SELECT employee_id, first_name, last_name, salary FROM employees WHERE salary > (SELECT MIN(salary) FROM employees WHERE department_id = 20) ORDER BY salary DESC, employee_id;

Why this works

Quantified comparisons (ANY, SOME, ALL) are an under-used SQL feature. They map directly to logical quantifiers: ∃ (ANY/SOME) and ∀ (ALL). Knowing the MIN/MAX equivalences makes them less mysterious — and lets you write portable SQL on engines (like SQLite) that lack the keywords.

Success check

An employee qualifies when their salary is greater than at least one HR salary.

Expected result

Use this output to verify values, aliases, ordering, and row count.

employee_idfirst_namelast_namesalary
100JohnSmith120000
105EmmaTaylor95000
107GraceWhite90000
101AliceJohnson85000
102BobWilson80000
104DavidBrown70000
109IvyMartinez68000
106FrankGreen65000
103CarolDavis60000

Learn the concepts behind this answer

Strengthen your understanding with these targeted learning topics:

Continue practicing

SQL Practice Online

Open the interactive workspace and practice across SQL topics.