Download Assignment3 Queries Hints for Selected Questions (1)

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Assignment-03 : Query Hints
Q2)Employee information for all employees in each state who were hired before the most recently hired person in that
state.
Below are the hints:
•
•
•
Table name : Employee_T,The same table needs to be used twice by alias name(let’s say E1,E2)
Get the SELECT MAX(EmployeeDateHired) from Employee_T , apply condition’ E1.EmployeeState =
E2.EmployeeState’ this query should acts as a sub query.
In the outer query, retrieve all the columns from Employee_T with alias name.
Q4) Names of employees, employee birthdate, manager name, manager’s birthdate for those employees born before
their manager was born; label columns per problem instructions.
Below are the hints:
•
•
•
Table name : Employee_T, The same table needs to be used twice by alias name(let’s say E1,E2)
Apply the condition as ‘WHERE E1.EmployeeSupervisor = E2.EmployeeID and E1.EmployeeBirthdate <
E2.EmployeeBirthdate.
Retrieve all the columns as mentioned in the question.
Q10) List for each vendor those materials that the vendor supplies the supply unit price of which is at least four times
the material standard price.
Below are the hints:
•
•
•
Tables to refer: Vendor_T, Supplies_T, Rawmaterial_T
Connect one table with another by using where condition as ‘Vendor_T.VendorID = Supplies_T.VendorID AND
Supplies_T.MaterialID = Rawmaterial_T.MaterialID AND Supplies_T.Supplyunitprice
>=4*Rawmaterial_T.Materialstandardprice.
Retrieve the columns as mentioned in the question.
Q14) Display the customer ID, name, and order ID for all customer orders. For those customers who do not have any
orders, include them in the display once, with a 0 value for OrderID.
Below are the hints:
•
•
Tables to refer: Customer_T, Order_T
Split the Query into 2 parts:
Part1: Retrieve all the columns mentioned in the question from Customer_T, Order_T by joing two tables using
the column ‘CustomerID’.
Part2:
SELECT CustomerID, CustomerName, 0
FROM Customer_T
WHERE NOT EXISTS
(SELECT * FROM Order_T
WHERE Order_T.CustomerID = Customer_T.CustomerID);
•
Join the both (part1&2) result sets using ‘UNION’ keyword.
Q15) List the IDs and names of all products that cost less than the average product price in their product line.
Below are the hints:
•
•
•
Tables to refer: Product_T, The same table needs to be used twice by alias names.
Subquery : get the AVG(ProductStandardPrice) from Product_T P1.
Outerquery : Retrieve the columns as mentioned the question from Product_T P and apply where condition as
p.ProductStandardPrice < (Subquery).
Q16) List of work centers that employ at least one person who has the skill ‘Q C1’.
Below are the hints:
•
•
•
Tables to refer: Worksin_T, Employee_T, EmployeeSkills_T.
In where condition, connect the tables(WorksIn_T, Employee_T) using EmployeeID,( Employee_T,
EmployeeSkills_T) using EmployeeID and EmployeeSkills_T.SkillID = 'QC1'
Retreive the WorkcenterID.
Q17) Display the Employee ID and Employee Name for those employees who do not possess the skill Router. Display the
results in order by EmployeeName.
Below are the hints:
•
Split the question into 3 part.(2 subqueries and 1 outer query).
Subquerie02: get the EmployeeID from EmployeeSkills_T ES, Skill_T S only when
SkillDescription = 'Router' and ES.SkillID = S.SkillID
Subquerie01: get the EmployeeID from EmployeeSkills_T ES, Skill_T S only when
SkillDescription <> 'Router' and ES.SkillID = S.SkillID and ES.EmployeeID not in
Subquerie02.
Outer Query: Retrieve the columns as specified in question only when EmployeeID not in ‘Subquerie01’.
Q23) List each customer who has bought computer desks and the number of units bought by each customer.
Below are the hints:
• Tables to refer: Order_T O, OrderLine_T OL , Customer_T C
• In select, mention the columns as specified in the question.
• Where condition : ProductID = '3', OL.OrderID= O.OrderID, O.CustomerID=C.CustomerID.
• Group by C.CustomerID, C.CustomerName.
Q25) For each product display in ascending order by product ID the product ID and description along with the customer
ID and name for the customer who has bought the most of that product; also show the total quantity ordered by that
customer (who has bought the most of that product). Use a correlated subquery.
Below are the hints:
•
•
•
•
Tables to refer: Customer_T C1, Product_T P1, Order_T O1.
Retrieve the product ID and description along with the customer ID and name, SUM(OL1.OrderedQuantity) AS
TotOrdered.
Where Conditions:
C1.CustomerID = O1.CustomerID AND .OrderID = OL1.OrderID AND OL1.ProductID = P1.ProductID
Group By and having code snippet:
GROUP BY
P1.ProductID, ProductDescription,
C1.CustomerID, CustomerName
HAVING
SUM(OL1.OrderedQuantity) >= ALL
(SELECT
SUM(OL2.OrderedQuantity)
FROM OrderLine_T OL2,
Order_T O2
WHERE OL2.ProductID = P1.ProductID AND
OL2.OrderID = O2.OrderID AND
O2.CustomerID <> C1.CustomerID
GROUP BY O2.CustomerID)