Unit 5 Seminar. Derived Table A derived table is a virtual table that’s calculated on the fly from a select statement. The biggest benefit of using derived.

Slides:



Advertisements
Similar presentations
Advanced SQL (part 1) CS263 Lecture 7.
Advertisements

Chapter 4 Joining Multiple Tables
A Guide to SQL, Seventh Edition. Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables.
Introduction to Structured Query Language (SQL)
Introduction to Structured Query Language (SQL)
Introduction to Oracle9i: SQL1 Basic SQL SELECT Statements.
Introduction to Structured Query Language (SQL)
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
Objectives After completing this lesson, you should be able to do the following: Write SELECT statements to access data from more than one table using.
02 | Advanced SELECT Statements Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program Manager.
SQL Joins.
CPS120: Introduction to Computer Science Information Systems: Database Management Nell Dale John Lewis.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
Database Systems: Design, Implementation, and Management Tenth Edition Chapter 8 Advanced SQL.
Introduction to Databases Chapter 7: Data Access and Manipulation.
Learningcomputer.com SQL Server 2008 – Entity Relationships in a Database.
Dinamic SQL & Cursor. Why Dinamic SQL ? Sometimes there is a need to dynamically create a SQL statement on the fly and then run that command. This can.
Lecture 2 of Advanced Databases Advanced SQL Instructor: Mr.Ahmed Al Astal.
Chapter 9 Joining Data from Multiple Tables
SQL advanced select using Oracle 1 7. Multiple Tables: Joins and Set Operations 8. Subqueries: Nested Queries.
Programming using C# Joins SQL Injection Stored Procedures
1 Definition of a subquery Nested subqueries Correlated subqueries The ISNULL function Derived tables The EXISTS operator Mixing data types: CAST & CONVERT.
1 Intro to JOINs SQL INNER JOIN SQL OUTER JOIN SQL FULL JOIN SQL CROSS JOIN Intro to VIEWs Simple VIEWs Considerations about VIEWs VIEWs as filters ALTER.
04 | Grouping and Aggregating Data Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program Manager.
SQL (DDL & DML Commands)
Chapter 4Introduction to Oracle9i: SQL1 Chapter 4 Joining Multiple Tables.
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.
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.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
SQL advanced select using Oracle 1. 2 Select Simple –data from a single table Advanced –data from more tables join sub-queries.
Chapter 12 Subqueries and Merge Statements
SQL/Lesson 7/Slide 1 of 32 Implementing Indexes Objectives In this lesson, you will learn to: * Create a clustered index * Create a nonclustered index.
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
05 | SET Operators, Windows Functions, and Grouping Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program.
# 1# 1 QueriesQueries How do we ask questions of the data? What is SELECT? What is FROM? What is WHERE? What is a calculated field? Spring 2010 CS105.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 5: SQL I Rob Gleasure robgleasure.com.
Thinking in Sets and SQL Query Logical Processing.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Unit-8 Introduction Of MySql. Types of table in PHP MySQL supports various of table types or storage engines to allow you to optimize your database. The.
There’s a particular style to it… Rob Hatton
SQL Triggers, Functions & Stored Procedures Programming Operations.
Slide 1 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla In this session, you will learn to: Query data by using joins Query.
 MySQL  DDL ◦ Create ◦ Alter  DML ◦ Insert ◦ Select ◦ Update ◦ Delete  DDL(again) ◦ Drop ◦ Truncate.
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
CSC314 DAY 9 Intermediate SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall USING AND DEFINING VIEWS  Views provide users controlled.
LEC-8 SQL. Indexes The CREATE INDEX statement is used to create indexes in tables. Indexes allow the database application to find data fast; without reading.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Advanced SQL Advanced Database Dr. AlaaEddin Almabhouh.
Module 5: Working with Subqueries. Writing Basic Subqueries Writing Correlated Subqueries Comparing Subqueries with Joins and Temporary Tables Using Common.
Chapter 5 Introduction to SQL.
Relational Database Design
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
02 | Advanced SELECT Statements
Database Systems: Design, Implementation, and Management Tenth Edition
Displaying Data from Multiple Tables
Oracle Join Syntax.
C1. SQL BAsic.
The Hidden Mysteries of Common Table Expressions
SQL Subquery.
Writing Basic SQL SELECT Statements
Chapter 8 Advanced SQL.
Oracle Join Syntax.
Database Systems: Design, Implementation, and Management Tenth Edition
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Subqueries Schedule: Timing Topic 25 minutes Lecture
Presentation transcript:

Unit 5 Seminar

Derived Table A derived table is a virtual table that’s calculated on the fly from a select statement. The biggest benefit of using derived tables over using temporary tables is that they require fewer steps, and everything happens in memory instead of a combination of memory and disk.

Derived Table The fewer the steps involved, along with less I/O, the faster the performance.

Example Problem: Generate a report that shows off the total number of orders each customer placed in 1996

