SQL way to count NULL and NOT NULL values ​​in a column

Working with NULL values ​​in SQL is a common challenge for every data analyst and database expert. This is especially true when dealing with NULL as it can be confusing and confusing.

However, it's important to understand what NULL values ​​are and what they mean to give you an accurate and comprehensive overview of your data.

What is the SQL value NULL?

A NULL means no value, not zero or space.

Therefore, traditional comparison operators like =, <, >, and <> cannot be used on it. However, if used, the result will be UNKNOWN.

Treat NULL value as an empty field on the left side while creating a record. You can create a table and insert a new column without adding values. Therefore, that field will be a NULL value. NULL values ​​can also be inserted into columns of any data type.

 

To illustrate this, you should create a new SQL table using the syntax below:

CREATE TABLE Employee ( FirstName VARCHAR(50), LastName VARCHAR(50), PhoneNum VARCHAR(15), Salary FLOAT ); INSERT INTO Employee (FirstName, LastName, PhoneNum, Salary) VALUES ('Maxwell', 'Ayomide', '812-345-6789', 150000.00), ('David', 'Tosin', NULL, 450000.00), ('Eben', 'Teniola', '912-345-6789', 590000.00), ('Kenneth', 'Olisa', '809-456-8732', NULL), ('Esther', 'Oge', NULL, NULL);

SQL way to count NULL and NOT NULL values ​​in a column Picture 1

You can also update NULL values ​​in a table using the beginner-friendly SQL command - UPDATE command. To do this, use the syntax below:

UPDATE Employee SET FirstName = 'Esther' WHERE Salary = 200000;

To see the results, run:

SELECT * FROM Employee;

SQL way to count NULL and NOT NULL values ​​in a column Picture 2

When are SQL NULL values ​​useful?

The value NULL can be used in different situations in SQL:

  1. When data is not available or not known at the time of data entry.
  2. When the data does not apply to the entity in question. For example, in a survey, a question asking participants to check whether they have children in the survey box may have some NULL values.

 

What is the SQL IS NULL condition?

The SQL IS NULL command is one of the important SQL commands that every programmer needs to know. This command is used to check for NULL values ​​and is best used when you search for NULL values. This command will return all NULL rows in the column specified in your query.

SELECT FirstName, LastName, PhoneNum FROM Employee WHERE PhoneNum IS NULL;

This query will return all NULL values ​​in the PhoneNum column .

SQL way to count NULL and NOT NULL values ​​in a column Picture 3

What is the SQL IS NOT NULL condition?

The SQL IS NOT NULL statement is the opposite of SQL IS NULL.

This command checks for non-empty values ​​(NOT NULL values). Therefore, it will always return all rows in a column with a value and include all NULL values ​​in the column specified in your query.

SELECT FirstName, LastName, PhoneNum FROM Employee WHERE PhoneNum IS NOT NULL;

This query will return all NOT NULL values ​​in the PhoneNum column.

SQL way to count NULL and NOT NULL values ​​in a column Picture 4

How to count SQL NULL values ​​in a column

The COUNT() command is used to count. It is a useful command when analyzing data in SQL tables and working with subqueries & clipboard.

Use this query to count the number of NULL values ​​in the PhoneNum column .

SELECT COUNT(*) AS [Total Number of NULL] FROM Employee WHERE PhoneNum IS NULL

You will get the result:

SQL way to count NULL and NOT NULL values ​​in a column Picture 5

 

How to count NOT NULL values ​​in a column

Use the NOT NULL command to count the number of non-NULL values ​​in the PhoneNum column.

SELECT COUNT(PhoneNum) AS [Total Number of Non-NULL Values] FROM Employee WHERE PhoneNum IS NOT NULL

Result:

SQL way to count NULL and NOT NULL values ​​in a column Picture 6

You can also use this query to place results into a table.

SELECT SUM(CASE WHEN PhoneNum is null THEN 1 ELSE 0 END) AS [Number Of Null Values], COUNT(PhoneNum) AS [Number Of Non-Null Values] FROM Employee

SQL way to count NULL and NOT NULL values ​​in a column Picture 7

In this query, the CASE and IS NULL statements are used to classify NULL in the PhoneNum column as 1. This value is added and kept in the Number Of Null Values ​​column .

Above is how to count SQL NULL and NOT Null values ​​in a column . Hope the article is useful to you.

4 ★ | 1 Vote

May be interested

  • How to Check Null in CHow to Check Null in C
    in c, null is a symbolic constant that always points to a nonexistent point in the memory. although many programmers treat it as equal to 0, this is a simplification that can trip you up later on. it's best to check your pointers against...
  • COUNT function in SQL ServerCOUNT function in SQL Server
    this article will show you in detail how to use functions that handle count () numbers in sql server with specific syntax and examples to better visualize and capture functions.
  • Learn Null Session attacksLearn Null Session attacks
    null sessions, called ipc $ on a windows platform server, are an anonymous connection to a shared network that allows users in the network to access freely.
  • Instructions to fix Excel column/row freezing not workingInstructions to fix Excel column/row freezing not working
    in some cases, you don't see the option to freeze excel columns or rows, which affects document processing and overall view of the data table.
  • FULL JOIN in SQLFULL JOIN in SQL
    full join in sql returns all records in the left table and the table must combine and fill in that null values ​​for values ​​do not match.
  • Steps to lock columns in ExcelSteps to lock columns in Excel
    locking columns in excel with a password helps you protect columns, prevent others from changing column content or editing columns, affecting all data of the table.
  • Count the number of characters in the paragraphCount the number of characters in the paragraph
    this is a quick, accurate way to count characters in word text.
  • Handling errors in CHandling errors in C
    programming languages ​​such as c language do not provide direct support for error handling but because they are system program languages, it provides the lowest level of return values. most functions of c and functions in unix return values ​​1 or null in any error case and set an errno error code for global variables and instructions for errors that occur during the function call.
  • How to use the COUNT function in ExcelHow to use the COUNT function in Excel
    the count function in excel is the basic counting function that is often used to count how many cells are in an area or in the entire spreadsheet.
  • Have you ever wondered why we count to 3?Have you ever wondered why we count to 3?
    have you ever wondered why we count to 3? let's find out in the article below!