SQL Basics

SELECT, WHERE, ORDER BY, NULL and CASE WHEN

40 min · interactive lesson with runnable code and 6 graded exercises.

What you'll be able to do

  • Read and write the core query shape in the order SQL runs it
  • Filter with WHERE, IN, BETWEEN, LIKE
  • Handle NULL correctly with IS NULL and COALESCE
  • Bucket and label rows with CASE WHEN, and clean messy text

The shape of a query

Written order vs run order

Every analytics query fits one shape. You write it top to bottom, but the database runs it in a different order — which explains most beginner errors (like using a column alias in WHERE).

Filtering rows

WHERE, AND/OR, IN, BETWEEN, LIKE

Sorting and limiting

ORDER BY, LIMIT, tie-breakers

NULL means unknown

IS NULL, COALESCE, and three-valued logic

NULL is not zero or an empty string — it is unknown. Any comparison with NULL (=, <>, >) gives unknown, and WHERE keeps only rows where the condition is true.

CASE WHEN

Buckets and labels

CASE returns the value for the first condition that is true — order your branches from most specific to least.

Cleaning text

TRIM, LOWER, REPLACE

Real data is messy. Our city column has a few values with stray spaces or lowercase letters — so the same city shows up as different groups.