Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count how many percent of values on each column are nulls

Is there a way, through the information_schema or otherwise, to calculate how many percent of each column of a table (or a set of tables, better yet) are NULLs?

like image 286
James Brown Avatar asked Dec 30 '25 04:12

James Brown


1 Answers

In PostgreSQL, you can easily compute it using the statistics tables if your autovacuum setting is on (check it by SHOW ALL;). You can also set the vacuum interval to configure how fast your statistics tables should be updated. You can then compute the NULL percentage (aka, null fraction) simply using the query below:

select attname, null_frac from pg_stats where tablename = 'table_name'
like image 89
Ali Naderi Avatar answered Jan 01 '26 20:01

Ali Naderi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!