jeudi 19 janvier 2012
SQL: the IN operator
Do you like this story?
The IN operator can be very useful when we need to perform SQL queries where we need to filter the results with more than one possible value.
Just to understand the power of the IN operator,  we need to go back a few steps and see how we usually filter data from a table with the OR operator.
SELECT * FROM users
WHERE name = 'John' OR name = 'James'The IN operator
Using the IN operator is much easier and, let me say it, more elegant (if that matters!). Let's see how we could write the above query, using it:SELECT * FROM users
WHERE name IN ('John', 'James')Other considerations
There's something more I would like you to consider. When we filter results, we sometimes use the LIKE operator, especially when we need to use only part of a string as filtering value. For example in the first above query, we could filter data by names starting with 'J' or 'S':SELECT * FROM users
WHERE name LIKE 'J%' OR name LIKE 'S%'SELECT * FROM users
WHERE name IN ('J%', 'S%')SELECT * FROM users
WHERE SOUNDEX(name) IN (SOUNDEX('J'), SOUNDEX('S'))And that's it. I hope you found the article useful. If you like, please leave a comment in the section below.

This post was written by: Franklin Manuel
Franklin Manuel is a professional blogger, web designer and front end web developer. Follow him on Twitter
Inscription à :
Publier les commentaires (Atom)


0 Responses to “SQL: the IN operator”
Enregistrer un commentaire