SQLite Union All Operator
Last Updated :
29 Jan, 2024
SQLite is a server-less database engine written in C programming language. It is developed by D. Richard Hipp in the year 2000. The main moto for developing SQLite is escaping complex database engines like MYSQL etc. It has become one of the most popular database engines as we use it in Television, Mobile Phones, Web browsers, and many more. It is written simply so that it can be embedded into other applications.
In this article, we will learn about the SQLite Union All operator, how it works, and its functionality with various examples.
SQLite UNION ALL Operator
SQLite UNION ALL operator combines multiple select statements and fetches the entire data. Union All fetches the duplicate rows too. The select statement must have the same number of columns with the same data type and the first select statement column names are used as the result set column names. It helps to unite the multiple select statements to retrieve the specified output. In simple words, it is used to fetch data from multiple tables.
- Here expression1, expression2,.....expression_n are nothing but the column names that we want to fetch.
- tables from which we are going to fetch the data.
- where the condition is optional.
SQLite Union All operator
This is how the union all operators is going to work as shown above. In the above example we can observe that two tables are combined together to retrieve the common rows with the duplicate columns.
Syntax:
SELECT expression1, expression2, ... expression_n
FROM tables
WHERE conditions
UNION ALL
SELECT expression1, expression2, ... expression_n
FROM tables
WHERE conditions;
Syntax Explanation: Here we are using the SELECT statement to fetch the data from the specified table and the condition of WHERE to be met inorder to move forward and followed by UNION ALL and the second select statement and table name from which we are going to fetch the data followed by the where condition(optional).
Example of SQLite Union All Operator
In this article we are going to use the department and employee table in order to understand the UNION ALL operator and below is the department table with two columns and they are dept_id and dept_name. If we don’t know How to Create Table in SQLite then refer this.
department table
The below is the employee table and table has four columns dept_id, emp_id, last_name and first_name.
Employee tableExample 1: Simple UNION All Operator
Query:
SELECT dept_id, dept_name
FROM department
WHERE dept_id <3
SELECT emp_id, last_name
FROM employee
WHERE emp_id<103;
Output:
Union All tableExplanation: Here we are going to fetch the department Id and name from the department table and where department Id is less than 3 and Union All is performed. The second select statement where we are going to fetch the employee Id and name from the employee table where employee Id is less than 103.
Here we can see that seven rows are fetched with the duplicate rows.
Example 2: UNION ALL Operator with Order By Clause
Now we are going to use UNION All operator with the ORDER BY clause. Order by clause is used to arrange the result set according to the order that we specify in our query. As we know union all is used to unite the multiple select statements and after fetching them the result set is arranged according to the specified query.
Syntax:
SELECT expression1, expression2, ... expression_n
FROM tables
WHERE conditions
UNION ALL
SELECT expression1, expression2, ... expression_n
FROM tables
WHERE conditions
ORDER BY;
Syntax Explanation: Here we are using the select statement to fetch the data from the specified table and the Where condition to be met inorder to move forward and followed by UNION ALL operator and the second select statement, followed by the table name from which we are going to fetch the data followed by the where condition and order by the specified column.
Query:
SELECT dept_id, dept_name
FROM department
WHERE dept_id <3
UNION ALL
SELECT emp_id, last_name
FROM employee
WHERE emp_id<103
ORDER BY dept_id desc;
Ouput:
union all order byExplanation: Here we are fetching the department id, department name, employee id and last name from department and employee table where department id is less than 3 and employee id is less than 103 in the descending order. Here seven rows are fetched with the two columns and they are department id and department name with the duplicate rows.
Example 3: UNION ALL Operator with Inner Join
Inner join is nothing but the simple join. We are going to use inner join with the union all to fetch the specified records from the tables. It only the returns the common records from both the tables.
Union ALL using Inner JOINSyntax:
SELECT columns
FROM table1
INNER JOIN table2
ON table1.column = table2.column
UNION ALL
SELECT columns
FROM table 2;
Syntax Explanation: Here we are going to combine two tables using the common column on dept_id and using INNER JOIN and applying Union All on the two SELECT statements.
Query:
SELECT d.dept_id
FROM department d
INNER JOIN employee e
ON d.dept_id = e.dept_id
UNION ALL
SELECT e.emp_id
FROM employee as e;
Output:
Unionall Inner joinExplanation: In the ouput we can see that eleven rows are retrieved that is dept_id and emp_id as those are the common rows in the two tables thay are joined together.
Example 4: UNION ALL Operator with Outer Join
The SQLite Union All does not support the Outer join. But we can use two left outer joins to form a Union All Outer Join. Below is the syntax that shows how we use left outer join to perform outer join functionality.
Syntax:
SELECT columns
FROM table1
LEFT JOIN table2
ON table1.column = table2.column
UNION ALL
SELECT columns
FROM table 2
LEFT JOIN table1
ON table1.column = table2.column;
Syntax Explanation: Two select statements followed by the table names and the columns names on which we are going to join them, by swapping the tables twice. We are joining the two tables using the left join inorder to perform the outer join.
Query:
SELECT d.dept_id
FROM department d
LEFT JOIN employee e
ON d.dept_id = e.dept_id
UNION ALL
SELECT e.dept_id
FROM employee as e
LEFT JOIN department d
ON d.dept_id = e.dept_id;
Output:
unionall outer joinExplanation: We are going to unite the department id column from two tables and in order to do that first we need to fetch the data by using the select statement and going to join the tables by using left outer joins. This is how the two left joins are joined together to work as a outer join and the department id column is retrieved with the twelve rows.
Conclusion
In this article we have learnt about the SQLite UNION ALL, UNION WITH ORDER BY operator with examples. UNION ALL is used to combine the result set of multiple select statement and allows duplicate rows and ORDER BY is used to arrange the rows in the specified order. By the end of the article you will get knowledge on the UNION ALL operator that is the functionality, when to use and where to use.
Similar Reads
SQLite Union Operator SQLite is a server-less database engine and it is written in c programming language. It is developed by D. Richard Hipp in the year 2000. The main moto for developing SQLite is to escape from using complex database engines like MYSQL etc. It has become one of the most popular database engines as we
5 min read
PL/SQL UNION ALL Operator In PL/SQL, the UNION ALL operator is a powerful tool that allows us to combine the results of two or more SELECT queries into a single result set. Unlike the UNION operator, which eliminates duplicate rows, UNION ALL includes all rows, including duplicates. This makes it faster and more efficient wh
4 min read
SQL UNION Operator The SQL UNION operator is used to combine the result sets of two or more SELECT queries into a single result set. It is a powerful tool in SQL that helps aggregate data from multiple tables, especially when the tables have similar structures.In this guide, we'll explore the SQL UNION operator, how i
4 min read
PL/SQL UNION Operator 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 Oper
3 min read
MySQL UNION ALL Operator The UNION ALL operator in MySQL combines the result sets of multiple SELECT statements by retaining all duplicate rows for improved performance and efficiency. It is particularly useful when complete data inclusion, including duplicates is required.In this article, We will learn about the MySQL UNIO
4 min read
MySQL UNION Operator Database management is an important concept of handling and organizing data effectively. One powerful Operator in MySQL for combining results from multiple queries is the UNION operator. This operator allows you to merge the results of two or more SELECT statements into a single result set, eliminat
4 min read
SQL UNION ALL UNION ALL Operator is used to combine the results of two or more SELECT statements into a single result set. Unlike the UNION operator, which eliminates duplicate records and UNION ALL includes all duplicates. This makes UNION ALL it faster and more efficient when we don't need to remove duplicates.
4 min read
SQL AND Operator In SQL, the AND operator is an essential tool used to combine multiple conditions in a WHERE clause. This allows us to filter records based on multiple criteria, making our queries more specific and tailored to our needs. When used correctly, the AND operator can help us retrieve data that satisfies
5 min read
SQL Server ALL Operator In SQL Server the logical operators are used to perform conditional checks in SQL Queries. There are many different types of Logical operators like ANY, AND, LIKE, EXISTS, NOT, IN, and BETWEEN in SQL Server. These operators are very useful to filter and compare single or multiple data in SQL Queries
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