#1 Data Analytics Program in India
₹2,499₹1,499Enroll Now
Step 8
3 min read

NOT NULL

Learn how to make required fields that cannot be empty.

What is NOT NULL?

NOT NULL means "this field must have a value". It cannot be empty.

Simple rule: Required fields use NOT NULL. Optional fields don't.

How NOT NULL Works

Basic Syntax

CREATE TABLE users ( user_id SERIAL PRIMARY KEY, name VARCHAR(100) NOT NULL, email VARCHAR(100) NOT NULL, phone VARCHAR(20) );

Example 1: Required Fields

CREATE TABLE users ( user_id SERIAL PRIMARY KEY, name VARCHAR(100) NOT NULL, -- Required email VARCHAR(100) NOT NULL, -- Required phone VARCHAR(20) -- Optional );

Example 2: Insert with Required Fields

-- Works: all required fields provided INSERT INTO users (name, email) VALUES ('John', 'john@email.com'); -- Works: phone is optional INSERT INTO users (name, email, phone) VALUES ('Mary', 'mary@email.com', '555-0001');

Example 3: Query Users

SELECT * FROM users WHERE phone IS NULL;

NOT NULL Rules

  1. Must have value - Cannot insert without it
  2. Use for important fields - Name, email, dates
  3. Skip for optional - Phone, address, notes
  4. NULL = no value - Not zero, not empty string

Try It Below

Use the playground to practice:

  • SELECT * FROM users;
  • SELECT * FROM users WHERE phone IS NULL;

What Comes Next

Next: Learn CHECK Constraint to validate data with rules.

Try NOT NULL

View all users. Name and email are required, phone is optional.

Source Table: students
user_idnameemailphone
1Johnjohn@email.com555-0001
2Marymary@email.com
3Peterpeter@email.com
3 rows
SQL Editor
Loading...

Find NULL Values

Find users without phone number (NULL means no value).

Source Table: students
user_idnameemailphone
1Johnjohn@email.com555-0001
2Marymary@email.com
3Peterpeter@email.com
3 rows
SQL Editor
Loading...

Find NOT NULL Values

Find users who provided phone number.

Source Table: students
user_idnameemailphone
1Johnjohn@email.com555-0001
2Marymary@email.com
3Peterpeter@email.com
3 rows
SQL Editor
Loading...

Finished this topic?

Mark it complete to track your progress and maintain your streak!

SkillsetMaster - AI, Web Development & Data Analytics Courses