Join query

15
Prepared by Aftab Alam Ashfaq Ahmad Waqar Ali Join Query

Transcript of Join query

Page 1: Join query

Prepared by Aftab Alam

Ashfaq AhmadWaqar Ali

Join Query

Page 2: 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.

Page 3: Join query

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.

Page 4: Join query

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.

Page 5: Join query

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

• In orders table we have following data

Page 6: Join query

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;

Page 7: Join query

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

Page 8: Join query

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;

Page 9: Join query

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

Page 10: Join query

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;

Page 11: Join query

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

Page 12: Join query

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;

Page 13: Join query

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

Page 14: Join query

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;

Page 15: Join query

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