IS NULL

Empty values: IS NULL

SQLite in your browser
Lesson concept What you'll do
1 / 4

Find the books that have no series, or the ones that do.

An empty value is NULL. Test it with IS NULL or IS NOT NULL, never = NULL.

The database
11 rows
book_idtitleauthorgenreyearpagesseries
1DraculaBram StokerHorror1897418NULL
2The Fault in Our StarsJohn GreenRomance2012313NULL
3Pride and PrejudiceJane AustenRomance1813432NULL
4Charlotte's WebE.B. WhiteChildren1952184NULL
5The Great GatsbyF. Scott FitzgeraldFiction1925180NULL
6Angels & DemonsDan BrownThriller2000616Robert Langdon
7MockingjaySuzanne CollinsScience Fiction2010390The Hunger Games
8The Fellowship of the RingJ.R.R. TolkienFantasy1954423The Lord of the Rings
9The Time MachineH.G. WellsScience Fiction1895118NULL
10The Hound of the BaskervillesArthur Conan DoyleMystery1902256Sherlock Holmes
11FrankensteinMary ShelleyHorror1818280NULL
Click a name to type it
Your query
 
Hint: click the green edit button on the board, or type it yourself
Query output Ready to run

Run your query to see the result table.

Press the green Run button or Ctrl+Enter.

Study notes

How IS NULL works

  • What is NULL?

    A value that's missing or unknown. It isn't 0 and it isn't empty text: it's no value at all. A book that stands alone has NULL as its series.

  • Why doesn't = NULL work?

    Comparing anything with an unknown gives "unknown", never true. So series = NULL finds nothing, even for rows where series is NULL.

  • How do I find NULLs?

    WHERE series IS NULL

    And IS NOT NULL for rows that do have a value.

  • What happens to NULL in sums?

    pages * NULL is NULL. COUNT(series) and SUM skip NULLs, which you'll use in the totals topic.

  • How is this like Excel?

    NULL is like an empty cell, and IS NULL is like ISBLANK. Excel treats an empty cell as 0 in sums; SQL keeps it as unknown.

Other databases

How other databases do it

The lessons run on SQLite. Most of SQL is the same everywhere; these are the places where this lesson's SQL differs.

Showing a default instead of NULL
DatabaseHowNote
SQLite (these lessons)COALESCE(series, 'none')IFNULL(series, ...) works too.
PostgreSQLCOALESCE(series, 'none')
MySQL / MariaDBCOALESCE(series, 'none')IFNULL(series, ...) works too.
SQL ServerCOALESCE(series, 'none')ISNULL(series, ...) works too.
OracleCOALESCE(series, 'none')NVL(series, ...) works too. Careful: Oracle stores empty text ('') as NULL.
Sybase ASECOALESCE(series, 'none')ISNULL(series, ...) works too.

Know someone fighting with a spreadsheet? Send them this page.