Database · Fresher-relevant
SQL interview questions on JOINs, aggregations, subqueries, indexing, and query optimisation — the most-tested database skill at Indian companies.
What is the difference between INNER JOIN, LEFT JOIN, and FULL OUTER JOIN?
Tip: INNER JOIN returns only matching rows in both tables. LEFT JOIN returns all left rows plus matches (NULLs where none). FULL OUTER JOIN returns all rows from both, matched where possible. Draw it mentally as set intersection vs union.
What is the difference between WHERE and HAVING?
Tip: WHERE filters individual rows before grouping; HAVING filters groups after GROUP BY and can use aggregates like COUNT(*) > 5. You cannot use an aggregate in WHERE. Logical order: FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY.
Write a query to find the Nth highest salary.
Tip: Use DENSE_RANK(): SELECT salary FROM (SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) rnk FROM employees) t WHERE rnk = N. DENSE_RANK handles ties correctly. Alternatively LIMIT 1 OFFSET N-1 on distinct salaries when there are no ties.
What is an index and what are its trade-offs?
Tip: An index (usually a B+ tree) speeds up lookups and range scans by avoiding full table scans, at the cost of extra storage and slower writes (each INSERT/UPDATE maintains it). Index columns used in WHERE/JOIN/ORDER BY; composite-index column order follows the leftmost-prefix rule.
Explain the ACID properties of a transaction.
Tip: Atomicity (all-or-nothing), Consistency (constraints preserved), Isolation (concurrent transactions do not corrupt each other), Durability (committed data survives crashes). Isolation levels (Read Committed, Repeatable Read, Serializable) trade consistency against concurrency.
What is normalisation and when would you denormalise?
Tip: Normalisation (1NF→3NF) removes redundancy and update anomalies by splitting data into related tables. Denormalise (duplicate data, add summary columns) deliberately for read-heavy/reporting workloads to avoid expensive joins — accepting redundancy for query speed.
What are window functions and how do they differ from GROUP BY?
Tip: Window functions (ROW_NUMBER, RANK, SUM() OVER, LAG/LEAD) compute across a set of rows related to the current row without collapsing them — GROUP BY collapses rows into one per group. Use OVER(PARTITION BY ... ORDER BY ...) for running totals, rankings, and period-over-period comparisons.
How do you find and fix a slow query?
Tip: Run EXPLAIN / EXPLAIN ANALYZE to see the plan: look for full table scans, missing indexes, and bad row estimates. Add or fix indexes, avoid functions on indexed columns in WHERE (non-sargable), select only needed columns, and consider rewriting correlated subqueries as joins.
InterviewEra generates role-specific questions using your actual projects and skills. Get scored feedback on technical depth, clarity, and structure — free to start.