MySQL: IN Operator

Profile picture for user arilio666

When you wanna query a list of values in order the IN operator plays a major role in that. In MySQL, the IN operator fetched the list of values from a table and can perform many operations upon it.

Many of wonder why I can't fetch value like this:

select * from customers where state = 'VA' or 'MA' or 'CA'

Where I wanna fetch all the states lists like this order but this will fetch me just 'VA' state details alone.

So IN operator plays a major role in this.

Syntax

SELECT * from TableName WHERE columnname IN (valuelist)

So for today's example, we will be using the customer's table and operate fetching details of the list of states specified with IN operator.

Example

SELECT * from customers WHERE state IN ('VA', 'MA', 'CA')

So using the IN operator we fetched the specific states using the list of them and it was possible because of the IN operator in the same way we can also exclude/negate if we don't want anything displayed mentioned in the condition.

The query goes like this.

SELECT * from customers WHERE state NOT IN ('VA', 'MA', 'CA')

So using this we negated the list of states from displaying.

Tags