Taller de Sub Consultas en SQL-1.1
Taller de Sub Consultas en SQL-1.1
Taller de Sub Consultas en SQL-1.1
Resuelva los puntos planteados en este taller, tomando como base el documento
Subconsultas.pdf. Al finalizar la sesión de clase remita el documento, de acuerdo con las
indicaciones del instructor. (1 hora)
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
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
2. Describa con sus propias palabras cual es la diferencia entre las
consultas y por qué deberiamos involucrar una subconsulta para obtener
el resultado deseado.
USE northwind
SELECT od.orderid, customerid
FROM orders AS or1 inner join [order details] AS od on
or1.OrderID=od.OrderID
where Quantity > 20 and od.ProductID = 23
GO
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
SELECT od.orderid, customerid, ProductID, Quantity
FROM orders AS or1 inner join [order details] AS od on
or1.OrderID=od.OrderID
where Quantity > 20 and od.ProductID = 23
GO