Set Operators in MySQL

Learn via video courses
Topics Covered

Overview

Set operators in MySQL allow you to manage and compare data sets. Data from one or more tables or queries can be combined, intersected, or subtracted using them. The three most frequently utilized set operators are UNION, INTERSECT, and EXCEPT. A single result set is created by combining the output of two or more SELECT operations using UNION. INTERSECT returns the rows shared by the result sets of two SELECT statements. The rows that are specific to the first SELECT statement and absent from the second SELECT statement are returned by EXCEPT.

Introduction

Relational databases employ set operators to modify and compare data sets. Simply put, a set operator creates a new set of data that satisfies particular requirements by combining or comparing two or more data sets.

The frequently used to set operators in MySQL are: UNION, UNION ALL, INTERSECT, and EXCEPT. With these operators, the output of two or more SELECT statements can be combined or compared.

To merge the output of two or more SELECT operations into a single result set, use UNION. Each SELECT statement's unique rows are included in the result set.

The UNION ALL operator in MySQL is used to combine the result sets of two or more SELECT statements into a single result set. Unlike the UNION operator, which removes duplicate rows, UNION ALL preserves all rows from all SELECT statements.

INTERSECT, on the other hand, returns only the rows that are shared by two SELECT statements' result sets. This operator is useful when comparing two tables to find only the common records between them.

Lastly, EXCEPT returns those rows from the first SELECT statement that are not present in the second. This operator is analogous to the difference between two sets in mathematics.

Set operators are powerful tools that allow you to manipulate data sets in various ways. As a result, you can increase the efficiency and performance of your database operations by incorporating them properly into your MySQL queries.

Necessary Condition for the Usage of Set Operators

Set operators in MySQL is a key feature that allows you to combine or compare data sets. However, certain prerequisites must be completed before utilizing set operators in your queries.

The first requirement is that the SELECT queries should have the same number of columns and be of the same data type. This ensures that the data sets are interoperable and easily integrated or compared.

According to the second criterion, the columns in the SELECT statements must be in the same order. This is significant because the set operators combine or compare rows based on the order of the columns.

Furthermore, the data types of the columns in the SELECT statements must be compatible. A set operator, for example, cannot be used to combine a column of integers with a column of strings.

Finally, the result set comprises unique rows when the UNION operator is used. Therefore, you must use the UNION ALL operator to include duplicate rows.

To summarise, before using set operators in MySQL queries, ensure all relevant conditions are met. This ensures that your queries run smoothly and produce accurate results.

UNION Operator

The UNION operator is one of the most often-used set operators in MySQL. It combines the results of two or more SELECT operations into a single result set.

The syntax for using the UNION operator is as follows:

Each SELECT statement's columns must be of the same data type and in the same order.

Here is an example of using the UNION operator to combine two tables:

Let's say we have two tables called employees and customers as follows:

employees:

idnamesalary
1John50000
2Jane60000
3Bob55000

customers:

idnamecity
1AliceBoston
2BobMiami
3CharlieAustin

Now, if we want to combine the names from both tables into a single result set, we can use the UNION operator like this:

Output:

name
Alice
Bob
Charlie
Jane
John

In this case, the result set will include all distinct names from the customers and employees tables. The result set contains only distinct values, i.e., there are no duplicates. Hence, Bob is included only once. If you want to include duplicates, you can use the UNION ALL operator instead of UNION.

You can also use the UNION operator with more than two SELECT queries by adding them and separating them with the UNION keyword.

UNION ALL Operator

In SQL, the UNION ALL operator merges the results of many SELECT operations into a single result set. The UNION ALL operator is similar to the UNION operator, with one major difference: UNION ALL does not eliminate duplicates from the result set. It instead returns all the records of both SELECT statements, including the duplicates.

The syntax for the UNION ALL operator is as follows:

Example:

Let's say we have two tables: employees and contractors. Both tables have the same columns - id, name, and department.

employees table:

idnamedepartment
E1JohnSales
E2JaneMarketing
E3WilliamFinance
E4SamanthaSales

contractors table:

idnamedepartment
C1MarkSales
C2JaneMarketing
C3WilliamFinance
C4SamanthaSales
C5AlexMarketing

We can use the UNION ALL operator to combine the results of two SELECT statements that select all columns from each table, like this:

The result set of this query will include all rows from both tables, with duplicate rows included:

Output:

idnamedepartment
E1JohnSales
E2JaneMarketing
E3WilliamFinance
E4SamanthaSales
C1MarkSales
C2JaneMarketing
C3WilliamFinance
C4SamanthaSales
C5AlexMarketing

INTERSECT Operator

In SQL queries, the INTERSECT operator is useful for determining the intersection of two result sets. It takes the output of two SELECT operations and returns only the rows that occur in both sets. In other words, the INTERSECT operator is used to locate records shared by two tables.

