Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
USE northwind
SELECT orderid, customerid
FROM orders
GO
LA SUBCONSULTA QUEDARIA
USE northwind
SELECT T.orderid, T.customerid
FROM ( SELECT orderid, customerid
FROM orders ) AS T
GO
Para dividir una consulta compleja en varios pasos lógicos, para responder
una consulta que depende de los resultados de otra consulta.
Ejemplo
USE EMPRESA
GO
Regional Distrito Capital
USE northwind
select ProductName, UnitPrice, avg (UnitPrice)as precioProm,
unitprice- avg (UnitPrice)as diferencia
from Products
where CategoryID = 6
group by ProductName, UnitPrice
GO
USE northwind
select ProductName, UnitPrice, (select avg(UnitPrice) from Products)
as PrecioPromedio,
UnitPrice -(select AVG(UnitPrice)from Products) as diferencia
from products
where CategoryID = 6
GO
USE northwind
SELECT od.orderid, customerid
FROM orders AS or1 inner join [order details] AS od on
or1.OrderID=od.OrderID
Regional Distrito Capital
LA SUBCONSULTA QUEDARIA
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 (SELECT quantity FROM [order details] AS od
WHERE or1.orderid = od.orderid AND od.productid = 23)>20
GO
USE northwind
GO