Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL - not null or condition

Tags:

sql

I'm quite new to SQL and am facing the following problem. Consider we have a table with two columns:

A         B
10        20
NULL      15
25        10
50        30
NULL      100

What I need is to select rows which satisfy the following conditions:

either
1) A is NULL
or
2) A < B

Which means, as a result I want to see such table:

A         B
10        20
NULL      15
NULL      100

I tried to use the following query:

SELECT * FROM mytable
WHERE A < B OR A is NULL

But it seems like it doesn't work the way I supposed it to.

How should a correct query look like? Thank you!

like image 696
fremorie Avatar asked Dec 05 '25 18:12

fremorie


1 Answers

What I queried was: "SELECT * FROM table WHERE ...some conditions on other columns... AND A IS NULL OR A < B"

You should put the OR comparison in parenthesis:

...AND (A IS NULL OR A < B)
like image 122
Tab Alleman Avatar answered Dec 08 '25 07:12

Tab Alleman



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!