In PL/SQL (Procedural Language/Structured Query Language), the UNION operator is one of the most commonly used set operators. It combines the result sets of two or more SELECT statements into a single result set while removing any duplicate rows.
In this article, We will learn about PL/SQL UNION Operator with the help of examples and so on.
PL/SQL UNION Operator
- The
UNION
operator in PL/SQL, as well as in SQL is used to combine the results of two or more SELECT queries into a single result set.
- This operator is particularly useful for merging similar datasets from different tables or queries into one comprehensive dataset.
Syntax
The syntax of the UNION operator is straightforward. Here’s how you can use it:
SELECT column1, column2, ...
FROM table1
WHERE condition
UNION
SELECT column1, column2, ...
FROM table2
WHERE condition;
Explanation:
- SELECT column1, column2, ... FROM table1 WHERE condition: The first SELECT statement retrieves data from table1 based on a specified condition.
- UNION: The UNION operator combines the result of the first query with the second.
- SELECT column1, column2, ... FROM table2 WHERE condition: The second SELECT statement retrieves data from table2.
Example of PL/SQL UNION Operator
Step 1: Create Two Tables: employees and contractors
-- Create the employees table
CREATE TABLE employees (
employee_id NUMBER(5),
name VARCHAR2(50)
);
-- Create the contractors table
CREATE TABLE contractors (
contractor_id NUMBER(5),
name VARCHAR2(50)
);
Step 2. Insert Sample Data into the Tables
-- Insert data into the employees table
INSERT INTO employees (employee_id, name) VALUES (101, 'Alice');
INSERT INTO employees (employee_id, name) VALUES (102, 'Bob');
INSERT INTO employees (employee_id, name) VALUES (103, 'Charlie');
-- Insert data into the contractors table
INSERT INTO contractors (contractor_id, name) VALUES (201, 'Alice');
INSERT INTO contractors (contractor_id, name) VALUES (202, 'David');
INSERT INTO contractors (contractor_id, name) VALUES (203, 'Eve');
Employees Table
Contractor Table Example 1: Combining Results from Two Tables
Suppose we have two tables: employees and contractors. Both have columns for employee IDs and names. We want to get a list of all people working in our organization.
SELECT employee_id, name FROM employees
UNION
SELECT contractor_id, name FROM contractors;
Output:
Combining Results from Two TablesExplanation: This query selects the employee_id and name from both tables and combines them. The UNION operator ensures that duplicate names are removed, resulting in a unique list of all workers, whether they are full-time employees or contractors. The result will include:
- All unique names from both tables.
- The combined list sorted in ascending order by default.
Example 2: Using UNION with Different Conditions
Let’s extend our query by adding conditions. Assume we want to list only those employees and contractors whose names start with the letter "A".
SELECT employee_id, name FROM employees WHERE name LIKE 'A%'
UNION
SELECT contractor_id, name FROM contractors WHERE name LIKE 'A%';
Output
Using UNION with Different ConditionsExplanation: The query filters out names starting with "A" from both tables and merges the results. The UNION operator ensures that duplicates are removed while combining the results. The output will:
- Include only names that start with "A".
- Exclude any duplicates between the two tables.
- Present the final list sorted in ascending order.
Conclusion
The UNION operator is a powerful tool in PL/SQL that allows for the combination of multiple result sets while eliminating duplicates. It is especially useful when you want to merge data from different tables that share similar structures. Remember, the number and types of columns must match across the combined queries.
Similar Reads
SQL Interview Questions Are you preparing for a SQL interview? SQL is a standard database language used for accessing and manipulating data in databases. It stands for Structured Query Language and was developed by IBM in the 1970's, SQL allows us to create, read, update, and delete data with simple yet effective commands.
15+ min read
SQL Tutorial SQL is a Structured query language used to access and manipulate data in databases. SQL stands for Structured Query Language. We can create, update, delete, and retrieve data in databases like MySQL, Oracle, PostgreSQL, etc. Overall, SQL is a query language that communicates with databases.In this S
11 min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
SQL Joins (Inner, Left, Right and Full Join) SQL joins are fundamental tools for combining data from multiple tables in relational databases. Joins allow efficient data retrieval, which is essential for generating meaningful observations and solving complex business queries. Understanding SQL join types, such as INNER JOIN, LEFT JOIN, RIGHT JO
6 min read
Normal Forms in DBMS In the world of database management, Normal Forms are important for ensuring that data is structured logically, reducing redundancy, and maintaining data integrity. When working with databases, especially relational databases, it is critical to follow normalization techniques that help to eliminate
7 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
ACID Properties in DBMS In the world of DBMS, transactions are fundamental operations that allow us to modify and retrieve data. However, to ensure the integrity of a database, it is important that these transactions are executed in a way that maintains consistency, correctness, and reliability. This is where the ACID prop
8 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read