Skip to main content
1:1 mock interviews with Sumit, scored on a real hiring rubric.Book a slot →
TOPIC 2 OF 6 · 2 MIN READ

SQL Joins and Common Interview Queries

SQL questions appear in almost every backend interview: joins, GROUP BY with HAVING, subqueries, window functions and the classic "second highest salary". Practise writing them by hand.

What you will learn

  • Use INNER, LEFT, RIGHT, FULL and self joins
  • Aggregate with GROUP BY and filter groups with HAVING
  • Use window functions such as ROW_NUMBER and RANK
  • Know the logical order SQL runs in

Key concepts

INNER vs LEFT JOIN

INNER JOIN keeps only rows that match in both tables. LEFT JOIN keeps every row from the left table and fills NULL where the right side has no match.

WHERE vs HAVING

WHERE filters rows before grouping. HAVING filters groups after aggregation (HAVING COUNT(*) > 5).

Window function

Computes a value across related rows without collapsing them: ROW_NUMBER() OVER (PARTITION BY dept ORDER BY salary DESC).

Logical order

FROM / JOIN → WHERE → GROUP BY → HAVING → SELECT → DISTINCT → ORDER BY → LIMIT. That is why you cannot use a SELECT alias in WHERE.

Step by step

Classic questions

-- 1. Second highest salary
SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees);

-- 2. Top earner per department
SELECT * FROM (
  SELECT e.*, DENSE_RANK() OVER (PARTITION BY dept_id ORDER BY salary DESC) AS r FROM employees e
) t WHERE r = 1;

-- 3. Users who never ordered
SELECT u.* FROM users u LEFT JOIN orders o ON o.user_id = u.id WHERE o.id IS NULL;

-- 4. Duplicate emails
SELECT email, COUNT(*) FROM users GROUP BY email HAVING COUNT(*) > 1;

-- 5. Employees earning more than their manager (self join)
SELECT e.name FROM employees e JOIN employees m ON e.manager_id = m.id WHERE e.salary > m.salary;

-- 6. Running total of revenue by day
SELECT day, revenue, SUM(revenue) OVER (ORDER BY day) AS running_total FROM daily_revenue;

Examples

ROW_NUMBER vs RANK vs DENSE_RANK

salary | ROW_NUMBER | RANK | DENSE_RANK
 900   |     1      |  1   |     1
 900   |     2      |  1   |     1
 800   |     3      |  3   |     2

Practise it

Try these without looking back. If you can explain each one out loud, you are ready for this question.

  • Write a query for the top 3 products by revenue in each category.
  • Find customers whose orders in 2026 total more than 10,000.
  • Solve 10 problems in the JioCoder SQL Lab without looking at hints.

Interview questions

What interviewers ask next on this topic. Answer out loud first, then open the model answer.

What is the difference between DELETE, TRUNCATE and DROP?

DELETE removes rows (optionally with WHERE), is logged per row and fires triggers. TRUNCATE empties the table quickly and resets storage. DROP removes the table itself.

What does COUNT(*) vs COUNT(column) return?

COUNT(*) counts rows. COUNT(column) counts rows where that column is not NULL. COUNT(DISTINCT column) counts unique non-null values.

UNION vs UNION ALL?

UNION removes duplicate rows, which needs a sort or hash. UNION ALL keeps everything and is faster. Use UNION ALL when you know there are no duplicates or you want them.

Summary

Know your joins, WHERE vs HAVING, window functions and the logical execution order, and be able to write the classic queries quickly and correctly.

Stuck on this one? Talk to Sumit.

Keys: F focus mode · ← → previous / next topic · Esc leave focus