TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst · 76.7K subscribers
Post #2253 2.87K
✅ SQL CTEs and Subqueries Interview Questions with Answers 🖥️

1. Find employees who earn more than the average salary.

Table: employees (employee_id, name, salary)
SELECT name, salary 
FROM employees
WHERE salary > (
SELECT AVG(salary)
FROM employees
);

Logic: Inner query calculates average salary. Outer query filters employees above average.

2. Find employees who earn the highest salary in each department.

Table: employees (employee_id, name, department, salary)
SELECT name, department, salary 
FROM employees e
WHERE salary = (
SELECT MAX(salary)
FROM employees
WHERE department = e.department
);

Logic: Subquery runs per department. Matches max salary inside same department.

3. Find departments where average salary is greater than 60,000.
SELECT department 
FROM (
SELECT department, AVG(salary) AS avg_salary
FROM employees
GROUP BY department
) t
WHERE avg_salary > 60000;

Logic: Inner query calculates department average. Outer query filters required departments.

4. Same query using CTE.

WITH dept_avg AS ( 
SELECT department, AVG(salary) AS avg_salary
FROM employees
GROUP BY department
)
SELECT department
FROM dept_avg
WHERE avg_salary > 60000;

Logic: CTE stores aggregated result. Final query reads from CTE.

5. Find customers who placed more orders than the average number of orders.

Tables: customers (customer_id, name), orders (order_id, customer_id)

SELECT customer_id 
FROM orders
GROUP BY customer_id
HAVING COUNT(order_id) > (
SELECT AVG(order_count)
FROM (
SELECT COUNT(order_id) AS order_count
FROM orders
GROUP BY customer_id
) x
);

Logic: Inner query calculates orders per customer. Next level gets average. HAVING filters customers above average.

6. Find top-selling product by total sales amount.
Table: sales (product_id, amount)

SELECT product_id
FROM sales
GROUP BY product_id
HAVING SUM(amount) = (
SELECT MAX(total_sales)
FROM (
SELECT SUM(amount) AS total_sales
FROM sales
GROUP BY product_id
) t
);

Logic: Inner query calculates sales per product. Outer query finds max and matches it.

7. Rewrite using CTE.

WITH product_sales AS (
SELECT product_id, SUM(amount) AS total_sales
FROM sales
GROUP BY product_id
)
SELECT product_id
FROM product_sales
WHERE total_sales = (
SELECT MAX(total_sales)
FROM product_sales
);

Logic: CTE avoids repeating aggregation. Cleaner and readable.

8. Find employees whose salary is greater than their department average.

WITH dept_avg AS (
SELECT department, AVG(salary) AS avg_salary
FROM employees
GROUP BY department
)
SELECT e.name, e.salary
FROM employees e
JOIN dept_avg d ON e.department = d.department
WHERE e.salary > d.avg_salary;

Logic: First compute department averages. Join back to employees. Filter higher earners.

Double Tap ♥️ For More
  • ❤ 2
More from @sqlanalyst
  1. Oct 13, 2026SQL Interview Series — Part 8 📌 Question 8: Find Customers Who Have Never Placed an Order…
  2. Oct 13, 2026𝗧𝗢𝗣 𝗙𝗥𝗘𝗘 𝗖𝗢𝗨𝗥𝗦𝗘𝗦 𝗧𝗢 𝗕𝗢𝗢𝗦𝗧 𝗬𝗢𝗨𝗥 𝗦𝗞𝗜𝗟𝗟𝗦 𝗜𝗡 𝟮𝟬𝟮𝟲! Explor…
  3. Oct 12, 2026SQL Interview Series — Part 6 📌 Question 6: Find the Top 2 Highest-Paid Employees in Each…
  4. Oct 12, 2026🚀 𝗪𝗵𝗶𝗰𝗵 𝗧𝗲𝗰𝗵 𝗦𝗸𝗶𝗹𝗹 𝗪𝗶𝗹𝗹 𝗬𝗼𝘂 𝗠𝗮𝘀𝘁𝗲𝗿 𝗶𝗻 𝟮𝟬𝟮𝟲? Want to buil…
  5. Oct 9, 2026SQL Interview Series — Part 5 📌 Question 5: Find Employees Who Earn More Than Their Manag…
  6. Oct 9, 2026🇮🇳 𝗚𝗢𝗩𝗘𝗥𝗡𝗠𝗘𝗡𝗧 𝗢𝗙 𝗜𝗡𝗗𝗜𝗔 — 𝗔𝗜𝗖𝗧𝗘 𝗜𝗡𝗧𝗘𝗥𝗡𝗦𝗛𝗜𝗣𝗦 𝟮𝟬𝟮𝟲 🚀…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →