Database Interview Questions

Learn the database fundamentals interviewers expect: tables and keys, core SQL statements, filtering and aggregation, joins, constraints, indexes, and transactions with COMMIT and ROLLBACK.

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?

What is the difference between WHERE and HAVING?

What is the difference between DELETE, TRUNCATE and DROP?

What is a transaction, and what do COMMIT and ROLLBACK do?

Ready to test yourself?

Apply what you've read with a 10-question quiz on Database.

Start Database quiz