简体   繁体   中英

SQL WHERE and LIKE clause duplicated

Hi guys I'm new to SQL and trying to learn it by myself. I have the following questions when I did the tutorial questions in sqlzoo website. Assume I have a table which contains info for all countries in the world.

When I want to query the country which its name containing 'A' and 'B' , I use the following query

SELECT name 
FROM world
WHERE name LIKE '%A%'
AND name LIKE '%B%'

here I can repeated use the name LIKE query

However, when I want to query the country which its name is Vietnam and Thailand, I can only use the query

SELECT name
FROM world 
WHERE name IN ('Thailand', 'Vietnam')

instead of

WHERE name = 'Thailand'
AND name = 'Vietnam'

Can somebody kindly explain the reason behind this? When I can use many AND xxxx and when I cannot use that.

Thanks so much !

WHERE name = 'Thailand'
AND name = 'Vietnam'

This one searches for the country which has both 'Thailand' and 'Vietnam' in its name. but

SELECT name
FROM world 
WHERE name IN ('Thailand', 'Vietnam')

This one searches Countries which names are either 'Thailand' OR 'Vietnam'

That is the deference between them.

WHERE name = 'Thailand' AND name = 'Vietnam'

AND is a boolean AND and return the result where the country name = Thailand and name = Vietnam in a single row.

Instead use

WHERE name = 'Thailand' OR name = 'Vietnam'

And returns to you the results which obey ALL the rules you have stated.

Can a country name be 'Thailand' AND the SAME country name be 'Vietnam', at the same time? i hope you see that the answer to this is no. So this is a classic example where you need to use 'OR' instead of 'AND', Why? because then you will get a result back where the name = 'Thailand' and also the result where the name = 'Vietnam', because both of them obey the 'OR' rule, meaning both names are Either 'Vietnam' or 'Thailand'.

The reason this wrong way of using AND instead of OR is working for you in the first case you mentioned is because you are using wildcards. and there are country names where you can have an 'A' and a 'B' at the SAME TIME in different places inside the SAME country name.

Hope this helps.

WHERE name IN ('Thailand', 'Vietnam')

Is the same as

WHERE name = 'Thailand'
OR name = 'Vietnam'

if you want to check condition to match any one element from the given list of element then you should use.

WHERE name IN ('Thailand', 'Vietnam')

and if you want to satisfy both condition then you should use below code

WHERE name = 'Thailand'
AND name = 'Vietnam'

Note :- result of AND condition should always false because name can be either 'Thailand' or 'Vietnam'

WHERE conditions are always applied per record . So

WHERE name = 'Thailand'
AND name = 'Vietnam'

does not mean: find all records in the table for the names 'Thailand' and 'Vietnam'.

It means: Find records that have a name that equals both 'Thailand' and 'Vietnam'. But a name can only be either 'Thailand' or 'Vietnam' or none of them, never both at the same time of course.

So use OR instead in order to find records where the name either equals 'Thailand' or 'Vietnam':

WHERE name = 'Thailand'
OR name = 'Vietnam'

And as you already know you can look up the list of countries instead with IN which makes it more readable

WHERE name IN ('Thailand', 'Vietnam')

and less prone to errors. A typical mistake:

WHERE name = 'Thailand' OR name = 'Vietnam'
AND size = 'big'

which translates to

WHERE name = 'Thailand' 
OR (name = 'Vietnam' AND size = 'big')

rather than

WHERE (name = 'Thailand' OR name = 'Vietnam')
AND size = 'big'

because AND has precedence over OR . So when using both AND and OR use parentheses. And as mentioned use IN instead of OR when possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM