Professional Documents
Culture Documents
Chapter 3
Chapter 3
Dean Smith
Founder, Atamai Analytics
How do sub-queries look?
Uncorrelated
Sub-query executes only once and returns the results to the outer query
Dean Smith
Founder, Atamai Analytics
Venn diagram - presence
Data present in both tables.
SELECT CustomerID
FROM Orders;
SELECT CustomerID
QUICK
FROM Orders; REGGC
SPLIR
CHOPS
...
SELECT CustomerID
FROM Orders;
SELECT CustomerID
FROM Orders;
Disadvantages
The number and order of columns in the SELECT statement must be the same between
queries
Dean Smith
Founder, Atamai Analytics
EXISTS
SELECT CustomerID, CustomerID CompanyName ContactName
CompanyName,
Alfreds
ContactName ALFKI Maria Anders
Fu erkiste
FROM Customers c
Laughing
WHERE EXISTS Yoshi
LAUGB Bacchus Wine
(SELECT 1 Tannamuri
Cellars
FROM Orders o
WHERE c.CustomerID = o.CustomerID);
QUICK QUICK-Stop Horst Kloss
... ... ...
EXISTS will stop searching the sub-query when the condition is TRUE
IN collects all the results from a sub-query before passing to the outer query
Results can contain any column from the outer query, and in any order
Disadvantages
Dean Smith
Founder, Atamai Analytics
INNER JOIN
SELECT c.CustomerID CustomerID CompanyName OrderID ...
,c.CompanyName Vins et alcools
VINET 10248 ...
,o.OrderID Chevalier
,o.OrderDate
HANAR Hanari Carnes 10250 ...
,o.ShippedDate
,o.Freight Victuailles en
VICTE 10251 ...
stock
FROM Customers c
INNER JOIN Orders o Suprêmes
SUPRD 10252 ...
ON c.CustomerID = o.CustomerID délices
... ... ... ...
Advantages
Disadvantage
The number and order of columns in the SELECT statement must be the same between
queries
Advantages
Results can contain any column from the outer query, and in any order
Disadvantage
Advantage
Results can contain any column from the outer query, and in any order
Disadvantages
No results returned because of the way NOT IN handles nulls in the sub-query
Advantage
Results can contain any column, from all joined queries, in any order
Disadvantage
Requirement to add the IS NULL WHERE lter condition with the exclusive
LEFT OUTER JOIN