0% found this document useful (0 votes)
8 views

SQL Scripting Test Intermediate To Advance

Uploaded by

amigablesab
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

SQL Scripting Test Intermediate To Advance

Uploaded by

amigablesab
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

SQL Scripting Test

You have the following tables you are requested to create scripts requested below.

Products Customers Suppliers


-ProductID -CustomerID -SupplierID
-ProductName -CompanyName -CompanyName
-SupplierID -ContactName -ContactName
-CategoryID -ContactTitle -ContactTitle
-QuanityPerUnit -Address -Address
-UnitPrice -City -City
-UnitInStock -Region -Region
-UnitOnOrder -PostalCode -PostalCode
-ReorderLevel -Country -Country
-Discontinued -Phone -Phone
-Fax -Fax
-Homepage

Order Details Orders Categories


-OrderID -OrderID -CategoryID
-ProductID -CustomerID -CategoryName
-UnitPrice -EmployeeID -Description
-Quantity -OrderDate -Picture
-Discount -RequiredDate
-ShippedDate
-ShipVia
-Freight
-ShipName
-ShipAddress
-ShipCity
-ShipRegion
-ShipPostalCode
-ShipCountry

1. Create a script that will return all customer and supplier by city.

ANSWER:

SELECT
'Customer' AS EntityType,
City,
CompanyName,
ContactName,
ContactTitle
FROM Customers
UNION ALL

SELECT
'Supplier' AS EntityType,
City,
CompanyName,
ContactName,
ContactTitle
FROM Suppliers
ORDER BY City, EntityType, CompanyName;

2. Create a script that will match all products to its corresponding categories.

ANSWER:

SELECT
p.ProductID,
p.ProductName,
c.CategoryName,
c.Description
FROM Products p
JOIN Categories c ON p.CategoryID = c.CategoryID;

3. Create a view that will return the total amount per order of discount. Discount column in
the table is presented in decimal form.

ANSWER:

CREATE VIEW OrderDiscounts AS


SELECT
od.OrderID,
SUM(od.UnitPrice * od.Quantity * od.Discount) AS TotalDiscount
FROM OrderDetails od
GROUP BY od.OrderID;

4. Create a script that will return if the product is above or below the products average unit price.

ANSWER:

SELECT
p.ProductID,
p.ProductName,
p.UnitPrice,
CASE
WHEN p.UnitPrice > (SELECT AVG(UnitPrice) FROM Products) THEN 'Above Average'
ELSE 'Below Average'
END AS PriceCategory
FROM Products p;

5. Create a view that will summarize the total amount ordered and count unique numbers
of product by customer.

ANSWER:

CREATE VIEW CustomerOrderSummary AS


SELECT
o.CustomerID,
c.CompanyName,
SUM(od.UnitPrice * od.Quantity * (1 - od.Discount)) AS TotalAmountOrdered,
COUNT(DISTINCT od.ProductID) AS UniqueProductCount
FROM Orders o
JOIN OrderDetails od ON o.OrderID = od.OrderID
JOIN Customers c ON o.CustomerID = c.CustomerID
GROUP BY o.CustomerID, c.CompanyName;

You might also like