All concepts

SELECT & Filtering

Describe which rows and columns you want; SQL filters, sorts, then limits.

SQL · Beginner · ~4 min

In plain English

Ask a table for the rows and columns you want. Everything else in SQL is a refinement of this one sentence.

Why it's worth your time

It's the most-used data skill in any AI role — most of your time is getting the data, not modelling it.

If you remember three things

  • Written SELECT → FROM → WHERE, executed FROM → WHERE → SELECT
  • SELECT * is fine exploring, wrong in production
  • NULL is not a value; it's the absence of one

Overview

SELECT is the core read statement: you declare which columns to return, which table to read, and which rows qualify. Though you write SELECT first, the engine evaluates FROM, then WHERE, then SELECT, then ORDER BY, then LIMIT. Understanding that logical order explains most beginner surprises.

In an interview

A SELECT query names columns, a source table, a WHERE filter, an optional sort, and a row cap. The trick is it doesn't run in written order: the database starts at FROM, filters rows with WHERE, projects columns in SELECT, sorts with ORDER BY, and finally trims with LIMIT.

Production defaults

Name your columns
explicitly in anything that ships. SELECT * breaks when the schema changes
Always LIMIT
while exploring. A missing LIMIT on a large table is how you page the on-call engineer
NULL
use IS NULL, never = NULL. The latter is never true, and never errors either

What breaks

  • Filter silently drops rows — NULLs. WHERE col != 'x' excludes rows where col IS NULL — add OR col IS NULL if you meant to keep them.
  • Query is slow on a big table — Full scan. Check for an index on the filtered columns before rewriting the query.

Watch it explained

SQL Explained in 100 Seconds — Fireship, 2:22

Related