Skip to content
Advertisement

In SQL get all rows with same value in column A that have only non-null values in column B

My SQL table has multiple columns, among them A and B. In A there’s some sort of identifier of some arbitrary object (say, a user or an item). Each object instance identified by A can have multiple entries in the database (A is not unique). Each object furthermore has a property B that is also stored in the database in the corresponding column B. That property can be NULL.

I now want to find all objects that have exclusively rows in the database with non-null values for B. So if there’s an object A=12345 which has at least one row with B=NULL, it should be excluded. I also want to group/squash/combine the results, so that I only get a list of A’s with the given condition.

For a simple and unoptimized afoot Python solution I could go on and solve it like this:

So, the question is: How would a proper SQL query look like for this purpose?

Advertisement

Answer

You can use aggregation:

count(b) counts the number of non-NULL values in b. This simply says that this is equal to all the rows for each a.

If you want the original rows, you can use not exists:

For performance, you want an index on t(a, b).

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement