unit-2-join
unit-2-join
Till now we have been selecting records from a single table at a time. but in real life we do not work only on a single table but we need
to select columns from two or more tables at a time for single query.
It Oracle Provides the facility to retrieve the data from multiple tables with the help of joins.
A join is used to combine rows from multiple tables. Join is performed whenever two or more tables is listed in the form caluse of an
SQL statement.
The Sql joins clause is used to combine records from two or more tables in a database .A Join is a means for combining fields from two
tables by using values common to each.
The most common scenario is a primary key from one to tables matches a foreign key in second table.
SQL JOINS
The SQL JOIN joins two tables based on a common column and selects records that have matching values in these columns.
SQL JOIN Syntax
The syntax of the SQL JOIN statement is:
● Table1 and Table2 are the two tables that are to be joined column1 is the column in table1 that is related to column2 in table2
Example
1
Prepared By:-
Darshana V.Halatwala
SELECT Customers.customer_id, Customers.first_name, Orders.item
FROM Customers JOIN Orders
ON Customers.customer_id = Orders.customer_id;
Here, the SQL command joins the Customers and Orders tables based on the common values in the customer_id columns of
both tables.
The result set will consist of
● customer_id and first_name columns from the Customers table item column from the Orders table
Types of Joins:-
1) Inner Join
2) Natural Join
3) Left (Outer) Join
4) Right (Outer) Join
5) Full Outer Join
2
Prepared By:-
Darshana V.Halatwala
Inner Join
Inner join produces only the set of records that match in both Table A and Table B
EXAMPLE
1)INNER JOIN:-
3
Prepared By:-
Darshana V.Halatwala
Example
SELECT Customers.customer_id, Customers.first_name, Orders.amount FROM Customers
INNER JOIN Orders
ON Customers.customer_id = Orders.customer_id;
4
Prepared By:-
Darshana V.Halatwala
Here, the SQL command
selects customer_id and first_name columns (from the Customers table) and the amount column (from the Orders table).
And, the result set will contain those rows where there is a match between customer_id (of the Customers table) and customer_id of
the Orders table
INNER JOIN With WHERE Clause
The SQL command joins two tables and selects rows where the is greater than or equal to 500
We can use AS aliases inside to make our snippet short and clean.
For example,
SELECT C.cat_name, P.prod_title FROM Categories C
INNER JOIN Products P
ON C.cat_id= P.cat_id;
Here, the SQL command selects common rows between and table.
Natural Join
Natural join is an SQL join operation that creates a join on the base of the common columns in the tables. To perform natural join there
must be one common attribute(Column) between two tables. Natural join will retrieve from multiple relations.
Syntax:-
We will perform the natural join query by using the following syntax.
SELECT *FROM TABLE1
Query:
Table-1: Department
6
Prepared By:-
Darshana V.Halatwala
Create Table employee
(
EMP_ID int,
EMP_NAME Varchar(20),
DEPT_NAME Varchar(255)
);
SELECT * FROM employee;
Output :
Query:
Output :
7
Prepared By:-
Darshana V.Halatwala
Query to Implement SQL Natural Join.
SELECT *
FROM employee
Output :
8
Prepared By:-
Darshana V.Halatwala
Example:-2
Table 1 Emp(Emp_id,E_name,addr)
Table2 Dept(dept_no,name,emp_id)
Query:-
Inner join:-
9
Prepared By:-
Darshana V.Halatwala
select e.ename from emp e,dept d
Natural Join joins two tables based on same Inner Join joins two table on the basis of the column
attribute name and datatypes. which is explicitly specified in the ON clause.
The resulting table will contain all the attributes In Inner Join, The resulting table will contain all the
of both the tables but keep only one copy of each attribute of both the tables including duplicate
common column columns also
10
Prepared By:-
Darshana V.Halatwala
NATURAL JOIN INNER JOIN
Syntax-
Syntax-
SELECT*
SELECT*
FROM table1 INNER JOIN table2 ON
FROM table1 NATURAL JOIN table2;
table1.Column_Name= table2.Column_Name;
11
Prepared By:-
Darshana V.Halatwala
Equi Join Example:-
1.EQUI JOIN :-
EQUI JOIN creates a JOIN for equality(any column used equal sign) or matching column(s) values of the relative tables.
EQUI JOIN also create JOIN by using JOIN with ON and then providing the names of the columns with their relative tables to check
equality using equal sign (=).
Syntax :
Example –
Or
Syntax :
SELECT column_list
FROM table1
JOIN table2
[ON (join_condition)]
12
Prepared By:-
Darshana V.Halatwala
Example –
SELECT student.name, student.id, record.class, record.city FROM student
JOIN record ON student.city = record.city;
OUTPUT:-
Hina 3 3 Delhi
Megh
4 3 Delhi
a
Gouri 6 3 Delhi
Hina 3 2 Delhi
Megh
4 2 Delhi
a
13
Prepared By:-
Darshana V.Halatwala
Gouri 6 2 Delhi
Hina 3 2 Delhi
Megh
4 2 Delhi
a
Gouri 6 2 Delhi
Example 2:-
emp(eno,ename,addr)
(1,ram,delhi) (2,varun,bharuch) (3,shayam,surat)
dept(deptno,location,eno)(d1,delhi,1)(d2,pune,2)
find the employee who worked in department having location same as address.
select e.ename from emp e ,dept d where e.eno=d.eno and e.addr=d.location;
NON EQUI JOIN performs a JOIN using comparison operator other than
14
Prepared By:-
Darshana V.Halatwala
Syntax:
SELECT *
Example –
15
Prepared By:-
Darshana V.Halatwala
name Id city
Hina 9 Delhi
Megha 9 Delhi
Gouri 9 Delhi
Hina 10 Delhi
Megha 10 Delhi
Gouri 10 Delhi
Hina 12 Delhi
Megha 12 Delhi
16
Prepared By:-
Darshana V.Halatwala
Join query examples:-
OUTER JOIN
The SQL joins two tables based on a common column, and selects records that have matching values in these columns
18
Prepared By:-
Darshana V.Halatwala
and remaining rows from the left table.
Example
SELECT Customers.customer_id, Customers.first_name, Orders.amount FROM Customers
LEFT JOIN Orders
ON Customers.customer_id = Orders.customer_id;
19
Prepared By:-
Darshana V.Halatwala
Here, the SQL command
selects customer_id and first_name columns (from the Customers table) and the amount column (from the Orders table).
And, the result set will contain those rows where there is a match between customer_id (of the Customers table) and
20
Prepared By:-
Darshana V.Halatwala
customer (of the Orders table) along with all the remaining rows from the Customers table.
Here, the SQL command joins two tables and selects rows where the is greater than or equal to 500.
21
Prepared By:-
Darshana V.Halatwala
Right outer join produces a complete set of records from Table B, with the matching records (where available) in Table A. If there is
no match, the left side will contain null.
The SQL joins two tables based on a common column, and selects records that have matching values in these columns and
remaining rows from the right table.
Example
ON Customers.customer_id = Orders.customer;
between customer_id (of the Customers table) and customer (of the Orders table) along with all the remaining rows from the Orders
table.
22
Prepared By:-
Darshana V.Halatwala
RIGHT JOIN With WHERE Clause
The SQL command can have an optional WHERE clause with the statement.
For example,
23
Prepared By:-
Darshana V.Halatwala
SELECT columns FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name;
24
Prepared By:-
Darshana V.Halatwala
SELECT Customers.customer_id, Customers.first_name, Orders.amount FROM
Customers
FULL OUTER JOIN Orders
ON Customers.customer_id = Orders.customer WHERE Orders.amount >= 500;
Here, the SQL command joins two tables and selects rows where the is greater than or equal to 500.
Self Join, just like its name suggests, is a type of join that combines the records of a table with itself.
A JOIN clause is used to combine rows from two or more tables based on a related column between them. A self JOIN is a regular join,
25
Prepared By:-
Darshana V.Halatwala
but the table is joined with itself – this is extremely useful for comparisons within a table.
Joining a table with itself means that each row of the table is combined with itself and with every other row of the table.
Syntax:-
Aliases for the actual table names are used to distin- guish column names from one another since both of the tables have the same name. T1
and T2 are aliases for the same table.
Example
1. User Info
26
Prepared By:-
Darshana V.Halatwala
First Name Last Name City
27
Prepared By:-
Darshana V.Halatwala
To match customers that are from the same city,
we have used the following SQL query that self joins the table:
Here s_id primary key in student table and course id is primary key in course table in study table here s_id and c_id is the references of this.
Study table:-
S1 C1 2016
S2 C2 2017
S1 C2 2017
A self join is a join in which a table is joined with itself (which is also called Unary relationships), especially when the table has a
FOREIGN KEY which references its own PRIMARY KEY. To join a table itself means that each row of the table is combined with
28
Prepared By:-
Darshana V.Halatwala
itself and with every other row of the table.
The self join can be viewed as a join of two copies of the same table. The table is not actually copied, but SQL performs the
command as though it were.
The syntax of the command for joining a table to itself is almost same as that for joining two different tables. To distinguish the column
names from one another, aliases for the actual the table name are used, since both the tables have the same name. Table name aliases
are defined in the FROM clause of the SELECT statement.
29
Prepared By:-
Darshana V.Halatwala
30
Prepared By:-
Darshana V.Halatwala