Skip to content
Advertisement

SQL how to exclude rows only if two conditions are met

How do you write a where clause to exclude rows only if two things are true? For instance, I want the inverse of

SELECT * FROM table
where 
start_num = end_num
 and time_taken < 100

So I only want to return values where the start_num does not equal the end_num, but only if the time_taken is greater than 100. Both those two things have to be false to remove rows.

I tried

SELECT * FROM table
where (start_num <> end_num and tripduration > 100)

but that doesn’t seem like it gives me the right number of rows in return

Advertisement

Answer

If I understand correctly, you can simply use:

where not (start_num = end_num and time_taken < 100)

Note that this is not exactly the same, because this also filters out values where any of the column have NULL values. It can easily be tweaked to handle that, if necessary.

This logic can also be written as:

where start_num <> end_num or time_taken >= 100
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement