Learning SQL: Questions and answers

Table of contents

Introduction

In the real world, interview questions usually share some common ones frequently asked, for FANG companies people usually study a whole year or use books to prepare for anything they could ask and crack those questions open. Here I want to show some common questions asked for senior developers and the logic behind.


1. Advanced SQL Queries

Write a query to find the second highest salary in a table.

SELECT MAX(salary)
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);

How would you find duplicate records in a table?

SELECT column_name, COUNT(*)
FROM table_name
GROUP BY column_name
HAVING COUNT(*) > 1;

This one can be a bit tricky, let’s use an example:

-- Sample Customer Table
CREATE TABLE customers (
    id INT,
    name VARCHAR(50),
    city VARCHAR(50)
);

-- Insert Sample Data
INSERT INTO customers VALUES
(1, 'John Smith', 'New York'),
(2, 'Jane Doe', 'Chicago'),
(3, 'John Smith', 'Boston'),
(4, 'Mike Brown', 'New York'),
(5, 'John Smith', 'Los Angeles'),
(6, 'Jane Doe', 'Miami');

Query Breakdown

SELECT name, COUNT(*)
FROM customers
GROUP BY name
HAVING COUNT(*) > 1;

Expected Result

name         COUNT(*)
-----------------------
John Smith   3
Jane Doe     2

Write a query to find employees who have the same salary.

SELECT e1.name, e1.salary
FROM employees e1
JOIN employees e2 ON e1.salary = e2.salary AND e1.id <> e2.id;

Let’s check this in more detail.

This SQL query finds employees with identical salaries but different employee IDs (essentially, duplicate salary records).

Detailed Breakdown

  1. FROM employees e1:

    • First table alias for employees
    • Starting point of the query
  2. JOIN employees e2:

    • Self-join (joining the table with itself)
    • Allows comparing each employee record with every other record
  3. ON e1.salary = e2.salary:

    • Matches rows with the same salary
    • Finds salary duplicates across different employees
  4. AND e1.id <> e2.id:

    • Ensures the matched rows are from different employees
    • Prevents matching an employee with themselves
    • <> means “not equal to”

Example Scenario

employees table:
ID | Name    | Salary
1  | Alice   | 50000
2  | Bob     | 60000
3  | Charlie | 50000
4  | David   | 70000

Result of Query:

Name    | Salary
Alice   | 50000
Charlie | 50000

Learnings


How would you retrieve the top 5 most recent orders for each customer?

SELECT customer_id, order_id, order_date
FROM (
    SELECT customer_id, order_id, order_date,
           ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date DESC) AS rn
    FROM orders
) AS ranked_orders
WHERE rn <= 5;

As usual, let’s check this one in detail:

Detailed Breakdown

  1. Inner Subquery:

    SELECT customer_id, order_id, order_date,
           ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date DESC) AS rn
    • ROW_NUMBER(): Assigns a unique sequential number to each row
    • OVER (PARTITION BY customer_id): Resets numbering for each customer
    • ORDER BY order_date DESC: Numbers rows from most recent to oldest
    • Creates a new column rn with the row ranking
  2. Outer Query:

    WHERE rn <= 5
    • Filters to keep only the top 5 rows (most recent orders) for each customer

Example Scenario

orders table:
customer_id | order_id | order_date
1           | 101      | 2023-01-01
1           | 102      | 2023-02-15
1           | 103      | 2023-03-20
1           | 104      | 2023-04-10
1           | 105      | 2023-05-05
1           | 106      | 2023-06-01

Result:

customer_id | order_id | order_date
1           | 106      | 2023-06-01
1           | 105      | 2023-05-05
1           | 104      | 2023-04-10
1           | 103      | 2023-03-20
1           | 102      | 2023-02-15

Learnings


2. Database Design

How would you design a database schema for a blog platform?


What is normalization, and why is it important?


When would you denormalize a database?


3. Performance Optimization

How would you optimize a slow SQL query?


What are indexes, and how do they work?


What is query caching, and how does it improve performance?


4. Joins and Relationships

Explain the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN.


How would you handle a many-to-many relationship in SQL?


5. Transactions and Concurrency

What is a transaction, and why is it important?


How would you handle deadlocks in a database?


6. Advanced SQL Features

What are window functions, and how are they used?


7. Real-World Scenarios

How would you handle a database migration?


8. Database-Specific Questions

What are the differences between MySQL and PostgreSQL?


9. Debugging and Troubleshooting

How would you debug a query that returns incorrect results?


10. Soft Skills and Architecture

How do you decide when to use SQL vs. NoSQL?


Conclusion

There are some interesting patterns here you can learn, while there could be more questions involved, these usually be similar to the ones you will be asked in interviews.

See you on the next post.

Sincerely,

Eng. Adrian Beria.