Professional Documents
Culture Documents
Week12 DB Lab - SQL
Week12 DB Lab - SQL
--******************************
--retrieve color,name ListPrice, and model of the products
select*
from Production.Product
select*
from Production.ProductModel
------------------------------------------------------------------
--retrieve products which has models
select prm.ProductModelID,pr.Color, pr.ListPrice,prm.Name,pr.Name
from Production.Product pr inner join Production.ProductModel prm
on pr.ProductModelID=prm.ProductModelID
--------------------------------------------------------------------
--retrieve all products,whether they have the model or not.
select pr.Color,pr.ListPrice,pr.Name,pr.ProductModelID prid,prm.ProductModelID
prmid
from Production.Product pr left join Production.ProductModel prm
on pr.ProductModelID=prm.ProductModelID
-------------------------------------------------------------------
--retrieve all models whether they have product or not
--------------------------------------------------------------
select pr.Color,pr.ListPrice,pr.Name,pr.ProductModelID prid,prm.ProductModelID
prmid
from Production.Product pr right join Production.ProductModel prm
on pr.ProductModelID=prm.ProductModelID
-------------------------------------------------------------------
---SubQuery----
/*
select
from
where column =, in ,>, <, etc.
(
select column
from
..................
)
--- retrieve all products whose colors will be same as the most expensive product's
color
select *
from Production.Product
where color =
(
select TOP 1 Color
from Production.Product
order by ListPrice DESC
)
select *
from Production.Product
where color in
(
select TOP 15 Color
from Production.Product
order by ListPrice DESC
)
-----------------------------------------------------------------------------------
--------
--retrieve all products whose ListPrice's are greater than avg ListPrice
select*
from Production.Product
where ListPrice >
(
select avg(ListPrice)
from Production.Product
)
--------------------------------------------------------------------------------
--retrie customers such that they have no orders
select *
from Sales.Customer
select *
from Sales.SalesOrderHeader
select*
from Sales.Customer
where CustomerID not in
(
select CustomerID
from Sales.SalesOrderHeader
)
-----------------------------------------------------------------
-----------------------------------------------------------------
------View---------------------------------------------------------------------
---you can ony use select, not insert, update, delete in views