Documente Academic
Documente Profesional
Documente Cultură
--Seleccione todos los campos de la tabla detalle de la orden, ordenada por cantidad
pedida. Ascendentemente.
select * from [Order Details] order by Quantity asc
--Obtener todos los productos, cuyo nombre comienzan con la letra P y tienen un precio
unitario comprendido entre 10 y 120:
select *from Products
select * from Products where ProductName like 'p%' and UnitPrice between 10 and 120
--Obtener todos los productos descontinuados y sin stock, que pertenecen a la categoria
1, 3, 4 y 7.
--select *from Products
select *from Products where (Discontinued = 1 and UnitsInStock = 0) and CategoryID in
(1,3,4,7)
--Obtener todas las órdenes hechas por el empleado con código: 2, 5 y 7 en el año 1996
--select * from Orders
select * from Orders where EmployeeID in (2,5,7) and year(OrderDate)= '1996'
--Seleccionar todos los campos del cliente, cuya compania empiecen con letraA hata la D y
pertenezcan al pais de USA, ordenarlos por la direccion.
--select * from Customers
select * from Customers where CompanyName like ('[a-d]%') order by Address
-- Seleccionar todos los campos del cliente, cuya compania empiecen con las letras de la
B a la G, y pertenezcan al pais de UK,
--ordenarlos por nombre de la compania
select * from Customers where CompanyName like ('[b-g]%') and Country = 'UK' order by
CompanyName
use Northwind
SELECT O.OrderID,CompanyName,O.CustomerID,O.OrderDate,
OD.UnitPrice,P.ProductName
FROM Customers AS C
INNER JOIN Orders AS O
ON C.CustomerID=O.CustomerID
INNER JOIN [Order Details] AS OD
ON O.OrderID=OD.OrderID
INNER JOIN Products AS P
ON P.ProductID=OD.ProductID
/* Obtener todas las ordenes hechas por el empleado King Robert,Davolio Nancy y Fuller
Andrew*/
select o.OrderID, o.EmployeeID,(LastName+ ' ' +FirstName) as ' Nombre Completo de
Empleado'
from Orders as o
inner join Employees as e on
o.EmployeeID = e.EmployeeID
where (e.LastName+ ' ' +e.FirstName) in ('King', 'Robert', 'Davolio', 'Nancy', 'Fuller
Andrew')
SELECT O.OrderID,C.CompanyName,S.CompanyName,FirstName,ProductName
FROM Customers AS C
INNER JOIN Orders AS O
ON C.CustomerID=O.CustomerID
INNER JOIN Employees AS E
ON O.EmployeeID=E.EmployeeID
INNER JOIN [Order Details] AS OD
ON O.OrderID=OD.OrderID
INNER JOIN Products AS P
ON P.ProductID=OD.ProductID
INNER JOIN Suppliers AS S
ON S.SupplierID=P.SupplierID
WHERE O.OrderID=10794
/* Listar los proveedores que tienen mas de 03 productos registrados o lo que tienen un
producto.
Ordenado por cantidad de productos descendentemente*/
--select *from Suppliers
select s.SupplierID, s.CompanyName, s.ContactName, (select COUNT (p.ProductID)
from Products as p where p.SupplierID = s.SupplierID) As 'Cantidad de Productos'
from Suppliers as s where (select count (p.ProductID) from Products as p where
p.SupplierID = s.SupplierID)>3
union
select S.SupplierID, S.CompanyName, S.ContactName,
(select count(P.ProductID)
from Products As P where P.SupplierID= S.SupplierID)
As 'Cantidad de Productos'
from Suppliers As S
where (select count(P.ProductID)
from Products As P where P.SupplierID= S.SupplierID) = 1
order by [Cantidad de Productos] desc