00:00

MySQL IS NOT NULL

Last updated: 4 min read

IS NOT NULL selects rows where a column has a value. It is how you find complete records, count filled-in fields and skip unknowns before calculating. It is the opposite of IS NULL.

1. Basic Usage

syntax.sql
WHERE column IS NOT NULL
IS NULL and IS NOT NULL split rows into two groupsSix job applicants. IS NOT NULL on the portfolio column returns the four applicants with a portfolio link; IS NULL returns the other two. Together they always cover every row.portfolio_urlIS NOT NULL → 4 rowsAsha, Ben, Chitra, DanIS NULL → 2 rowsEva, Farid
Figure 1: every row is either NULL or NOT NULL for a column; the two conditions never overlap.

Table: applicants (6 rows)

idnameemailphoneportfolio_urlyears_expreferred_by
1Ashaasha@mail.example98450 10001https://asha.dev4NULL
2Benben@mail.exampleNULLhttps://ben.design21
3ChitraNULL98450 10003https://chitra.io7NULL
4Dandan@mail.example98450 10004https://dan.codesNULL3
5Evaeva@mail.example98450 10005NULL11
6FaridNULLNULLNULL5NULL
query.sql
SELECT name, portfolio_url FROM applicants WHERE portfolio_url IS NOT NULL;

Result: 4 rows

nameportfolio_url
Ashahttps://asha.dev
Benhttps://ben.design
Chitrahttps://chitra.io
Danhttps://dan.codes

2. Why <> NULL Doesn't Work

query.sql
SELECT name FROM applicants WHERE portfolio_url <> NULL;

<> NULL: always empty

name
No rows
query.sql
SELECT name FROM applicants WHERE NOT (portfolio_url IS NULL);

NOT (… IS NULL): same as IS NOT NULL

name
Asha
Ben
Chitra
Dan

3. Combining with Other Conditions

query.sql
SELECT name, email, phone FROM applicants WHERE email IS NOT NULL AND phone IS NOT NULL;   -- fully contactable

Result: 3 rows

nameemailphone
Ashaasha@mail.example98450 10001
Dandan@mail.example98450 10004
Evaeva@mail.example98450 10005
query.sql
SELECT name, years_exp FROM applicants WHERE years_exp IS NOT NULL AND years_exp >= 4;

Result: 3 rows

nameyears_exp
Asha4
Chitra7
Farid5
years_exp >= 4 alone already excludes NULLs (NULL >= 4 isn't TRUE). The explicit IS NOT NULL makes the intent clear but isn't required here.

4. COUNT(column) Counts Non-NULL Values

query.sql
SELECT COUNT(*)             AS applicants,
       COUNT(email)         AS with_email,
       COUNT(phone)         AS with_phone,
       COUNT(portfolio_url) AS with_portfolio,
       COUNT(referred_by)   AS referred
FROM applicants;

Result: 1 row

applicantswith_emailwith_phonewith_portfolioreferred
64443

A data-completeness report as percentages:

query.sql
SELECT ROUND(100 * COUNT(email) / COUNT(*))         AS email_pct,
       ROUND(100 * COUNT(phone) / COUNT(*))         AS phone_pct,
       ROUND(100 * COUNT(portfolio_url) / COUNT(*)) AS portfolio_pct
FROM applicants;

Result: 1 row

email_pctphone_pctportfolio_pct
676767

5. Counting Filled Fields per Row

IS NOT NULL returns 1 or 0, so you can add the results to score each record:

query.sql
SELECT name,
       (email IS NOT NULL) + (phone IS NOT NULL) + (portfolio_url IS NOT NULL) + (years_exp IS NOT NULL) AS fields_filled
FROM applicants
ORDER BY fields_filled DESC, name;

Result: 6 rows

namefields_filled
Asha4
Ben3
Chitra3
Dan3
Eva3
Farid1

6. IS NOT NULL with Joins

After a LEFT JOIN, unmatched rows have NULLs on the right side. IS NOT NULL keeps only the matched ones, here applicants who were referred, with their referrer's name:

query.sql
SELECT a.name AS applicant, r.name AS referred_by
FROM applicants a
LEFT JOIN applicants r ON r.id = a.referred_by
WHERE a.referred_by IS NOT NULL;

Result: 3 rows

applicantreferred_by
BenAsha
DanChitra
EvaAsha

7. IS NOT NULL in UPDATE

update.sql
UPDATE applicants SET portfolio_url = LOWER(TRIM(portfolio_url)) WHERE portfolio_url IS NOT NULL;
SELECT COUNT(*) AS cleaned FROM applicants WHERE portfolio_url IS NOT NULL;

Result: 1 row

cleaned
4

8. IS NOT NULL vs the NOT NULL Constraint

IS NOT NULL (condition)NOT NULL (constraint)
WhereIn WHERE, SELECT, CASE…In CREATE / ALTER TABLE
What it doesTests a value when you queryPrevents NULLs from being stored
ExampleWHERE email IS NOT NULLemail VARCHAR(40) NOT NULL

If a column should never be empty, enforce it with the NOT NULL constraint rather than filtering later.

9. Practice Exercises

Exercise 1: Applicants with a portfolio but no phone number.
solution.sql
SELECT name FROM applicants WHERE portfolio_url IS NOT NULL AND phone IS NULL;

Result: 1 row

name
Ben
Exercise 2: Average experience of applicants whose experience is known, and how many that is.
solution.sql
SELECT COUNT(years_exp) AS known, ROUND(AVG(years_exp), 1) AS avg_years FROM applicants WHERE years_exp IS NOT NULL;

Result: 1 row

knownavg_years
53.8

10. Summary

  • IS NOT NULL keeps rows where the column has a value; <> NULL never works.
  • COUNT(column) counts non-NULL values; COUNT(*) counts rows.
  • IS NOT NULL returns 1/0, useful for completeness scores.
  • Use it after LEFT JOINs to keep matched rows.
  • Use the NOT NULL constraint to stop NULLs being stored at all.