CS232L-Lab06
CS232L-Lab06
LAB#06
Postgres Joins
Objective
The objective of this session is to
1. Introduction of Joins
2. Types of Joins
3. Examples
Instructions
• Open the handout/lab manual in front of a computer in the lab during the session.
• Practice each new command by completing the examples and exercise.
• Turn-in the answers for all the exercise problems as your lab report.
• When answering problems, indicate the commands you entered and the output displayed.
1
CS232-L – LAB 6
POSTGRES JOINS
A PostgreSQL Join statement is used to combine data or rows from one(self-join) or more tables based on a
common field(or column) between them. These common fields are generally the Primary key of the first
table and Foreign key of other tables.
There are 4 basic types of joins supported by PostgreSQL, namely:
SELECT columns
FROM table1
INNER JOIN table2
ON table1.column = table2.column;
2
CS232-L – LAB 6
EXAMPLE:
Sample table: Customer Sample table: Item Sample table: Invoice
SELECT *
FROM invoice
ON invoice.item_no=item.item_no;
Output:
SELECT *
FROM invoice
ON invoice.item_no=item.item_no
WHERE
item.rate>=10;
Output:
3
CS232-L – LAB 6
Outer Join
LEFT OUTER JOIN or LEFT JOIN
While joining the table using the left outer join, PostgreSQL first does normal join and then it starts scanning
from the left table. PostgreSQL left join retrieves all rows from the left table and all matching rows from the
right table. If there is no match in both tables, the right tables have null values.
Syntax:
Select *
FROM table1
LEFT [ OUTER ] JOIN table2
ON table1.column_name=table2.column_name
The PostgreSQL LEFT OUTER JOIN would return the all records from table1 and only those records from table2 that intersect
with table1.
Example:
Code:
SELECT item.item_no,item_descrip,
invoice.invoice_no,invoice.sold_qty
FROM item
ON item.item_no=invoice.item_no;
OR
Code:
SELECT item.item_no,item_descrip,
invoice.invoice_no,invoice.sold_qty
4
CS232-L – LAB 6
FROM item
ON item.item_no=invoice.item_no;
Output:
Explanation :
In the above example, the item_no I8 of item table not exists in the invoice table, and for this rows of item table
a new row in invoice table have generated and sets the NULL for this rows.
from the right table. PostgreSQL right join retrieves all rows from the right table and all matching rows from the
left table. If there is no match in both tables, the left tables have null values.
Syntax:
Select *
FROM table1
RIGHT [ OUTER ] JOIN table2
ON table1.column_name=table2.column_name;
In this visual diagram, the PostgreSQL RIGHT OUTER JOIN returns the shaded area:
5
CS232-L – LAB 6
The PostgreSQL RIGHT OUTER JOIN would return the all records from table2 and only those
Example:
Code:
SELECT invoice.invoice_no,invoice.sold_qty,
item.item_no,item_descrip
FROM invoice
ON item.item_no=invoice.item_no;
OR
Code:
SELECT invoice.invoice_no,invoice.sold_qty,
item.item_no,item_descrip
FROM invoice
ON item.item_no=invoice.item_no;
Output:
6
CS232-L – LAB 6
In the above example, the item_no I8 of item table not exists in the invoice table, and for this row in the item
table, a new row have been generated in the invoice table and sets the value NULL for this row.
PostgreSQL full outer join returns all rows from the left table as well as the right table. It will put null when
the full outer join condition was not satisfied. While joining the table using FULL OUTER JOIN first, it will
join be using an inner join. The combination of left and right join is known as a full outer join.
Syntax:
SELECT * | column_name(s)
FROM table_name1
FULL [OUTER] JOIN table_name2
ON table_name1.column_name=table_name2.column_name
The PostgreSQL FULL OUTER JOIN would return the all records from both table1 and table2.
Example:
Code:
SELECT item.item_no,item_descrip,
invoice.invoice_no,invoice.sold_qty
FROM invoice
ON invoice.item_no=item.item_no
ORDER BY item_no;
7
CS232-L – LAB 6
OR
Code:
SELECT item.item_no,item_descrip,
invoice.invoice_no,invoice.sold_qty
FROM invoice
ON invoice.item_no=item.item_no
ORDER BY item_no;
Output:
Explanation
In the above example, the matching rows from both the table item and invoice have appeared, as well
as the unmatched row i.e. I8 of item table which is not exists in the invoice table have also appeared,
and for this row of item table a new row in invoice table have generated and sets the value NULL .
The Cross Join creates a cartesian product between two sets of data. This type of join does not maintain any
relationship between the sets; instead returns the result, which is the number of rows in the first table multiplied
by the number of rows in the second table. It is called a product because it returns every possible combination
Syntax:
SELECT [* | column_list]
FROM table1
CROSS JOIN table2;
OR
SELECT [* | column_list]
FROM table1,table2;
EXAMPLE:
OR
Code:
SELECT customer.cust_no, customer.cust_name,
invoice.invoice_no,invoice.cust_no,invoice.item_no,
invoice.sold_qty,invoice.disc_per
FROM customer,invoice;
Output:
9
CS232-L – LAB 6
Explanation
In the above example, the 'customer' table and 'invoice' table join together to return a
cartesian product. Here in the above example the two rows of 'customer' table joining with 4
rows of 'invoice' table and makes a product of 4*2 rows.
ANTI-JOINS
Even after learning the principles of inner, outer, left, and right joins, you might still have a nagging question:
how do I find values from one table that are NOT present in another table?
That's where anti joins come in. They can be helpful in a variety of business situations when you're trying to
find something that hasn't happened, such as:
Anti-join will return rows, when no matching records are found in the second table. It is quite opposite to the
semi join, since it is returning records from the first table, when there is no match in the second table.
10
CS232-L – LAB 6
• A left anti join : This join returns rows in the left table that have no matching rows in the right table.
• A right anti join : This join returns rows in the right table that have no matching rows in the left table.
Syntax:
An anti join doesn't have its own syntax - meaning one actually performs an anti join using a combination of
other postgres sql queries. To find all the values from Table_1 that are not in Table_2, you'll need to use a
combination of LEFT JOIN and WHERE.
SELECT * FROM Table_1 t1 LEFT JOIN Table_2 t2 ON t1.id = t2.id WHERE t2.id IS NULL
The above entire query will return only values that are in Table_1 but not in Table_2.
Example:
PostgreSQL optimizes both LEFT JOIN / IS NULL and NOT EXISTS in the same way.
11
CS232-L – LAB 6
Semi-join:
Semi join will return a single value for all the matching records from the other table. That is, if the second table
has multiple matching entries for the first table's record, then it will return only one copy from the first table.
However, a normal join it will return multiple copies from the first table.
EXAMPLE:
ORDER BY departments.department_id;
Output:
12
CS232-L – LAB 6
Semi or anti joins are kind of sub join types to the joining methods such as hash, merge, and nested loop, where
the optimizer prefers to use them for EXISTS/IN or NOT EXISTS/NOT IN operators.
Difference between anti-join and semi-join
While a semi-join returns one copy of each row in the first table for which at least one match is found, an anti-
join returns one copy of each row in the first table for which no match is found.
EQUI-JOINS:
Oracle Equi join returns the matching column values of the associated tables. It uses a comparison operator in the
Equijoin also can be performed by using JOIN keyword followed by ON keyword and then specifying names of
Syntax:
SELECT *
FROM table1
JOIN table2
[ON
(join_condition)]
13
CS232-L – LAB 6
What is the difference between Equi Join and Inner Join in SQL?
An equijoin is a join with a join condition containing an equality operator. An equijoin returns only the rows
that have equivalent values for the specified columns.
An inner join is a join of two or more tables that returns only those rows (compared using a comparison
operator) that satisfy the join condition.
SELF JOIN
Self Join is a specific type of Join. In Self Join,
14
CS232-L – LAB 6
Syntax:
SELECT a.name, b.age, a.SALARY
FROM CUSTOMERS a, CUSTOMERS b
WHERE a.SALARY < b.SALARY;
Output:
Natural Join:
A natural join is a join that creates an implicit join based on the same column names in the joined
tables.
SELECT select_list
FROM T1
15
CS232-L – LAB 6
A natural join can be an inner join, left join, or right join. If you do not specify a join explicitly
e.g., INNER JOIN, LEFT JOIN, RIGHT JOIN, PostgreSQL will use the INNER JOIN by default.
16