You are on page 1of 6

ANSI On Equijoin Syntax ANSI Equijoin Syntax

• ANSI SQL Multi Table On


• ANSI SQL ON
SELECT e.empno, Bring in first table join

SELECT d.department_name, l.loc_id,


l.city d.dname,
FROM departments d l.state_tx
JOIN locations l ON ( d.location_id = l.id FROM locations l
); JOIN dept d ON ( d.location_id = l.id )
JOIN emp e ON ( d.deptno = e.deptno );

Use ON when join column Bring in Bring in


names are different List join conditions here second third table
like traditional syntax table join join

FURTHER SQL FEATURES NESTED QUERIES

INTRODUCTION A SELECT STATEMENT MAY BE NESTED


SUB QUERIES INSIDE ANOTHER SELECT STATEMENT.
CORRELATED QUERIES
USING SUBQUERIES TO CREATE JOINS THERE ARE SEVERAL CIRCUMSTANCES
SELF JOINS WHERE THIS MAY BE IMPORTANT:

Reference: SUB-QUERIES
Connolly & Begg, Database Systems, CORRELATED QUERIES
Fifth Edition pp 154-161. JOINS
Fourth Edition pp 137-153. FROM CLAUSES
ETC.
SUB-QUERIES

THE SUB QUERY IS EVALUATED ONCE ONLY:

EG PRINT THE ORDERS WHERE THE VALUE OF THE


ORDER IS ABOVE THE AVERAGE VALUE OF AN
ORDER: APPROACH TO RESOLVING A SUB-QUERY:
1. CALCULATE THE AVERAGE OF THE VALUE ATTRIBUTE
l9q1 (749.2)
SELECT ORDER_NUM, ORDER_DATE 2. RETRIEVE EACH OF THE ROWS FROM THE TABLE
FROM ORDER WHERE THE VALUE ATTRIBUTE IS GREATER THAN 749.2
WHERE VALUE > ( SELECT AVG(VALUE) 3. PROJECT THE ORDER_NUM AND ORDER_DATE
FROM ORDER); ATTRIBUTES.
Functions such as MAX() can not be incorporated directly into the
WHERE clause.

PART
Part Description Price
No
223A 22mm clip 0.10
1212 22mm con 1.50
6341SS 3m tube 4.50

APPROACH TO RESOLVING A SUB-QUERY: List the descriptions of parts where the price is above the
1. CALCULATE THE AVERAGE OF THE VALUE ATTRIBUTE average price of a part.
(749.2)
2. RETRIEVE EACH OF THE ROWS FROM THE TABLE SELECT DESCRIPTION
WHERE THE VALUE ATTRIBUTE IS GREATER THAN 749.2 l9q2 FROM PART
3. PROJECT THE ORDER_NUM AND ORDER_DATE WHERE PRICE > ( SELECT AVG(PRICE)
ATTRIBUTES. FROM PART )
CORRELATED QUERIES

THE CORRELATED QUERY IS EVALUATED ONCE FOR EACH


OF THE TUPLES RETURNED BY THE MAIN QUERY

EG PRINT THE ORDERS WHERE THE VALUE OF THE ORDER IS


ABOVE THE AVERAGE VALUE OF THE ORDERS FOR THAT Average = 656.7
CUSTOMER Approach to resolving a correlated-query:
1. Retrieve the first row
SELECT * 2. Get the cust_num
FROM ORDER 3. Calculate the average of the value attribute for all the rows with
WHERE VALUE > ( SELECT AVG(VALUE) this cust_num
FROM ORDER X 4. If the value attribute of the current row is greater than the average
WHERE ORDER.CUST_NUM = X.CUST_NUM) value
calculated return the row
X IS AN ALIAS FOR ORDER 5. Repeat for all rows in the table

