Docsity

Database Systems: SQL Joins and Subqueries - Prof. Mark Llewellyn, Study notes of Deductive Database Systems

Partial preview of the text

Download Database Systems: SQL Joins and Subqueries - Prof. Mark Llewellyn and more Study notes Deductive Database Systems in PDF only on Docsity!

COP 4710: Database Systems (Chapter 5)

Page 1

COP 4710: Database Systems

Fall 2007

Chapter 5 – Introduction To SQL – Part 2

School of Electrical Engineering and Computer Science

University of Central Florida

Instructor :

Dr. Mark [email protected] 236, 407-823-

http://www.cs.ucf.edu/courses/ccop4710/fall

COP 4710: Database Systems (Chapter 5)

Page 2

Mark Llewellyn

Processing Multiple Tables – Joins

•^

Join –

a relational operation that causes two or more tables with a

common domain to be combined into a single table or view

•^

Equi-join –

a join in which the joining condition is based on equality

between

values

in

the

common

columns;

common

columns

appear

redundantly in the result table

•^

Natural join –

an equi-join in which one of the duplicate columns is

eliminated in the result table

•^

Outer join –

a join in which rows that do not have matching values in

common columns are nonetheless included in the result table (as opposedto

inner

join, in which rows must have matching values in order to appear

in the result table)

•^

Union join

  • includes all columns from each table in the join, and an

instance for each row of each table The common columns in joined tables are usually the primary key of thedominant table and the foreign key of the dependent table in 1:M relationships

COP 4710: Database Systems (Chapter 5)

Page 4

These tables are used in queries that follow

COP 4710: Database Systems (Chapter 5)

Page 5

•^

For each customer who placed an order, what is thecustomer’s name and order number?SELECT CUSTOMER_T.CUSTOMER_ID, CUSTOMER_NAME, ORDER_IDFROM CUSTOMER_T, ORDER_TWHERE CUSTOMER_T.CUSTOMER_ID = ORDER_T.CUSTOMER_ID;

Join involves multiple tables in FROM clause

Natural Join Example

WHERE clause performs theequality check for commoncolumns of the two tables

COP 4710: Database Systems (Chapter 5)

Page 7

•^

List the customer name, ID number, and order numberfor all customers. Include customer information evenfor customers that do have an orderSELECT CUSTOMER_T.CUSTOMER_ID, CUSTOMER_NAME,

ORDER_ID
FROM CUSTOMER_T, LEFT OUTER JOIN ORDER_TON CUSTOMER_T.CUSTOMER_ID = ORDER_T.CUSTOMER_ID;

Outer Join Example

(Microsoft Syntax)

LEFT OUTER JOIN syntax withON keyword instead of WHERE Æ

causes customer data to appear even if there is no correspondingorder data

COP 4710: Database Systems (Chapter 5)

Page 8

•^

List the customer name, ID number, and order number forall customers. Include customer information even forcustomers that do have an orderSELECT CUSTOMER_T.CUSTOMER_ID, CUSTOMER_NAME, ORDER_IDFROM CUSTOMER_T, ORDER_TWHERE CUSTOMER_T.CUSTOMER_ID = ORDER_T.CUSTOMER_ID(+);

Outer Join Example

(Oracle Syntax)

Outer join in Oracle uses regular joinsyntax, but adds (+) symbol to theside that will have the missing data

COP 4710: Database Systems (Chapter 5)

Page 10

Results from a four-table join

From CUSTOMER_T table

From ORDER_T table

From PRODUCT_T table

COP 4710: Database Systems (Chapter 5)

Page 11

Mark Llewellyn

Processing Multiple Tables Using Subqueries

-^

Subquery

placing

an

inner

query

(SELECT

statement) inside an outer query.

-^

Options:– In a condition of the WHERE clause.– As a “table” of the FROM clause.– Within the HAVING clause.

-^

Subqueries can be:– Noncorrelated – executed once for the entire outer query.– Correlated – executed once for each row returned by the

outer query.

COP 4710: Database Systems (Chapter 5)

Page 13

Correlated vs. Noncorrelated Subqueries

•^

Noncorrelated subqueries:– Do not depend on data from the outer query.– Execute once for the entire outer query.

•^

Correlated subqueries:– Make use of data from the outer query.– Execute once for each row of the outer query.– Can use the EXISTS operator.

COP 4710: Database Systems (Chapter 5)

Page 14

Processing anoncorrelatedsubquery

No reference to datain outer query, sosubquery executesonce only^ These are the onlycustomers that haveIDs in the ORDER_Ttable

The subqueryexecutes andreturns thecustomer IDs fromthe ORDER_T table

The outer query onthe results of thesubquery

COP 4710: Database Systems (Chapter 5)

Page 16

Processing acorrelatedsubquery

Subquery refers to outer-query data, so executes oncefor each row of outer query

Note: only theorders thatinvolveproducts withNatural Ash willbe included inthe final results

COP 4710: Database Systems (Chapter 5)

Page 17

•^

Show all products whose price is higher than the averageSELECT PRODUCT_DESCRIPTION, STANDARD_PRICE, AVGPRICEFROM

(SELECT AVG(STANDARD_PRICE) AVGPRICE FROM PRODUCT_T),PRODUCT_TWHERE STANDARD_PRICE > AVG_PRICE;

Another Subquery Example

The WHERE clause normally cannot include aggregate functions, but because the aggregate isperformed in the subquery its result can be used in the outer query’s WHERE clause

One column of the subquery is anaggregate function that has an aliasname. That alias can then be referredto in the outer query

Subquery forms the derived table usedin the FROM clause of the outer query

COP 4710: Database Systems (Chapter 5)

Page 19

SQL Join Operations

(cont.)

-^

The FROM clause indicates which tables are to be joined. Ifthree or more tables are specified, the join operation takesplace two tables at a time, starting from left to right.

-^

The join condition is specified in the WHERE clause. In theexample, a natural join is effected on the attribute V_CODE.

-^

The SQL join syntax shown above is sometimes referred toas an “old-style” join.

-^

The tables on pages 55 and 56, summarize the SQL joinoperations.

SELECT P_CODE, P_DESCRIPT, P_PRICE, V_NAME

FROM PRODUCT, VENDORWHERE PRODUCT.V_CODE = VENDOR.V_CODE;

COP 4710: Database Systems (Chapter 5)

Page 20

Mark Llewellyn

SQL Cross Join Operation

•^

A cross

join in SQL is equivalent to a Cartesian

product in standard relational algebra.

The cross

join syntax is:

SELECT

column-list

FROM

table1, table

;

SELECT

column-list

FROM

table1 CROSS JOIN table

;

old style syntaxnew style syntax