Skip to content
Home » SQL IS NOT NULL

SQL IS NOT NULL

The IS NOT NULL is used in SQL to find a non-NULL value. It returns TRUE if a non-NULL value is found, otherwise it returns FALSE.



SYNTAX

SELECT col1, col2, ..
FROM Table
WHERE Expression IS NOT NULL

expression is the expression to check for a NOT NULL value.

Lets take a look at IS NOT NULL in SQL , first we will create a sample table and insert some records into table.

CREATE TABLE dbo.EmpData
( EmpId INT,
Name VARCHAR(100),
Code VARCHAR(10),
Department VARCHAR(50),
Age INT
)

INSERT INTO dbo.EmpData
(EmpId, Name, Code, Department, Age)
VALUES
(1, 'Ajit Singh', null, 'CS', 35),
(2, 'Mohit Sharma', 'EmXTY', null, 31),
(3, 'Sujit Kumar', 'fMFSf', 'ES', 39),
(4, 'Mukul Aggarwal', 'Hrtms', null, 41),
(5, 'Sumit viyas', null, 'CS', 32),
(6, 'Sanjay Rawat', null, 'ES', 64),
(7, 'Gaurav Kumar', 'Ryrgt', null, 43),
(8, 'Amit kumar', null, null, 28)

Now we have a table named EmpData as shown below.

SELECT * FROM dbo.EmpData

Lets see how to use IS NOT NULL to find the non-null values in table.



As you can see the output of table EmpData in above screenshot, there are few null values in Code and Department columns.

Lets select the employee records from table EmpData, whose Code is not null.

SELECT * FROM dbo.EmpData
WHERE Code IS NOT NULL

You can see, it returns only those records whose code values is not null.

Lets see one more example, this time we will select all employees from EmpData table whose Code values, or Department name is not null.

SELECT * FROM dbo.EmpData
WHERE Code IS NOT NULL OR Department IS NOT NULL

You can see, it returns only those employee’s records whose either code values is not null or department name is not null.

Lets select all employees from EmpData table whose Code values, and Department name is not null.

SELECT * FROM dbo.EmpData
WHERE Code IS NOT NULL and Department IS NOT NULL

You can see, it returns only those employees whose code value and department name is not null.



Also Read..

SQL IS NULL

SQL Server NOT NULL Constraint

SQL Basics TutorialSQL Advance TutorialSSRSInterview Q & A
SQL Create tableSQL Server Stored ProcedureCreate a New SSRS Project List Of SQL Server basics to Advance Level Interview Q & A
SQL ALTER TABLESQL Server MergeCreate a Shared Data Source in SSRSSQL Server Question & Answer Quiz
SQL DropSQL Server PivotCreate a SSRS Tabular Report / Detail Report
..... More.... More....More
Power BI TutorialAzure TutorialPython TutorialSQL Server Tips & Tricks
Download and Install Power BI DesktopCreate an Azure storage accountLearn Python & ML Step by stepEnable Dark theme in SQL Server Management studio
Connect Power BI to SQL ServerUpload files to Azure storage containerSQL Server Template Explorer
Create Report ToolTip Pages in Power BICreate Azure SQL Database ServerDisplaying line numbers in Query Editor Window
....More....More....More

Loading

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.