Open In App

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: 

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 GeeksForGeeks.

Query:



CREATE DATABASE GeeksForGeeks

Output:

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

Query:

USE GeeksForGeeks

Output:

Step 3: Create a table WORKER inside the database GeeksForGeeks. 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.

Article Tags :
SQL