Condividi tramite


Sottoquery con IN

Il risultato di una sottoquery introdotta dalla parola chiave IN (o NOT IN) è un elenco di zero o più valori. I risultati restituiti dalla sottoquery vengono utilizzati dalla query esterna.

Nella seguente query vengono trovati i nomi di tutti i prodotti con nome "Wheels" creati da Adventure Works Cycles.

USE AdventureWorks2008R2;
GO
SELECT Name
FROM Production.Product
WHERE ProductSubcategoryID IN
    (SELECT ProductSubcategoryID
     FROM Production.ProductSubcategory
     WHERE Name = 'Wheels');

Il risultato è il seguente:

Name

----------------------------

LL Mountain Front Wheel

ML Mountain Front Wheel

HL Mountain Front Wheel

LL Road Front Wheel

ML Road Front Wheel

HL Road Front Wheel

Touring Front Wheel

LL Mountain Rear Wheel

ML Mountain Rear Wheel

HL Mountain Rear Wheel

LL Road Rear Wheel

ML Road Rear Wheel

HL Road Rear Wheel

Touring Rear Wheel

(14 righe interessate)

Questa istruzione viene valutata in due fasi: la query interna restituisce il numero di identificazione della sottocategoria corrispondente al nome Wheel (17), dopodiché tale valore viene sostituito nella query esterna, che trova i nomi dei prodotti corrispondenti ai numeri di identificazione di sottocategoria in Product.

USE AdventureWorks2008R2;
GO
SELECT Name
FROM Production.Product
WHERE ProductSubcategoryID IN ('17');

L'utilizzo di un join anziché di una sottoquery per la risoluzione di questo e altri problemi analoghi consente di visualizzare nel risultato colonne che derivano da più tabelle. Se, ad esempio, si desidera includere nel risultato il nome della sottocategoria del prodotto, è necessario utilizzare un join.

Use AdventureWorks2008R2;
GO
SELECT p.Name, s.Name
FROM Production.Product p
INNER JOIN Production.ProductSubcategory s
ON p.ProductSubcategoryID = s.ProductSubcategoryID
AND s.Name = 'Wheels';

Il risultato è il seguente:

Name Name

LL Mountain Front Wheel Wheels

ML Mountain Front Wheel Wheels

HL Mountain Front Wheel Wheels

LL Road Front Wheel Wheels

ML Road Front Wheel Wheels

HL Road Front Wheel Wheels

Touring Front Wheel Wheels

LL Mountain Rear Wheel Wheels

ML Mountain Rear Wheel Wheels

HL Mountain Rear Wheel Wheels

LL Road Rear Wheel Wheels

ML Road Rear Wheel Wheels

HL Road Rear Wheel Wheels

Touring Rear Wheel Wheels

(14 righe interessate)

Nella seguente query vengono trovati i nomi di tutti i fornitori per i quali si ha un buon classamento creditizio, da cui Adventure Works Cycles ordina almeno 20 prodotti e il cui intervallo medio per il recapito è inferiore a 16 giorni.

Use AdventureWorks2008R2;
GO
SELECT Name
FROM Purchasing.Vendor
WHERE CreditRating = 1
AND BusinessEntityID IN
    (SELECT BusinessEntityID
     FROM Purchasing.ProductVendor
     WHERE MinOrderQty >= 20
     AND AverageLeadTime < 16);

Il risultato è il seguente:

Name

--------------------------------------------------

Compete Enterprises, Inc

International Trek Center

First National Sport Co.

Comfort Road Bicycles

Circuit Cycles

First Rate Bicycles

Jeff's Sporting Goods

Competition Bike Training Systems

Electronic Bike Repair & Supplies

Crowley Sport

Expert Bike Co

Team Athletic Co.

Compete, Inc.

(13 righe interessate)

Viene innanzitutto valutata la query interna, la quale restituisce i numeri di identificazione dei fornitori che soddisfano le qualificazioni della sottoquery. Dopodiché viene valutata la query esterna. Si noti che nella clausola WHERE sia della query interna che di quella esterna è possibile includere più condizioni.

Se si utilizza un join, la stessa query viene formulata nel modo seguente:

USE AdventureWorks2008R2;
GO
SELECT DISTINCT Name
FROM Purchasing.Vendor v
INNER JOIN Purchasing.ProductVendor p
ON v.BusinessEntityID = p.BusinessEntityID
WHERE CreditRating = 1
AND MinOrderQty >= 20
AND AverageLeadTime < 16;

Un join può sempre essere espresso come sottoquery. Spesso, ma non sempre, una sottoquery può essere espressa come join. Ciò è possibile perché i join sono simmetrici, ovvero l'unione in join delle tabelle A e B restituisce sempre gli stessi risultati indipendentemente dall'ordine delle tabelle nel join. Questo non avviene invece quando si utilizza una sottoquery.

Vedere anche

Concetti