Approach to resolving a correlated-query: Average = 838.7 Approach to resolving a correlated-query: Average = 838.7
1. Retrieve the first row 1. Retrieve the first row
2. Get the cust_num 2. Get the cust_num
3. Calculate the average of the value attribute for all the rows with 3. Calculate the average of the value attribute for all the rows with
this cust_num this cust_num
4. If the value attribute of the current row is greater than the average 4. If the value attribute of the current row is greater than the average
value value
calculated return the row calculated return the row
5. Repeat for all rows in the table 5. Repeat for all rows in the table
Approach to resolving a correlated-query: Average = 656.7 Approach to resolving a correlated-query: Average = 838.7
1. Retrieve the first row 1. Retrieve the first row
2. Get the cust_num 2. Get the cust_num
3. Calculate the average of the value attribute for all the rows with 3. Calculate the average of the value attribute for all the rows with
this cust_num this cust_num
4. If the value attribute of the current row is greater than the average 4. If the value attribute of the current row is greater than the average
value value
calculated return the row calculated return the row
5. Repeat for all rows in the table 5. Repeat for all rows in the table

List the details of the most recent order for each


Average = 656.7 customer
Approach to resolving a correlated-query:
1. Retrieve the first row SELECT CUST_NUM, ORDER_NUM,VALUE
2. Get the cust_num FROM ORDER
3. Calculate the average of the value attribute for all the rows with WHERE ORDER_DATE = (SELECT MAX(ORDER_DATE)
MAX(ORDER_DATE)
this cust_num FROM ORDER X
4. If the value attribute of the current row is greater than the average l9q4 WHERE ORDER.CUST_NUM = X.CUST_NUM)
value
calculated return the row
5. Repeat for all rows in the table
SUB QUERIES CAN BE USED TO CREATE JOINS. SELF JOINS.
SELECT DISTINCT Cust_name
FROM customr, ordr Sometimes it is useful to be able to join a table to itself. Typically
l9q5
WHERE customr.Cust_num = ordr.Cust_num such a table will contain its own primary key as a foreign key
AND order_date > '12-AUG-10' Manages
has the same effect as:
SELECT Cust_name
FROM customr EMPLOYEE
l9q6 WHERE Cust_num in ( SELECT Cust_num
FROM ordr Red
and WHERE order_date > '12-AUG-10')
SELECT Cust_name Blue Pink
FROM customr C
WHERE EXISTS( SELECT *
FROM ordr Purple Green
l9q7 WHERE C.Cust_num = ordr.Cust_num
AND order_date > '12-AUG-10');
The exists condition is true if the result of evaluating the inner query is not empty.

SELF JOINS (2). SELF JOINS (2).


Problem: how to find the names of employees and their managers. To Problem: how to find the names of employees and their managers. To
resolve a query such as this, aliases can be used to give the table resolve a query such as this, aliases can be used to give the table
different names different names

Select Emp.Name, Manager.Name Select Emp.Name, Manager.Name


From Employee Emp, Employee Manager From Employee Emp, Employee Manager
Where Emp.Manager_ID = Manager.ID Where Emp.Manager_ID = Manager.ID
l9q8

Emp Manager
SELF JOINS (3) SELF JOINS (3)

What parts do you need to make an engine?. What parts do you need to make an engine?.
PARTLIST
SuperPart SubPart
Product_name Part Product_name Part
Product_name Part
engine cylinder head engine cylinder head engine cylinder head
engine block
engine block engine block
cylinder head valve set
cylinder head valve set
cylinder head valve set cylinder head head casting cylinder head head casting
cylinder head head casting block block casting block block casting
block piston block piston
block block casting
block piston
engine
Select SuperPart.Product_name, SubPart.Part
cylinder head block l9q9 From Partlist SuperPart, Partlist SubPart
Where SuperPart.Part = SubPart.Product_name
valve set head casting block casting piston

Conclusion

SELECT statements may be nested within one another

In some systems ( eg oracle ) the phrasing of a SELECT statement can


influence the way in which the query is interpreted

In a sub-query, the nested query is evaluated once fpr the whole query.

In a correlated query, the subquery is evaluated for every row which is


returned by the main query.

You might also like