Derived Table SELECT C.CustomerID, C.CompanyName, COUNT(O.OrderID) AS TotalOrders FROM Customers C LEFT OUTER JOIN Orders O ON C.CustomerID = O.CustomerID WHERE YEAR(O.OrderDate) = 1996 GROUP BY C.CustomerID, C.CompanyName

Problem with previous query But there’s something missing. Customers that didn’t place an order in 1996 aren’t showing up.

Derived Table (cont) SELECT C.CustomerID, C.CompanyName, COUNT(O.OrderID) AS TotalOrders FROM Customers C LEFT OUTER JOIN Orders O ON C.CustomerID = O.CustomerID WHERE (YEAR(O.OrderDate) = 1996 OR O.OrderDate IS NULL) GROUP BY C.CustomerID, C.CompanyName

Problem with is null version If a customer has placed an order, but just not in the year 1996 they won’t show up. This is because the “is null” check finds customers that have never placed an order—it still doesn’t do anything to add customers who’ve placed an order, but just not in 1996.

Derived Table (cont) SELECT C.CustomerID, C.CompanyName, COUNT(dOrders.OrderID) AS TotalOrders FROM Customers C LEFT OUTER JOIN /* start our derived table */ (SELECT * FROM Orders WHERE YEAR(Orders.OrderDate) = 1996) AS dOrders /* end our derived table */ ON C.CustomerID = dOrders.CustomerID GROUP BY C.CustomerID, C.CompanyName

Derived Table (cont) You should now see a row returned for each Customer and the total number or orders placed in the year 1996—including the customers that didn’t place an order. The reason this works is because the LEFT JOIN will include all matches or null values. If the LEFT JOIN has matches (like in our first query,) but non that match the criteria those rows are excluded.

Common Table Expressions (CTE) A common table expression (CTE) can be thought of as a temporary result set that is defined within the execution scope of a single SELECT, INSERT, UPDATE, DELETE, or CREATE VIEW statement.

CTE (cont) Common Table Expressions offer the same functionality as a view, but are ideal for one-off usages where you don't necessarily need a view defined for the system.

CTE (cont) A CTE is similar to a derived table in that it is not stored as an object and lasts only for the duration of the query. A CTE can be self-referencing and can be referenced multiple times in the same query.

CTE (cont) CTEs allow you to separate part of your T- SQL logic from your main query instead of using a view, correlated subquery, or temp table.

CTE Syntax Start with the word WITH, followed by a name for your CTE. Follow the name of the CTE with the word AS and a set of parentheses. Inside the parentheses, type in a valid SELECT query. You can then use the CTE in your main query just as if the CTE was a table or view.

CTE Syntax WITH emp (SELECT columnname FROM tablenname AS column alias INNER JOIN tablename as column alias ON table.column = table.column) You then can use the CTE in a query SELECT columnname FROM CTE AS column alias Etc.

CTE WITH emp AS ( SELECT EmployeeID, FirstName, LastName, E.Title, ManagerID FROM HumanResources.Employee AS E INNER JOIN Person.Contact AS C ON E.ContactID = C.ContactID )

Query using CTE SELECT A.EmployeeID, A.FirstName, A.LastName, A.Title, A.ManagerID, B.FirstName AS MgrFirstName, B.LastName AS MgrLastName, B.Title AS MgrTitle FROM emp AS A INNER JOIN emp AS B ON A.ManagerID = B.EmployeeID;

Example The query returns a list of the customers along with the products ordered on the most recent order.

Example WITH maxDate AS ( SELECT MAX(OrderDate) AS MaxOrderDate, CustomerID FROM Sales.SalesOrderHeader GROUP BY CustomerID), orders AS ( SELECT SalesOrderID, soh.CustomerID, OrderDate FROM Sales.SalesOrderHeader AS soh INNER JOIN maxDate ON soh.CustomerID = maxDate.CustomerID AND soh.OrderDate = maxDate.MaxOrderDate) SELECT CustomerID,ProductID, sod.SalesOrderID,OrderDate FROM orders INNER JOIN Sales.SalesOrderDetail AS sod ON sod.SalesOrderID = orders.salesOrderID;

CTE’s and Recursion Recursion is the process of defining a solution to a problem in terms of itself. For example, a teacher needs to sort a stack of tests alphabetically by the students' names. She could process the tests one at a time and, for each test, insert it into the appropriate spot to the left (called insertion sort).insertion sort

Recursive CTE 2 pieces: The base case - what to do when you're done recursing. After dividing the tests into separate piles of say, eight elements per pile, the base case is to sort these piles via insertion sort. The recursive step - the action to perform that involves plugging the input "back into" the system. For merge sort, the recursive step is the division of one pile into two. Then into four. Then into eight, and so on, until the base case is reached.

Recursive CTE Translation: This translates into two SQL queries - one that gets the "initial" data UNIONed with one that performs the recursion.

Recursive Deconstructed The WITH clause is the definition of the CTE and it precedes the outer query, which refers back to the CTE. Within the WITH clause, the anchor member is a SELECT statement that acts as the seed for recursion.

Recursive Deconstructed It is merged using the UNION ALL operator to the recursive member, which is a SELECT statement that refers back to the CTE; hence it is recursive.

