Idbslab 10
Idbslab 10
Experiment 10
Processing multiple tables using SQL joins.
SQL JOINS
The JOIN keyword is used to query data from two or more tables, based on a relationship
between certain columns in these tables.
TYPES OF JOINS:
JOIN/INNER JOIN: Return rows when there is at least one match in both tables
LEFT JOIN: Return all rows from the left table, even if there are no matches in the right
table
RIGHT JOIN: Return all rows from the right table, even if there are no matches in the
left table
FULL JOIN: Return rows when there is a match in one of the tables
The INNER JOIN keyword return rows when there is at least one match in both tables.
SELECT column_name(s)
FROM table_name1
INNER JOIN table_name2
ON table_name1.column_name=table_name2.column_name
The INNER JOIN keyword return rows when there is at least one match in both tables. If there
are rows in "Persons" that do not have matches in "Orders", those rows will NOT be listed
The LEFT JOIN keyword returns all rows from the left table (table_name1), even if there are no
matches in the right table (table_name2).
Syntax
SELECT column_name(s)
FROM table_name1
LEFT JOIN table_name2
ON table_name1.column_name=table_name2.column_name
Now we want to list all the persons and their orders - if any, from the tables above.We use the
following SELECT statement:
The LEFT JOIN keyword returns all the rows from the left table (Persons), even if there are no
matches in the right table (Orders).
The RIGHT JOIN keyword Return all rows from the right table (table_name2), even if there are
no matches in the left table (table_name1).
SELECT column_name(s)
FROM table_name1
RIGHT JOIN table_name2
ON table_name1.column_name=table_name2.column_name
Now we want to list all the orders with containing persons - if any, from the tables above.We use
the following SELECT statement:
The RIGHT JOIN keyword returns all the rows from the right table (Orders), even if there are no
matches in the left table (Persons)
The FULL JOIN keyword return rows when there is a match in one of the tables.
SELECT column_name(s)
FROM table_name1
FULL JOIN table_name2
ON table_name1.column_name=table_name2.column_name
The "Persons" table:
Now we want to list all the persons and their orders, and all the orders with their persons.
NOTE:
A primary key from one table and a foreign key that references the table with the primary key
will share a common domain and are frequently used to establish a join.
Occasionally, joins will be established using columns that share a common domain but not the
primary-foreign key relationship, and that will also work.
The result of a join operation is a single table. Selected columns from all the tables are included.
Each row returned contains data from rows in the different input tables where values for the
common columns match.
Task 1:
List customer name, identification number, and order number for all orders listed in the
Order table. Include the order number, even if there is no customer name and
identification number available.
Task 2:
CUSTOMER_T
ORDER_T
What are the names of all customers who have placed orders? List the same in the
ascending order of ORDER_ID.
Task 3:
List customer name, identification number, and order number for all customers listed in
the CUSTOMER_T table. Include the customer identification number and name even if
there is no order available for that customer.
Task 4:
List customer name, identification number, and order number for all orders listed in the
ORDER_T table. Include the order number and name even if there is no customer name
and identification number available.