0% found this document useful (0 votes)
402 views3 pages

WEEK 4 - What Is Common Table Expressions

Common table expressions (CTE) allow for temporary result sets that can be referenced within a single SELECT, INSERT, UPDATE, or DELETE statement. CTEs are useful for queries involving multiple levels of aggregation or when temporary results need to be stored. Recursive CTEs allow querying hierarchical data by defining an initial seed query and a recursive query that references the CTE, and are subject to restrictions to prevent unlimited recursion.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
402 views3 pages

WEEK 4 - What Is Common Table Expressions

Common table expressions (CTE) allow for temporary result sets that can be referenced within a single SELECT, INSERT, UPDATE, or DELETE statement. CTEs are useful for queries involving multiple levels of aggregation or when temporary results need to be stored. Recursive CTEs allow querying hierarchical data by defining an initial seed query and a recursive query that references the CTE, and are subject to restrictions to prevent unlimited recursion.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

What is Common Table Expressions?

- Common table expression (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. Using Common Table Expression - The common table expressions are defined using the WITH clause. Example 1: WITH CountEmployees (dept_id, n) AS ( SELECT 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 ) 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 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 = ( SELCT min(amt) FROM DeptPayroll )

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. What is Recursive Common Table Expression - Recursive common table expression allow you to query tables that represent hierarchical information. - A recursive common table expression is composed of an initial sub query or seed and a recursive sub query. Example 1: 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, e.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 8 FROM manager ORDER BY mgmt_level, emp_lnmae, emp_fname Restrictions on Recursive Common Table Expressions - 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 sub query. - The recursive subquery cannot contain DISTINCT, or a GROUP BY or an ORDER BY clause. - The recursive subquery cannot 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 settings of the MAX_RECURSIVE_ITERATIONS option.

You might also like