0% found this document useful (0 votes)
2 views

unit-2-join

The document explains SQL joins, which allow for the retrieval of data from multiple tables based on common columns. It covers various types of joins including Inner Join, Natural Join, Left Outer Join, Right Outer Join, and Full Outer Join, along with their syntax and examples. Additionally, it discusses the use of aliases and the differences between Natural Join and Inner Join.

Uploaded by

pandyaj036
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

unit-2-join

The document explains SQL joins, which allow for the retrieval of data from multiple tables based on common columns. It covers various types of joins including Inner Join, Natural Join, Left Outer Join, Right Outer Join, and Full Outer Join, along with their syntax and examples. Additionally, it discusses the use of aliases and the differences between Natural Join and Inner Join.

Uploaded by

pandyaj036
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

SQL Joins:-

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:

SELECT columns_from_both_tables FROM table1


JOIN table2
ON table1.column1 = table2.column2
Here,

● 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

-- join the Customers and Orders tables


-- based on the common values of their customer_id columns

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

SQL JOIN and Aliases Name


-- use alias C for Customers table
-- use alias O for Orders table
SELECT C.customer_id, C.first_name, O.amount FROM Customers C
JOIN Orders O
ON C.customer_id = O.customer_id;

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

Syntax of INNER JOIN:-


SELECT columns_from_both_tables FROM table1
INNER JOIN table2
ON table1.column1 = table2.column2

EXAMPLE

1)INNER JOIN:-

The SQL joins two tables based on a common column,

and selects records that have matching values in these columns.

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

Here's an example of the with the WHERE clause:

The SQL command joins two tables and selects rows where the is greater than or equal to 500

SELECT Customers.customer_id, Customers.first_name, Orders.amount FROM Customers


INNER JOIN Orders
ON Customers.customer_id = Orders.customer_id WHERE Orders.amount >= 500;

SQL INNER JOIN With AS Alias

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.

SQL INNER JOIN With Three Tables


We can also join more than two tables using the .
For example,

SELECT C.customer_id, C.first_name, O.amount, S.status FROM Customers C


5
Prepared By:-
Darshana V.Halatwala
INNER JOIN Orders O
ON C.customer_id = O.customer_id INNER JOIN Shippings S
ON C.customer_id = S.customer_id;

Here, the SQL command


joins Customers and Orders table based on
and joins Customers and Status table based on
The command returns those rows where there is a match between column values in both join conditions.

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

NATURAL JOIN TABLE2;

Query:

Table-1: Department

Create Table department


(
DEPT_NAME Varchar(20),
MANAGER_NAME Varchar(255)
);
Table-2: Employee

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:

SELECT * FROM department;

Output :

7
Prepared By:-
Darshana V.Halatwala
Query to Implement SQL Natural Join.

SELECT *

FROM employee

NATURAL JOIN department;

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:-

Find the employee name who is working in a department.

Ans:-select e_name from emp natural join dept

Inner join:-
9
Prepared By:-
Darshana V.Halatwala
select e.ename from emp e,dept d

where e.deptno = d.deptno;

Natural JOIN vs INNER JOIN

NATURAL JOIN INNER JOIN

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

In Natural Join, If there is no condition specifies


In Inner Join, only those records will return which
then it returns the rows based on the common
exists in both the tables
column

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 :

SELECT column_list FROM table1, table2....


WHERE table1.column_name = table2.column_name;

Example –

SELECT student.name, student.id, record.class, record.city FROM student, record


WHERE student.city = record.city;

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:-

name id class city

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;

2.NON EQUI JOIN :

NON EQUI JOIN performs a JOIN using comparison operator other than

equal(=) sign like >, <, >=, <= with conditions.

14
Prepared By:-
Darshana V.Halatwala
Syntax:

SELECT *

FROM table_name1, table_name2

WHERE table_name1.column [> | < | >= | <= ] table_name2.column;

Example –

SELECT student.name, record.id, record.city FROM student, record


WHERE Student.id < Record.id ;
Output :

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:-

create table emp1


(
Empno number(10) primary key,
emp_name varchar(20)
);

create table dept111 (


dept_no number(6) primary key,
dept_name varchar(20),
emp_no number(10) references emp1(emp_no)
);
insert into dept111 values(11,’aaa’,1)
insert into dept111 values(12,’bbb’,2)
insert into dept111values(13,’ccc’,1)
desc emp1
select * from emp1
join query
Write an SQL query to retrieve all columns from two tables, emp1 and dept111, by performing a join operation.
select e1.*,d1.*
from emp1 e1,dept111 d1 where e1.emp_no=d1.emp_no;
OR
select *
from emp1 e1,dept111 d1 where e1.emp_no=d1.emp_no;

