SQL Scripting Test Intermediate To Advance
SQL Scripting Test Intermediate To Advance
You have the following tables you are requested to create scripts requested below.
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:
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: