Saturday 12 July 2014

Count of NULL values in each column in a table

Display the count of all NULL values under each column in a table.

I have a table with random values ,but some of the values under each columns as NULL's.

Input:                                                                                                                  Output:
                                                       









SELECT 'ID' AS COLNAME ,COUNT(*) FROM ABC WHERE ID IS NULL
UNION ALL
SELECT 'NAME' AS COLNAME ,COUNT(*) FROM ABC WHERE NAME IS NULL
UNION ALL
SELECT 'HIREDATE' AS COLNAME ,COUNT(*) FROM ABC WHERE HIREDATE IS NULL

Note:UNION ALL is used just to club the result fetched from each query.You can use UNION as well,but UNION checks for any duplicate rows.Since we know that there wont be any duplicate rows then we can use straight away UNION ALL in order to avoid the duplicate check.

No comments:

Post a Comment