1. JOIN , SET OPERATIONS
STD-XII
INFORMATICS PRACTICES
LESSON-8
2. I) INTRODUCTION pg(395)
II) JOINING TABLES-
A join is a query that combines rows from two or more tables.
In a join query, more than one table is listed in the from clause of select statement.
The function of combining data from multiple tables is called joining.
The joining of 2 tables can be restricted(based on some condition) or unrestricted(nobinding condition)
UNRESTRICTED JOIN(CartesianProduct)(Cross Join)-When two tables are joined without any binding condition,
such a type of join is called unrestricted join and the result is called the CartesianProduct.
3. All possible concatenations are formed of all rows of both the tables emp and dept. That is , when no particular
rows(using where clause) and columns(using select list) are selected. Such an operation is known as Unrestricted
Join.
Def-A cross join (cartesian product) is a very basic type of join that simply matches each rowfromone table to
every rowfromanother table.
Def-A Cartesian Product-shows all possible concatenations formedby all the rows of both the tables.
A cartesian product is formedwhen-
-A join condition is omitted
-A join condition is invalid
-All rows in the firsttable are joined to all rows in the second table.
4. (RESTRICTED JOIN) (JOIN)
In a selectquery fetching rows fromtwo tables, if we put a restrictionby adding a condition(where clause), it
will no longer be the Cartesian product. It will now be called a restrictedjoinor simply a join.
Def- Join is a query which combines rows fromtwo or more tables based on a condition.
Fromthe tables below ,can u tell me the locationof Smith????(needfor a join)
5. WE come back to the earlier query-What is the locationof Smith.
Select ename,loc
from dept,emp
where dept.deptno=emp.deptno
and ename=‘smith’;
If we don’t write the condition ename=‘smith’, then the result will look like
This is the join statement
The 2 tables have to be joinedon a common column.
This col in both the tables –
1.Should be of similar dts
2.May not have the same name and in that case the
query will look like-
Select ename,loc
From emp,dept
Where deptno=dno;
6. Joins can be created between views and tables also-
Eg-
So to avoidthis
duplicatecolumn→
or
or
Qualifiedfield name
III)Qualified Field Names-
7. Prog2-WAQtodisplay the ename, sal , deptno, dname from both the tables.
Order this table in ascending order of deptno
Qualified field name-It is a column name written in a query preceded by its table name. If a column is commonto
both the tables SQL needs to know the table from where it should be shown . If this column is not proceded with
its table name SQL gives an error.
IV TABLE ALIAS-
It is a new (temporary)name givento the table in the fromclause.
Prog3-WAQtodisplay the deptno, deptname, empno,nameof all the employees.Order the rows 1st by deptno and
then by empno.
Qualifiedfield name
8. Additional Search conditions in JOINS-
Prog4-
Prog5-Displaydetails like deptno, dname, empno, ename, job and salary. Order the rows by employee number
with dept number. These details should be only for employee earnings at least 1500 and stores department.
9. Prog6-WAQtodisplay the deptno, dname, empno, ename, sal of all the employees who work in the ‘Acc’. Sort the
rows in descending order of deptno
V) JOINING MORE THAN 2 TABLES- an eg of a Non-equi join
Prog7-Create the following table salgrade
*increase all the salaries by 1000
Not an equi join
10. TYPES OF JOINS-
1.Equi-Join:-A join in which the columns are compared for equality.
In an equi join-Allthe columns from the joiningtable appearin the output even if they are identical.
Prog8 -WAQ to displayall the fields from both the tables(emp & dept)using an equi-join.
2.Non-Equi Join:- A non-equi join is a query which specifies some relation, other than equalitybetween the columns of
the joining table.
Prog9-WAQ to displaythe empname, their sal and grade for all the employees who belon to department 10
output
11. 3.Natural Join-An equi-join minus one of the two-identical columns is called a Natural Join. The result of an
equi-join contains two identical columns,if one of the two identical columns is eliminated the result is called a
Natural Join.
A join in which only one of the identical columns coming fromthe Join tables exists is called a natural join.
Prog10-WAQto display all the fields from the emp and dept table using a natural join.
Joins covered so far-
1.Cross join
2.Equi join
3.Non-equi join
4.NaturalJoin
12. JOIN CLAUSE-Joiningtables using the join clause of the SQL statement:-
SYNTAX-
1. Cartesian Product/Unrestricted Join/Cross Join using the join clause
Prog11- Using the join clause create a cartesian product of the two tables emp and dept;
Cross join-It is a very basic type of a join which simply matches each row from one table to every row from
another table.
Select *
From <table1>
[Cross][Natural] join<table2>
[on(<join-condition>)|using(<join fields>)];
or
13. 2. Equi-Join using the join clause
Prog12-Create an equi join for emp and dept table using the join clause
Equi-Join using Join clause with condition
Prog13-WAQto display the Equi-Join of tables emp and dept for employees having sal greater than 1300
3.Natural Join using the join clause
Prog14-Createa natural join of emp and dept table using the join clause[using sub clause]
If name deptno
is not same in
both the tables,
then this query
will give an error.
14. Prog15-A natural join of dept and emp tables for all emp with sal>1300(using the natural keyword)
Prog16-Createa natural join using the JOIN CLAUSE(using subclause) between emp and dept with employees of
sal>1300
15. DIFF BETWEEN USING AND ON SUBCLAUSES OF THE JOIN CLAUSE
LEFT AND RIGHT JOINS-
When you join tables based on some condition, u may find that only some, not all rows from either table match the
rows of other table. When u display an equi-join or natural join, it shows only the matched rows. What if u want to
know which all rows from a table did not match with other. In such a case, MYSQL left or right join can be very helpful
and it refers to the order in which the tables are put together and the results are displayed.
On subclause Using subclause
1) It creates an equi-join. 1)It creates a Naturaljoin
2) The on subclause requires a complete
Join condition.
2)The using clause requires just the name
of the join field.
3)Eg select * from emp
Join dept
on(dept.deptno=emp.deptno);
3)Eg-select * from emp
Join dept
Using(deptno);
LEFT JOIN-
SYNTAX select <select list>
From <table1>
LEFT JOIN<table2>
On<joining-condition>;
LEFT JOIN-
16. In Left Join all the rows from the first table will be returned whether there are matched in the second table or
not. For unmatched rows of first table, NULL is shown in columns of second table.
Prog17-Writethe output of the following query-
Prog18-Writethe output
Dept table is written first so it
will appear 1st in the output
Here emp is written first so it will
appearfirst on the left side.
17. RIGHT JOIN- All the rows from the 2nd table are going to be returned whether or not there are matches in the 1st
table.
Prog19-Writethe output
Prog20-Writethe output of
Emp is the 1st
Therefore emp will be
displayed1st on the left side
It is a right join, so all the values from the
right side table will appear, but emp will
come on the left side.
18. Def-
Cross join
Equi join
Non-equi join
Natural join
Left join
Right join
*Note
Select*
From
Where (join can come here)
Group by
Having
Order by
20. PERFORMING SET OPEARTIONS ON RELATION
Sql Joins tend to combine columns fromtwo or more tables(width wise) and SQL set operations tend to combine rows from
two or more tables(length wise)
We will be learningabout 3 SET operations-
UNION
Multiplequeries can be combinedin one by forming a unionof them.
The sql UNION operatorallowsmanipulationof results returned by 2 or more queries by combining the results of each query
into a single result set.
Eg-
UNION
INTERSECTION
MINUS
The Union of these two can be created as follows-
1)The duplicaterow of Kush has been automatically removed.
2)By defaultthe UNION operatorremoves the duplicaterows from the result
3)If the ALL option is used then all rows ,including duplicates,are included in the results.
21. The general form of the UNION operator is-
Select statement
UNION [ALL]
Select statement;
Prog-1
22. SQL UNION With WHERE returns the distinct
values from both the tables
SQL UNION ALL With WHERE returns duplicate
values from both the tables
Prog-2
Prog-3
Note: The column names in the result-set are usually equal to the column names in the first SELECT statement
28. INTERSECT Operator
In SQL, you can use an intersect operation(intersect) to return rows that are common between two tables.
INTERSECT operation returns common rows from both the left and right tables.
This is useful when u want to find results that are in common between two queries. Prog9-
Inner join is same
as join.
same