Interview Q&A / SQL & DBMS
SQL & DBMS interview questions
Joins, keys, normalisation, indexes and transactions for placement and backend interviews.
Prefer rapid revision? Practise SQL & DBMS short interview answers →
- What are the ACID properties of a transaction?
ACID stands for Atomicity, Consistency, Isolation and Durability. A transaction either fully happens or not at all, leaves the data valid according to its rules, behaves as if it ran without interference from other transactions, and stays saved once committed, even if the server crashes.
BeginnerIn-depth3 min read - What is the difference between a primary key, a unique key and a foreign key?
A primary key uniquely identifies each row and cannot be NULL; a table has only one. A unique key also prevents duplicates, but a table can have many and they usually allow NULL. A foreign key is a column that must match a primary or unique key in another table, which keeps related data consistent.
BeginnerIn-depth3 min read - What is the difference between INNER JOIN and LEFT JOIN?
INNER JOIN returns only rows that have a match in both tables. LEFT JOIN returns every row from the left table, and fills the right table's columns with NULL when there is no match.
BeginnerIn-depth2 min read - What is the difference between WHERE and HAVING?
WHERE filters individual rows before they are grouped, so it cannot use aggregate functions like SUM or COUNT. HAVING filters groups after GROUP BY has combined the rows, so it is where conditions on aggregates go.
BeginnerIn-depth3 min read - Write an SQL query to find the second highest salary.
Find the highest salary that is less than the maximum, with SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees). For the Nth highest, use DENSE_RANK() so that duplicate salaries share a rank, or ORDER BY DISTINCT salary with LIMIT and OFFSET.
BeginnerIn-depthCoding3 min read - How do the LIKE wildcards % and _ work in SQL?
In LIKE, % matches any number of characters including none, and _ matches exactly one character. Use NOT LIKE to exclude, and ESCAPE to search for a literal % or _.
BeginnerQuick answer1 min read - How do you fetch only the first N rows in MySQL, SQL Server and Oracle?
MySQL and PostgreSQL use LIMIT n, SQL Server uses SELECT TOP n, and the SQL standard (Oracle 12c and later, PostgreSQL, SQL Server) uses FETCH FIRST n ROWS ONLY. Always pair it with ORDER BY.
BeginnerQuick answer1 min read - How does GROUP BY work with more than one column?
GROUP BY with several columns makes one group for each unique combination of those column values, and aggregates are computed inside each combination.
BeginnerQuick answer1 min read - How is a window function different from GROUP BY?
GROUP BY collapses rows into one row per group. A window function computes an aggregate or rank over a set of related rows but keeps every original row in the output.
BeginnerQuick answer1 min read - In what order does SQL actually execute the clauses of a SELECT query?
The logical order is FROM and JOINs, WHERE, GROUP BY, HAVING, SELECT, DISTINCT, ORDER BY, then LIMIT. It differs from the written order, which is why a SELECT alias works in ORDER BY but not in WHERE.
BeginnerQuick answer1 min read - What are the advantages and disadvantages of stored procedures?
Advantages are less network traffic, precompiled plans, central business rules and access control. Disadvantages are harder version control, testing and debugging, database-specific syntax and logic split between app and database.
BeginnerQuick answer1 min read - What are the disadvantages of having too many indexes?
Every index uses disk and memory, and must be updated on each INSERT, UPDATE and DELETE, which slows writes. Extra indexes can also confuse the optimizer and slow maintenance such as bulk loads and backups.
BeginnerQuick answer1 min read - What do COMMIT, ROLLBACK and SAVEPOINT do?
COMMIT makes a transaction's changes permanent. ROLLBACK undoes everything since the transaction began. SAVEPOINT marks a point inside a transaction so you can roll back only part of it.
BeginnerQuick answer1 min read - What do the NOT NULL, DEFAULT and CHECK constraints do?
NOT NULL forbids missing values, DEFAULT supplies a value when none is given on insert, and CHECK rejects rows whose values fail a condition. Together they keep bad data out at the database level.
BeginnerQuick answer1 min read - What does SELECT DISTINCT do when you list more than one column?
DISTINCT removes duplicate rows, not duplicate values in one column. Two rows are duplicates only if every selected column is equal, and NULLs are treated as equal to each other.
BeginnerQuick answer1 min read - What happens when a subquery used with = returns more than one row?
The database raises an error such as "subquery returns more than 1 row", because = expects a single value. Use IN, ANY or ALL for multiple values, or make the subquery return one row with an aggregate or LIMIT.
BeginnerQuick answer1 min read - What is a CROSS JOIN and when would you use it?
A CROSS JOIN pairs every row of one table with every row of the other, giving rows1 x rows2 results. It is useful for generating combinations, such as every size with every colour.
BeginnerQuick answer1 min read - What is a derived table (subquery in FROM)?
A derived table is a subquery in the FROM clause that acts like a temporary table for that one statement. It must have an alias, and it is useful for filtering or aggregating before joining.
BeginnerQuick answer1 min read - What is a self join and when do you need one?
A self join joins a table to itself using two different aliases. It is used when rows relate to other rows in the same table, such as an employee and their manager.
BeginnerQuick answer1 min read - What is an index in a database and how does it speed up queries?
An index is a separate sorted structure, usually a B-tree, that maps column values to row locations. It lets the database jump to matching rows instead of scanning the whole table, at the cost of extra storage and slower writes.
BeginnerQuick answer1 min read - What is autocommit and how does it affect transactions?
In autocommit mode every SQL statement is its own transaction and is committed as soon as it succeeds. To group statements into one atomic unit, start an explicit transaction with BEGIN or turn autocommit off.
BeginnerQuick answer1 min read - What is SQL injection and how do you prevent it?
SQL injection is when user input is concatenated into a query and changes its meaning. Prevent it with parameterized queries or prepared statements, input validation and least-privilege database accounts.
BeginnerQuick answer1 min read - What is the difference between a unique index and a primary key?
Both enforce uniqueness and create an index. A primary key also forbids NULL, there is only one per table, and it identifies each row. A table can have many unique indexes, and they usually allow NULLs.
BeginnerQuick answer1 min read - What is the difference between COUNT(*), COUNT(column) and COUNT(DISTINCT column)?
COUNT(*) counts all rows, COUNT(column) counts rows where that column is not NULL, and COUNT(DISTINCT column) counts the different non-NULL values.
BeginnerQuick answer1 min read - What is the difference between LEFT JOIN and RIGHT JOIN?
LEFT JOIN keeps every row of the left table and fills NULLs where the right has no match. RIGHT JOIN does the opposite. They are mirror images, and any RIGHT JOIN can be rewritten as a LEFT JOIN by swapping the tables.
BeginnerQuick answer1 min read - Why can't you use a column alias in the WHERE clause?
WHERE runs before SELECT, so the alias does not exist yet when the filter is evaluated. Repeat the expression, or put the query in a subquery or CTE and filter outside.
BeginnerQuick answer1 min read - Why does SELECT with GROUP BY fail when you pick a column that is not grouped?
After grouping, each group becomes one output row, so a column that is neither in GROUP BY nor inside an aggregate has many possible values and the database cannot pick one. Group by it or aggregate it.
BeginnerQuick answer1 min read - Write a query to list departments that have more than 5 employees.
Group by department and filter groups with HAVING COUNT(*) > 5. WHERE cannot be used because the count exists only after grouping.
BeginnerQuick answerCoding1 min read