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.

Slides:



Advertisements
Similar presentations
© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
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.
DatabaseDatabase cs453 Lab8 1 Ins.Ebtesam AL-Etowi.
SQL Subqueries Objectives of the Lecture : To consider the general nature of subqueries. To consider simple versus correlated subqueries. To consider the.
Copyright  Oracle Corporation, All rights reserved. 6 Writing Correlated Subqueries.
18 Copyright © Oracle Corporation, All rights reserved. Advanced Subqueries.
1 Lecture 3: Subqueries DCO11310 Database Systems and Design By Rose Chang.
Introduction to Oracle9i: SQL1 Subqueries. Introduction to Oracle9i: SQL2 Chapter Objectives Determine when it is appropriate to use a subquery Identify.
CSEN 5314 Quiz What type of join is needed when you wish to include rows that do not have matching values? A. Equi-joinB. Natural join C. Outer.
Sub Queries Pertemuan 5 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
Module 8: Implementing Views. Overview Introduction Advantages Definition Modifying Data Through Views Optimizing Performance by Using Views.
Tutorial 5 Multi-table queries. Tutorial 5 objectives Displaying Data from Multiple Tables –[ ]Write SELECT statements to access data from more than one.
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.
Banner and the SQL Select Statement: Part Four (Multiple Connected Select Statements) Mark Holliday Department of Mathematics and Computer Science Western.
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.
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi.
Best Practices Transact-SQL Cont....  Combining Data from Multiple Tables Introduction to Joins Using Inner Joins Using Outer Joins Using Cross Joins.
Module 8: Querying Full-Text Indexes. Overview Introduction to Microsoft Search Service Microsoft Search Service Components Getting Information About.
1 Agenda – 10/24/2013 Answer questions from lab on 10/22. Present SQL View database object. Present SQL UNION statement.
Modern Database Management Chapter 8: Advanced SQL.
Unit 4 Queries and Joins. Key Concepts Using the SELECT statement Statement clauses Subqueries Multiple table statements Using table pseudonyms Inner.
Join, Subqueries and set operators. Obtaining Data from Multiple Tables EMPLOYEES DEPARTMENTS … …
Module 4 Designing and Implementing Views. Module Overview Introduction to Views Creating and Managing Views Performance Considerations for Views.
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.
Subqueries Steve Perry 1.
Module 7: Modifying Data. Overview Using Transactions Inserting Data Deleting Data Updating Data Performance Considerations.
Chapter 12 Subqueries and Merge Statements
While you are waiting for class to start... (1)Login to SQL Server 2012 Management Studio (2) Execute the file called “SQLLab4.sql”. It is located on the.
SQL Select Statement IST359.
Module 7: Implementing Views. Overview Introducing Views Defining and Using Views Using Views to Optimize Performance.
Copyright © 2004, Oracle. All rights reserved. Using the Set Operators.
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.
6 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using Subqueries.
Subqueries.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
Oracle9i Developer: PL/SQL Programming Chapter 11 Performance Tuning.
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.
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.
CFUNITED – The premier ColdFusion conference Beyond Basic SQL for CF Nate Nelson
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.
6 Copyright © 2006, Oracle. All rights reserved. Retrieving Data Using Subqueries.
Using Subqueries to Solve Queries
Structured Query Language
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
Multiple Table Queries
02 | Advanced SELECT Statements
Subqueries Schedule: Timing Topic 25 minutes Lecture
Module 8: Querying Full-Text Indexes
20761A 10: Using Subqueries Module 10   Using Subqueries.
Using Subqueries to Solve Queries
Writing Correlated Subqueries
Querying Multiple Tables
06 | Using Subqueries and APPLY
20761B 12: Using Set Operators Module 12   Using Set Operators.
Writing SELECT Queries
07 | Using Table Expressions
Correlated Subqueries
20761B 10: Using Subqueries Module 10   Using Subqueries.
Using Subqueries to Solve Queries
Subqueries Schedule: Timing Topic 25 minutes Lecture
Retrieving Data by Using Subqueries
Using Subqueries to Solve Queries
Subqueries.
Using Subqueries to Solve Queries
Subqueries Schedule: Timing Topic 25 minutes Lecture
Presentation transcript:

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 to Correlate Data Using the EXISTS and NOT EXISTS Clauses

Introduction to Subqueries Why to Use Subqueries To break down a complex query into a series of logical steps To answer a query that relies on the results of an other query Why to Use Joins Rather Than Subqueries SQL Server executes joins faster than subqueries How to Use Subqueries

