postgreSQL IN Operator

The PostgreSQL IN condition is used to reduce the need to use multiple OR conditions in a SELECT, INSERT, UPDATE, or DELETE statement.The postgreSQL IN operator is used to test if the expression matches any value in the list of values.
IN operator works as a shorthand for multiple OR conditions.The IN operator can be used with the SELECT, INSERT, UPDATE, or DELETE statements.

IN Operator Syntax :

The syntax of the IN Operator is −

SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);


IN Operator Example :

Consider the Customer table with the following records -

CustomerID CustomerName Age Address CustomerSalary
1 Aarav 36 Udaipur 35000
2 Vivaan 33 Mumbai 30000
3 Reyansh 28 Chennai 40000
4 Muhammad 29 Udaipur 50000
5 Sai 27 Mumbai 27000


TEST CASE 1 :- The following code is an example, which would fetch the record from Customer table where CustomerSalary is 30000, 40000, 25000.

#lfcdb=# SELECT * FROM Customer
WHERE CustomerSalary IN (30000, 40000, 25000);
   

The result for the respective postgreSQL query is as follows −

CustomerID CustomerName Age Address CustomerSalary
2 Vivaan 33 Mumbai 30000
3 Reyansh 28 Chennai 40000



TEST CASE 2:- The following code is an example, which would fetch the record from Customer table where CustomerSalary is NOT 30000, 40000, 25000.

#lfcdb=# SELECT * FROM Customer
WHERE CustomerSalary NOT IN (30000, 40000, 25000);
   

The result for the respective postgreSQL query is as follows −

CustomerID CustomerName Age Address CustomerSalary
1 Aarav 36 Udaipur 35000
4 Muhammad 29 Udaipur 50000
5 Sai 27 Mumbai 27000





Visit :


Discussion


* You must be logged in to add comment.