Q:

Number of rows with specified number of null columns

How to get information how many rows in a table have particular number of nulls in it?
I want to get something like this:  
Number of nulls | Number of rows
        0       |      10
        1       |       4
        2       |      11

Motivation:
I need this for data mining purposes. If for example I have an observation that has in almost all columns null value then I need to get rid of this observation, but there can be also a situation where many observations have small value of nulls which is acceptable.

A:

With just SQL, you'll have to resort to unpleasant code, something like:
SELECT CASE WHEN column1 IS NULL THEN 1 ELSE 0 END
       + CASE WHEN column2 IS NULL THEN 1 ELSE 0 END
       + ... AS num_nulls,
       COUNT(*) as num_rows
FROM table 
GROUP BY num_nulls;

Also note that not all SQL dialects support referencing a calculated column by alas in the GROUP BY clause, so you might end up with a much uglier query. Needless to say, you'll also have to make a different query for each table. You could of course generate the query using some INFORMATION_SCHEMA voodoo...

