SQL BASIC QUERIES YOU NEED TO KNOW PART 2
This is a follow-up course for SQL BASIC QUERIES YOU NEED TO KNOW PART 1
The IN operator in SQL is used to filter data based on matching values in a specific column with a set of given values.
The basic syntax for the IN operator is:
SELECT column1, column2, ...
FROM table_name
WHERE column IN (value1, value2, ...);
To find all customers whose last name is ‘Doe’ or ‘Johnson’:
SELECT * FROM customers
WHERE last_name IN ('Doe', 'Johnson');
This query would return all rows from the “customers” table where the last_name column is either ‘Doe’ or ‘Johnson’.
You can also use the NOT IN operator to find records that do not match a set of given values.
For example, to find all customers whose last name is not ‘Doe’ or ‘Johnson’:
SELECT * FROM customers
WHERE last_name NOT IN ('Doe', 'Johnson');
It’s important to note that the IN operator can be slow when used with large sets of values, especially when the column is not indexed. In that case, you can use a JOIN statement with a subquery to achieve similar results, which can be faster and more efficient.