03 ohp slide handout 1

3
F0004 * Property of STI Page 1 of 11 Common Table Expressions What is Common Table Expressions? Common table expressions (CTE) are temporary result set that are known only within the scope of a single SELECT, INSERT, UPDATE, DELETE or CREATE VIEW statement. Common table expressions are generally useful in a query that involves multiple aggregate functions. 1 _________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ F0004 * Property of STI Page 3 of 11 Common Table Expressions Using Common Table Expressions Example 2: WITH CountEmployees(dept_id, n) AS ( SELECT dept_id, count(*) AS n FROM employee GROUP BY dept_id ) SELECT a.dept_id, a.n, b.dept_id, b.n FROM CountEmployees AS a JOIN CountEmployees AS b ON a.n = b.n AND a.dept_id < b.dept_id 3 __________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ F0004 * Property of STI Page 2 of 11 Common Table Expressions Using Common Table Expressions The common table expressions are defined using the WITH clause. Example 1: WITH CountEmployees(dept_id, n) AS ( SELECT dept_id, count(*) AS n FROM employee GROUP BY dept_id ) SELECT dept_id, n FROM CountEmployees WHERE n = ( SELECT max(n) FROM CountEmployees ) 2 _________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ F0004 * Property of STI Page 4 of 11 Common Table Expressions Using Common Table Expressions Example 3: WITH CountEmployees(dept_id, n) AS ( SELECT dept_id, count(*) AS n FROM employee GROUP BY dept_id ), DeptPayroll( dept_id, amt ) AS ( SELECT dept_id, sum(salary) AS amt FROM employee GROUP BY dept_id ) SELECT count.dept_id, count.n, pay.amt FROM CountEmployees AS count JOIN DeptPayroll AS pay ON count.dept_id = pay.dept_id WHERE count.n = ( SELECT max(n) FROM CountEmployees ) OR pay.amt = ( SELECT min(amt) FROM DeptPayroll ) 4 __________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________

Transcript of 03 ohp slide handout 1

Page 1: 03 ohp slide handout 1

F0004

* Property of STI

Page 1 of 11

Common Table Expressions

What is Common Table Expressions?

Common table expressions (CTE)

are temporary result set that are

known only within the scope of a

single SELECT, INSERT, UPDATE,

DELETE or CREATE VIEW statement.

Common table expressions are

generally useful in a query that

involves multiple aggregate

functions.

1 _________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________

F0004

* Property of STI

Page 3 of 11

Common Table Expressions

Using Common Table Expressions

Example 2: WITH CountEmployees(dept_id, n)

AS

( SELECT dept_id, count(*) AS n

FROM employee GROUP BY dept_id )

SELECT a.dept_id, a.n,

b.dept_id, b.n

FROM CountEmployees AS a JOIN

CountEmployees AS b

ON a.n = b.n AND a.dept_id <

b.dept_id

3 __________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________

F0004

* Property of STI

Page 2 of 11

Common Table Expressions

Using Common Table Expressions

The common table expressions are

defined using the WITH clause.

Example 1:WITH CountEmployees(dept_id, n)

AS

( SELECT dept_id, count(*) AS n

FROM employee GROUP BY dept_id )

SELECT dept_id, n

FROM CountEmployees

WHERE n = ( SELECT max(n)

FROM CountEmployees )

2 _________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________

F0004

* Property of STI

Page 4 of 11

Common Table Expressions

Using Common Table Expressions

Example 3: WITH

CountEmployees(dept_id, n) AS

( SELECT dept_id, count(*) AS n

FROM employee GROUP BY

dept_id ),

DeptPayroll( dept_id, amt ) AS

( SELECT dept_id, sum(salary)

AS amt

FROM employee GROUP BY dept_id )

SELECT count.dept_id, count.n,

pay.amt

FROM CountEmployees AS count

JOIN DeptPayroll AS pay

ON count.dept_id = pay.dept_id

WHERE count.n = ( SELECT max(n)

FROM CountEmployees )

OR pay.amt = ( SELECT min(amt)

FROM DeptPayroll )

4 __________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________

Page 2: 03 ohp slide handout 1

F0004

* Property of STI

Page 5 of 11

Common Table Expressions

Exercise

Assume that you need to

determine which class has the

most number of students. The

student table lists all the students

and specifies in which class each

belong. Using common table

expressions, find the following:

1. Extract the class with the most

students. Extract the class with

the fewest students.

2. List the class that has the highest

GPA of students.

5 _________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________

F0004

* Property of STI

Page 7 of 11

Common Table Expressions

What is Recursive Common Table Expressions

Recursive common table

expressions allow you to query

tables that represent hierarchical

information.

A recursive common table

expression is composed of an initial

subquery or seed and a recursive

subquery.

7 __________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________

F0004

* Property of STI

Page 6 of 11

Common Table Expressions

Applications of Common Table Expressions

Common table expressions are

useful whenever multiple levels of

aggregation must occur within a

single query.

Views within a procedure that

must contain a reference to a

program variable.

Queries that use temporary result

set to store a set of values.

6 _________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________

F0004

* Property of STI

Page 8 of 11

Common Table Expressions

Example

WITH RECURSIVE

manager ( emp_id, manager_id,

emp_fname, emp_lname, mgmt_level ) AS

( ( SELECT emp_id, manager_id, --initial subquery

emp_fname, emp_lname, 0

FROM employee AS e

WHERE manager_id = emp_id )

UNION ALL

( SELECT e.emp_id, e.manager_id, -- recursive subquery

e.emp_fname, e.emp_lname, m.mgmt_level + 1

FROM employee AS e JOIN manager AS m

ON e.manager_id = m.emp_id

AND e.manager_id <> e.emp_id

AND m.mgmt_level < 20 ) )

SELECT * FROM manager

ORDER BY mgmt_level, emp_lname, emp_fname

8 __________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________

Page 3: 03 ohp slide handout 1

F0004

* Property of STI

Page 9 of 11

Common Table Expressions

Restrictions on Recursive Common Table Expression

Recursive common table expressions cannot be mutually recursive.

The only set operator permitted between the initial subquery and the recursive subquery is UNION ALL.

Within the definition of a recursive subquery, a self-reference to the recursive table expression can appear only within the FROM clause of the recursive subquery.

9 _________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________

F0004

* Property of STI

Page 11 of 11

Common Table Expressions

Exercise

Using the recursive common table

expression, write a query that

displays the Fibonacci sequence.

TIP: The Fibonacci sequence is the sequence in

which each number is the sum of the two

preceding numbers such as 1, 1, 2, 3, 5, 8, 13,

21, 34, 55, 89, 144, 233, 377, 610, 987, 1597,

2584, 4181, ... (each number is the sum of

the previous two). The Fibonacci sequence,

generated by the rule f1 = f2 = 1 , fn+1 = fn +

fn-1, is well known in many different areas of

mathematics and science.

11 _________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________

F0004

* Property of STI

Page 10 of 11

Common Table Expressions

Restrictions on Recursive Common Table Expression

The recursive subquery cannot contain DISTINCT, or a GROUP BY or an ORDER BY clause.

The recursive subquery can not make use of any aggregate function.

To prevent runaway recursive queries, an error is generated if the number of levels of recursion exceeds the current setting of the MAX_RECURSIVE_ITERATIONS option.

10 ________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________