Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

MySQL, <> clause and OR logic

Beginning SQL learner here. I have the following database ‘drink_name’ with drinks (taken from Head First SQL):

| Blackthorn       |
| Blue Moon        |
| Oh My Gosh       |
| Lime Fizz        |
| Kiss on the Lips |
| Hot Gold         |
| Lone Tree        |
| Greyhound        |
| Indian Summer    |
| Bull Frog        |
| Soda and It      |

This is the ‘drink_name’ column. Then I have another column with ‘main’ ingredients. Among them soda, iced water, etc etc.

I’m playing around and I came upon the following query:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

SELECT drink_name FROM easy_drinks WHERE main <> 'soda' OR main <> 'iced tea';

And I’m trying to understand why this query returns EVERY entry. What’s the logic behind this? I know if I wanted to have only the ones without those ingredients I can use AND, and then only the ones without soda or iced tea would come up, but this one I’m puzzled about.
Thanks.

>Solution :

This has not much to do with SQL, it’s simple boolean logic: your condition is always true. The same way that x <> 1 OR x <> 0 is true for any number, main <> 'soda' OR main <> 'iced tea' will be true for any value of main.

Or maybe you didn’t know that WHERE TRUE returns every entry in SQL?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading