The Hidden Mysteries of Common Table Expressions

Slides:



Advertisements
Similar presentations
Welcome to CODE SPREAD Simple Concepts of Coding | Programming.
Advertisements

Advanced SQL (part 1) CS263 Lecture 7.
Chapter 4 Joining Multiple Tables
February 18, 2012 Lesson 3 Standard SQL. Lesson 3 Standard SQL.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Module 7 Designing Queries for Optimal Performance.
Database Systems More SQL Database Design -- More SQL1.
Introduction to Structured Query Language (SQL)
02 | Advanced SELECT Statements Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program Manager.
Objectives After completing this lesson, you should be able to do the following: Define subqueries Describe the types of problems that the subqueries.
Sofia, Bulgaria | 9-10 October TSQL Enhancements in SQL Server 2005 Stephen Forte CTO, Corzen Inc Microsoft Regional Director NY/NJ (USA) Stephen Forte.
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
Views: Limiting Access to Data A view is a named select statement that is stored in a database as an object. It allows you to view a subset of rows or.
Chapter 7 Advanced SQL Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
DBMS Languages. Data Definition Language (DDL) Used to define the conceptual and internal schemas Includes constraint definition language (CDL) for describing.
SQL advanced select using Oracle 1 7. Multiple Tables: Joins and Set Operations 8. Subqueries: Nested Queries.
04 | Grouping and Aggregating Data Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program Manager.
CpSc 462/662: Database Management Systems (DBMS) (TEXNH Approach) Stored Procedure James Wang.
Join, Subqueries and set operators. Obtaining Data from Multiple Tables EMPLOYEES DEPARTMENTS … …
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
PL / SQL By Mohammed Baihan. What is PL/SQL? PL/SQL stands for Procedural Language extension of SQL. PL/SQL is a combination of SQL along with the procedural.
1 Chapter 10 Joins and Subqueries. 2 Joins & Subqueries Joins – Methods to combine data from multiple tables – Optimizer information can be limited based.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
Intermediate SQL: Aggregated Data, Joins and Set Operators.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
05 | SET Operators, Windows Functions, and Grouping Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program Manager.
Thinking in Sets and SQL Query Logical Processing.
There’s a particular style to it… Rob Hatton
 CONACT UC:  Magnific training   
In this session, you will learn to: Create and manage views Implement a full-text search Implement batches Objectives.
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
Module 5: Working with Subqueries. Writing Basic Subqueries Writing Correlated Subqueries Comparing Subqueries with Joins and Temporary Tables Using Common.
IFS180 Intro. to Data Management Chapter 10 - Unions.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
MySQL Subquery Source: Dev.MySql.com
Top 50 SQL Interview Questions & Answers
Relational Database Design
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
The Lost Science of Set Theory
The Lost Science of Set Theory
02 | Advanced SELECT Statements
ATS Application Programming: Java Programming
Views, Stored Procedures, Functions, and Triggers
Displaying Data from Multiple Tables
Using the Set Operators
Using the Set Operators
Chapter 5 - Functions Outline 5.1 Introduction
07 | Using Table Expressions
The Killing Cursors Cyndi Johnson
Top Tips for Better TSQL Stored Procedures
Using Table Expressions
Tally Ho! -- Explore the Varied Uses of Tally Tables
Database systems Lecture 3 – SQL + CRUD
C1. SQL BAsic.
Cyndi Johnson Senior Software Engineer at AdvancedMD Killing Cursors.
Contents Preface I Introduction Lesson Objectives I-2
Relational Database Design
Chapter 8 Advanced SQL.
Retrieving Data by Using Subqueries
IST 318 Database Administration
Cyndi Johnson Senior Software Engineer at AdvancedMD Killing Cursors.
Using the Set Operators
Manipulating Data Lesson 3.
Presentation transcript:

The Hidden Mysteries of Common Table Expressions Aaron N. Cutshall, MSCIS, MSHI The Hidden Mysteries of Common Table Expressions

