w3resource

SQL Null operator

Description

SQL Null check is performed using either IS NULL or IS NOT NULL to check whether a value in a field is NULL or not.

When a field value is NULL it means that the database assigned nothing in that field for that row. The NULL is not zero or blank. It represents an unknown or inapplicable value. It can’t be compared using AND / OR logical operators. The special operator ‘IS’ is used with the keyword ‘NULL’ to locate ‘NULL’ values. NULL can be assigned in both type of fields i.e. numeric or character type of field.

Syntax:

SELECT [column_name1,column_name2 ]
FROM [table_name]
WHERE [column_name] IS [NOT] NULL;

Parameters:

Name Description
column_name,column_name1.. Name of the column of the table.
table_name Name of the table.

Example:

Sample table: listofitem


To get data of all columns from the 'listofitem' table with following condition -

1. coname column contain NULL value,

the following sql statement can be used :

SQL Code:

SELECT *  
FROM listofitem  
WHERE coname IS NULL;

Output:

Sql Null operator

Sql Not null operator

Sample table: listofitem


To get data of all columns from the 'listofitem' table with the following condition -

1. 'coname' column must have a value,

the following sql statement can be used:

SQL Code:

SELECT * 
FROM listofitem 
WHERE coname IS NOT NULL; 

Output:

Sql Null operator

Outputs of the said SQL statement shown here is taken by using Oracle Database 10g Express Edition.

See our Model Database.

Practice SQL Exercises

Want to improve the above article? Contribute your Notes/Comments/Examples through Disqus.



SQL: Tips of the Day

How to create id with AUTO_INCREMENT on Oracle?

Identity column is now available on Oracle 12c:

create table t1 (
    c1 NUMBER GENERATED by default on null as IDENTITY,
    c2 VARCHAR2(10)
    );

or specify starting and increment values, also preventing any insert into the identity column (GENERATED ALWAYS) (again, Oracle 12c+ only)

create table t1 (
    c1 NUMBER GENERATED ALWAYS as IDENTITY(START with 1 INCREMENT by 1),
    c2 VARCHAR2(10)
    );

Alternatively, Oracle 12 also allows to use a sequence as a default value:

CREATE SEQUENCE dept_seq START WITH 1;

CREATE TABLE departments (
  ID           NUMBER(10)    DEFAULT dept_seq.nextval NOT NULL,
  DESCRIPTION  VARCHAR2(50)  NOT NULL);

ALTER TABLE departments ADD (
  CONSTRAINT dept_pk PRIMARY KEY (ID));

Database: Oracle

Ref: https://bit.ly/388Dx0O