CTE’s and hierarchies CTEs can also be used to recursively enumerate hierarchical data.

CTE to examine Hierarchies ;WITH cte_name ( ) AS ( UNION ALL ) query_statement_using_cte_name

Hierarchy Example Show the levels that directly report to the Product Development Manager

Steps 1.We create the anchor member as the record which is for the Product Development Manager. As part of this query, we create two pseudo columns. One for indicating the level (called OrgLevel) and for sorting the records in the right fashion (called SortKey). The sort key for us is the primary key of the table converted to a binary column. 2.After the anchor query, we now use this as the input and form the recursive query. Note that the recursive query increments the OrgLevel column and also builds the SortKey column. 3.Since we want only the people who directly report to the product development manager, we specify the condition OrgLevel < 1. What happens if we omit this condition? That is the next sample…

Code WITH SampleOrgChart (Level, Position, ReportingLevel, OrgLevel, SortKey) AS ( -- Create the anchor query. This establishes the starting – point SELECT a.LevelID, a.Position, a.ReportingLevelID, 0, CAST (a.LevelID AS VARBINARY(900)) FROM dbo.SampleOrg a WHERE a.Position = 'Product Development Manager' UNION ALL -- Create the recursive query. This query will be executed -- until it returns no more rows SELECT a.LevelID, a.Position, a.ReportingLevelID, b.OrgLevel+1, CAST (b.SortKey + CAST (a.LevelID AS BINARY(4)) AS VARBINARY(900)) FROM dbo.SampleOrg a INNER JOIN SampleOrgChart b ON a.ReportingLevelID = b.Level WHERE b.OrgLevel < 1 ) SELECT * FROM SampleOrgChart ORDER BY SortKey

Inline Table-Valued Functions (TVFs) An inline table-valued function can be viewed as a select statement with parameters.

Syntax To create an inline table-valued function, you need to use the "RETURNS TABLE" clause in the "CREATE FUNCTION" statement. There should be no function body, except for a RETURN statement with a SELECT subquery

Inline Table-Valued Functions (TVFs) Inline Table-Valued Functions return a resultset, as opposed to a single scalar value. A table valued function specifies the TABLE keyword in its RETURN clause.

Syntax CREATE FUNCTION [ owner_name. ] fn_name ( [ [ AS ] type } [,...n ] ]) RETURNS TABLE [ AS ] RETURN [ ( ] select-statement [ ) ]

Example An Inline Table-Valued Function created by this command: CREATE FUNCTION datesales as datetime) RETURNS TABLE AS RETURN ( SELECT * FROM sales WHERE ord_date

Syntax info A variable has a name that begins with symbol and always must be given a data type.

Call the Function USE PUBS GO select * from datesales('09/13/1994')

Results

Example 2 USE PUBS GO CREATE FUNCTION getAuthorsByState CHAR(2) ) RETURNS TABLE AS RETURN ( SELECT au_fname + ' ' + au_lname AS aName FROM authors WHERE state )

Example 2 (cont) UDF named "getAuthorsByState" Objective: return a list of names of authors whose state field matches the value passed in through the input

Step-Through 1 st : a basic SQL query to concatenate the values of the au_fname and au_lname fields with a space. SELECT au_fname + ' ' + au_lname AS aName FROM authors WHERE state

Step-Through (cont) 2 nd : In the where clause, we tell SQL Server to only return authors whose state matches the value of input parameter. WHERE state

Step-Through (cont) 3 rd : To test our new inline-table valued UDF, clear the code window in query analyzer and enter and execute the following code: USE PUBS GO SELECT * FROM getAuthorsByState('CA')

Test Function

Apply SQL Server 2005 introduced the APPLY operator, which is very much like a join clause and which allows joining between two table expressions i.e. joining a left/outer table expression with a right/inner table expression.join clause

Apply (cont) The APPLY operator allows you to join two table expressions; the right table expression is processed every time for each row from the left table expression.

Apply (cont) The APPLY operator comes in two variants, the CROSS APPLY and the OUTER APPLY. The CROSS APPLY operator returns only those rows from left table expression (in its final output) if it matches with right table expression. In other words, the right table expression returns rows for left table expression match only.

Apply (cont) the OUTER APPLY operator returns all the rows from left table expression irrespective of its match with the right table expression. For those rows for which there are no corresponding matches in right table expression, it contains NULL values in columns of right table expression.

Apply (cont) the CROSS APPLY is semantically equivalent to INNER JOIN (or to be more precise its like a CROSS JOIN with a correlated sub-query) with a implicit join condition of 1=1 whereas OUTER APPLY is semantically equivalent to LEFT OUTER JOIN.

Comparison Cross Apply: SELECT * FROM Department D CROSS APPLY ( SELECT * FROM Employee E WHERE E.DepartmentID = D.DepartmentID ) A GO

Comparison Inner Join SELECT * FROM Department D INNER JOIN Employee E ON D.Departme ntID = E.DepartmentID GO

Same results achieved