![]() |
The MySQL IN operator is used to filter data based on a specified set of values. It is a shorthand for multiple OR conditions which allows us to specify a list of values in a WHERE clause to match records that have any of those values. This makes your SQL queries more concise and easier to read. MySQL IN OperatorThe IN operator is a powerful and efficient way to filter data within a specified set of values. It is particularly useful when we need to match a column’s value against multiple possible values. Instead of writing multiple OR conditions, we can use the IN operator to check multiple values. It can be used with any data type. Syntax:SELECT column1, column2, ... Parameters:
Demo MySQL DatabaseWe will create a sample database to see how to use the IN operator. Create ‘studentsInfo‘ table: CREATE TABLE studentsInfo ( Output
MySQL IN Operator ExampleLet’s see some examples of IN Operator and understand it works in MySQL: Example 1: Select students who are in grades ‘A’ or ‘B’SELECT id, name, grade, city Output:
Explanation: This query retrieves students whose grades are either ‘A’ or ‘B’. The Example 2: Select students whose city is not Mumbai or BangaloreSELECT id, name, city Output:
Explanation: This query retrieves students whose cities are neither ‘Mumbai‘ nor ‘Bangalore‘. The Example 3: Select students aged 18, 20, or 22SELECT id, name, age, city Output:
Explanation: This query retrieves students whose ages are either 18, 20, or 22. The ConclusionThe MySQL ‘IN‘ operator is a powerful tool for filtering data by matching column values against a specified set. It simplifies complex queries and enhances readability, making it easier to manage and analyze data. By effectively utilizing the IN operator, you can simplify your SQL queries, improve performance, and handle multiple conditions with ease, making database management more efficient. |
Reffered: https://www.geeksforgeeks.org
Databases |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 22 |