07 | Using Table Expressions

Slides:



Advertisements
Similar presentations
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Advertisements

Module 6: Working with Subqueries. Overview Introduction to Subqueries Using a Subquery as a Derived Table Using a Subquery as an Expression Using a Subquery.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
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.
Module 3: Changes to Transact-SQL. Overview Accessing Object Information New Transact-SQL Syntax Changes to Objects Distributed Queries.
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
Module 8: Implementing Stored Procedures. Introducing Stored Procedures Creating, Modifying, Dropping, and Executing Stored Procedures Using Parameters.
Module 1: Introduction to Transact-SQL
Module 9 Designing and Implementing Stored Procedures.
Module 9: Introduction to Programming Objects. Overview Displaying the Text of a Programming Object Introduction to Views Advantages of Views Creating.
Graeme Malcolm | Senior Content Developer, Microsoft Geoff Allix | Principal Technologist, Content Master.
Module 11: Programming Across Multiple Servers. Overview Introducing Distributed Queries Setting Up a Linked Server Environment Working with Linked Servers.
04 | Grouping and Aggregating Data Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program Manager.
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
Deanne Larson.  Variables allow you to store data values temporarily for later use in the same batch where they were declared. I describe batches later.
Module 18 Querying XML Data in SQL Server® 2008 R2.
Module 4 Designing and Implementing Views. Module Overview Introduction to Views Creating and Managing Views Performance Considerations for Views.
10 | Programming with Transact-SQL Graeme Malcolm | Senior Content Developer, Microsoft Geoff Allix | Principal Technologist, Content Master.
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Views, Algebra Temporary Tables. Definition of a view A view is a virtual table which does not physically hold data but instead acts like a window into.
(SQL - Structured Query Language)
05 | SET Operators, Windows Functions, and Grouping Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program.
Module 3: Using XML. Overview Retrieving XML by Using FOR XML Shredding XML by Using OPENXML Introducing XQuery Using the xml Data Type.
Stored Procedures / Session 4/ 1 of 41 Session 4 Module 7: Introducing stored procedures Module 8: More about stored procedures.
Module 10 Merging Data and Passing Tables. Module Overview Using the MERGE Statement Implementing Table Types Using Table Types As Parameters.
In this session, you will learn to: Create and manage views Implement a full-text search Implement batches Objectives.
Module 9: Implementing Functions. Overview Creating and Using Functions Working with Functions Controlling Execution Context.
Module 5: Working with Subqueries. Writing Basic Subqueries Writing Correlated Subqueries Comparing Subqueries with Joins and Temporary Tables Using Common.
Module 5: Joining Multiple Tables. Overview Using Aliases for Table Names Combining Data from Multiple Tables Combining Multiple Result Sets.
IFS180 Intro. to Data Management Chapter 10 - Unions.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Querying with Transact-SQL
Structured Query Language
Using XML in SQL Server and Azure SQL Database
02 | Advanced SELECT Statements
11 | Error Handling and Transactions
Sorting and Filtering Data
10 | Programming with Transact-SQL
Querying Multiple Tables
09 | Modifying Data Graeme Malcolm | Senior Content Developer, Microsoft Geoff Allix | Principal Technologist, Content Master.
20761A 10: Using Subqueries Module 10   Using Subqueries.
05 | Using Functions and Aggregating Data
20761A 11: Using Set Operators Module 11   Using Set Operators.
03 | Querying Multiple Tables with Joins
Using Window Ranking, Offset, and Aggregate Functions
08 | Grouping Sets and Pivoting Data
Sorting and Filtering Data
Querying Multiple Tables
06 | Using Subqueries and APPLY
04 | Using Set Operators Graeme Malcolm | Senior Content Developer, Microsoft Geoff Allix | Principal Technologist, Content Master.
20761B 12: Using Set Operators Module 12   Using Set Operators.
09 | Modifying Data Graeme Malcolm | Senior Content Developer, Microsoft Geoff Allix | Principal Technologist, Content Master.
පාඨමාලා මාතෘකා Microsoft SQL Server Databases සැකසීම
Writing SELECT Queries
02 | Querying Tables with SELECT
20761B 10: Using Subqueries Module 10   Using Subqueries.
Using Table Expressions
More SQL: Complex Queries, Triggers, Views, and Schema Modification
C1. SQL BAsic.
The Hidden Mysteries of Common Table Expressions
T-SQL Performance Tuning
Chapter 8 Advanced SQL.
IST 318 Database Administration
02 | Querying Tables with SELECT
Intermediate Query Structure and Development
Grouping and Aggregating Data
Presentation transcript:

