Free courses in AI, Data Science & Machine Learning Written lessons · Quizzes · Final exams
Future Data Academy
Log inGet started ↗
← SQL learning path
Beginner · SQLITE

NULLs and expressions

Keep unknown separate from zero.

Understand the concept

NULL means unknown or absent. It is not zero or an empty string. Use IS NULL, not = NULL. Arithmetic involving NULL normally stays NULL, preventing unknown quantities from silently becoming measured values.

Apply the method

COALESCE chooses the first non-NULL argument. It is useful for a display label or a deliberately defined zero for no matching sales. Replacing an unknown amount with zero before computing an average changes the metric. State that assumption.

Avoid a common mistake

CASE evaluates branches in order. Put the NULL case before numeric bands. Use decimal operands for fractional division and round the final output rather than every intermediate amount unless the business rule requires it.

Worked query

Return id for orders with an unknown amount, ordered by id.

SELECT id FROM orders WHERE amount IS NULL ORDER BY id;
Run and explore this example →

Check your understanding

Before running your query, predict the output columns and approximate row count. After running it, inspect a missing value, a tie and a record with no match when available. Explain why the result answers the question.

Practice this topic

  1. Missing amounts — Return id for orders with an unknown amount, ordered by id.
  2. Known salaries — Return name for employees with known salaries, ordered by id.
  3. Display missing values — Return id and display_amount, replacing NULL amount with 0, ordered by id. This is a display convention.
  4. Inventory value — Return name and stock_value (price times stock), ordered by id.
  5. Monthly salary — Return name and monthly_salary rounded to two decimals (annual salary / 12.0), ordered by id. Preserve NULL.
  6. Order size bands — Return id and band: unknown for NULL, large for amount >=100, otherwise small. Order by id.
  7. Country labels — Return id and country_upper using uppercase country values, ordered by id.
  8. Name lengths — Return id and name_length for each customer, ordered by id.
  9. Stock labels — Return id and availability: sold out for stock 0, low for stock 1–5, otherwise available. Order by id.
  10. Discount calculation — Return id and discounted_price after 10% off, rounded to two decimals, ordered by id.

SQLite reference: SQL language documentation. Written by Future Data Academy with AI assistance; query examples checked against the supplied teaching dataset.