Example 1
Find employees who earn above the company average
The inner query computes the average salary (72500). The outer query then filters to only the rows where salary exceeds that value. Alice (70000) and Bob (50000) fall below the average and are excluded.
CREATE TABLE employees (id INT, name VARCHAR(50), salary INT);
INSERT INTO
employees (id, name, salary)
VALUES
(1, 'Alice', 70000),
(2, 'Bob', 50000),
(3, 'Carol', 90000),
(4, 'Dave', 80000);SELECT
name,
salary
FROM
employees
WHERE
salary > (
SELECT
AVG(salary)
FROM
employees
)
ORDER BY
salary DESC;| name | salary |
|---|---|
| Carol | 90000 |
| Dave | 80000 |
Output is identical across all engines.