You are on page 1of 8

CHAPTER NINE

9. SQL JOIN AND SET OPERATORS


After completing this chapter, you will be able to do the following:
• Union
• Intersection
• Set difference/ Minus
• Inner Join
• Natural Join
• Equijoin
• Cross Join
• Outer Join (Left Outer Join, Right Outer Join, and Full Outer Join)
If you want to access more than one table through a select statement. If you want to combine two or
more table, then SQL JOIN and SET operators are used. It combines rows of that tables in one table
and one can retrieve the information by a SELECT statement. The joining of two or more tables is
based on common field between them. Example SQL query without join and set operators:
Staff table
StaffID Name Age City Salary
1 Amare 22 Addis Ababa 18000
2 Muluken 32 Adama 20000
3 Messay 25 Bahir Dar 22000
4 Alemu 20 Hawassa 12000
Payment table
PaymentID DATE StaffID Amount
101 30/12/2009 1 300.00
102 22/02/2010 3 250.00
103 23/02/2010 4 350.00
SELECT StaffID, Name, Age, Amount FROM STAFF, PAYMENT WHERE TAFF.StaffID =
PAYMENT.StaffID;
This will produce the result like this:
StaffID Name Age Amount
1 Amare 22 300.00
2 Muluken 32 250.00
3 Messay 25 350.00
1 Amare 22 300.00

9.1. Set Operators


9.1.1. Union Operator
The UNION operator selects only distinct values by default. To allow duplicate values, use UNION
ALL operator.
Syntax: SELECT column(s) FROM table1 UNION SELECT column(s) FROM table2;
1
By: Worku Muluye
Example Union: Select * from student UNION select * from instructor;
Syntax: SELECT column(s) FROM table1 UNION ALL SELECT column(s) FROM table2;
Example Union All: Select * from student UNION ALL select * from instructor;
Note: The column names in the result-set are usually equal to the column names in the first SELECT
statement in the UNION.
In order to perform the Union, Intersection, and the Difference operations on two relations, the two
relations should be union compatible.
• Each SELECT statement within UNION must have the same number of columns.
• The columns must also have similar data types.
• The columns in each SELECT statement must also be in the same order.
INSTRUCTOR
ID Fname Lname Age
1 Tomas Henok 19
2 Rahel Yonas 21
3 Yohannes Zekarias 30
Instructor Union Student

STUDENT
ID Fname Lname Age
1 Tomas Henok 19
2 Rahel Yonas 21
3 Abebe Isayas 16
Example Union: Select * from student UNION select * from instructor;
INSTRUCTOR Union STUDENT
ID Fname Lname Age
1 Tomas Henok 19
2 Rahel Yonas 21
3 Abebe Isayas 16
3 Yohannes Zekarias 30
Example Union All: Select * from student UNION ALL select * from instructor;
INSTRUCTOR UNION ALL STUDENT
ID Fname Lname Age
1 Tomas Henok 19
2 Rahel Yonas 21
3 Abebe Isayas 16
1 Tomas Henok 19
2 Rahel Yonas 21
3 Yohannes Zekarias 30

2
By: Worku Muluye
9.1.2. Intersection Operation
The result of this operation is a relation that includes all tuples that are in both Relation1 and
Relation2. The two operands must be "type compatible".
Syntax: SELECT * FROM Relation1 INTERSECT SELECT * FROM Relation2;
Example: Select * from student INTERSECT select * from instructor;
INSTRUCTOR INTERSECTION STUDENT
1 Tomas Henok 19
2 Rahel Yonas 21
9.1.3. Set Difference (MINUS) Operation
The result of this Set Difference (MINUS) operation is a relation that includes all tuples that are in
Relation1 but not in Relation2. The two operands must be "type compatible”.
Syntax: SELECT * FROM Relation1 EXCEPT SELECT * FROM Relation2;
Example: select * from Instructor EXCEPT select * from Student; -- Instructor - Student
INSTRUCTOR EXCEPT STUDENT
ID Fname Lname Age
3 Yohannes Zekarias 30
Example: select * from student except select * from instructor; -- Student - Instructor
STUDENT EXCEPT INSTRUCTOR
ID Fname Lname Age
3 Abebe Isayas 16
9.2. Join Operators
As the name shows, JOIN means to combine something. In case of SQL, JOIN means "to combine
two or more tables". A JOIN is a means for combining fields from two tables by using values
common to each.
9.2.1. Self-Join
A self-Join is a regular join, but the table is joined with itself as like if that is the second table. Self-
join is often very useful to convert a hierarchical structure to a flat structure.
Syntax: SELECT column-name(s) FROM table1 T1, table1 T2 WHERE condition;
9.2.2. Inner Join
The Inner Join selects records that have matching values in both tables. If you want to combine two
or more table, then SQL JOIN statement is used. The joining of two or more tables is based on common
field between them. It is also called simple join is the most common type of join.
Syntax: SELECT column-name(s) FROM table1 INNER JOIN table2 ON table1.column-name =
table2.column-name;

