xxxxxxxxxx
# IS NULL
SELECT column_names FROM table_name WHERE column_name IS NULL;
# IS NOT NULL
SELECT column_names FROM table_name WHERE column_name IS NOT NULL;
Using 'NULL' and 'NOT NULL'
About `NULL` and `NOT NULL` in SQL, Examples of how to use these conditions in
SQL queries.
xxxxxxxxxx
-- To Check if column contains 'NULL' values, use the 'IS NULL' condition.
-- Example, select all the rows where the column "column_name" is NULL.
-- Query will return all rows where the `column_name` has `NULL` values.
SELECT * FROM table_name WHERE column_name IS NULL;
#-----------------------------------------------------------------------#
-- To check if a column doesn't contain `NULL` values, Use the `IS NOT NULL` condition.
-- Select all rows where the column "column_name" IS NOT NULL.
-- Query will return all rows where `column_name` doesn't have `NULL` values.
SELECT * FROM table_name WHERE column_name IS NOT NULL;
#------------------------------------------------------------------------#
# Combine Example
-- Select rows where "column1" is NULL and "column2" is NOT NULL.
-- Return rows where `column` is `NULL` and `column2` is NOT NULL.
-- Can also combine these condition with other clauses, such as `where`, `AND`, `OR`, etc..
SELECT * FROM table_name
WHERE column1 IS NULL
AND column2 IS NOT NULL;
xxxxxxxxxx
CREATE TABLE example_table (
id INT NOT NULL,
name VARCHAR(50) NOT NULL,
age INT
);
xxxxxxxxxx
CREATE TABLE Colleges (
college_id INT NOT NULL,
college_code VARCHAR(20),
college_name VARCHAR(50)
);
xxxxxxxxxx
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255) NOT NULL,
Age int
);
xxxxxxxxxx
By default, a column can hold NULL values.
The NOT NULL constraint enforces a column to NOT accept NULL values.
This enforces a field to always contain a value, which means that you cannot insert a new record, or update a record without adding a value to this field.
Sql NOT NULL in creating a table
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255) NOT NULL,
Age int
);