Sub-queries

Improving Query Performance in SQL Server

Dean Smith

Founder, Atamai Analytics

How do sub-queries look?

Sub-query

Improving Query Performance in SQL Server

How do sub-queries look?

Sub-query with outer-query

Improving Query Performance in SQL Server

Sub-query with FROM

Sub-query with FROM

Improving Query Performance in SQL Server

Sub-query with FROM

SELECT OrderID, 
       CustomerID, 
       NumDays
FROM 
    (SELECT *, 
     DATEDIFF(DAY,OrderDate,ShippedDate) AS NumDays 
     FROM Orders) AS o
WHERE NumDays >= 35;
OrderID CustomerID NumDays
10380 HUNGO 35
10427 PICCO 35
10545 LAZYK 35
10593 LEHMS 35
10660 HUNGC 37
10777 GOURL 37
10924 BERGS 35
Improving Query Performance in SQL Server

Sub-query with WHERE

Sub-query with WHERE

Improving Query Performance in SQL Server

Sub-query with WHERE

SELECT CustomerID
       ,CompanyName 
FROM Customers
WHERE CustomerID 
         IN (SELECT CustomerID 
         FROM Orders 
         WHERE Freight > 800);
CustomerID CompanyName
QUEEN Queen Cozinha
QUICK QUICK-Stop
SAVEA Save-a-lot Markets
Improving Query Performance in SQL Server

Sub-query with SELECT

Sub-query with SELECT

Improving Query Performance in SQL Server

Sub-query with SELECT

SELECT CustomerID, 
       CompanyName, 
        (SELECT AVG(Freight) 
        FROM Orders o 
        WHERE c.CustomerID = o.CustomerID) AS AvgFreight
FROM Customers c;
CustomerID CompanyName AvgFreight
ALFKI Alfreds Futterkiste 37.6
ANATR Ana Trujillo Emparedados y helados 24.4
ANTON Antonio Moreno Taquería 38.4
... ... ...
Improving Query Performance in SQL Server

Types of sub-queries

Uncorrelated sub-query

SELECT CustomerID
       ,CompanyName 
FROM Customers
WHERE CustomerID IN

(SELECT CustomerID FROM Orders WHERE Freight > 800);
  • Sub-query does not contain a reference to the outer query
  • Sub-query can run independently of the outer query
  • Used with WHERE and FROM

Correlated sub-query

SELECT CustomerID, 
       CompanyName,

(SELECT AVG(Freight) FROM Orders o WHERE c.CustomerID = o.CustomerID) AS AvgFreight
FROM Customers c;
  • Sub-query contains a reference to the outer query
  • Sub-query cannot run independently of the outer query
  • Used with WHERE and SELECT
Improving Query Performance in SQL Server

Sub-query performance

Correlated

  • Sub-query executes for each row in the outer query

Uncorrelated

  • Sub-query executes only once and returns the results to the outer query
Improving Query Performance in SQL Server

Sub-query vs. INNER JOIN

Correlated sub-query

SELECT CustomerID, 
       CompanyName, 
        (SELECT AVG(Freight) 
        FROM Orders o 
        WHERE c.CustomerID = o.CustomerID) AS AvgFreight
FROM Customers c;

INNER JOIN

SELECT c.CustomerID,
       c.CompanyName, 
       AVG(o.Freight)
FROM Customers c
INNER JOIN Orders o
    ON c.CustomerID = o.CustomerID
GROUP BY c.CustomerID,
         c.CompanyName;
Improving Query Performance in SQL Server

Let's practice!

Improving Query Performance in SQL Server

Preparing Video For Download...