Interview Q&A / SQL & DBMS

INTERVIEW Q&A · 69 QUESTIONS

SQL & DBMS interview questions

Joins, keys, normalisation, indexes and transactions for placement and backend interviews.

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. What are insertion, update and deletion anomalies?

    They are problems in a badly designed table where redundant data forces you to add dummy values, edit many rows for one change, or lose unrelated facts when deleting a row. Normalization removes them.

    BeginnerQuick answer1 min read
  7. 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
  8. 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
  9. 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
  10. 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
  11. What does a table need to be in First Normal Form, and what breaks it?

    A table is in 1NF when every column holds a single atomic value, there are no repeating groups, and each row is unique. Storing a comma-separated list of values in one cell breaks 1NF.

    BeginnerQuick answer1 min read
  12. 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
  13. 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
  14. What is a composite key and when do you need one?

    A composite key is a key made of two or more columns that together identify a row. It is needed when no single column is unique, such as a student and subject pair in a marks table.

    BeginnerQuick answer1 min read
  15. 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
  16. 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
  17. What is a functional dependency in DBMS?

    A functional dependency X -> Y means that if two rows have the same X value they must have the same Y value, so X determines Y. Normalization is built on finding and organizing these dependencies.

    BeginnerQuick answer1 min read
  18. 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
  19. 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
  20. 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
  21. 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
  22. What is the difference between a super key, a candidate key and a primary key?

    A super key is any set of columns that uniquely identifies a row. A candidate key is a minimal super key with no removable column. The primary key is the one candidate key you choose to identify rows.

    BeginnerQuick answer1 min read
  23. 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
  24. 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
  25. 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
  26. 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
  27. 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
  28. 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
  29. Can a foreign key be NULL, and can it reference the same table?

    Yes to both. A nullable foreign key means the relationship is optional for that row. A foreign key can also reference the same table's primary key, which is how hierarchies like employee and manager are stored.

    IntermediateQuick answer1 min read
  30. Can you INSERT, UPDATE or DELETE through a view?

    Only if the view is simple, based on one table without DISTINCT, GROUP BY, aggregates or joins that make the target row ambiguous. Otherwise the database cannot map the change to base rows. WITH CHECK OPTION blocks rows leaving the view.

    IntermediateQuick answer1 min read