Database PPT 2
Database PPT 2
Database PPT 2
• Write subqueries
select
from instructor
Multiple table queries
The from clause lists the relations involved in the query
Corresponds to the Cartesian product operation of the relational
algebra
select
from instructor, teaches
select
from instructor, teaches
where instructor.ID = teaches.ID
• Find the course ID, semester, year and title of each course offered by the
Comp. Sci. department
Quiz
• Find the course ID, semester, year and title of each course offered by the
Comp. Sci. department
• Natural join:
• Same as equi-join
• one of the duplicate columns is eliminated in the result table
1. Equi-Join Example
• For each customer who placed an order, what is the
customer’s name and order number?
1. Equi-Join Example
• For each customer who placed an order, what is the
customer’s name and order number?
SELECT *
FROM Customer_T, Order_T
WHERE Customer_T.CustomerID = Order_T. CustomerID
ORDER BY OrderId;
Customer ID
appears twice in the
result
1. Equi-Join Example – Syntax
• For each customer who placed an order, what is the
customer’s name and order number?
SELECT *
FROM Customer_T INNER JOIN Order_T
ON Customer_T.CustomerID = Order_T. CustomerID
ORDER BY OrderId;
Customer ID
appears twice in the
result
1. Equi-Join Example – Syntax
• For each customer who placed an order, what is the
customer’s name and order number?
SELECT *
FROM Customer_T INNER JOIN Order_T
ON Customer_T.CustomerID = Order_T. CustomerID
ORDER BY OrderId;
An INNER join will only return rows from each table that have matching rows
in the other.