Intermediate Interview Questions

Explain the different types of joins.

  • INNER JOIN: only matching rows from both tables.
  • LEFT JOIN: all rows from the left table + matches (NULL where none).
  • RIGHT JOIN: all rows from the right table + matches.
  • FULL OUTER JOIN: all rows from both, matched where possible.
  • CROSS JOIN: Cartesian product, every combination.
  • SELF JOIN: a table joined to itself (e.g. employee and manager).

Table A has 5 rows and table B has 3 rows. How many rows can each join return?

JoinMinimumMaximum
INNER JOIN0 (no matches)15 (every row matches every row)
LEFT JOIN515
RIGHT JOIN315
FULL OUTER JOIN5 (all B rows match some A row)15
CROSS JOIN1515

Tip: Duplicates in the join column multiply rows. This is the trick in most "how many rows" questions.

How do you find rows in one table that have no match in another?

Example

LEFT JOIN + IS NULL (or NOT EXISTS):

SELECT c.CustomerName
FROM Customers c
LEFT JOIN Orders o ON o.CustomerID = c.CustomerID
WHERE o.OrderID IS NULL;

Result: 2 rows

CustomerName
Fjord Supplies
Harbor Lights

What is the difference between a join and a subquery?

A join combines columns from several tables into one result; a subquery computes a value or set used by the outer query. Many subqueries can be rewritten as joins. Modern optimizers often produce the same plan, but joins are usually clearer when you need columns from both tables, and EXISTS subqueries are clearer for "is there any..." tests.

What is a correlated subquery?

A subquery that refers to a column of the outer query, so it runs once per outer row.

Example

Employees paid above their department's average:

SELECT FirstName, Department, Salary FROM Employees e
WHERE Salary > (SELECT AVG(Salary) FROM Employees WHERE Department = e.Department);

Result: 2 rows

FirstNameDepartmentSalary
RahulSales72000
JamesSupport67000

Why can NOT IN return no rows?

If the subquery returns even one NULL, x NOT IN (..., NULL) evaluates to UNKNOWN for every row, so nothing is returned. Use NOT EXISTS or filter NULLs inside the subquery.

Example

Orders.EmployeeID contains a NULL, so this finds nobody:

SELECT FirstName FROM Employees
WHERE EmployeeID NOT IN (SELECT EmployeeID FROM Orders);

Result: 0 rows

FirstName

What is a view? Can you update data through a view?

A view is a saved SELECT that behaves like a virtual table; it stores no data. Simple single-table views without aggregates, DISTINCT or GROUP BY are updatable in most databases; complex ones need INSTEAD OF triggers. A materialized view stores the result and must be refreshed.

What is a CTE and how is it different from a subquery or temp table?

A CTE (WITH name AS (...)) is a named subquery that exists only for one statement. It improves readability, can be referenced multiple times and can be recursive. A temporary table is physically created, lives for the session, can be indexed and reused across statements.

What is an index and how does it improve performance?

An index is a sorted data structure (usually a B-tree) over one or more columns that lets the database locate rows without scanning the whole table. It speeds up WHERE, JOIN and ORDER BY, but slows down writes and uses storage.

Clustered vs non-clustered index?

A clustered index defines the physical order of rows; only one per table (usually the primary key). A non-clustered index is a separate structure with keys and row pointers; a table can have many.

When will a query not use an index?

  • A function or calculation on the indexed column: WHERE YEAR(d) = 2024.
  • Leading wildcard: LIKE '%abc'.
  • Filtering on a non-leading column of a composite index.
  • Implicit type conversion (comparing a VARCHAR column with a number).
  • Low selectivity, where the optimizer decides a scan is cheaper.

Explain 1NF, 2NF and 3NF.

  • 1NF: atomic values, no repeating groups, a primary key.
  • 2NF: 1NF + no partial dependency on part of a composite key.
  • 3NF: 2NF + no transitive dependency (non-key columns depend only on the key).

"Every non-key attribute depends on the key, the whole key, and nothing but the key."