- SQL - Discussion
- SQL - Useful Resources
- SQL - Useful Functions
- SQL - Quick Guide
- SQL - Questions and Answers
- SQL - Datatype Functions
- SQL - Conversion Functions
- SQL - JSON Functions
- SQL - Cursor Functions
- SQL - Logical Functions
- SQL - Statistical Functions
- SQL - Text & Image Functions
- SQL - Numeric Functions
- SQL - Aggregate Functions
- SQL - String Functions
- SQL - Date Functions
- SQL - Database Tuning
- SQL - IN vs EXISTS
- SQL - Group By vs Order By
- SQL - Common Table Expression
- SQL - Cursors
- SQL - Date & Time
- SQL - Auto Increment
- SQL - Using Sequences
- SQL - Handling Duplicates
- SQL - Sub Queries
- SQL - Transactions
- SQL - NULL Values
- SQL - Stored Procedures
- SQL - Default Constraint
- SQL - Check Constraint
- SQL - Null Functions
- SQL - Min & Max
- SQL - Hosting
- SQL - Injection
- SQL - Comments
- SQL - Wildcards
- SQL - Non-Clustered Index
- SQL - Clustered Index
- SQL - Unique Index
- SQL - Primary Key
- - 工会诉Join
- SQL - Inner Join
- SQL - Using Joins
- SQL - Aliases
- SQL - EXCEPT Operator
- SQL - INTERSECT Operator
- SQL - UNION vs UNION ALL
- SQL - UNION Operator
- SQL - BETWEEN Operator
- SQL - NOT NULL
- SQL - IS NOT NULL
- SQL - IS NULL
- SQL - NOT EQUAL
- SQL - NOT Operator
- SQL - CASE
- SQL - EXISTS Operator
- SQL - ANY, ALL Operators
- SQL - IN Operator
- SQL - LIKE Operator
- SQL - BOOLEAN (BIT) Operator
- SQL - AND & OR
- SQL - Having Clause
- SQL - Group By Clause
- SQL - Order By Clause
- SQL - Distinct Clause
- SQL - Top Clause
- SQL - Where Clause
- SQL - Rename Views
- SQL - Drop Views
- SQL - Update Views
- SQL - Create Views
- SQL - Sorting Results
- SQL - Delete Query
- SQL - Update Query
- SQL - Insert Into Select
- SQL - Select Into
- SQL - Select Query
- SQL - Insert Query
- SQL - Constraints
- SQL - Delete Table
- SQL - Drop Table
- SQL - Alter Tables
- SQL - Temporary Tables
- SQL - Clone Tables
- SQL - Truncate Table
- SQL - Rename Table
- SQL - Show Tables
- SQL - Create Table
- SQL - Backup Database
- SQL - Show Database
- SQL - Rename Database
- SQL - Select Database
- SQL - Drop Database
- SQL - Create Database
- SQL - Expressions
- SQL - Operators
- SQL - Data Types
- SQL - Syntax
- SQL - Databases
- SQL - RDBMS Concepts
- SQL - Overview
- SQL - Home
5. 图瓦卢
- 页: 1
- 页: 1
- 结构-创建指数
- 页: 1
- 页: 1
- 页: 1
- SQL - Foreign Key
- 文 件
- ∗ E/CN.6/2009/1。
- 页: 1
- 页: 1
- 文 件
- 页: 1
- 页: 1
- 文 件
- 页: 1
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
SQL - IS NOT NULL
The IS NOT NULL query in SQL is used to fetch all the rows that contain non-null values in a column.
A NULL value in the context of the relational database model denotes an unidentified value. If we think of a theoretical way, the NULL value still points to an unknown value, but this unknown value is not equal to a value of zero or a field with empty fields.
Null value in SQL
The SQL NULL is the term used to represent a missing value. A NULL value in a table is a value in a field that appears to be blank.
A field with a NULL value is a field with no value. It is very important to understand that a NULL value is different than a zero value or a field that contains spaces. For checking null values we can use two basic operators
IS NULL
IS NOT NULL
IS NOT NULL in SQL
Retrieving the rows with non-NULL values in a column is done using the IS NOT NULL operator. When it is used, the condition is met when either the column contains a value that is not null or when the expression that comes before the IS NOT NULL does not evaluate to null.
Syntax
Following is the syntax for IS NOT NULL −
SELECT column_names FROM table_name WHERE column_name IS NOT NULL;
Example
Let’s consider a table named "Car" that we are going to create in our database and which contains some null values in the fields. Execute the below query to create a table
SQL> CREATE TABLE Car ( ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, OWNER CHAR (25), PRICE DECIMAL (18, 2) NOT NULL, PRIMARY KEY (ID) );
Now we are going to populate the above-created table by using the below query.
SQL> INSERT INTO Car (ID,NAME,OWNER,PRICE) VALUES (1, Bmw , SRK , 2000.00 ); INSERT INTO Car (ID,NAME,OWNER,PRICE) VALUES (2, Audi , RAM , 3000.00 ); INSERT INTO Car (ID,NAME,OWNER,PRICE) VALUES (3, RX100 , NULL, 4000.00 ); INSERT INTO Car (ID,NAME,OWNER,PRICE) VALUES (4, Benz , NULL, 5000.00 );
Verification
To check whether the table is created or not, let’s execute the below query.
SQL> SELECT * FROM Car;
On executing it, it will display a table as shown below −
+----+-------+-------+---------+ | ID | NAME | OWNER | PRICE | +----+-------+-------+---------+ | 1 | Bmw | SRK | 2000.00 | | 2 | Audi | RAM | 3000.00 | | 3 | RX100 | NULL | 4000.00 | | 4 | Benz | NULL | 5000.00 | +----+-------+-------+---------+
IS NOT NULL with SELECT statement
We can use IS NOT NULL operator with a SELECT statement to filter rows based on whether a particular column contains a non-NULL value.
Example
In the following example, we are going to show how the IS NOT NULL condition is going to be used to select rows with non-NULL values.
SQL> SELECT * FROM Car WHERE OWNER IS NOT NULL;
Output
On executing the above query, it will generate an output as shown below −
+----+------+-------+---------+ | ID | NAME | OWNER | PRICE | +----+------+-------+---------+ | 1 | Bmw | SRK | 2000.00 | | 2 | Audi | RAM | 3000.00 | +----+------+-------+---------+
IS NOT NULL with COUNT() function
We can also use the IS NOT NULL operator with the COUNT() function in SQL to count the number of rows that do not contain NULL values in a particular column. This function is utipzed along with the SQL SELECT command.
Syntax
Following is the syntax for COUNT() function −
SELECT COUNT(column_name) FROM table_name WHERE condition;
Example
To determine how many rows are having non-NULL values, use the COUNT() method along with IS NOT NULL. Execute the following query that is shown below −
SQL> SELECT COUNT(*) FROM CAR WHERE OWNER IS NOT NULL;
Output
On executing the above query, it will generate an output as shown below −
+----------+ | COUNT(*) | +----------+ | 2 | +----------+
IS NOT NULL with DELETE statement
We can also use the DELETE statement with IS NOT NULL operator to delete all rows that do not contain NULL values in a particular column.
Example
In the following example, we are going to use IS NOT NULL along with the delete statement to delete the rows that contain non-NULL values. Execute the below query to delete the non-NULL values.
SQL> DELETE FROM Car WHERE OWNER IS NOT NULL;
Verification
To check whether the non-NULL values are removed from the table, execute the following query −
SQL> SELECT * FROM Car;
On executing the above query, it will generate the following output as shown below −
+----+-------+-------+---------+ | ID | NAME | OWNER | PRICE | +----+-------+-------+---------+ | 3 | RX100 | NULL | 4000.00 | | 4 | Benz | NULL | 5000.00 | +----+-------+-------+---------+
IS NOT NULL with UPDATE statement
We can use the UPDATE statement with the "IS NOT NULL" operator in SQL to update the value of a column for all rows that contain a non-NULL value in a particular column.
Example
In this case, we are going to check how we are going to use "is not null" in an update statement. Consider the following table "Car" in our database and run the following query to see how the update statement was used.
SQL> UPDATE Car SET OWNER = DHRUVA WHERE OWNER IS NOT NULL;
Verification
Run the below query to verify whether the table has been updated or not −
SQL> SELECT * FROM Car;
On executing the above query, it will generate the following output as shown below −
+----+-------+--------+---------+ | ID | NAME | OWNER | PRICE | +----+-------+--------+---------+ | 1 | Bmw | DHRUVA | 2000.00 | | 2 | Audi | DHRUVA | 3000.00 | | 3 | RX100 | NULL | 4000.00 | | 4 | Benz | NULL | 5000.00 | +----+-------+--------+---------+Advertisements