Back to Ghost in the Repo

SQL for
interviews.

For your first SQL interview. No SQL needed to start.
SQL for Interviews cover

Learn to answer out loud, not just to write the query.

Each chapter takes a question interviewers ask and works it through on one small shop database you can load yourself: what to ask first, the simple version, the complete query with its real output, the trap, and the follow-ups to expect. Every complete query was run on PostgreSQL 18, MySQL 8.4 and SQLite 3.51, and where they disagree, the book shows you.

PDF, 88 pages. Free with Google sign-in.ZIP with the shop database and the settings for SQLite. Chapter 00 shows you how to set them up.

From a correct query
to a good answer.

Interviewers judge how you think as well as what you type, so every chapter uses the same four steps.

  1. Say it back.

    Repeat the question in your own words, so a misunderstanding shows up early.

  2. Ask about the data.

    Can this column be NULL? Can two rows tie? Must empty groups appear?

  3. Start simple.

    Get the rows right, then add one part at a time, so the interviewer can see each step work.

  4. Test the edge.

    Think through the NULL, the tie and the empty group before you say you are done.

What’s inside.

Each chapter ends with exercises and worked solutions you can check by hand.

  1. Set up the shop database

    Install SQLite, load the practice data, and learn the four steps of a good answer.

  2. SELECT, WHERE and ORDER BY

    Show the three highest-paid employees, and the NULL sort that changes the answer by database.

  3. NULL, the trap in every interview

    Why a “not equal” test loses rows, and why COUNT(*) and COUNT(column) can differ.

  4. GROUP BY and HAVING

    Find departments with an average salary above 90,000, and when to use WHERE instead.

  5. Joins

    INNER, LEFT, RIGHT and FULL joins, and two traps that give a wrong count with no error.

  6. Finding what is missing

    Find customers who never ordered, three ways, and why NOT IN can return nothing.

  7. Self joins

    Find employees who earn more than their manager, by joining a table to itself.

  8. Subqueries and CTEs

    Find customers who spent more than the average customer, with and without a named step.

  9. Second highest salary

    The classic question, three ways, extended to the Nth highest, with ties and empty results.

  10. ROW_NUMBER, RANK and DENSE_RANK

    What a window function is, and how the three differ on tied salaries.

  11. Top N and latest row per group

    Top two earners in each department and each customer’s latest order, with PARTITION BY.

  12. Running totals, LAG and LEAD

    Monthly revenue, the change from the month before, and the running total.

  13. Duplicates

    Find repeated emails, then remove the extras without losing any orders.

  14. Dates, strings and CASE

    A monthly report with one column per status, and where date and text functions differ.

  15. Gaps and islands

    Find customers who ordered on at least three days in a row.

  16. Keys, constraints and normalization

    Primary, unique and foreign keys, and 1NF, 2NF and 3NF in words an interviewer accepts.

  17. Indexes and transactions

    Fix a slow query and prove the fix worked, then ACID and isolation levels.

  18. Mock interview

    Twenty mixed questions with no chapter title to help, and a theory sheet to read before the real thing.