In this Oracle tutorial, we will learn how to use IN condition in SQL. IN condition is used in SQL with every database.
IN condition helps to check multiple values in a column or we can say, it reduces the use of OR condition in a SQL statement.
As it is a condition and helps to limit the rows to be returned by SQL statement, it is used just after the WHERE clause.
Syntax-1: Using the expressions list
Syntax-2: Using the subquery/select query
Parameters or Arguments:
This is the column of the table in which we need to look the values.
value1, value2, … value_n
These are values that we need to check in the specified column. These values should be aligned between single quotes and comma separated as these are char values.
This is a SELECT statement whose result set will be tested against the values in the specified column.
Below explample is showing how we can select a few rows from Employees table (HR schema) using equality operator.
SELECT first_name,last_name,email FROM employees WHERE first_name = 'Steven' OR first_name = 'Neena' OR first_name = 'Lex' OR first_name = 'Alexander' OR first_name = 'Bruce';
Example-2 will show how easy it is when we use IN condition.
SELECT first_name, last_name, email FROM employees WHERE first_name IN ('Steven','Neena','Lex','Alexander','Bruce');
Both the SQL statements will give the same output but IN condition helps to shorten the code.
Example-3 will show you how we can we a select statement in IN condition.
SELECT first_name, last_name, email FROM employees WHERE first_name IN (SELECT first_name FROM employees WHERE department_id in (90,100));
Eventually, 3rd example shows both methods of using IN condition in SQL statement.
When we supply char values to the IN condition, we can only supply 1000 values.
If you have any questions/suggestions, please comment below or write me an email.
If you like this blog, do share with your friends and colleagues on your social media.
For more updates join my facebook group and do like my facebook page.