Querying into Data (DQL)

Nulls Decoded

Learning Outcome

4

Categorize data using CASE

3

Apply the CASE statement for conditional logic

2

Use IS NULL and IS NOT NULL in queries

1

Understand the concept of NULL values

lets recall

Comparison operator

IN

Imagine you Instagram wants to:

Find users who haven’t added a bio

Show which users completed their profile

Categorize users based on followers

SQL provides:

  • IS NULL / IS NOT NULL

  • CASE Statement

Null

The system holds a placeholder - No Data

The value does not exist yet or was never provided 

IS NULL

Imagine we have too
Find users who have added bio.

SELECT
User_ID,
username,
Bio
FROM users
WHERE bio IS NULL;
User ID Username Bio
101 mukesh NULL
102 Rahuk NULL
103 Ruhi Darth Vader enthusiast
104 Salman NULL
105 Mohan Loves solo travel 
106 akashay NULL

 Instagram identifying incomplete profiles.

User ID Username Bio
101 mukesh NULL
102 Rahuk NULL
104 Salman NULL
106 akashay NULL
SELECT
User_ID,
username,
Bio
FROM users
WHERE bio IS NULL;

IS NOT NUL

Imagine now we have to
Find users detail who have added bio.

User ID Username Bio
101 mukesh NULL
102 Rahuk NULL
103 Ruhi Darth Vader enthusiast
104 Salman NULL
105 Mohan Loves solo travel 
106 akashay NULL
SELECT
User_ID,
username,
Bio
FROM users
WHERE bio IS NOT NULL;

Shows users with completed profiles.

User ID Username Bio
103 Ruhi Darth Vader enthusiast
105 Mohan Loves solo travel 
SELECT
User_ID,
username,
Bio
FROM users
WHERE bio IS NOT NULL;

CASE Statement

CASE Statement

Used to apply conditional logic inside SQL query.

Works like:

  • IF–ELSE in programming
SELECT column_name,
CASE
   WHEN condition THEN result
   ELSE result
END AS new_column
FROM table_name;
SELECT 
User_ID
username,
followers,
CASE
   WHEN followers >= 100000 THEN 'Influencer'
   WHEN followers >= 10000 THEN 'Creator'
   ELSE 'Regular User'
END AS category
FROM users;

Imagine you want to
Categorize users based on followers.

User_ID Username followers
101 Mukesh 9069
102 Akshay Kumar 80670
103 Ranveer Allabadia 123
104 Rahul_more 687
105 Neha_negi 123500
106 foodie_mohan 16384

 Instagram categorizing users automatically.

User_ID Username followers Category
101 Mukesh 9069 Regular User
102 Akshay Kumar 80670 Creator
103 Ranveer Allabadia 123 Regular User
104 Rahul_more 687 Regular User
105 Neha_negi 123500 Influencer
106 foodie_mohan 16384 Creator
SELECT
User_ID,
username,
followers,
CASE
   WHEN followers >= 100000 THEN 'Influencer'
   WHEN followers >= 10000 THEN 'Creator'
   ELSE 'Regular User'
END AS category
FROM users;
SELECT 
User_Id,
username,
Bio,
CASE
   WHEN bio IS NULL THEN 'Incomplete Profile'
   ELSE 'Complete Profile'
END AS profile_status
FROM users;

Imagine now you have to
Categorize users based on followers.

User ID Username Bio
101 Rahul NULL
102 Mukesh NULL
103 Shiwam Darth Vader enthusiast
104 Ruhi NULL
105 Disha Loves solo travel 
106 Mohan NULL

Easy profile monitoring.

SELECT
User_ID,
username,
Bio,
CASE
   WHEN bio IS NULL THEN 'Incomplete Profile'
   ELSE 'Complete Profile'
END AS profile_status
FROM users;
User ID Username Bio profile_status
101 Rahul NULL Incomplete Profile
102 Mukesh NULL Incomplete Profile
103 Shiwam Darth Vader enthusiast Complete Profile
104 Ruhi NULL Incomplete Profile
105 Disha Loves solo travel  Complete Profile
106 Mohan NULL Incomplete Profile

Summary

5

Important for interviews and real-world SQL queries

4

Widely used in analytics dashboards

3

CASE works like IF–ELSE

2

IS NULL detects missing values

1

NULL represents missing data

Quiz

How do you check for NULL values correctly?

A. bio = NULL

B. bio == NULL

C. bio IS NULL

D. bio != NULL

How do you check for NULL values correctly?

A. bio = NULL

B. bio == NULL

C. bio IS NULL

D. bio != NULL

Quiz-Answer

Nulls Decoded

By Content ITV

Nulls Decoded

  • 20