The PL/SQL IN operator is a powerful tool used in SQL queries to check if a value matches any value in a list or a subquery result. It simplifies querying multiple values and can make your SQL code cleaner and more readable.
The IN operator is typically used in the WHERE clause to filter results based on a specified list of values. In this article, we will explain the concept of PL/SQL IN Operator in detail with proper syntax, examples, and output.
What is PL/SQL IN Operator?
The IN operator allows you to specify multiple values in a WHERE clause. It is a shorthand for multiple OR conditions. You can use it to simplify queries that involve checking a column for multiple possible values.
Simple Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE column_name IN (value1, value2, ...);
Explanation :
This query selects specific columns (column1
, column2
, etc.) from a table where the values in column_name
match any of the values listed in the IN
clause (value1
, value2
, etc.). The IN
operator simplifies filtering by allowing multiple criteria in a single condition.
With Subquery:
SELECT column1, column2, ...
FROM table_name
WHERE column_name IN (SELECT column_name FROM other_table WHERE condition);
Explanation :
This query retrieves specific columns from a table where the values in column_name
match any values returned by a subquery. The subquery selects column_name
from another table based on a specified condition, allowing you to filter data in the main query based on dynamically retrieved results from the subquery.
Example of PL/SQL IN Operator
The IN
operator in PL/SQL is used to filter data by specifying a set of values or a subquery that the target column must match. It simplifies SQL queries by allowing you to compare a column against multiple values in a single condition, making the code more concise and readable.
The IN
operator is especially useful when you need to check if a column value exists within a predefined list or a result set from a subquery. It eliminates the need for multiple OR
conditions and helps in managing complex queries more effectively.
1. Employees Table
Query:
CREATE TABLE employees (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(50),
dept_id INT
);
INSERT INTO employees (emp_id, emp_name, dept_id) VALUES (1, 'Alice', 10);
INSERT INTO employees (emp_id, emp_name, dept_id) VALUES (2, 'Bob', 20);
INSERT INTO employees (emp_id, emp_name, dept_id) VALUES (3, 'Charlie', 10);
INSERT INTO employees (emp_id, emp_name, dept_id) VALUES (4, 'David', 30);
INSERT INTO employees (emp_id, emp_name, dept_id) VALUES (5, 'Eve', 20);
Output:
emp_id | emp_name | dept_id |
---|
1 | Alice | 10 |
2 | Bob | 20 |
3 | Charlie | 10 |
4 | David | 30 |
5 | Eve | 20 |
Explanation:
The output shows the contents of the employees
table after inserting five records. Each row represents an employee with a unique emp_id
, their emp_name
, and the dept_id
indicating the department they belong to. The table accurately reflects the data inserted through the provided SQL queries.
2. Departments Table
Query:
CREATE TABLE departments (
dept_id INT PRIMARY KEY,
dept_name VARCHAR(50)
);
INSERT INTO departments (dept_id, dept_name) VALUES (10, 'HR');
INSERT INTO departments (dept_id, dept_name) VALUES (20, 'Finance');
INSERT INTO departments (dept_id, dept_name) VALUES (30, 'IT');
Output:
dept_id | dept_ name |
---|
10 | HR |
20 | Finance |
30 | IT |
Explanation:
The output displays the contents of the departments
table after inserting three records. Each row shows a unique dept_id
paired with its corresponding dept_name
, representing different departments: HR, Finance, and IT. This table correctly reflects the data inserted through the provided SQL queries
Example 1: Retrieve Employees in Specific Departments
The query selects the names of employees who belong to specific departments identified by their IDs. It checks if the dept_id of each employee matches any of the given department IDs (e.g., 10 or 20).
Query:
SELECT emp_name
FROM employees
WHERE dept_id IN (10, 20);
Output:
emp_name |
---|
Alice |
Bob |
Charlie |
Eve |
Explanation:
- The result includes names of employees who belong to departments 10 (HR) or 20 (Finance).
- This output displays all employees whose dept_id matches either of these values.
Example 2: Find Departments with Employees
This query identifies the departments that have at least one employee. It uses a subquery to retrieve department IDs from the employees table and then checks if each department in the departments table matches any of these IDs.
Query:
SELECT dept_name
FROM departments
WHERE dept_id IN (SELECT dept_id FROM employees);
Output:
Explanation:
- The result lists department names that have at least one employee.
- The departments returned are those with IDs present in the employees table.
Example 3: List Employees Not in Specific Departments
The query lists employees who are not in a particular department. By excluding a specific dept_id (e.g., 30), it retrieves names of employees from all other departments.
Query:
SELECT emp_name
FROM employees
WHERE dept_id NOT IN (30);
Output:
emp_name |
---|
Alice |
Bob |
Charlie |
Eve |
Explanation:
- The result displays names of employees not working in the department with ID 30 (IT).
- Employees from all other departments are included in the result.
Example 4: Retrieve Employees from Departments Listed in Another Table
This query finds employees who work in departments that are listed in the departments table with specific names (e.g., 'HR' or 'Finance'). It uses a subquery to select department IDs based on department names and then retrieves employee names corresponding to those IDs.
Query:
SELECT e.emp_name
FROM employees e
WHERE e.dept_id IN (SELECT d.dept_id FROM departments d WHERE d.dept_name IN ('HR', 'Finance'));
Output:
emp_name |
---|
Alice |
Bob |
Charlie |
Eve |
Explanation:
- The result shows names of employees working in departments named 'HR' or 'Finance'.
- This output is filtered based on department names found in the departments table.
Conclusion
The PL/SQL IN
operator is a powerful and flexible tool for querying data based on a set of values or the result of a subquery. It allows you to efficiently filter records by specifying multiple criteria in a single condition, simplifying your SQL code and improving its readability.
The IN
operator is particularly useful when you need to match a column against a list of possible values or when working with subqueries that return multiple rows. By using IN
, you can reduce the need for complex OR
conditions and streamline your queries, making your PL/SQL programs more maintainable and efficient.
Similar Reads
PL/SQL AND Operator
The PL/SQL AND operator is used to combine multiple conditions in a WHERE clause of an SQL query. It allows you to refine your query by ensuring that all specified conditions are met. AND queries which help in filtering data more precisely and can be crucial for retrieving accurate results from a da
7 min read
PL/SQL NOT Operator
PL/SQL, an extension of SQL in Oracle, offers various operators that allow us to perform logical operations on data. One such operator is the NOT operator, which is used to negate a condition, meaning it will return true if the condition is false and vice versa. The NOT operator is commonly used in
6 min read
PL/SQL MINUS Operator
The MINUS operator in PL/SQL (and Oracle SQL) is a powerful tool used to compare two datasets and identify differences. It effectively subtracts the result set of the second query from the result set of the first query, returning all rows from the first query that do not appear in the second. In thi
4 min read
SQL IN Operator
The SQL IN operator filters data based on a list of specific values. In general, we can only use one condition in the Where clause, but the IN operator allows us to specify multiple values. In this article, we will learn about the IN operator in SQL by understanding its syntax and examples. IN Opera
4 min read
PL/SQL LIKE Operator
The PL/SQL LIKE operator is a powerful tool used in SQL queries to search for patterns in character data. It allows you to match strings based on specific patterns defined by wildcards. This operator is commonly used in SELECT, UPDATE, and DELETE statements to filter records based on partial or comp
6 min read
PL/SQL Operators
The PL/SQL language offers various operators for data manipulation and logical processing. There are several types of these operators which include arithmetic operators, relational operators, comparison operators, and logical operators. In this guide, we will learn about the various PL/SQL operators
4 min read
PL/SQL EXISTS Operator
The EXISTS operator in PL/SQL is a powerful tool used to check the existence of records in a subquery. Unlike traditional comparison operators that evaluate data values, EXISTS focuses on whether a set of conditions returns any rows. It is commonly used to determine the presence or absence of record
6 min read
PL/SQL INTERSECT Operator
The PL/SQL INTERSECT operator is a powerful SQL set operation that allows us to return only the rows that are common to two or more SELECT queries. Unlike UNION or UNION ALL, which combine the results of different queries, INTERSECT focuses on finding the overlap between them. In this article, We wi
3 min read
PL/SQL ALL, ANY Operator
The ALL and ANY operators in PL/SQL are powerful tools used to compare a given value against a set of values returned by a subquery. These operators allow for more dynamic and flexible queries by evaluating conditions against multiple results. The ALL operator checks if a condition holds true for ev
4 min read
PL/SQL BETWEEN Operator
When working with databases, filtering data based on certain criteria is a common requirement. PL/SQL offers various operators to facilitate this, with one of the most effective being the BETWEEN operator. This operator enables us to select values that lie in a specific range whether those values ar
4 min read