0% found this document useful (0 votes)
134 views4 pages

Ejercicios Base de Datos

This document contains SQL queries and exercises on a database for a course on database administration. It includes queries to list employee names, top selling products in 1997, subqueries to find worst customers, top selling products in 1998, top selling categories, and top selling employees. It also classifies customers into categories based on total purchase amounts.

Uploaded by

Jhanie LH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
134 views4 pages

Ejercicios Base de Datos

This document contains SQL queries and exercises on a database for a course on database administration. It includes queries to list employee names, top selling products in 1997, subqueries to find worst customers, top selling products in 1998, top selling categories, and top selling employees. It also classifies customers into categories based on total purchase amounts.

Uploaded by

Jhanie LH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

CURSO:

Administracin de Base de Datos


ALUMNA:
Gianella Isabel
Hurtado Lovera

AULA:

C04BT1-2015-2

PROFESOR:

Guevara Gamboa Flix Antonio

2015

EJERCICIOS PROPUESTOS DE SQL SERVER:


--17) Listar el nombre y apellido del empleado en una columna llamada Nom_Ape
select (LastName+space(1)+FirstName) as nom_ape ,* FROM Employees

--18) Listar que productos se vendieron ms en el ao 1997:


Select [ProductName] as 'producto',[QuantityPerUnit] as 'detalles',[Quantity]as'cantidad',
[OrderDate] as 'fecha'
from [Order Details] AS o join Products AS a on o.[ProductID] =a.[ProductID]
join Orders as e on e.OrderID=o.OrderID
where year(OrderDate) = 1997 order by cantidad desc

--SUB CONSULTAS
--1. Listar los 10 peores clientes que han sido registrados en la Base de Datos
select TOP 10 [CustomerID] As 'Cdigo', [ContactName] As
'Cliente',[CompanyName] AS Empresa ,[Address] As 'Direccin', (select sum(freight)
from Orders As O where O.CustomerID = C.CustomerID) As
'Monto Comprado' ,City As 'Ciudad' from Customers As C
order by 'Monto Comprado' ASC

--2. Listar los 10 productos ms vendidos en el supermercado en el ao 1998


select top 10 [ProductName] as 'producto',[QuantityPerUnit] as 'detalles',
[Quantity]as'cantidad',[OrderDate] as 'fecha'
from [Order Details] AS o join Products AS a on o.[ProductID]=a.[ProductID]
join Orders as e on e.OrderID=o.OrderID
where year(OrderDate) = 1998 order by cantidad desc

--3. Listar las 3 categoras ms vendidas

select top 3 ca.CategoryName ,COUNT(pro.CategoryID)as cantidad from Categories as


ca
inner join Products as pro on pro.CategoryID=ca.CategoryID
group by ca.CategoryName order by cantidad desc

--4. Listar los 5 empleados que facturaron ms


Select top 5 (LastName+space(1)+FirstName) as empleado, SUM(d.UnitPrice *
d.Quantity) as
Total_Facturado From Employees as e
JOIN Orders p on e.EmployeeID = p.EmployeeID
JOIN "order details" d on p.OrderID = d.OrderID
Group by (LastName+space(1)+FirstName) order by SUM(d.UnitPrice * d.Quantity) desc

--5. Esta subconsulta muestra los 5 mejores productos vendidos en el supermercado


select top 5 p.ProductName, sum( Quantity) as cantidad_vendida from Products as p
inner join [Order Details] as od on od.ProductID=p.ProductID
inner join Orders as o on o.OrderID=od.OrderID
group by p.ProductName order by cantidad_vendida desc

--6. Mostrar una categora de clientes segn su poder de consumo


--a. Clase d 0-50
select [CustomerID] As 'Cdigo', [ContactName] As
'Cliente',[CompanyName] AS Empresa ,[Address] As 'Direccin', (select sum(freight)
from Orders As O where O.CustomerID = C.CustomerID) As
Consumo ,City As 'Ciudad' from Customers As C
group by [CustomerID],[ContactName],[CompanyName],[Address],city
having (select sum(freight)
from Orders As O where O.CustomerID = C.CustomerID) <50
order by consumo asc
--b. Clase c + 50 -> 200
select [CustomerID] As 'Cdigo', [ContactName] As
'Cliente',[CompanyName] AS Empresa ,[Address] As 'Direccin', (select sum(freight)
from Orders As O where O.CustomerID = C.CustomerID) As
consumo ,City As 'Ciudad' from Customers As C
group by [CustomerID],[ContactName],[CompanyName],[Address],city
having (select sum(freight)
from Orders As O where O.CustomerID = C.CustomerID) >49 AND (select sum(freight)
from Orders As O where O.CustomerID = C.CustomerID)<200
order by consumo asc
--c. Clase b +200 -> 5000
select [CustomerID] As 'Cdigo', [ContactName] As
'Cliente',[CompanyName] AS Empresa ,[Address] As 'Direccin', (select sum(freight)
from Orders As O where O.CustomerID = C.CustomerID) As
consumo ,City As 'Ciudad' from Customers As C
group by [CustomerID],[ContactName],[CompanyName],[Address],city
having (select sum(freight)
from Orders As O where O.CustomerID = C.CustomerID) >200 AND (select sum(freight)
from Orders As O where O.CustomerID = C.CustomerID)<5000
order by consumo asc
--d. Clase a + 5000
select [CustomerID] As 'Cdigo', [ContactName] As
'Cliente',[CompanyName] AS Empresa ,[Address] As 'Direccin', (select sum(freight)
from Orders As O where O.CustomerID = C.CustomerID) As
consumo ,City As 'Ciudad' from Customers As C
group by [CustomerID],[ContactName],[CompanyName],[Address],city
having (select sum(freight)
from Orders As O where O.CustomerID = C.CustomerID)>5000
order by consumo asc

You might also like