Professional Documents
Culture Documents
Assignment 3
Assignment 3
Q2)
SELECT CustomerName, OrderID, OrderDate
FROM Customer_T s, Order_T o
WHERE s.CustomerId=o.CustomerID and CustomerState='NY'
ORDER BY CustomerName ASC
Q3)
select sum( OrderedQuantity*ProductStandardPrice) [Total Sale]
from Order_T, OrderLine_T ,Product_T , ProductLine_T
where OrderLine_T.ProductID = Product_T.ProductID and
Product_T.ProductLineID=ProductLine_T.ProductLineID
and ProductLineName='BASIC' and Order_T.OrderID= OrderLine_T.OrderID and YEAR(
OrderDate)=2018
Q4)
select ProductLineName , count(ProductID) [Number of Products]
from ProductLine_T s left join Product_T o on s.ProductLineID=o.ProductLineID
group by ProductLineName
Q5)
Q6)
select ProductID, ProductDescription, ProductionCost [Production Cost],
ProductStandardPrice
from (select Product_T.ProductID, Product_T.ProductDescription,ProductionCost ,
ProductStandardPrice
from Product_T , ( select Uses_T.ProductID, Product_T.ProductDescription,
sum(QuantityRequired* MaterialStandardPrice)[ProductionCost]
from Uses_T , RawMaterial_T , Product_T
where Uses_T.MaterialID=RawMaterial_T.MaterialID and
Uses_T.ProductID=Product_T.ProductID
group by Uses_T.ProductID, Product_T.ProductDescription) P
where Product_T.ProductID=P.ProductID ) S
where ProductionCost >0.5 * ProductStandardPrice
Q7) with LEFT JOIN, NOT IN, NOT EXISTS (Codes are in query file)
--Q7/a
select o.CustomerId, CustomerName
from Customer_T o LEFT JOIN Order_T s
on o.CustomerID =s.CustomerID
where OrderDate is NULL
--Q7/b
SELECT DISTINCT CustomerId, CustomerName
FROM Customer_T
WHERE CustomerID not in
(SELECT distinct CustomerID
From Order_T
)
--Q7/c
SELECT DISTINCT CustomerId, CustomerName
FROM Customer_T ol
WHERE Not Exists
(SELECT distinct CustomerID
From Order_T
where CustomerID=ol.CustomerID
Q8)
CREATE PROCEDURE ListSalesByState @state varchar(50) AS
begin
select Product_T.ProductID, ProductDescription, CustomerName,
sum(OrderedQuantity*ProductStandardPrice)[Sales $]
from Customer_T, Order_T, OrderLine_T,Product_T
where Customer_T.CustomerId=Order_T.CustomerID and Order_T.OrderID=OrderLine_T.OrderID
and OrderLine_T.ProductID=Product_T.ProductID and CustomerState = @state
GROUP BY Product_T.ProductID, ProductDescription, CustomerName
ORDER BY Product_T.ProductID
end
Q10)
Q12)
select CustomerName, max(OrderID) As OrderID , max(LastOrder) [OrderDate]
Q14)
select mn.ProductLineID, mn.[ProductDescription (Cheapest)], mx.[ProductDescription (Most
Expensive)]
from (select b.ProductLineID , b.ProductDescription [ProductDescription (Most
Expensive)], b.ProductStandardPrice
from Product_T b
where b.ProductStandardPrice= (select max (b1.ProductStandardPrice)
from Product_T b1 where b1.ProductLineID=b.ProductLineID)) mx,
(select c.ProductLineID , c.ProductDescription [ProductDescription (Cheapest)] ,
c.ProductStandardPrice
from Product_T c
where c.ProductStandardPrice= (select min (c1.ProductStandardPrice)
from Product_T c1 where c1.ProductLineID=c.ProductLineID)) mn
where mx.ProductLineID=mn.ProductLineID
Q15)
select sh.OrderID,sh.OrderDate, CustomerName,sh.[Full Shipping Address]
from Customer_T o ,(select OrderID,o.CustomerID,o.OrderDate, (ShipAddress+ ShipCity +'
'+ ShipState) [Full Shipping Address]
from Order_T o, CustomerShipAddress_T s where
o.ShipAdrsID=s.ShipAddressID) sh
where o.CustomerID=sh.CustomerId
--Q16/b
select v.VendorID , VendorName
from Vendor_T v, Supplies_T s
where s.MaterialID= 'NC058' and v.VendorID=s.VendorID
intersect
select v.VendorID , VendorName
from Vendor_T v, Supplies_T s
where s.MaterialID= 'NF3' and v.VendorID=s.VendorID
Q17)
select Manager [SupervisorName], EmployeeName, Em.Age
from (SELECT E1.EmployeeName, DATEDIFF(hour, E1.EmployeeBirthDate ,GETDATE())/8766 AS
Age, E2.EmployeeName AS Manager
FROM Employee_T E1,Employee_T E2
WHERE E1.EmployeeSupervisor = E2.EmployeeID) Em,
(select e.EmployeeSupervisor, e.EmployeeName [The Oldest], (DATEDIFF(hour,
e.EmployeeBirthDate ,GETDATE())/8766) As AGE
from Employee_T e
where (DATEDIFF(hour, e.EmployeeBirthDate ,GETDATE())/8766) = (select
min(DATEDIFF(hour, E1.EmployeeBirthDate ,GETDATE())/8766) AS Age
from Employee_T e1
where e1.EmployeeSupervisor=e.EmployeeSupervisor)) Ag
where Em.Age=Ag.AGE