MySQL IN OPERATOR
MySQL IN operator is used to specify multiple values in a WHERE clause. It allows you to test whether a value matches any value in a list or a subquery result. It's a shorthand for multiple OR conditions, making queries more concise and readable.
Syntax
SELECT column1,column2,column3... FROM table_name where column_name IN (value1, value2, ...);
Demo Database:
- Table Name : customers
customer_id | customer_no | customer_name | contact | country | |
---|---|---|---|---|---|
1 | CS0001 | Nielsen | 830180289158 | patric@erssmail.com | US |
2 | CS0002 | Tom | 301295114077 | tom@test.com | US |
3 | CS0003 | Sam | 443486006841 | sam@test.com | India |
4 | CS0004 | Jhon Smith | 797812460888 | jhon smith@test.com | US |
5 | CS0005 | Edwards | 369569573912 | edwards@test.com | Malaysia |
6 | CS0006 | Wilkinson | 902956510286 | wilkinson@test.com | Malaysia |
7 | CS0007 | Kevyn | 817079751978 | wilkinson@test.com | India |
8 | CS0008 | Dodson | 830180289156 | dodson@test.com | Malaysia |
MySQL IN Examples
1) The statement selects all the customers are located in 'India','Malaysia'.
Example
select * from customers where country IN ('India','Malaysia');Try it Yourself
2) The statement selects all the customers are not located in 'India','Malaysia'.
Example
select * from customers where country NOT IN ('India','Malaysia');Try it Yourself
3) The given SQL statement retrieves all customers from the customer table where the customer_id matches the customer_id of orders that have the order status is Pending.
Example
select * from customers where customer_id IN (select customer_id from orders where status='Pending');Try it Yourself