3
By: Worku Muluye
9.2.3. Natural Join Operation
The natural join performs an equijoin of the two relations R and S over all common attributes. A
natural join will remove duplicate attribute. In most systems a natural join will require that the
attributes have the same name to identity the attributes to be used in the join. This may require a
renaming mechanism. Even if the attributes do not have same name, we can perform the natural join
provided that the attributes should be of same domain.
Syntax: SELECT * FROM table1 NATURAL JOIN table2;
9.2.4. Equijoin Operation
Equijoin is a simple SQL join and Uses the equal sign (=) as the comparison operator for the condition.
In the result of an equijoin we always have one or more pairs of attributes (whose names need not be
identical) that have identical values in every tuple since we used the equality logical operator.
9.2.5. Cross Join
When each row of first table is combined with each row from the second table, known as Cartesian
join or cross join. In general words we can say that SQL CROSS JOIN returns the Cartesian product
of the sets of rows from the joined table. We can specify a CROSS JOIN in two ways:
Syntax1: SELECT * FROM [TABLE1] CROSS JOIN [TABLE2];
Synta2: SELECT * FROM [ TABLE1], [TABLE2];
Let us take an example of two tables,
STUDENT
FirstName Department-ID Skill-Level
Abebe 1 2
Dawit 1 3
Pawlos 2 2
Rahel 3 5
Department Table:
DEPARTMENT
Department-ID Department-Name
1 IS
2 IT
3 SE
Example: SELECT * FROM Student CROSS JOIN Department;
After executing this query, you will find the following result:
Player Department-ID Skill-Level Department-ID Department-Name
Abebe 1 2 1 IS
Dawit 1 3 1 IS
Pawlos 2 2 1 IS
Rahel 3 5 1 IS
Abebe 1 2 2 IT
Dawit 1 3 2 IT
4
By: Worku Muluye
Pawlos 2 2 2 IT
Rahel 3 5 2 IT
Abebe 1 2 3 SE
Dawit 1 3 3 SE
Pawlos 2 2 3 SE
Rahel 3 5 3 SE
9.2.6. Outer Join
Outer join is another version of the JOIN operation where non-matching tuples from the first Relation
are also included in the resulting Relation where attributes of the second Relation for non-matching
tuples from Relation one will have a value of NULL. In outer join, matched pairs are retained
unmatched values in other tables are left null.
9.2.6.1. Left Outer Join
The SQL left join returns all the values from the left table and it also includes matching values from
right table, if there are no matching join value it returns NULL.
Syntax: SELECT table1.column1, table2.column2...FROM table1 LEFTJOIN table2
ON table1.column_field = table2.column_field;
CUSTOMER
CustomerID Name Age Salary
1 Abebe 51 56000
2 Amare 21 25000
3 Dawit 24 31000
4 Solomon 23 32000
5 Sara 23 42000
ORDER TABLE:
ORDER
O-ID Date CustomerID Amount
001 20-01-2012 2 3000
002 12-02-2012 2 2000
003 22-03-2012 3 4000
004 11-04-2012 4 5000
Join these two tables with LEFT JOIN:
SQL SELECT CustomerID, Name, Amount, Date FROM CUSTOMER LEFT JOIN ORDER ON
CUSTOMER.CustomerID = ORDER.CUSTOMERID;
This will produce the following result:
CustomerID Name Amount Date
1 Abebe NULL NULL
2 Amare 3000 20-01-2012
2 Amare 2000 12-02-2012
3 Dawit 4000 22-03-2012
4 Solomon 5000 11-04-2012
5 Sara NULL NULL