Inner join with where condition


Write an SQL query to retrieve the employee name and the department number from the table. where the emp_no is equal to
2.
17
Prepared By:-
Darshana V.Halatwala
select e1.ename,d1.dept_no from emp1 e1
inner join dept111 d1
on e1.emp_no=d1.emp_no where e1.emp_no=2;

OUTER JOIN

Left Outer join(LEFT JOIN):-


Left outer join produces a complete set of records from Table A, with the matching records (where available) in Table B. If there is no
match, the right side will contain null.

Syntax of LEFT JOIN


The syntax of is:
SELECT columns_from_both_tables FROM table1
LEFT JOIN table2
ON table1.column1 = table2.column2

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.

LEFT JOIN With WHERE Clause


The SQL command can have an optional WHERE clause with the statement. For example,

SELECT Customers.customer_id, Customers.first_name, Orders.amount FROM Customers


LEFT JOIN Orders
ON Customers.customer_id = Orders.customer_id WHERE Orders.amount >= 500;

Here, the SQL command joins two tables and selects rows where the is greater than or equal to 500.

SQL LEFT JOIN With AS Alias


We can use AS aliases inside to make our snippet short and clean.
For example,

SELECT C.cat_name, P.prod_title FROM Categories1 C


LEFT JOIN Products P
ON C.cat_id= P.cat_id;
Here, the SQL command selects common rows between and table.

SQL RIGHT JOIN:-

(Right Outer Join)

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.

Syntax of RIGHT JOIN


The syntax of is:

SELECT columns_from_both_tablesFROM table1 RIGHT JOIN table2


ON table1.column1 = table2.column2

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

SELECT Customers.customer_id, Customers.first_name, Orders.amount FROM Customers


RIGHT JOIN Orders

ON Customers.customer_id = Orders.customer;

Here's how this code work:-

Example: SQL RIGHT JOIN


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 (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,

SELECT Customers.customer_id, Customers.first_name, Orders.amount FROM Customers


RIGHT 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.

SQL RIGHT JOIN With AS Alias


We can use AS aliases inside to make our snippet short and clean. For
example,
SELECT C.cat_name, P.prod_title FROM Category C RIGHT JOIN Products P
ON C.cat_id= P.cat_id;
SQL FULL OUTER JOIN:-
Full outer join produces the set of all records in Table A and Table B, with matching records from both sides where available.

If there is no match, the missing side will contain null.

Syntax of FULL OUTER JOIN


The syntax of is:

23
Prepared By:-
Darshana V.Halatwala
SELECT columns FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name;

SQL FULL OUTER JOIN with the help of examples.


The SQL joins two tables based on a common
column, and selects records that have matching values in these columns and remaining rows from both of the tables.
Example

SELECT Customers.customer_id, Customers.first_name, Orders.amount FROM Customers


FULL OUTER JOIN Orders
ON Customers.customer_id = Orders.customer_id;

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 (of the
Orders table) along with all the remaining rows from both of the tables.
FULL OUTER JOIN With WHERE Clause
The SQL command can have an optional WHERE clause with the statement. For example,

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.

SQL FULL OUTER JOIN With AS Alias


We can use AS aliases inside to make out
snippet short and clean. For example,
SELECT C.cat_name, P.prod_title FROM Category C
FULL OUTER JOIN Products P
ON C.cat_id= P.cat_id;

Self Join, just like its name suggests, is a type of join that combines the records of a table with itself.

What is Self Join in SQL?

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:-

SELECT column_names FROM table1 T1, table1


T2 WHERE condition;

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

The following table has been created:

1. User Info

26
Prepared By:-
Darshana V.Halatwala
First Name Last Name City

John Doe Lahore

Sam Smith Karachi

Shawn Magen Lahore

First Name Last Name City

Homer Simpson Lahore

Bart Green Karachi

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:

SELECT A.City, B.name FirstName1, A.name FirstName2


FROM user_info A, user_info B WHERE A.name <> B.name AND A.city = B.city

Example of self join:-

Find the student id who is enrolled in at least two courses.


Select t1.s_id from study t1,study t2 where t1.s_id=t2.s_id and t1.c_id<>t2.c_id;

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:-

s_id c_id year

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.

See the syntax :


SELECT a.column_name, b.column_name... FROM table1 a, table1 b
WHERE a.common_filed = b.common_field;

29
Prepared By:-
Darshana V.Halatwala
30
Prepared By:-
Darshana V.Halatwala

You might also like