8 questions

SQL interview questions and answers

Junior questions

  1. 1. What types of JOIN are there, and how do they differ?

    Answer

    INNER JOIN returns only rows that have a match in both tables. LEFT JOIN returns every row of the left table, and where there is no match the right table's columns are filled with NULL. RIGHT JOIN does the same for the right table, and FULL JOIN returns all rows from both sides. CROSS JOIN produces a Cartesian product with no matching condition.

    Follow-up question: How do you find customers who have no orders at all?

    Learn the topic in the course: SQL and PostgreSQL

  2. 2. What are a primary key and a foreign key?

    Answer

    A primary key uniquely identifies a row in a table: its values are unique and cannot be NULL, and a table has only one. A foreign key is a column that references the primary or a unique key of another table. It guarantees integrity: you cannot insert a reference to a row that does not exist. What happens when the parent row is deleted is set with ON DELETE CASCADE, SET NULL or RESTRICT.

    Follow-up question: How does a natural key differ from a surrogate key, and which would you choose?

    Learn the topic in the course: SQL and PostgreSQL

  3. 3. How does GROUP BY work together with aggregate functions?

    Answer

    GROUP BY combines rows with equal values in the given columns into one group. Aggregate functions COUNT, SUM, AVG, MIN, MAX are computed separately for each group. In SELECT you may list only the GROUP BY columns or aggregates. For example, SELECT customer_id, COUNT(*) FROM orders GROUP BY customer_id counts each customer's orders.

    Follow-up question: How does COUNT(*) differ from COUNT(column)?

    Learn the topic in the course: SQL and PostgreSQL

  4. 4. What is the difference between WHERE and HAVING?

    Answer

    WHERE filters individual rows before grouping, so you cannot use aggregate functions in it. HAVING filters groups that are already formed after GROUP BY and works with aggregates. For example, HAVING COUNT(*) > 5 keeps only customers with more than five orders. Conditions on plain columns belong in WHERE, because then fewer rows are processed.

    Follow-up question: In what logical order are the parts of a SELECT query executed?

    Learn the topic in the course: SQL and PostgreSQL

  5. 5. How does NULL work in SQL, and how do you check for it correctly?

    Answer

    NULL means a missing value, not zero and not an empty string. Any comparison with NULL yields UNKNOWN, so col = NULL never matches, you have to write IS NULL or IS NOT NULL. Aggregates such as SUM and AVG ignore NULL. You can substitute a default value with COALESCE.

    Follow-up question: Why can NOT IN with a subquery that returns NULL produce an empty result?

    Learn the topic in the course: SQL and PostgreSQL

Middle questions

  1. 1. What is an index, how does it speed up queries, and what does it cost?

    Answer

    An index is a separate data structure, a B-tree by default in PostgreSQL, that lets the database find rows without a full table scan. It speeds up WHERE, JOIN and ORDER BY on the indexed columns. The cost is that an index takes space and slows down INSERT, UPDATE and DELETE, because it has to be updated too. In a composite index the column order matters, and you check whether an index is used with EXPLAIN ANALYZE.

    Follow-up question: Why might a query with WHERE lower(email) = ... not use a regular index on email?

    Learn the topic in the course: SQL and PostgreSQL

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

    Answer

    A transaction is a group of operations executed as one unit: either all changes are committed with COMMIT, or all are undone with ROLLBACK. Atomicity means exactly this all or nothing behaviour. Consistency guarantees the database moves from one valid state to another while respecting its constraints. Isolation defines how much concurrent transactions see each other's changes, and Durability guarantees that committed data survives a crash.

    Follow-up question: What isolation levels does PostgreSQL have, and which one is the default?

    Learn the topic in the course: SQL and PostgreSQL

  3. 3. What is the N+1 query problem, and how do you fix it?

    Answer

    N+1 happens when code first fetches a list of N records with one query, and then runs another query for each of them to get related data. That is N+1 round trips to the database instead of one or two, and on large lists the delay becomes noticeable. You fix it with a JOIN, a single query with WHERE id IN (...), or eager loading in the ORM. A SQL query log helps you spot the problem.

    Follow-up question: How does this problem show up in GraphQL, and what is DataLoader?

    Learn the topic in the course: SQL and PostgreSQL

Train these questions

Study the cards, answer by text or voice and get an AI grade. Needs a signed in account.

Answer out loud and get graded

Here you get the questions with model answers. In the ShortLesson app you answer yourself, by text or voice, and AI grades the answer against the rubric and asks a follow-up, like a real interviewer.

The app is coming to the stores soon.

In the knowledge base

Reference pages for the technologies on this topic: who maintains them, when to use them and what interviews ask.

Other topics