Using a Subquery as a Derived Table Is a Recordset Within a Query That Functions as a Table Takes the Place of a Table in the FROM Clause Is Optimized with the Rest of the Query USE northwind SELECT T.orderid, T.customerid FROM ( SELECT orderid, customerid FROM orders ) AS T GO USE northwind SELECT T.orderid, T.customerid FROM ( SELECT orderid, customerid FROM orders ) AS T GO

Using a Subquery as an Expression Is Evaluated and Treated as an Expression Is Executed Once for the Query USE pubs SELECT title, price,( SELECT AVG(price) FROM titles) AS average,price-(SELECT AVG(price) FROM titles) AS difference FROM titles WHERE type='popular_comp' GO USE pubs SELECT title, price,( SELECT AVG(price) FROM titles) AS average,price-(SELECT AVG(price) FROM titles) AS difference FROM titles WHERE type='popular_comp' GO

 Using a Subquery to Correlate Data Evaluating a Correlated Subquery Mimicking a JOIN Clause Mimicking a HAVING Clause

Evaluating a Correlated Subquery Back to Step 1 USE northwind SELECT orderid, customerid FROM orders AS or1 WHERE 20 < (SELECT quantity FROM [order details] AS od WHERE or1.orderid = od.orderid AND od.productid = 23) GO USE northwind SELECT orderid, customerid FROM orders AS or1 WHERE 20 < (SELECT quantity FROM [order details] AS od WHERE or1.orderid = od.orderid AND od.productid = 23) GO Outer query passes column values to the inner query Inner query uses that value to satisfy the inner query Inner query returns a value back to the outer query The process is repeated for the next row of the outer query Example 1

Mimicking a JOIN Clause Correlated Subqueries Can Produce the Same Result as a JOIN Clause Joins Let the Query Optimizer Determine How to Correlate Data Most Efficiently USE pubs SELECT DISTINCT t1.type FROM titles AS t1 WHERE t1.type IN (SELECT t2.type FROM titles AS t2 WHERE t1.pub_id <> t2.pub_id) GO USE pubs SELECT DISTINCT t1.type FROM titles AS t1 WHERE t1.type IN (SELECT t2.type FROM titles AS t2 WHERE t1.pub_id <> t2.pub_id) GO Example 1

Mimicking a HAVING Clause Subquery with the Same Result As a HAVING Clause Using a HAVING Clause Without a Subquery USE pubs SELECT t1.type, t1.title, t1.price FROM titles AS t1 WHERE t1.price > ( SELECT AVG(t2.price) FROM titles AS t2 WHERE t1.type = t2.type ) GO USE pubs SELECT t1.type, t1.title, t1.price FROM titles AS t1 WHERE t1.price > ( SELECT AVG(t2.price) FROM titles AS t2 WHERE t1.type = t2.type ) GO USE pubs SELECT t1.type, t1.title, t1.price FROM titles AS t1 INNER JOIN titles AS t2 ON t1.type = t2.type GROUP BY t1.type, t1.title, t1.price HAVING t1.price > AVG(t2.price) GO USE pubs SELECT t1.type, t1.title, t1.price FROM titles AS t1 INNER JOIN titles AS t2 ON t1.type = t2.type GROUP BY t1.type, t1.title, t1.price HAVING t1.price > AVG(t2.price) GO Example 1 Example 2

Using the EXISTS and NOT EXISTS Clauses Use with Correlated Subqueries Determine Whether Data Exists in a List of Values SQL Server Process Outer query tests for the existence of rows Inner query returns TRUE or FALSE No data is produced USE northwind SELECT lastname, employeeid FROM employees AS e WHERE EXISTS (SELECT * FROM orders AS o WHERE e.employeeid = o.employeeid AND o.orderdate = '9/5/97') GO USE northwind SELECT lastname, employeeid FROM employees AS e WHERE EXISTS (SELECT * FROM orders AS o WHERE e.employeeid = o.employeeid AND o.orderdate = '9/5/97') GO Example 1

Recommended Practices Use Subqueries to Break Down a Complex Query Use Table Name Aliases for Correlated Subqueries Use the INSERT…SELECT Statement to Add Rows from Other Sources to an Existing Table Use the EXISTS Operator Instead of the IN Operator

Lab A: Working with Subqueries

Review Introduction to Subqueries Using a Subquery as a Derived Table Using a Subquery as an Expression Using a Subquery to Correlate Data Using the EXISTS and NOT EXISTS Clauses