How to Count Null and Non-Null Values in PostgreSQL
SELECT
SUM(CASE WHEN col IS NULL THEN 1 ELSE 0 END) count_nulls, -- counts the nulls in the column
COUNT(col) count_not_nulls -- counts the non-nulls in the column
FROM table_name;
The above SQL query will return the count of null and non-null values in the column col
from the table table_name
. The SUM
function is used to count the null values in the column, and the COUNT
function is used to count the non-null values in the column. Inside the sum, we use a CASE
statement to check if the value is null, and if it is, we return 1, otherwise we return 0. This way, the SUM
function will sum up all the 1s, which will give us the count of null values in the column. The COUNT
function will count all the non-null values in the column.
Counting Only Non-Null Values in SQL
You can easily count only the non-null values in a column using the COUNT
function in SQL. Here's an example:
SELECT COUNT(col) count_not_nulls
FROM table_name;
When passing a column name to the COUNT
function, it will only count the non-null values in the column. The above SQL query will return the count of non-null values in the column col
from the table table_name
.
Counting Only Null Values in SQL Using Count
You can also count only the null values in a column using the COUNT
function in SQL. Here's an example:
SELECT COUNT(*) - COUNT(col) count_nulls
FROM table_name;
Here, we are using the COUNT(*)
function to count all the rows in the table, and then subtracting the count of non-null values in the column from the total count of rows. This will give us the count of null values in the column.
Counting Only Null Values in SQL Using SUM
You can also count only the null values in a column using the SUM
function in SQL. Here's an example:
SELECT SUM(CASE WHEN col IS NULL THEN 1 ELSE 0 END) count_nulls
FROM table_name;
Here, we are using the SUM
function to sum up the result of the CASE
statement. The CASE
statement checks if the value in the column is null, and if it is, it returns 1, otherwise it returns 0. This way, the SUM
function will sum up all the 1s, which will give us the count of null values in the column.
Turn your SQL into Beautiful Dashboards
Create amazing KPI dashboards directly from your SQL database with Dashase
- Create charts, tables, and widgets
- Collaboration - Shared Dashboards
- AI assisted query generation - GPT-4
- Supports PostgreSQL, MySQL, SQL Server, and more
PostgreSQL Snippets
- Counting Array Length in PostgreSQL
- Concatenating Strings in PostgreSQL
- Joining strings with separators in PostgreSQL - CONCAT_WS()
- How to Count Null and Non-Null Values in PostgreSQL
- How to create a table in PostgreSQL
- How to delete rows in PostgreSQL
- How to perform full text search in PostgreSQL
- Handling NULL values with the COALESCE function
- How to insert rows into a table in PostgreSQL
- List databases in PostgreSQL
- Split string in PostgreSQL using regexp_split_to_array
- Split string in PostgreSQL using split_part
- Using substring() to get parts of a string in PostgreSQL
- How to update rows in PostgreSQL