Just who is this guy? 30+ Years Sr. Healthcare Data Architect Past Local Group Leader Speaker – various events 30+ Years M.S. Computer Information Systems B.S. Computer Science M.S. Health Informatics

Something to consider… “Common sense is seeing things as they are; and doing things as they ought to be.” – Harriet Beecher Stowe 1811-1896 (author of Uncle Tom’s Cabin)

Agenda What are Common Table Expressions? How to Use Best Practices Purposes and Uses How They Are Created How to Use Single and Multiple CTEs Recursive CTE Best Practices Tips & Tricks Things to Keep in Mind Closing Thoughts

Definition of a Common Table Expression Introduced to SQL Server 2005 Temporary result set (not stored) Within the scope of a single statement Expression that acts like a derived table Common (applicable) to the entire query Can be used instead of sub-queries Used to improve readability and clarity

Syntax of a CTE -- Syntax for SQL Server, Azure SQL Database, Azure SQL Data Warehouse, Parallel Data Warehouse [ WITH <common_table_expression> [ ,...n ] ] <common_table_expression>::= expression_name [ ( column_name [ ,...n ] ) ] AS ( CTE_query_definition )

Syntax of a CTE expression_name – Name for CTE Must be unique within entire query Any reference uses the CTE and not the base object column_name – Name for each CTE column No duplicate names # of columns must match # of columns in expression List is optional if all columns in expression are named CTE_query_definition – SELECT statement for CTE Must meet same requirements as for creating a view Multiple queries must be separated with set operators

Example of a CTE -- Define the CTE expression name and column list WITH cteSales (SalesPersonID, SalesOrderID, SalesYear) AS ( -- Define the CTE query SELECT SalesPersonID, SalesOrderID, YEAR(OrderDate) FROM Sales.SalesOrderHeader WHERE SalesPersonID IS NOT NULL ) -- Define the outer query referencing the CTE name SELECT SalesPersonID, COUNT(SalesOrderID) AS TotalSales, SalesYear FROM cteSales GROUP BY SalesYear, SalesPersonID ORDER BY SalesPersonID, SalesYear;

Guidelines for Creating and Using CTEs A CTE must be followed by a single statement: SELECT, INSERT, UPDATE, or DELETE References some or all the CTE columns Can also be specified in a CREATE VIEW statement Multiple CTE query definitions are allowed with a set operator: UNION ALL, UNION, INTERSECT, or EXCEPT

Guidelines for Creating and Using CTEs A CTE can reference: Itself (in a recursive CTE) Previously defined CTEs in the same WITH clause Forward referencing is not allowed Only a single WITH statement is allowed A CTE cannot use the following: ORDER BY (except when a TOP clause is specified) INTO OPTION clause with query hints FOR BROWSE

Guidelines for Creating and Using CTEs A statement previous to a CTE must be followed by a semicolon A query referencing a CTE can be used to define a cursor Remote server tables can be referenced in the CTE An error occurs when query hints referencing a CTE conflict with hints within the CTE

Demo 1

Recursive CTE Recursion is where a process executes itself Functions make recursive calls backwards Starts with a known value Works back to a base case Factorial sequence: Base case: F1 = 1 Fn = n * Fn-1 See Steve Stedman’s post

Recursive CTE A rCTE can in effect “call” itself A base query joined with “recursive” query Works forward, not in reverse as functions Starts with a base case Works forward through completion of data Used most often in hierarchical situations Employees and Managers Bill of Materials Kits and Assemblies

Recursive CTE (example) -- Declare the recursive CTE WITH cteManagers(ManagerID, EmployeeID, Title, EmployeeLevel) AS ( -- Declare the base case SELECT ManagerID, EmployeeID, Title, 0 AS EmployeeLevel FROM dbo.MyEmployees WHERE ManagerID IS NULL -- Combine both parts with UNION ALL UNION ALL -- Declare the recursive case SELECT e.ManagerID, e.EmployeeID, e.Title, m.EmployeeLevel + 1 FROM dbo.MyEmployees AS e INNER JOIN cteManagers AS m ON m.EmployeeID = e.ManagerID ) SELECT ManagerID, EmployeeID, Title, EmployeeLevel FROM cteManagers ORDER BY ManagerID;

