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

Handling NULL values ​​is necessary while analyzing data. Lets learn with TipsMake.com.com how to count blank and non-empty 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);

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;

When are SQL NULL values ​​useful?

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

  • When data is not available or not known at the time of data entry.
  • 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 .

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.

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:

 

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:

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

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.

Related posts
Other Program articles
  • 3 best paid HTML editing tools 2023

    mặc dù các công cụ soạn thảo html miễn phí cung cấp các tính năng tuyệt vời nhưng hầu hết đều không có các tính năng cụ thể để xây dựng những trang web nâng cao.
  • How will Slack change in 2023?

    slack đang thiết kế lại ứng dụng dành cho desktop của mình để tập trung trở lại vào năng suất, giúp sử dụng dễ dàng và nhanh chóng hơn.
  • What is Thymeleaf? How to use Thymeleaf in Spring Boot application

    thymeleaf là một công cụ template java. với thymeleaf, bạn sẽ có quyền kiểm soát cách ứng dụng xử lý mẫu tạo ra.
  • How to teach kids to code with Raspberry Pi

    raspberry pi, với mức giá thấp và dễ sử dụng, là một thiết bị tuyệt vời để giới thiệu cho giới trẻ về máy tính và lập trình.
  • Difference between C and C++

    c và c++ thoạt nhìn có vẻ giống nhau nhưng chúng có rất nhiều điểm khác biệt. hãy cùng tipsmake.com.com liệt kê những điểm khác biệt giữa c và c++ nhé!
  • How to Implement Infinite Scrolling in Vue

    infinite scrolling hữu ích khi bạn cần hiện khối dữ liệu lớn trong ứng dụng. Ở bài viết này, hãy cùng nhau học cách tạo infinite scrolling - load vô tận trong vue nhé!
Category

System

Windows XP

Windows Server 2012

Windows 8

Windows 7

Windows 10

Wifi tips

Virus Removal - Spyware

Speed ​​up the computer

Server

Security solution

Mail Server

LAN - WAN

Ghost - Install Win

Fix computer error

Configure Router Switch

Computer wallpaper

Computer security

Mac OS X

Mac OS System software

Mac OS Security

Mac OS Office application

Mac OS Email Management

Mac OS Data - File

Mac hardware

Hardware

USB - Flash Drive

Speaker headset

Printer

PC hardware

Network equipment

Laptop hardware

Computer components

Advice Computer

Game

PC game

Online game

Mobile Game

Pokemon GO

information

Technology story

Technology comments

Quiz technology

New technology

British talent technology

Attack the network

Artificial intelligence

Technology

Smart watches

Raspberry Pi

Linux

Camera

Basic knowledge

Banking services

SEO tips

Science

Strange story

Space Science

Scientific invention

Science Story

Science photo

Science and technology

Medicine

Health Care

Fun science

Environment

Discover science

Discover nature

Archeology

Life

Travel Experience

Tips

Raise up child

Make up

Life skills

Home Care

Entertainment

DIY Handmade

Cuisine

Christmas

Application

Web Email

Website - Blog

Web browser

Support Download - Upload

Software conversion

Social Network

Simulator software

Online payment

Office information

Music Software

Map and Positioning

Installation - Uninstall

Graphic design

Free - Discount

Email reader

Edit video

Edit photo

Compress and Decompress

Chat, Text, Call

Archive - Share

Electric

Water heater

Washing machine

Television

Machine tool

Fridge

Fans

Air conditioning

Program

Unix and Linux

SQL Server

SQL

Python

Programming C

PHP

NodeJS

MongoDB

jQuery

JavaScript

HTTP

HTML

Git

Database

Data structure and algorithm

CSS and CSS3

C ++

C #

AngularJS

Mobile

Wallpapers and Ringtones

Tricks application

Take and process photos

Storage - Sync

Security and Virus Removal

Personalized

Online Social Network

Map

Manage and edit Video

Data

Chat - Call - Text

Browser and Add-on

Basic setup