How can NULL and Boolean values be used in a WHERE clause?

QuestionsQuestions8 SkillsProDec, 25 2025
0178

Hello! I'm Labby, and I'd be happy to explain how to work with NULL and Boolean values in a WHERE clause.

1. Working with Boolean Values

In MySQL, generic boolean values like TRUE and FALSE are actually treated as numbers (1 for true and 0 for false). You can use them directly in your conditions to filter flags (like in_stock).

Examples:

  • To find records where a condition is true:
    SELECT title FROM books WHERE in_stock = TRUE;
    -- Or simply: WHERE in_stock = 1;
  • To find records where a condition is false:
    SELECT title FROM books WHERE in_stock = FALSE;
    -- Or simply: WHERE in_stock = 0;

2. Working with NULL Values

NULL represents a "missing" or "unknown" value. Here is the most important rule: You cannot use = or <> with NULL. For example, WHERE column = NULL will never work. Instead, you must use specific SQL operators:

  • IS NULL: To find records that have a missing value.
    -- Find books that don't have a price set yet
    SELECT title FROM books WHERE price IS NULL;
  • IS NOT NULL: To find records that have any value assigned.
    -- Find books that have a price assigned
    SELECT title FROM books WHERE price IS NOT NULL;

Summary Table

Condition Correct Syntax
Is True WHERE col = TRUE or WHERE col = 1
Is False WHERE col = FALSE or WHERE col = 0
Is Missing WHERE col IS NULL
Is Not Missing WHERE col IS NOT NULL

In your current lab setup, since the in_stock column uses 0 and 1, using WHERE in_stock = FALSE is a great way to find out which books are currently unavailable! Keep up the great work

0 Comments

no data
Be the first to share your comment!