Recursive CTE A rCTE is essentially a loop to UNION the following: CEO who has no manager (base case) Employees that report to the CEO (first level) Employees that report to the 1st level managers Employees that report to the 2nd level managers etc. ** NOT SET BASED **

Recursive CTE 1 A 2 B C 3 D E F G 4 H I

Guidelines for Recursive CTEs A rCTE must contain at least two query definitions: One or more base (anchor) members One or more recursive members Multiple base and recursive members can be defined: All base members are declared first Recursive members are declared second Only a recursive member references the rCTE

Guidelines for Recursive CTEs Base members must be combined by: UNION (ALL) INTERSECT EXCEPT Only UNION ALL is allowed between last base member and first recursive member Number and data type of columns in all members must match A recursive member can only reference CTE once

Guidelines for Recursive CTEs Not allowed in query of a recursive member: SELECT DISTINCT or TOP GROUP BY or HAVING PIVOT Scalar aggregation LEFT, RIGHT or FULL OUTER JOIN (INNER JOIN is OK) Subqueries Query hints on recursive reference

Guidelines for Recursive CTEs All returned columns are considered nullable Incorrect rCTEs may cause an infinite loop Limit recursion levels with MAXRECURSION hint: Valid values between 0 and 32,767 (integer) System default is 100 When 0 is specified, no limit is applied Only one hint is allowed

Guidelines for Recursive CTEs A view with a rCTE cannot modify data Cursors may be defined on queries using rCTEs Used only for a SELECT statement Only fast forward-only and static (snapshot) allowed Any other cursor type is converted to static Analytic and aggregate functions in recursive part Apply only to current recursion level Not to the whole set

Demo 2

Tips & Tricks Create a tally table on the fly Delete duplicate records Break strings into multiple segments Identify missing data Fibonacci sequence: Base case: F0 = 0; F1 = 1 Fn = Fn-1 + Fn-2 Code Project Example

Demo 3

Things to Keep in Mind CTE and the semi-colon: Improve Clarity: Previous statement must be terminated It is NOT the same as a prefix!! Use semi-colons regularly for all statements Improve Clarity: Use CTE to reduce confusion, not create it Use appropriate naming conventions Be consistent in usage

Things to Keep in Mind Benefits over derived tables/inline views: Top Down Programming Declare query before use Can execute CTEs individually Improved Readability Separate query from a big mess into readable chunks Provide more room for inline comments Check out SQLServerCentral.com for more

Things to Keep in Mind Performance considerations: Avoid querying data unnecessarily Filter records before additional processing Identify columns needed to avoid unnecessary I/O Avoid multiple references where possible Code consolidation makes reuse of query easier when appropriate Causes re-execution of query for each reference Complex queries should use temp tables or table variables Recursive CTE is a loop: Not Set Based

References Aaron Bertrand: https://blogs.sentryone.com/aaronbertrand/backtobasics-ctes/ Essential SQL: https://www.essentialsql.com/recursive-ctes-explained/ Microsoft: https://docs.microsoft.com/en-us/sql/t-sql/queries/with-common-table-expression-transact-sql?view=sql-server-2014 Red Gate: https://www.red-gate.com/simple-talk/sql/t-sql-programming/sql-server-cte-basics/

Questions & Comments Aaron N. Cutshall BONUS: A TON of free eBooks from Microsoft, RedGate and SentryOne! PRESENTATION FEEDBACK: Your thoughts needed Improve presentations Make this event even more valuable!!! Aaron N. Cutshall @sqlrv www.linkedin.com/in/sqlrv aaron@sqlrv.com www.sqlrv.com