MySQL IN
The IN
operator is used to filter records based on a list of values. It is a simpler way to use multiple OR
conditions.
Examples with Tamil Kings
1. Using IN with Multiple Values
SELECT * FROM tamil_kings_auto_increment
WHERE king_name IN ('Raja Raja Chola', 'Rajendra Chola I');
Code Explanation: This query selects records where the king_name
is either 'Raja Raja Chola' or 'Rajendra Chola I'.
2. Using NOT IN
SELECT * FROM tamil_kings_auto_increment
WHERE king_name NOT IN ('Karikala Chola', 'Vijayalaya Chola');
Code Explanation: This query selects records where the king_name
is not 'Karikala Chola' or 'Vijayalaya Chola'.
Best Practices
- Use
IN
to simplify queries with multipleOR
conditions. - Be mindful of the performance impact when using
IN
with large lists of values.
Key Takeaways
- The
IN
operator filters records based on a list of values. - It simplifies queries and improves readability.