The syntax of the INTERSECT operator is as follows:

Example:

Assume we have two tables named table1 and table2 with the following data:

table1:

idnamecity
1AliceBoston
2BobMiami
3JaneAustin

table2:

idnamecity
4AliceBoston
5DavidAustin
6RachelMiami

To retrieve the common rows between the two tables based on the name and city columns, we can use the following SQL query:

Output:

namecity
AliceBoston

The INTERSECT operator returns only the rows that appear in both SELECT statements. In this example, it only returns the record with Alice and Boston since that is the only record that appears in both tables.

EXCEPT Operator

The EXCEPT operator is used to deduct the results of one query from the results of another. In other words, it only returns the entries unique to the first query and which do not appear in the second query.

The syntax of the EXCEPT operator is as follows:

Let's look at an example to see how the EXCEPT operator is used. Assume we have two tables, employees and managers.

employees table:

idnamedepartment
1AliceHR
2BobIT
3CharlieMarketing
4DavidIT
5EmilySales
6FrankFinance

managers table:

idnamedepartment
2BobIT
5EmilySales
7GeorgeFinance

Now, we want to find the employees who are not managers. The following SQL query can be used:

Output:

idnamedepartemnt
1AliceHR
3CharlieMarketing
4DavidIT
6FrankFinance

This query will only return the employees whose names do not appear in the managers table. In other words, only employees who are not managers will be included in the result set. In this case, the employees table has two rows that are also present in the managers table. Therefore, those rows will be removed from the output.

Combining Set Operators

By mixing the set operators in various ways, you may design complicated queries that can help you extract useful insights from your data. Consider using set operators the next time you need to write a more sophisticated query to make the process easier and more efficient.

Assume that we have a table named customers as:

idnamecity
1AliceBoston
2BobMiami
3AmandaAustin
4DavidBoston
5EmilyMiami

Now, consider the following example.

We want to combine the UNION and INTERSECT operators in MySQL with the same customers table. Suppose we want to find the names of customers who either live in Boston or Miami and whose names start with the letter 'A'. We can use the UNION and INTERSECT operators to achieve this. Here's how the query would look:

Output:

name
Alice
David

The above query will first execute the innermost SELECT statement, which retrieves the names of customers whose name starts with 'A'. Then, it will execute the second SELECT statement, which retrieves the names of customers who live in Miami. After that, it will apply the INTERSECT operator to these two result sets to find the names that are common in both result sets.

Finally, it will apply the UNION operator to the resulting set and the first SELECT statement, which retrieves the names of customers who live in Boston.

Using these set operators in MySQL, you can create more complex queries to get the information you need from your database.

Using Parentheses for Clarity

Using parenthesis when combining set operators to clarify and avoid confusion is critical. Parentheses indicate the order in which operations are performed, giving you control over the query's sequencing. Incorrect or unexpected consequences can occur if parenthesis is not used correctly. To avoid errors and ensure correct results, always use parenthesis to group the set operators in a form that makes sense and clearly explains the desired query.

Explanation of how to use parentheses to clarify complex queries

Using parentheses in complex queries can help to clarify the order of operations and make the query easier to read and understand. When a SQL query involves multiple conditions and operators, the order of evaluation can sometimes be ambiguous. Using parentheses makes it clear which conditions should be evaluated together.

Examples of using parentheses to clarify query logic

Consider the following example using the previous table customers. We want to find the names of the customers who live in Boston, and either whose names start with 'A' or who also live in Miami. Here's how the query would look:

Output:

name
Alice

In this query, we first execute the innermost SELECT statement, which retrieves the names of customers whose name starts with 'A'. Then, we execute the second SELECT statement, which retrieves the names of customers who live in Miami. After that, we apply the UNION operator to these two result sets to find the names that are either in the first set or the second set.

Finally, we apply the INTERSECT operator to the resulting set and the first SELECT statement, which retrieves the names of customers who live in Boston. There is only one customer i.e. Alice who meets these criteria.

By using parentheses to group the second and third SELECT statements, we change the order of operations, and therefore the output. The parentheses force the UNION operator to be evaluated before the INTERSECT operator.

Conclusion

  • Set operators in MySQL are strong tools that allow you to manage and compare data sets. They combine, intersect, and remove data from one or more tables or queries.

  • UNION merges the results of two or more SELECT statements into a single result set.

  • UNION ALL combines the results of two or more SELECT statements into a single result set, including duplicate rows.

  • INTERSECT only returns entries shared by the result sets of two SELECT statements.

  • EXCEPT returns only rows unique to the first SELECT statement and not found in the second.

  • To use set operators in MySQL, ensure that the queries being combined have the same number and order of columns, and the columns being compared must have compatible data types.

See Also

  • SELECT clause.
  • WHERE clause.
  • Logical Operators in MySQL.