06 | Using Subqueries and APPLY

Slides:



Advertisements
Similar presentations
Chapter 4 Joining Multiple Tables
Advertisements

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.
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.
Introduction to Oracle9i: SQL1 Subqueries. Introduction to Oracle9i: SQL2 Chapter Objectives Determine when it is appropriate to use a subquery Identify.
Structured Query Language DML MIS 520 – Database Theory Fall 2001 (Day) Lecture 10/11.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
02 | Advanced SELECT Statements Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program Manager.
SQL Joins.
Copyright 2007, Paradigm Publishing Inc. BACKNEXTEND 3-1 LINKS TO OBJECTIVES Save a Filter as a Query Save a Filter as a Query Parameter Query Inner, Left,
1 ICS 184: Introduction to Data Management Lecture Note 10 SQL as a Query Language (Cont.)
A Guide to MySQL 5. 2 Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables Use a subquery.
Graeme Malcolm | Senior Content Developer, Microsoft Geoff Allix | Principal Technologist, Content Master.
04 | Grouping and Aggregating Data Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program Manager.
JOINS cis 407 Inner join Right and left outer join Full join Cross join.
Database Development Tr ươ ng Quý Quỳnh. References UDEMY: SQL Database MasterClass: Go From Pupil To Master! Database Systems - A Practical Approach.
Module 18 Querying XML Data in SQL Server® 2008 R2.
Chapter 4Introduction to Oracle9i: SQL1 Chapter 4 Joining Multiple Tables.
Unit 4 Queries and Joins. Key Concepts Using the SELECT statement Statement clauses Subqueries Multiple table statements Using table pseudonyms Inner.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
10 | Programming with Transact-SQL Graeme Malcolm | Senior Content Developer, Microsoft Geoff Allix | Principal Technologist, Content Master.
Retrieving XML Data from SQL server.  Using the FOR XML Clause to Retrieve Data Retrieving Data in XML Format How SQL Server Generates XML Using the.
Chapter 12 Subqueries and Merge Statements
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
1 SQL – IV Grouping data from tables in SQL –The concept of grouping –GROUP BY clause –HAVING Clause –Determining whether values are unique –Group by using.
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.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
Chapter 7 Subqueries. Chapter Objectives  Determine when it is appropriate to use a subquery  Identify which clauses can contain subqueries  Distinguish.
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.
Module 9: Implementing Functions. Overview Creating and Using Functions Working with Functions Controlling Execution Context.
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.
Module 5: Joining Multiple Tables. Overview Using Aliases for Table Names Combining Data from Multiple Tables Combining Multiple Result Sets.
Querying with Transact-SQL
Boost your T-SQL with the APPLY Operator
Using XML in SQL Server and Azure SQL Database
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
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.
© 2010, Mike Murach & Associates, Inc.
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
08 | Grouping Sets and Pivoting Data
Sorting and Filtering Data
Querying Multiple Tables
04 | Using Set Operators Graeme Malcolm | Senior Content Developer, Microsoft Geoff Allix | Principal Technologist, Content Master.
David M. Kroenke and David J
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.
Writing SELECT Queries
02 | Querying Tables with SELECT
07 | Using Table Expressions
20761B 10: Using Subqueries Module 10   Using Subqueries.
Aggregations Various Aggregation Functions GROUP BY HAVING.
Using Table Expressions
SQL Subquery.
Chapter 8 Advanced SQL.
Database Management System
Subqueries.
Database Systems: Design, Implementation, and Management Tenth Edition
Geo-Databases: lecture 4 Complex Queries in SQL
02 | Querying Tables with SELECT
Pivoting and Grouping Sets
Grouping and Aggregating Data
Presentation transcript:

06 | Using Subqueries and APPLY Graeme Malcolm | Senior Content Developer, Microsoft Geoff Allix | Principal Technologist, Content Master

Module Overview Introduction to Subqueries Scalar or Multi-Valued? Self-Contained or Correlated? Using APPLY with Table-Valued Functions

Introduction to Subqueries Subqueries are nested queries: queries within queries Results of inner query passed to outer query Inner query acts like an expression from perspective of outer query SELECT * FROM…

Scalar or Multi-Valued? Scalar subquery returns single value to outer query Can be used anywhere single- valued expression is used: SELECT, WHERE, and so on Multi-valued subquery returns multiple values as a single column set to the outer query Used with IN predicate SELECT orderid, productid, unitprice, qty FROM Sales.OrderDetails WHERE orderid = (SELECT MAX(orderid) AS lastorder FROM Sales.Orders); SELECT custid, orderid FROM Sales.orders WHERE custid IN ( SELECT custid FROM Sales.Customers WHERE countryregion = N'Mexico');

Using Subqueries

Self-Contained or Correlated? Most subqueries are self-contained and have no connection with the outer query other than passing it results Correlated subqueries refer to elements of tables used in outer query Dependent on outer query, cannot be executed separately Behaves as if inner query is executed once per outer row May return scalar value or multiple values SELECT orderid, empid, orderdate FROM Sales.Orders AS O1 WHERE orderdate = (SELECT MAX(orderdate) FROM Sales.Orders AS O2 WHERE O2.empid = O1.empid) ORDER BY empid, orderdate;

Creating a Correlated Subquery

Using APPLY with Table-Valued Functions CROSS APPLY applies the right table expression to each row in left table Conceptually similar to CROSS JOIN between two tables but can correlate data between sources OUTER APPLY adds rows for those with NULL in columns for right table Conceptually similar to LEFT OUTER JOIN between two tables SELECT S.supplierid, s.companyname, P.productid, P.productname, P.unitprice FROM Production.Suppliers AS S CROSS APPLY dbo.fn_TopProductsByShipper(S.supplierid) AS P

Using APPLY with Table-Valued Functions

Using Subqueries and APPLY Introduction to Subqueries Scalar or Multi-Valued? Self-Contained or Correlated? Using APPLY with Table-Valued Functions Lab: Using Subqueries and APPLY