5
By: Worku Muluye
9.2.6.2. Right Outer Join
The SQL right join returns all the values from the rows of right table. It also includes the matched
values from left table but if there is no matching in both tables, it returns NULL.
Syntax: SELECT table1.column1, table2.column2..., FROM table1 RIGHT JOIN table2 ON
table1.column_field = table2.column_field;
let us take an example with 2 tables table1 is CUSTOMER table and table2 is ORDER table.
two tables in this example to elaborate all the things:
CUSTOMER
Customer-ID Name Age Salary
1 Abebe 51 56000
2 Amare 21 25000
3 Dawit 24 31000
4 Solomon 23 32000
5 Sara 23 42000
ORDER TABLE:
ORDER
O-ID Date Customer-ID Amount
001 20-01-2012 2 3000
002 12-02-2012 2 2000
003 22-03-2012 3 4000
004 11-04-2012 4 5000
SELECT CUSTOMER-ID, Name, Amount, Date FROM CUSTOMER RIGHT JOIN ORDER
ON CUSTOMER. CUSTOMER-ID = ORDER.CUSTOMER-ID;
Customer –ID Name Amount Date
2 Amare 3000 20-01-2012
2 Amare 2000 12-02-2012
3 Dawit 4000 22-03-2012
4 Solomon 5000 11-04-2012
9.2.6.3. Full Outer Join
The SQL full join is the result of combination of both left and right outer join and the join tables have
all the records from both tables. It puts NULL on the place of matches not found. SQL full outer join
and SQL join are same. SQL full outer join is used to combine the result of both left and right outer
join and returns all rows (don’t care its matched or unmatched) from the both participating tables.
Syntax: SELECT *FROM table1 FULL OUTER JOIN table2 ON table1. Colname=table2. colname;
Note: here table1 and table2 are the name of the tables participating in joining and colname is the
column of the participating tables. Let us take two tables to demonstrate full outer join:
CUSTOMER

6
By: Worku Muluye
Customer–ID Name Age Salary
1 Abebe 51 56000
2 Amare 21 25000
3 Dawit 24 31000
4 Solomon 23 32000
5 Sara 23 42000
ORDER TABLE:
ORDER
O-ID Date CustomerID Amount
001 20-01-2012 2 3000
002 12-02-2012 2 2000
003 22-03-2012 3 4000
004 11-04-2012 4 5000
SELECT CUSTOMERID, Name, Amount, Date FROM CUSTOMER FULL JOIN ORDER
ON CUSTOMER. CUSTOMERID = ORDER.CUSTOMERID;
Let us take two tables to demonstrate full outer join:
table_A
A M
1 M
2 N
4 O

table_B
A N
2 P
3 Q
5 R
Resulting table
A M A N
2 N 2 P
1 M Null Null
4 O Null Null
Null Null 3 Q
Null Null 5 R
Because this is a full outer join so all rows (both matching and non-matching) from both tables are
included in the output. Here only one row of output displays values in all columns because there is
only one match between table_A and table_B.
Let’s retrieve data from three tables such as Department, Course, and Student.
Syntax: SELECT Table1.Table1-Column-name, Table2.Table2Colname FROM Table2
INNER JOIN Table1 ON Table2.Table2 or Table1Colname= Table1.Table2 or Table1Colname
ORDER BY Table2 or Table1Colname;
7
By: Worku Muluye
Table Department
DEPARTMENT
DepID DepName DepLocation
Dep1 IS CCI-145
Dep2 SE CCI-145
Dep3 CS CCI-145
Table Student
STUDENT
StudentID FirstName LastName DepID
1 Abebe Dawit Dep1
2 Ermias Yonas Dep1
3 Sara Abraham Dep2
Table Course
COURSE
CourseCode CourseTitle DepID
INSY2061 Database Dep1
INSY2063 OOP Dep1
INSY2064 Algorithm Dep1
Example: SELECT Department.DepID, Department.DepName, Student.StudentID,
Student.FirstName, Course.CourseCode, Course.CourseTitle FROM Department
LEFT JOIN Student ON Department.DepID= Student.DepID LEFT JOIN Course
ON Department.DepID= Course.DepID
DepID DepName StudentID FirstName CourseCode CourseTitle
Dep1 IS 1 Abebe INSY2061 Database
Dep2 SE 2 Ermias Null Null
Dep3 CS Null Null Null Null

8
By: Worku Muluye

You might also like