Joins Set Operations

advertisement
Experiment No. 8
Joins and Set Operations
Aim : To implement joins and set operations.
Theory :
JOINS
In databases, data is spread over multiple tables. Sometimes we may want data
from two or more tables. A join is an operation which combines results from two
or more tables. A single SQL can manipulate data from all the tables.
Types of JOIN
Inner Join / Equi Join
1. Outer Join
 Left Join
 Right Join
 Full Join
2. Cross Join / Cartesian Product
3. Self-Join
Inner Join :
The INNER JOIN keyword return rows when there is at least one match in both
tables. Includes only matching rows from both the tables where there is a match
OR An inner join between two (or more) tables is the Cartesian product that
satisfies the join condition in the WHERE clause. Most commonly used joins
Syntax :
SELECT column_name(s)
FROM table_name1
INNER JOIN table_name2
ON table_name1.column_name=table_name2.column_name
Eg.
Let us consider two tables.
The "Persons" table:
P_Id LastName FirstName Address
City
1
Hansen
Ola
Timoteivn 10 Sandnes
2
Svendson Tove
Borgvn 23 Sandnes
3
Pettersen Kari
Storgt 20
Stavanger
PIIT, New Panvel
Subject In charge – Smita Joshi
Experiment No. 8
The "Orders" table:
O_Id
OrderNo
1
77895
2
44678
3
22456
4
24562
5
34764
Joins and Set Operations
P_Id
3
3
1
1
15
List all the persons with any orders.
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
INNER JOIN OrdersON Persons.P_Id=Orders.P_Id
ORDER BY Persons.LastName;
Left OuterJoin :
The LEFT OUTER JOIN keyword returns all rows from the left table
(table_name1), even if there are no matches in the right table (table_name2).
Syntax :
SELECT column_name(s)
FROM table_name1
LEFT JOIN table_name2
ON table_name1.column_name=table_name2.column_name
Eg.
List all the persons and their orders - if any, from the tables above.
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
LEFT JOIN Orders
ON Persons.P_Id=Orders.P_Id
ORDER BY Persons.LastName;
PIIT, New Panvel
Subject In charge – Smita Joshi
Experiment No. 8
Joins and Set Operations
Right OuterJoin :
The RIGHT OUTER JOIN keyword returns all the rows from the right table
(table_name2), even if there are no matches in the left table (table_name1).
Syntax :
SELECT column_name(s)
FROM table_name1
RIGHT JOIN table_name2
ON table_name1.column_name=table_name2.column_name
Eg.
List all the orders with containing persons - if any, from the tables above.
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
RIGHT JOIN Orders
ON Persons.P_Id=Orders.P_Id
ORDER BY Persons.LastName;
Full Outer Join :
The FULL OUTER JOIN keyword return rows when there is a match in one of the
tables.
Syntax :
SELECT column_name(s)
FROM table_name1
FULL JOIN table_name2
ON table_name1.column_name=table_name2.column_name
Eg.
List all the persons and their orders, and all the orders with their persons.
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
FULL JOIN Orders
ON Persons.P_Id=Orders.P_Id
ORDER BY Persons.LastName;
PIIT, New Panvel
Subject In charge – Smita Joshi
Experiment No. 8
Joins and Set Operations
Self Join :
A self-join is a query in which a table is joined (compared) to itself. Self-joins are
used to compare values in a column with other values in the same column in the
same table.
Syntax :
SELECT column_list FROM table_A AS A
INNER JOIN table_A AS B
ON A.column_name1 = B.column_name2, ...
WHERE row_conditions;
Eg.
List the person name located in the same city.
SELECT a.LastName,b.FirstName
FROM Persons p
INNER JOIN Person q
ON a.LastName = b.FirstName;
SET OPERATIONS
Multiple queries can be put together & their result can be combined using
 UNION
 INTERSECT
 EXCEPT / MINUS
Points to be considered
 Number of columns & data types of the columns selected must be identical
in all SELECT statement. The names of the columns need not be identical
 Operates over all the columns being selected
 Null values are not ignored during duplicate checking
 Output is sorted in ascending order of the first column of the SELECT
clause
PIIT, New Panvel
Subject In charge – Smita Joshi
Experiment No. 8
Joins and Set Operations
Union :
Returns all distinct rows selected by either query. Union acts as OR operator.
Syntax :
SELECT column_name(s) FROM table_name1
UNION
SELECT column_name(s) FROM table_name2
Eg.
Consider following Tables
Student_comp
Roll_no
S_Name
01
Silvester
02
Roger
03
Student_it
Roll_no
01
02
03
04
Mark
S_Name
Jacob
Abby
Julio
Silvester
List all the different students in computer & IT.
SELECT S_Name FROM Student_comp
UNION
SELECT S_Name FROM Student_it;
PIIT, New Panvel
Subject In charge – Smita Joshi
Experiment No. 8
Joins and Set Operations
Union All :
Returns all rows selected by either query, including all duplicates.
Syntax :
SELECT column_name(s) FROM table_name1
UNION ALL
SELECT column_name(s) FROM table_name2
Eg.
List all the students in computer & IT.
SELECT S_Name FROM Student_comp
UNION ALL
SELECT S_Name FROM Student_it;
Intersect :
Returns all distinct rows selected by the query. Intersect acts as AND operator.
Syntax :
SELECT column_name(s) FROM table_name1
INTERSECT
SELECT column_name(s) FROM table_name2
Eg.
List the students who have same names.
SELECT * FROM Student_comp
INTERSECT
SELECT * FROM Student_it;
Minus :
Returns all distinct rows selected by the first query but not the second.
Syntax :
SELECT column_name(s) FROM table_name1
MINUS
SELECT column_name(s) FROM table_name2
PIIT, New Panvel
Subject In charge – Smita Joshi
Experiment No. 8
Joins and Set Operations
Eg.
Display names of students from computer but not in IT.
SELECT S_Name FROM Student_comp
MINUS
SELECT S_Name FROM Student_it;
Conclusion: Thus we have implemented joins to read data from multiple tables.
PIIT, New Panvel
Subject In charge – Smita Joshi
Download