You are on page 1of 2

select CategoryName, count(*) as cantidad

from Products p inner join Categories c on p.CategoryID=c.CategoryID


group by CategoryName

--subconsulta
select count(*) from Products
--ejercicio
--mostrar la cantitad total de compras, la maxima compra para cada cliente

--consulta global

select CompanyName,
(select sum(Quantity) from OrderDetails d where o.OrderID=d.OrderID) as cantidad,
(select max(UnitPrice*Quantity) from OrderDetails d where o.OrderID=d.OrderID) as
maximo
from Customers c inner join Orders o on c.CustomerID=o.CustomerID

--subconsulta
select *from Orders
select sum(Quantity) from OrderDetails
select max (unitprice*Quantity) from OrderDetails

--utilizando group by
select CompanyName ,sum (Quantity) as cantidad , max (UnitPrice*Quantity) as
maximo
from
Customers c inner join Orders o on c.CustomerId=o.CustomerID
inner join OrderDetails d on d.OrderID=d.OrderID
group by CompanyName

-- subconsultas de condicion
select *from orders
select *from Products

--mostrar los productos comprados por los 3 primeros clientes cuyos


precios de los productos
--esten por debajp del promedio de los precios

--consulta global
select distinct p.ProductName from Customers c inner join Orders o on
c.CustomerID=o.CustomerID
inner join OrderDetails d on o.OrderID=d.OrderID
inner join Products p on d.ProductID=p.ProductID
where c.CustomerID in (select top 3 CustomerID from Customers) and
p.UnitPrice < (select avg(UnitPrice) from Products)

--subconsulta7
select top 3 * from Customers
--subconsulta
select avg (UnitPrice) from Products

--ejemplos
--cosnsulta global
select * from Products where CategoryID in (select CategoryID from Categories where
CategoryName like 'con%')
order by CategoryID
--subconsulta
select CategoryID from Categories where CategoryName like 'con%'

ejercicio mostrar los prodcutos que no se vendieron en el año 1998 y en los ultimos
dias del ultimo mes

-- cosulta global
select *from Products

--subconsulta
select * from Products where ProductID not in (
select d.ProductID from Orders o inner join OrderDetails d on o.OrderID=d.OrderID
where year(o.OrderDate) = 1998 and month(o.OrderDate)=
(select max(month(OrderDate)) from Orders where year(OrderDate) = 1998) and
day(OrderDate)>=(select max(day(OrderDate))-1 from Orders where year(OrderDate) =
1998 and month(OrderDate)=5)
)

You might also like