How to Insert Rows with NULL Values in SQL?

In SQL, due to lack of data, we sometimes need to insert rows with NULL values in the tables. Here, the keyword NULL(without quotes) is used to represent no data. There are some key points of Null value: 

  • NULL value is different from a zero value.
  • A NULL value is used to represent a missing value, but that it usually has one of three different interpretations:
    • The value unknown (value exists but is not known)
    • Value not available (exists but is purposely withheld)
    • Attribute not applicable (undefined for this tuple)
  • It is often not possible to determine which of the meanings is intended. Hence, SQL does not distinguish between the different meanings of NULL.

Syntax:

INSERT INTO TABLE_NAME values
(COLUMN_VALUE,NULL,........);

Step 1: Create a Database. For this use the below command to create a database named w3wiki.

Query:

CREATE DATABASE w3wiki

Output:

Step 2: Use the w3wiki database. For this use the below command.

Query:

USE w3wiki

Output:

Step 3: Create a table WORKER inside the database w3wiki. This table has 4 columns namely STUDENT_ID, STUDENT_NAME,  STUDENT_STANDARD, ENGLISH, MATHS, and PHYSICS containing student id, student name, standard, and marks of various subjects.

Query:

CREATE TABLE WORKER(
W_NAME VARCHAR(20),
CITY VARCHAR(20),
AGE INT);

Output:

Step 4: Display the structure of the WORKER table.

Query:

EXEC SP_COLUMNS 'WORKER';

Output:

Step 5: Insert 10 rows into the WORKER table.

Query:

INSERT INTO WORKER VALUES('SAM','ONTARIO',NULL);
INSERT INTO WORKER VALUES('TIM',NULL,56);
INSERT INTO WORKER VALUES(NULL,'CAIRO',43);
INSERT INTO WORKER VALUES(NULL,'MUMBAI',NULL);
INSERT INTO WORKER VALUES(NULL,NULL,NULL);

Output:

Step 6: Display all the rows of the WORKER table including the 0(zero) values.

Query:

SELECT * FROM WORKER;

Output:

Thus, in this way, we can insert NULL values into a table.