Sunteți pe pagina 1din 33

ADMINISTRACION DE BASE DE DATOS

ADMINISTRACION
DE BASE DE DATOS
Ejercicios

2016

Valor Creativo

Nombre:
Delgado Lucas Kimberly Nayeska
Curso:
5 Nivel A
Docente:
Ing.Carlos Manosalva

2016

ADMINISTRACION DE BASE DE DATOS


Ejercicios sentencias SQL basicos
Laboratorio #1
1. use master
go
select * from sys.databases

2. select * from sys.dm_tran_locks


where resource_database_id = DB_ID()

use AdventureWorks2014
select name from Production.ProductCategory
go

3. use AdventureWorks2014
select name from Production.ProductCategory
go

Laboratorio #2
1) select ProductID, Name,Color,ListPrice
from Production.Product

2) select ProductID, Name,Color,ListPrice


from Production.Product
where ListPrice !=0

3) select ProductID, Name,Color,ListPrice


from Production.Product
where Color is NULL

4) select ProductID, Name,Color,ListPrice


from Production.Product
where Color != 'NULL'

5) select ProductID, Name,Color,ListPrice


from Production.Product
where Color is not NULL and ListPrice !=0

6) select Name+ ':' +Color as name_and_color


from Production.Product
where Color is not NULL

7) select 'Name:' +Name+'--------' +'Color:' as name_and_color


from Production.Product
where Color is not NULL

8) select ProductID, Name


from Production.Product
where ProductID <500 and ProductID >400

9) select ProductID, Name, Color


from Production.Product
where Color = 'Black' or Color = 'blue'

10) select name, ListPrice


from Production.Product
where name like 'S%'

11) select name, ListPrice


from Production.Product
where name like 'A%' or name like 'S%'
order by name

12) select name, ListPrice


from Production.Product
where name like 'SPO%' and name not like '____K%'

13) select Color


from Production.Product
group by Color

14) select ProductSubcategoryID, Color


from Production.Product
where Color is not NULL and ProductSubcategoryID is not NULL
group by ProductSubcategoryID, Color

15) select ProductSubCategoryID, LEFT([Name],35) AS [Name]


, Color, ListPrice
from Production.Product
where Color IN ('Red','Black')
AND ListPrice BETWEEN 1000 AND 2000
OR ProductSubCategoryID = 1
order by ProductID

16) select [Name],


isnull(Color, 'Unknown') as Color ListPrice
from Production.Product

Laboratorio#3
1) select COUNT(ProductNumber) from Production.Product

2) select COUNT(ProductSubcategoryID) from Production.Product


where ProductSubcategoryID is not NULL

3) select COUNT(ProductSubcategoryID) as productIdCategory,


count(ProductNumber) as CountedProduct from Production.Product
group by ProductSubcategoryID

4) select count (ProductSubcategoryID) from Production.Product


where ProductSubcategoryID is not NULL

select count (ProductSubcategoryID) as NosubCat


from Production.Product
where ProductSubcategoryID is not NULL

5) select count(ProductID)as productoID, SUM(Quantity)as TheSum


from Production.ProductInventory
group by ProductID

6) select ProductID, SUM(Quantity)as TheSum


from Production.ProductInventory
where ProductID > 40
group by ProductID
having SUM(Quantity) <100

7) select Shelf, ProductID, SUM(Quantity)as TheSum


from Production.ProductInventory
where ProductID > 40
group by ProductID, Shelf
having SUM(Quantity) <100

8) select avg(Quantity) as theAvg from Production.ProductInventory


where LocationID = 10

9) select Shelf,ProductID, avg(Quantity) as theAvg from Production.ProductInventory


where LocationID = 10
group by Shelf,ProductID

10) SELECT Color, Class,count(ProductID) as thecount, avg(ListPrice) as avgPrice


from Production.Product
where Color is not NULL and Class is not NULL
group by GROUPING SETS( Color, Class),ListPrice

11) SELECT ProductSubcategoryID, COUNT(Name) as Counted,


as IsGrandTotal
FROM Production.Product
GROUP BY ROLLUP (ProductSubcategoryID)
12) select ROW_NUMBER() OVER(ORDER BY Name ASC) as id,Name
from Production.ProductCategory

13) SELECT inventario.ProductID, producto.Name, inventario.LocationID, inventario.Quantity,


RANK() OVER
(PARTITION BY inventario.LocationID ORDER BY inventario.Quantity DESC) AS Rank
FROM Production.ProductInventory AS inventario INNER JOIN Production.Product AS producto
ON inventario.ProductID = producto.ProductID
WHERE inventario.LocationID BETWEEN 2 AND 4
ORDER BY inventario.LocationID

Laboratorio#4
I.
select country.Name, province.Name FROM Person.CountryRegion AS country
INNER JOIN Person.StateProvince AS province
On country.CountryRegionCode = province.CountryRegionCode

II.
select ROW_NUMBER() OVER(ORDER BY country.Name ASC) as id,
country.Name, province.Name FROM Person.CountryRegion AS country
INNER JOIN Person.StateProvince AS province
On country.CountryRegionCode = province.CountryRegionCode
where country.Name = 'Canada' or country.Name = 'Germany'

III.
select SalesOrderID, OrderDate,SalesPersonID, BusinessEntityID, Bonus, SalesYTD
from Sales.SalesOrderHeader as O INNER JOIN Sales.SalesPerson as P
ON O.TerritoryID=P.TerritoryID

IV.
select SalesOrderID, SalesPersonID,Jobtitle, Bonus, SalesYTD
from Sales.SalesOrderHeader as O INNER JOIN Sales.SalesPerson as P
ON O.TerritoryID=P.TerritoryID
INNER JOIN HumanResources.Employee as E
ON E.BusinessEntityID = P.BusinessEntityID

V.
select SalesOrderID, SalesPersonID,FirstName, LastName, Bonus
from Sales.SalesOrderHeader as O INNER JOIN Sales.SalesPerson as P
ON O.TerritoryID=P.TerritoryID
INNER JOIN HumanResources.Employee as E
ON E.BusinessEntityID = P.BusinessEntityID
INNER JOIN Person.Person as pp
ON pp.BusinessEntityID = P.BusinessEntityID

VI.
select SalesOrderID, SalesPersonID,FirstName, LastName, Bonus
from Sales.SalesOrderHeader as O INNER JOIN Sales.SalesPerson as P
ON O.TerritoryID=P.TerritoryID
INNER JOIN Person.Person as pp
ON pp.BusinessEntityID = P.BusinessEntityID

Laboratorio#5

1. - SELECT [Name] FROM Production.Product

WHERE ProductSubcategoryID =
(SELECT ProductSubcategoryID
FROM Production.ProductSubcategory WHERE [Name] = 'Saddles')

2.-

S-ar putea să vă placă și