Skip to content
Advertisement

SQL If statement – check if row count of subquery = 1

Any suggestions on how to do this? I have a complex if statement in a query that needs to check on various conditions for a given table. EG:

However, for that first subquery, I also need to make sure it only yields one row. Exists just checks for 1 or more rows, but how do I constrain it to only return 1 if that first subquery has ONLY one row?

Advertisement

Answer

If you specifically need exactly one row, you can use aggregation:

I strongly recommend EXISTS when you just need to check existence, because it is faster than aggregation.

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