Database Interview Questions
Database questions show up in almost every software engineering interview loop, from backend and full-stack roles to data positions. Early rounds focus on fundamentals: how relational data is modelled, how to read and change it with SQL, and how the database keeps that data correct. Interviewers use these questions to check that you can write a working query and explain what it does to the data.
The best preparation is knowing a small set of ideas precisely rather than a long list of keywords vaguely. Know what a primary key guarantees, what a JOIN produces, how WHERE differs from HAVING, and what happens when a transaction rolls back. Most harder questions are built on top of those.
Key concepts
Tables, Rows & Schemas
A relational database organises data into tables. Each row is one record, such as a single user or order, and each column is an attribute with a declared data type (INTEGER, VARCHAR, BOOLEAN) that constrains which values it can hold. The schema describes the whole structure: tables, columns, types, and the relationships between them. SQL databases enforce this schema up front, which is a defining difference from most NoSQL stores.
Keys & Constraints
A primary key uniquely identifies each row: its values must be unique and can never be NULL. It can be a single column, often an auto-increment id, or a composite of several columns whose combination is unique. A foreign key enforces referential integrity by requiring that a value in one table matches a key in another. UNIQUE, NOT NULL and DEFAULT constraints add further rules, so bad data is rejected by the database itself instead of by every application that writes to it.
Reading & Changing Data
Four statements cover most day-to-day work: SELECT reads rows, INSERT adds them, UPDATE changes them, and DELETE removes them. Pair UPDATE and DELETE with a WHERE clause; without one, every row in the table is affected. Know the structural commands too: CREATE TABLE defines a table, ALTER TABLE changes its columns, TRUNCATE empties it but keeps the table, and DROP removes the table entirely.
Filtering, Sorting & NULL
WHERE filters rows by a condition. IN matches any value in a list, BETWEEN tests an inclusive range, and LIKE matches text patterns using the % and _ wildcards. ORDER BY sorts results, ascending by default, LIMIT caps how many rows come back, and DISTINCT removes duplicates. NULL means an unknown or missing value, not zero or an empty string, so test for it with IS NULL rather than = NULL.
Aggregation: GROUP BY & HAVING
Aggregate functions summarise many rows into one value: COUNT(*) counts rows, SUM and AVG total and average a numeric column, and MIN and MAX find the extremes. GROUP BY splits rows into groups that share a value so the aggregate runs once per group, for example total sales per customer. HAVING then filters those groups the way WHERE filters individual rows: WHERE runs before grouping, HAVING after.
Joins & Views
A JOIN combines rows from two or more tables based on a related column, typically a foreign key matching a primary key. It is how data split across tables to avoid duplication gets reassembled for a query. AS gives tables and columns short aliases so join conditions and results stay readable. A view is a saved query, often a join, that behaves like a virtual table without storing any data of its own.
Indexes
An index lets the database find rows by a column's value without scanning the whole table, much like the index at the back of a book. It speeds up queries that filter or sort on that column. The cost is extra storage and slower writes, because every INSERT, UPDATE and DELETE must keep the index up to date, so index the columns your queries actually search on rather than every column.
Transactions & Backups
A transaction groups several statements into one unit of work that either fully completes or has no effect. COMMIT makes its changes permanent and visible to others; ROLLBACK undoes them and returns the data to its prior state. The classic example is a bank transfer, where the debit and the credit must succeed or fail together. Backups are the complementary safety net: they let you restore data after accidental deletion, corruption or hardware failure.
Sample interview questions
What is the difference between a primary key and a foreign key?
Answer: A primary key uniquely identifies each row in its own table: values must be unique and cannot be NULL, and a table has only one. A foreign key is a column in one table that refers to the primary (or unique) key of another, such as orders.customer_id pointing at customers.id. The database then rejects an order for a customer that does not exist, which is called referential integrity.
Why it matters: It is the most common opening database question. It checks that you understand how relational tables connect, which every question about joins and schema design builds on.
What is the difference between WHERE and HAVING?
Answer: WHERE filters individual rows before they are grouped; HAVING filters groups after GROUP BY has run, so it can test aggregates. For example, WHERE status = 'paid' keeps only paid orders, and HAVING COUNT(*) > 5 then keeps only the customers with more than five of them. An aggregate such as COUNT(*) cannot appear in a WHERE clause.
Why it matters: It tests whether you understand the order in which a query is evaluated, not just the syntax. Mixing the two up is one of the most common SQL mistakes.
What is the difference between DELETE, TRUNCATE and DROP?
Answer: DELETE removes rows: the ones matching its WHERE clause, or every row if there is none. TRUNCATE removes all rows at once, much faster than DELETE, but keeps the table and its columns. DROP removes the table itself, structure included, so it no longer exists.
Why it matters: Interviewers want to see that you know which commands change data and which change structure, and that you treat destructive commands with care.
What is a transaction, and what do COMMIT and ROLLBACK do?
Answer: A transaction is a group of statements treated as a single unit of work: either all of them take effect or none do. COMMIT makes the changes permanent and visible to other sessions. ROLLBACK discards every change made since the transaction began. Transferring money between two accounts is the classic case: the debit and the credit must both happen, or neither.
Why it matters: Transactions are the foundation for ACID guarantees and isolation levels, which come up in harder rounds. Getting the basics right sets up that conversation.
Ready to test yourself?
Apply what you've read with a 10-question quiz on Database.
Start Database quiz