You are on page 1of 3

--Joins

--1)Inner (outer) join -->returns records that exists in both tables


--2)Left join----->returns all records from left and matching values from right
--3)Right join---->returns all records from right and matching values from left
--4)Full join----->returns all records when there is match in either left or right
table
--5)cross product ---> m rows from left, n rows from right. m*n records you will
have

--******************************
--retrieve color,name ListPrice, and model of the products

select*
from Production.Product

select*
from Production.ProductModel

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 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
-------------------------------------------------------------------

select pr.Color,pr.ListPrice,pr.Name,pr.ProductModelID prid,prm.ProductModelID


prmid
from Production.Product pr full join Production.ProductModel prm
on pr.ProductModelID=prm.ProductModelID

---SubQuery----

/*

select
from
where column =, in ,>, <, etc.
(
select column
from
..................
)

1) dont forget () in subquery


2)column's data type will same
3)if you have 1 returning vaalue you can use =, if more than 1 use in
*/

--- 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

Create VIEW vwProductPrice


as
select Name, color,ListPrice,
case
when ListPrice between 0 and 1000 then 'cheap'
when ListPrice between 1001 and 2000 then 'normal'
when ListPrice>2000 then 'expensive'
else 'other'
end Price
from Production.Product

select* from vwProductPrice

You might also like