07 | Using Table Expressions Graeme Malcolm | Senior Content Developer, Microsoft Geoff Allix | Principal Technologist, Content Master

Module Overview Views Temporary Tables Table Variables Table-Valued Functions Derived Tables Common Table Expressions

Querying Views Views are named queries with definitions stored in a database Views can provide abstraction, encapsulation and simplification From an administrative perspective, views can provide a security layer to a database Views may be referenced in a SELECT statement just like a table CREATE VIEW Sales.vSalesOrders AS SELECT oh.OrderID, oh.Orderdate, oh.CustomerID, od.LineItemNo, od.ProductID, od.Quantity FROM Sales.OrderHeaders AS oh JOIN Sales.OrderDetails AS od ON od.OrderID = oh.OrderID; SELECT OrderID, CustomerID, ProductID FROM Sales.vSalesOrder ORDER BY OrderID;

Querying Views

Temporary Tables CREATE TABLE #tmpProducts (ProductID INTEGER, ProductName varchar(50)); GO … SELECT * FROM #tmpProducts; Temporary tables are used to hold temporary result sets within a user’s session Created in tempdb and deleted automatically Created with a # prefix Global temporary tables are created with ## prefix

Table Variables DECLARE @varProducts table (ProductID INTEGER, ProductName varchar(50)); … SELECT * FROM @varProducts Introduced because temporary tables can cause recompilations Used similarly to temporary tables but scoped to the batch Use only on very small datasets

Temporary Tables and Table Variables

Table-Valued Functions CREATE FUNCTION Sales.fn_GetOrderItems (@OrderID AS Integer) RETURNS TABLE AS RETURN (SELECT ProductID, UnitPrice, Quantity FROM Sales.OrderDetails WHERE OrderID = @OrderID); … SELECT * FROM Sales.fn_GetOrderItems (1025) AS LineItems; TVFs are named objects with definitions stored in a database TVFs return a virtual table to the calling query Unlike views, TVFs support input parameters TVFs may be thought of as parameterized views

Using Table-Valued Functions

Derived Tables Introduction SELECT orderyear, COUNT(DISTINCT custid) AS cust_count FROM (SELECT YEAR(orderdate) AS orderyear, custid FROM Sales.Orders) AS derived_year GROUP BY orderyear; Derived tables are named query expressions created within an outer SELECT statement Not stored in database – represents a virtual relational table Scope of a derived table is the query in which it is defined

Derived Tables Guidelines Derived tables must: Have an alias Have unique names for all columns Not use an ORDER BY clause (without TOP or OFFSET/FETCH) Not be referred to multiple times in the same query Derived tables may: Use internal or external aliases for columns Refer to parameters and/or variables Be nested within other derived tables

Derived Tables Specifying Column Aliases Column aliases may be defined inline: Or externally: SELECT orderyear, COUNT(DISTINCT custid) AS cust_count FROM ( SELECT YEAR(orderdate) AS orderyear, custid FROM Sales.Orders) AS derived_year GROUP BY orderyear; SELECT orderyear, COUNT(DISTINCT custid) AS cust_count FROM ( SELECT YEAR(orderdate), custid FROM Sales.Orders) AS derived_year(orderyear, custid) GROUP BY orderyear;

Using Derived Tables

Common Table Expressions (CTEs) WITH CTE_year (OrderYear, CustID) AS ( SELECT YEAR(orderdate), custid FROM Sales.Orders ) SELECT OrderYear, COUNT(DISTINCT CustID) AS Cust_Count FROM CTE_year GROUP BY orderyear; CTEs are named table expressions defined in a query CTEs are similar to derived tables in scope and naming requirements Unlike derived tables, CTEs support multiple references and recursion

Common Table Expressions Recursion WITH OrgReport (ManagerID, EmployeeID, EmployeeName, Level) AS ( SELECT e.ManagerID, e.EmployeeID, EmployeeName, 0 FROM HR.Employee AS e WHERE ManagerID IS NULL UNION ALL SELECT e.ManagerID, e.EmployeeID, e.EmployeeName, Level + 1 INNER JOIN OrgReport AS o ON e.ManagerID = o.EmployeeID ) SELECT * FROM OrgReport OPTION (MAXRECURSION 3); Specify a query for the anchor (root) level Use UNION ALL to add a recursive query for other levels Query the CTE, with optional MAXRECURSION option

Using Common Table Expressions

Using Table Expressions Views Temporary Tables Table Variables Table-Valued Functions Derived Tables Common Table Expressions Lab: Using Table Expressions