SQL Interview Questions and Answers

SQL appears in backend, full stack and data interviews at every level. Freshers get joins, keys and the classic "second highest salary" query; mid-level candidates are asked about indexes and transactions; senior candidates are expected to read an execution plan and explain why a query is slow.

Usually asked in backend and full stack interviews, from intern / fresher to senior (5+ years).

Intern / Fresher

  1. Q1Intern / Fresher
    What is the difference between INNER JOIN and LEFT JOIN in SQL?

    INNER JOIN returns only rows that have a match in both tables, while LEFT JOIN returns every row from the left table and fills the right table's columns with NULL when there is no match.

  2. Q2Intern / Fresher
    What is the difference between WHERE and HAVING in SQL?

    WHERE filters individual rows before grouping, while HAVING filters groups after GROUP BY, so only HAVING can use aggregate functions like COUNT or SUM.

  3. Q3Intern / Fresher
    What is the difference between DELETE, TRUNCATE and DROP in SQL?

    DELETE removes selected rows and can be rolled back, TRUNCATE quickly removes all rows and keeps the table structure, and DROP removes the entire table including its structure.

  4. Q4Intern / Fresher
    What is the difference between a primary key and a foreign key?

    A primary key uniquely identifies each row in its own table and cannot be NULL, while a foreign key is a column that references the primary key of another table to link the two.

Junior (0-2 years)

  1. Q1Junior (0-2 years)
    How do you find the second highest salary in SQL?

    Use a subquery that takes the MAX salary below the overall MAX, or use DENSE_RANK() and pick rank 2, which also handles ties and generalises to the Nth highest.

  2. Q2Junior (0-2 years)
    How do you find and delete duplicate rows in SQL?

    Find duplicates with GROUP BY and HAVING COUNT(*) > 1, then delete them by numbering rows within each duplicate group with ROW_NUMBER() and removing every row after the first.

  3. Q3Junior (0-2 years)
    What is the difference between UNION and UNION ALL in SQL?

    UNION combines two result sets and removes duplicate rows, while UNION ALL combines them and keeps every row, which makes it faster.

  4. Q4Junior (0-2 years)
    What is database normalization and what are the normal forms?

    Normalization is organising tables to reduce redundancy and update anomalies: 1NF means atomic values, 2NF removes partial dependencies on part of a composite key, and 3NF removes dependencies between non-key columns.

Mid-level (2-5 years)

  1. Q1Mid-level (2-5 years)
    What are ACID properties in a database?

    ACID stands for Atomicity, Consistency, Isolation and Durability: a transaction either fully succeeds or fully fails, keeps the data valid, does not interfere with other transactions, and survives crashes once committed.

  2. Q2Mid-level (2-5 years)
    What is an index in SQL and how does it improve performance?

    An index is a separate sorted data structure (usually a B-tree) that lets the database find rows without scanning the whole table, at the cost of extra storage and slower writes.

  3. Q3Mid-level (2-5 years)
    What is the difference between a clustered and a non-clustered index?

    A clustered index defines the physical order in which the table's rows are stored, so there can be only one per table, while a non-clustered index is a separate structure that points back to the rows.

Senior (5+ years)

  1. Q1Senior (5+ years)
    What are transaction isolation levels in SQL and what problems do they prevent?

    Isolation levels (Read Uncommitted, Read Committed, Repeatable Read, Serializable) control how much concurrent transactions can see of each other's changes, trading consistency against concurrency to prevent dirty reads, non-repeatable reads and phantom reads.

  2. Q2Senior (5+ years)
    How do you optimize a slow SQL query?

    Read the execution plan with EXPLAIN to find full scans and expensive joins, add or fix indexes for the filter and join columns, select only the columns and rows you need, and rewrite predicates so indexes can be used.