SQL Interview Questions · Question 04

Write a query to find employees earning more than the company average salary?

Interview preparation resource from Gate Smashers.

Interview-ready answer

Compute the company average with a scalar subquery and return employees whose salary exceeds it: SELECT employee_name, salary FROM Employees WHERE salary > ( SELECT AVG(salary) FROM Employees );

Employees (sample)
employee_idemployee_namedepartmentsalarymanager_id
101AaravEngineering95000105
102MeeraEngineering72000105
103KabirSales65000106
104IshaSales92000106
105RohanEngineering90000NULL
106NehaSales88000NULL
SQL querysql
SELECT employee_name, salary
FROM Employees
WHERE salary > (
  SELECT AVG(salary)
  FROM Employees
);
Understand it clearly

Direct answer (query)

Use a scalar subquery to calculate the average salary for the whole table, then filter employees whose salary is greater than that average. The exact query is provided in the code block below.

How it works

The query uses an uncorrelated scalar subquery that returns a single AVG(salary) value for the entire Employees table. The outer WHERE clause compares each employee's salary to that single average value and returns only those exceeding it.

  • Subquery: Returns one AVG(salary) value for the complete table.
  • Outer filter: Compares each employee's salary with the subquery result and keeps those greater than the average.

NULL salary behavior

AVG ignores NULL salaries by default. If your business rules require treating NULL as zero or handling them differently, explicitly apply COALESCE or other logic before averaging; otherwise, do not assume NULL represents zero.

Sample data reference

A sample Employees table is provided below to illustrate the dataset the query runs against. The code block contains the exact SQL statement shown earlier.