Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Question 1

We need an output which has the RateChangeDate column with only the date, full name (first name, middle name,
and last name) and weekly salary (given they work 40 hours a week). The hourly rates for each employee is given in
HumanResources.EmployeePayHistory and Person.Person

Query:

SELECT CONVERT (DATE, RateChangeDate) AS RateChangeDate,


CONCAT(p.FirstName, ' ', p.MiddleName, ' ', p.LastName) AS FullName,
(eph.Rate * 40*7) AS WeeklySalary
FROM HumanResources.EmployeePayHistory eph
JOIN Person.Person p ON p.BusinessEntityID = eph.BusinessEntityID;

Result:

Question 2

We need to change the display of product line categories to make them more understandable. The following
values need to be substituted respectively; 'R'-'Road', 'M'-'Mountain', 'T'-'Touring', 'S'-'Other sale items'. If nothing
is mentioned, then substitute with 'Not for sale'. Return ProductNumber, category, and name of the product. Sort
the result set in ascending order on ProductNumber. Table: production.Product

Query: SELECT CAST(hur.RateChangeDate as VARCHAR(10) ) AS FromDate , CONCAT(LastName, ',


', FirstName, ' ', MiddleName)
AS NameInFull , (40 * hur.Rate) AS SalaryInAWeek FROM Person.Person AS pp INNER JOIN
HumanResources.EmployeePayHistory
AS hur ON hur.BusinessEntityID = pp.BusinessEntityID
ORDER BY NameInFull;

Result:
Question 3

Find the number of employees working in each of the 16 departments. Which Department has occupied the 10th
place when the result is arranged from least strength to most? Name the Department and the number of
employees working. (Tables: [HumanResources].[Department] & [HumanResources].
[EmployeeDepartmentHistory])

Query: SELECT TOP 16 D.Name AS DepartmentName,


COUNT(EDH.BusinessEntityID) AS EmployeeCount
FROM [HumanResources].[Department] D
JOIN [HumanResources].[EmployeeDepartmentHistory] EDH ON D.DepartmentID =
EDH.DepartmentID
GROUP BY D.Name
ORDER BY
EmployeeCount ASC

Result:

Question 4

Find the Quantity ordered in each Sales Territory. (HINT: check the tables which have Sales.[…] for your reference
tables.

Query: SELECT
ST.Name AS SalesTerritory,
SUM(SOD.OrderQty) AS QuantityOrdered
FROM
[Sales].[SalesOrderHeader] AS SOH,
[Sales].[SalesOrderDetail] AS SOD,
[Sales].[SalesTerritory] AS ST
WHERE
SOH.SalesOrderID = SOD.SalesOrderID
AND SOH.TerritoryID = ST.TerritoryID
GROUP BY ST.Name
Result:

Question 5

From the following table write a query in SQL to retrieve the total sales for each year. Filter the result set for those
orders where order year is on or before 2016. Return the year part of orderdate and total due amount. Sort the
result in ascending order on year part of order date. Use table Sales.SalesOrderHeader

Query: SELECT YEAR(OrderDate) AS OrderYear,


SUM(TotalDue) AS TotalSales
FROM [Sales].[SalesOrderHeader]
WHERE YEAR(OrderDate) <= 2016
GROUP BY YEAR(OrderDate)
ORDER BY OrderYear ASC;

Result:

You might also like