Join query

Post on 15-Jan-2017

325 views 0 download

Transcript of Join query

Prepared by Aftab Alam

Ashfaq AhmadWaqar Ali

Join Query

What is Join Query?• Join is used to combine records from

two or more tables in a database.• In database Join means combining

fields from two tables by using values common to each.

• The data which is fetch from two or more tables, is appear as single set of data.

Syntax• Simple Join Query : SELECT ColumnName_list from table1,table2 WHERE table1.col = table2.col;

But it is old version.

• Simple join is also called inner join. but the syntax of inner join is different, Both work same as simple query does.

SELECT ColumnName_list from table1 INNER JOIN

table2 ON table1.col = table2.col;

• It is also recommended syntax by Oracle.

Example• Consider two tables CUSTOMER and ORDER

• CUSTOMER In customer table we have three columns.Customer_id

name age.

• ORDERIn ORDER table we have order_idorder_datecustomer_idamount number

• customer_id is common in both tables. And also we can use it for relationship between these two tables.

Customer and Order tables• In customer table we have following data

• In orders table we have following data

Joining tablesTo joining the previous two tables

• We use the following queries: Old: select order_id, name, order_date, amount from customer, orders WHERE customer.customer_id = orders.customer_id;

Recommend: select order_id, name, order_date, amount from customer INNER JOIN orders on customer.customer_id = orders.customer_id;

Types of Join Query1. Inner Join2. Left Join3. Right Join4. Full Join

There are also more types of join but we will discuss only the above types

Inner Join• It selects all rows from both tables when

there is a columns and its values match between two tables.

• In other words it returns rows when there is a match in both tables.

Syntax:

SELECT column_name(s)FROM table1INNER JOIN table2 ON

table1.column_name=table2.column_name;

Inner Join Example• Query Select * from customer inner join orders on customer.customer_id = orders.customer_id;

Left Join• It returns all rows from the left table (table1),

with the matching rows in the right table (table2). The result is NULL in the right side when there is no match.

• In other words it returns all rows from the left table, even if there are no matches in the right table.

Syntax:SELECT column_name(s)FROM table1

LEFT JOIN table2 ON table1.column_name=table2.column_name;

Left Join Example• Query Select * from customer left join orders on customer.customer_id = orders.customer_id;

Right Join• The RIGHT JOIN keyword returns all rows from

the right table (table2), with the matching rows in the left table (table1). The result is NULL in the left side when there is no match.

• In other words returns all rows from the right table, even if there are no matches in the left table.

Syntax:SELECT column_name(s)FROM table1RIGHT JOIN table2 ON

table1.column_name=table2.column_name;

Right Join Example• Query Select * from customer right join orders on customer.customer_id = orders.customer_id;

Full Join• It combines the results of both left and right

table.• The joined table will contain all records from

both tables, and fill in NULLs for missing matches on either side.

Syntax:SELECT column_name(s)FROM table1FULL JOIN table2 ON

table1.column_name=table2.column_name;

Full Join Example• Query Select * from customer full join orders on customer.customer_id = orders.customer_id;