Skip to content
Tech Interview Prep
Technical interview guide

SQL Fundamentals

SELECT, WHERE, and JOIN: retrieving and combining rows from relational tables.

Minutes to read
3
Practice MCQs
25
Interview Q&A
25
Edition
v4
Editorial status
Reviewed

Scope: SQL principles with PostgreSQL 18 examples; vendor-specific behavior must be verified.

Overview

Curated: · Written: · Reviewed:

SQL Fundamentals

In one line

SQL describes the set of rows you want and the planner chooses how to produce it. Reach for SELECT, WHERE and JOIN whenever the answer is rows from one or more relational tables, and reason about the result as a set rather than a loop.

Key definitions

  • Logical processing order: FROM and JOIN, then WHERE, GROUP BY, HAVING, SELECT, DISTINCT, ORDER BY, LIMIT/OFFSET. Aliases from SELECT are not visible to WHERE.
  • Three-valued logic: any comparison with NULL yields UNKNOWN, and WHERE keeps only TRUE rows.
  • Fan-out: a join to a many-side table repeats each left row, inflating any SUM computed afterwards.
  • Semi-join and anti-join: EXISTS and NOT EXISTS test existence without adding columns.
  • Sargable predicate: a WHERE condition an index can serve, such as created_at >= '2024-06-01', not date(created_at) = ....
  • Keyset pagination: fetch the next page from the last seen sort key and id.

How it works

  • FROM and JOIN build the row set first. The ON clause decides what counts as a match.
  • WHERE filters individual rows before any grouping, so row-level filters belong there.
  • GROUP BY collapses rows, then HAVING filters the groups, so aggregate conditions belong in HAVING.
  • SELECT, DISTINCT, ORDER BY and LIMIT run last. Only ORDER BY defines row order.
FROM and JOIN WHERE GROUP BY HAVING SELECT and window functions DISTINCT ORDER BY LIMIT and OFFSET

When to use and when not to

NeedUseAvoid
Keep unmatched left rowsLEFT JOIN, right-table predicate in ONSame predicate in WHERE, which drops NULL-extended rows
Test existenceEXISTS or NOT EXISTSNOT IN over a nullable subquery
Combine disjoint sourcesUNION ALLUNION, which adds a sort or hash for duplicates that cannot exist
Aggregate thresholdHAVING on the aggregateAggregate inside WHERE, which is an error
Several counts in one passCOUNT(*) FILTER (WHERE ...)Repeated self joins
Deep paging on changing dataKeyset paginationOFFSET, which walks every skipped row
Moneynumericdouble precision
Instants in timetimestamptztimestamp without time zone

Traps interviewers probe

  1. Filtering a LEFT JOIN in WHERE. The predicate discards the NULL-extended rows and the join behaves as an INNER JOIN. Fix: put the right-table condition in ON. For MCQ 4 that keeps all 4,120 orders.
  2. NOT IN against a subquery that returns NULL. x NOT IN (1, 2, NULL) is never TRUE, so the query returns zero rows silently. Fix: use NOT EXISTS.
  3. Fan-out inflating a total. Adding a one-to-many join multiplies each parent row, so SUM doubles. Fix: aggregate the child table first, or use EXISTS when you only test for existence.
  4. Equality with NULL. WHERE salary = NULL returns no rows. Fix: IS NULL, or IS DISTINCT FROM when NULL should compare as a value.
  5. Sanitising input instead of parameterising. Stripping quotes does not help when the value is numeric: 1 UNION SELECT id, balance FROM accounts passes straight into the concatenated text and returns account balances. Fix: send the query text and values separately as bound parameters.

Interview tips

Opening. Say what the result is as a set, then state which rows each join keeps and where each filter runs. That answers most MCQ options before you reach them.

Follow-ups to expect.

  • Why does a predicate in ON differ from one in WHERE for a LEFT JOIN?
  • Why does a join on a shared placeholder value explode? A join of 4,000 rows to 500 rows on one shared value emits 4,000 × 500 = 2,000,000 rows.
  • Why is OFFSET slow at depth, and how does keyset fix it?
  • How does a recursive CTE stop on a cycle? A UNION does not stop it when a depth counter differs on every row. Use a depth cap, or the CYCLE clause in PostgreSQL 14 and later.
  • Why is a missing ORDER BY a bug even when the results look stable?

30-second outline. Rows come from FROM and the joins, and ON decides matches. WHERE filters rows and HAVING filters groups. NULL is unknown, so test it with IS NULL and prefer NOT EXISTS over NOT IN. Order is defined only by ORDER BY. Use parameterised queries for input, and keyset pagination for deep lists.

Feedback on this overview

Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.