MySQL IN Operators with WHERE Condition

We learn to mysql in operator, in subquery, in clause, in array, installer, like in mysql, between, not in with examples

MySQL IN() function

SELECT 
    branch_code, 
    area, 
    mobile, 
    place
FROM
    branchs
WHERE
    place IN ('INDIA' , 'JAPAN');

You can achieve the same result with the OR operator as the following query: MySQL WHERE Clause With AND, OR, IN, NOT IN

SELECT 
    branch_code, 
    area, 
    mobile
FROM
    branchs
WHERE
    place = 'INDIA' OR place = 'JAPAN';    

To get branchs that do not locate in INDIA and JAPAN, you use NOT IN in the WHERE clause as follows:

SELECT 
    branch_code, 
    area, 
    mobile
FROM
    branchs
WHERE
    place NOT IN ('INDIA' , 'JAPAN');

MySQL IN with a subquery

SELECT    
	stockNumber, 
	memberNumber, 
	is_check, 
	shippedDate
FROM    
	stocks
WHERE stockNumber IN
(
	 SELECT 
		 stockNumber
	 FROM 
		 stockDetails
	 GROUP BY 
		 stockNumber
	 HAVING SUM(qtyOrdered * amountEach) > 60000
);

First, the subquery returns a list of stock numbers whose values are greater than 60,000 using the GROUP BY and HAVING clauses:

SELECT 
    stockNumber
FROM
    stockDetails
GROUP BY 
    stockNumber
HAVING 
    SUM(qtyOrdered * amountEach) > 60000;

Second, the outer query uses the IN operator in the WHERE clause to get data from the stocks table:

SELECT 
    stockNumber, 
    memberNumber, 
    is_check, 
    shippedDate
FROM
    stocks
WHERE
    stockNumber IN (10165,10287,10310);

I hope you get an idea about MySQL IN() function.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1.   Ajax Dynamic Dependent Dropdown using Laravel Example
  2.   Laravel 6 Update Increment column
  3.   how to check install laravel version?
  4.   Html Form class not found in Laravel
  5.   what does pairwise distinct mean?
  6.   Import and Export CSV File using Laravel 6 and MySQL
  7.   Laravel Eloquent query where exists MySQL
  8.   Ajax Login System with jQuery PHP and MySQLi
  9.   Custom Helper Functions in Laravel 6 Example
  10.   Increase WordPress Memory Limit using PHP
Tags:, , , , , ,