You are on page 1of 14

SET OPERATORS

• UNION
• INTERSECT
• UNION ALL
• MINUS
Set operators

• Set operators allow you to combine the results of multiple separate queries into a
single result set.
• UNION : Set operator returns all distinct rows selected by either query. That means
any duplicate rows will be removed
• UNION ALL : set operator returns all rows selected by either query. That means any
duplicates will remain in the final result set
• INTERSECT : set operator returns all distinct rows selected by both queries. That
means only those rows common to both queries will be present in the final result set
• MINUS : set operator returns all distinct rows selected by the first query but not the
second.
Set Operators

• All of these Set operators remove duplicates, except for the Union All
operator
• The output column names are referred from the first query i.e. when we run
the SELECT statements with any of the Set operators and result set of each
of the queries may have different column names, so the result of the
SELECT statement refers the column names from the first query in the
operation.
• SQL JOIN is more often used combine columns from multiple related tables
whereas SET Operators combines rows from multiple tables.
• When the expression types are the same but differ in precision, scale, or
length, the result is determined based on the same rules for 
combining expressions
Rules to Use Set Operators

1.Expressions in each row or the number of columns that are defined in


each query must have the same order
2.Subsequent SQL statement row sets must match the data type of the first
query
3.Parentheses are allowed to construct other set operators in the same
statement
4.It possible to have an ORDER BY clause, but that should be the last
statement of the SQL
5.GROUP BY and HAVING clauses can be applied to the individual query
Joins and Union

• Both Joins and UNIONS can be used to combine data from two or more tables.
• Joins and Unions can be used to combine data from one or more tables.  The difference lies in how
the data is combined.
• Union is a set operator that can be used to combine the result set of two different
SELECT statement. In the union number of columns and data type should be the
same. tables are associated with each other and we used foreign key to maintain
relationships between tables.
• We used join clause to retrieve data from associated tables. The join condition indicates
how column in each table are matched against each other. 
• joins combine data into new columns.  If two tables are joined together, then the data from the
first table is shown in one set of column alongside the second table’s column in the same row.
• unions combine data into new rows.  If two tables are “unioned” together, then the data from the
first table is in one set of rows, and the data from the second table in another set.  The rows are in
the same result.
Joins and Union
• Each row in the result contains columns from BOTH table A and B.  Rows are
created when columns from one table match columns from another.  This match is
called the join condition.
• This makes joins really great for looking up values and including them in results. 
• This is usually the result of denormalizing (reversing normalization) and involves
using the foreign key in one table to lookup column values by using the 
primary key in another.
• In a union, each row within the result is from one table OR the other.  In a union,
columns aren’t combined to create results, rows are combined.
JOIN and UNION
1.JOIN combines data from one (self join) or more tables horizontally, whereas
UNION combines data vertically.
2.JOIN combines columns and UNION combines rows.
3.JOIN combines data when the involved tables have common attributes for at
least one column, whereas UNION combines data when the involved tables
have (1) the same number of columns, and (2) the columns are compatible.
4.In real world database, JOINs are normally based on primary keys and foreign
keys in join condition, whereas UNION does not have union condition except
point 3 above.
5.FULL (OUTER) JOIN can potentially return very large result sets (Note: MySQL
does not support FULL JOIN).
6.Variants of JOIN are inner join, left join, right join, full join, and cross join,
whereas UNION's variants are UNION and UNION ALL.
7. In not very strict terms, INTERCEPT and MINUS/EXCEPT could also be viewed
as variants of UNION because they combine data vertically as well, just like
what UNION does.
•  Union used where you have two results whose rows you want to
include in the same result. 
• A use case may be that you have two tables:  Teachers and Students. 
You would like to create a master list of names and birthdays sorted by
date.
• To do this you can use a union to first combine the rows into a single
result and then sort them.
Sr. No. Key Join UNION

1 Basic  It can be used to retrieve It can be used to combine the


matched records between both result set of two different SELECT
tables or more tables  statement.

2         Data type  Result set can have different Data type should be same as the
types of data types   result set of each select
statement

3 Duplicate  It doesn't remove duplicate data. It removes duplicate rows


between the various select
statements.
UNION
•  Union operator
combines the results
of two or more queries
into a distinct single
result set that includes
all the rows that
belong to all queries in
the Union. In this
operation, it combines
two more queries and
removes the
duplicates.
UNION ALL
• The Union operator combines the results of two or more queries
into a single result set that includes all the rows that belong to
all queries in the Union.
• In simple terms, it combines the two or more row sets and
keeps duplicates.
Intersect
• Intersect operator keeps
the rows that are common
to all the queries
EXCEPT

• EXCEPT operator lists the rows in


the first that are not in the
second.
Examples
• Tables ( student ,subject) created and add values
 Create table student (roll no number(3),sname varchar(10);;
 Insert into student values(………….);
 Create table subject(rollno number(3), subjected varchar(10),subjectname varchar(15));

UNION
 > select * from Student UNION select * from subject;
(this exclude duplicates)
 > select * from Student UNION ALL select * from subject;
(UNION ALL operator returns all the rows from both the queries, including the duplicate)
 > select * from Student UNION ALL select * from subject;
 > select * from Student Except select * from subject;

You might also like