Interview Prep

25 SQL Interview Questions and Answers

12 min read · Updated August 13, 2026

Questions grouped from fundamentals to intermediate concepts, roughly in the order they tend to come up. Click any question to expand its answer. Where a query is shown, you can copy it straight into the SQL Playground and run it.

Q1. What is SQL?

SQL (Structured Query Language) is the standard language for creating, querying, and managing data in relational databases. It covers defining schemas (DDL), reading and modifying data (DML), controlling access (DCL), and managing transactions (TCL).

Q2. What is the difference between SQL and MySQL?

SQL is the language itself. MySQL is one specific database management system (like PostgreSQL, SQL Server, or Oracle) that implements SQL, along with its own extensions and tooling.

Q3. What are DDL, DML, DCL, and TCL?

DDL (Data Definition Language) defines structure — CREATE, ALTER, DROP. DML (Data Manipulation Language) works with data — SELECT, INSERT, UPDATE, DELETE. DCL (Data Control Language) manages permissions — GRANT, REVOKE. TCL (Transaction Control Language) manages transactions — COMMIT, ROLLBACK, SAVEPOINT.

Q4. What is a primary key?

A column (or combination of columns) that uniquely identifies each row in a table. It cannot contain NULL values, and a table can have only one primary key.

Q5. What is a foreign key?

A column that references the primary key of another table, used to enforce a relationship between the two — for example, Employees.dept_id referencing Departments.dept_id.

Q6. What is the difference between a primary key and a unique key?

Both enforce uniqueness, but a table can have only one primary key (which also cannot be NULL), while it can have several unique keys, and most databases allow a unique column to contain a single NULL value.

Q7. What is the difference between WHERE and HAVING?

WHERE filters individual rows before grouping and cannot use aggregate functions. HAVING filters groups after GROUP BY has run and can use aggregates like COUNT() or SUM(). See our full GROUP BY vs HAVING guide.

Q8. What is the difference between DELETE, TRUNCATE, and DROP?

DELETE removes rows one at a time, can be filtered with WHERE, and can be rolled back. TRUNCATE removes all rows at once and resets auto-increment counters, but generally can't be filtered. DROP removes the table itself, structure and all.

Q9. What are the types of SQL JOINs?

INNER JOIN (only matching rows), LEFT JOIN (all left rows, matched or NULL), RIGHT JOIN (all right rows, matched or NULL), and FULL OUTER JOIN (all rows from both sides). See the full JOINs guide for worked examples.

Q10. What is a self join?

A join where a table is joined to itself, typically to compare rows within the same table — classic example: matching each employee to their manager, who is also a row in the Employees table.

SELECT e.first_name AS employee, m.first_name AS manager
FROM Employees e
LEFT JOIN Employees m ON m.emp_id = e.manager_id;
Q11. What is normalization?

The process of organizing tables to reduce data redundancy and avoid update anomalies, typically by splitting data into related tables. The common normal forms: 1NF (atomic columns, no repeating groups), 2NF (no partial dependency on part of a composite key), and 3NF (no column depends on a non-key column).

Q12. What is denormalization, and when would you use it?

Deliberately introducing redundancy (e.g. duplicating a column across tables, or pre-computing aggregates) to reduce the number of joins needed for read-heavy workloads, trading some write complexity and storage for faster reads.

Q13. What is an index, and why does it speed up queries?

An index is a separate, sorted data structure (typically a B-tree) that lets the database look up rows matching a condition without scanning the entire table. It speeds up reads on the indexed column(s) at the cost of extra storage and slightly slower writes, since the index must also be updated.

Q14. What is the difference between a clustered and a non-clustered index?

A clustered index determines the physical order rows are stored on disk — a table can have only one. A non-clustered index is a separate structure that points back to the actual rows, and a table can have several.

Q15. When would you use a subquery instead of a JOIN?

When you only need to filter or compare against a value computed from another table, rather than pull columns from it into the result. See the full subqueries guide.

Q16. What is a correlated subquery?

A subquery that references a column from the outer query, so it can't be run independently — it's conceptually re-evaluated once for every row the outer query considers (e.g. an EXISTS check per customer).

Q17. What are aggregate functions?

Functions that compute a single result from a set of rows: COUNT(), SUM(), AVG(), MIN(), and MAX() are the most common, typically used with GROUP BY.

Q18. What is the difference between UNION and UNION ALL?

Both stack the results of two SELECT statements with the same column shape. UNION removes duplicate rows from the combined result (extra processing); UNION ALL keeps every row, including duplicates, and is faster.

Q19. What is a transaction, and what does ACID mean?

A transaction is a group of one or more SQL statements executed as a single unit — either all of them succeed, or none do. ACID: Atomicity (all-or-nothing), Consistency (valid state to valid state), Isolation (concurrent transactions don't interfere), Durability (once committed, changes survive a crash).

Q20. What is the difference between COMMIT and ROLLBACK?

COMMIT permanently saves all changes made in the current transaction. ROLLBACK undoes them, returning the data to how it was before the transaction started.

Q21. What are window functions?

Functions that compute a value across a set of rows related to the current row (a "window"), without collapsing rows into groups the way GROUP BY does — so you keep every original row plus a computed column.

SELECT
    first_name,
    dept_id,
    salary,
    ROW_NUMBER() OVER (PARTITION BY dept_id ORDER BY salary DESC) AS rank_in_dept
FROM Employees;
Q22. What is the difference between RANK() and DENSE_RANK()?

Both assign a rank within a window, with ties getting the same rank. RANK() leaves a gap after ties (1, 2, 2, 4), while DENSE_RANK() doesn't (1, 2, 2, 3).

Q23. What is a view?

A saved SELECT query that behaves like a virtual table — you can query it like a regular table, but it doesn't store data itself (unless it's a materialized view); it just re-runs the underlying query each time.

Q24. How do you find duplicate rows in a table?

Group by the column(s) that should be unique, and filter to groups with more than one row using HAVING:

SELECT email, COUNT(*) AS occurrences
FROM Employees
GROUP BY email
HAVING COUNT(*) > 1;
Q25. What's the difference between a JOIN and a UNION?

A JOIN combines columns from two tables side by side, based on a matching condition, producing wider rows. A UNION stacks the rows of two queries on top of each other, producing more rows — the queries must return the same number and type of columns.

Practice before the interview

The best way to remember these is to run them. Every query above works against the Playground's sample tables.

Open the Playground