Uploaded by Maody

SQL Northwind database questions with answers

advertisement
Akhbar El-Yom Academy
Computer Science Department - First Year
Course: Databases
SQL Training Ques�ons
1.What is the total number of orders in the Orders table?
Answer: SELECT COUNT(*) FROM Orders;
Complexity level: 1
2.What is the total number of products in the Products table?
Answer: SELECT COUNT(*) FROM Products;
Complexity level: 1
3.What is the total number of customers in the Customers table?
Answer: SELECT COUNT(*) FROM Customers;
Complexity level: 1
4.What is the total number of employees in the Employees table?
Answer: SELECT COUNT(*) FROM Employees;
Complexity level: 1
5.What is the name of the company that supplied the product with id 1?
Answer: SELECT CompanyName FROM Suppliers WHERE SupplierID = (SELECT
SupplierID FROM Products WHERE ProductID = 1);
Complexity level: 2
Akhbar El-Yom Academy
Computer Science Department - First Year
Course: Databases
6.What is the name of the product that has the highest unit price in the Products
table?
Answer: SELECT ProductName FROM Products WHERE UnitPrice = (SELECT
MAX(UnitPrice) FROM Products);
Complexity level: 2
7.What is the name of the customer who has the longest company name in the
Customers table?
Answer: SELECT TOP 1 CompanyName FROM Customers ORDER BY
LEN(CompanyName) DESC;
Complexity level: 3
8.What is the total revenue generated by each employee in the year 1997?
Answer: SELECT e.FirstName + ' ' + e.LastName AS EmployeeName,
SUM(od.UnitPrice * od.Quan�ty) AS TotalRevenue FROM Employees e JOIN
Orders o ON e.EmployeeID = o.EmployeeID JOIN [Order Details] od ON
o.OrderID = od.OrderID WHERE YEAR(o.OrderDate) = 1997 GROUP BY
e.EmployeeID, e.FirstName, e.LastName;
Complexity level: 3
9.What is the name of the employee who has the most orders in the Orders
table?
Answer: SELECT TOP 1 e.FirstName + ' ' + e.LastName AS EmployeeName,
COUNT() AS OrderCount FROM Employees e JOIN Orders o ON e.EmployeeID =
o.EmployeeID GROUP BY e.EmployeeID, e.FirstName, e.LastName ORDER BY
COUNT() DESC;
Complexity level: 3
Akhbar El-Yom Academy
Computer Science Department - First Year
Course: Databases
10.What is the name of the customer who has the highest total order amount in
the Orders and Order Details tables?
Answer: SELECT TOP 1 c.CompanyName, SUM(od.UnitPrice * od.Quan�ty) AS
TotalAmount FROM Customers c JOIN Orders o ON c.CustomerID = o.CustomerID
JOIN [Order Details] od ON o.OrderID = od.OrderID GROUP BY c.CustomerID,
c.CompanyName ORDER BY SUM(od.UnitPrice * od.Quan�ty) DESC;
Complexity level: 4
11.What is the name of the customer who has the highest average order amount
in the Orders and Order Details tables?
Answer: SELECT TOP 1 c.CompanyName, AVG(od.UnitPrice * od.Quan�ty) AS
AvgAmount FROM Customers c JOIN Orders o ON c.CustomerID = o.CustomerID
JOIN [Order Details] od ON o.OrderID = od.OrderID GROUP BY c.CustomerID,
c.CompanyName ORDER BY AVG(od.UnitPrice * od.Quan�ty) DESC;
Complexity level: 4
12.What is the name of the product that has the most units in stock in the
Products table?
Answer: SELECT TOP 1 ProductName FROM Products ORDER BY UnitsInStock
DESC;
Complexity level: 2
13.What is the name of the customer who placed the earliest order in the
Orders table?
Answer: SELECT TOP 1 c.CompanyName FROM Customers c JOIN Orders o ON
c.CustomerID = o.CustomerID ORDER BY o.OrderDate;
Complexity level: 3
Akhbar El-Yom Academy
Computer Science Department - First Year
Course: Databases
14.What is the name of the customer who placed the latest order in the Orders
table?
Answer: SELECT TOP 1 c.CompanyName FROM Customers c JOIN Orders o ON
c.CustomerID = o.CustomerID ORDER BY o.OrderDate DESC;
Complexity level: 3
15.What is the name of the employee who sold the most products in the Order
Details table?
Answer: SELECT TOP 1 e.FirstName + ' ' + e.LastName AS EmployeeName,
SUM(od.Quan�ty) AS TotalSold FROM Employees e JOIN Orders o ON
e.EmployeeID = o.EmployeeID JOIN [Order Details] od ON o.OrderID =
od.OrderID GROUP BY e.EmployeeID, e.FirstName, e.LastName ORDER BY
SUM(od.Quan�ty) DESC;
Complexity level: 4
16.What is the name of the customer who placed the most orders in the Orders
table?
Answer: SELECT TOP 1 c.CompanyName, COUNT() AS OrderCount FROM
Customers c JOIN Orders o ON c.CustomerID = o.CustomerID GROUP BY
c.CustomerID, c.CompanyName ORDER BY COUNT() DESC;
Complexity level: 4
Akhbar El-Yom Academy
Computer Science Department - First Year
Course: Databases
17.What is the name of the employee who sold the most units in the Order
Details table?
Answer: SELECT TOP 1 e.FirstName + ' ' + e.LastName AS EmployeeName, SUM(od.Quan�ty)
AS TotalSold FROM Employees e JOIN Orders o ON e.EmployeeID = o.EmployeeID JOIN [Order
Details] od ON o.OrderID = od.OrderID GROUP BY e.EmployeeID, e.FirstName, e.LastName
ORDER BY SUM(od.Quan�ty * od.UnitPrice) DESC;
Complexity level: 4
18.What is the name of the customer who placed the highest number of orders
in the year 1997?
Answer: SELECT TOP 1 c.CompanyName, COUNT() AS OrderCount FROM
Customers c JOIN Orders o ON c.CustomerID = o.CustomerID WHERE
YEAR(o.OrderDate) = 1997 GROUP BY c.CustomerID, c.CompanyName ORDER BY
COUNT() DESC;
Complexity level: 4
19.What is the name of the customer who placed the highest number of orders
in the month of January in the Orders table?
Answer: SELECT TOP 1 c.CompanyName, COUNT() AS OrderCount FROM
Customers c JOIN Orders o ON c.CustomerID = o.CustomerID WHERE
MONTH(o.OrderDate) = 1 GROUP BY c.CustomerID, c.CompanyName ORDER BY
COUNT() DESC;
Complexity level: 4
Akhbar El-Yom Academy
Computer Science Department - First Year
Course: Databases
20.What is the name of the employee who sold the most units in the year 1997
in the Order Details table?
Answer: SELECT TOP 1 e.FirstName + ' ' + e.LastName AS EmployeeName,
SUM(od.Quan�ty) AS TotalSold FROM Employees e JOIN Orders o ON
e.EmployeeID = o.EmployeeID JOIN [Order Details] od ON o.OrderID =
od.OrderID WHERE YEAR(o.OrderDate) = 1997 GROUP BY e.EmployeeID,
e.FirstName, e.LastName ORDER BY SUM(od.Quan�ty) DESC;
Complexity level: 5
21.What is the total revenue generated by each category in the Products,
Categories, and Order Details tables?
Answer: SELECT c.CategoryName, SUM(od.UnitPrice * od.Quan�ty) AS
TotalRevenue FROM Categories c JOIN Products p ON c.CategoryID =
p.CategoryID JOIN [Order Details] od ON p.ProductID = od.ProductID GROUP BY
c.CategoryName;
Complexity level: 3
22.What is the total revenue generated by each supplier in the Suppliers,
Products, and Order Details tables?
Answer: SELECT s.CompanyName, SUM(od.UnitPrice * od.Quan�ty) AS
TotalRevenue FROM Suppliers s JOIN Products p ON s.SupplierID = p.SupplierID
JOIN [Order Details] od ON p.ProductID = od.ProductID GROUP BY
s.CompanyName;
Complexity level: 3
Akhbar El-Yom Academy
Computer Science Department - First Year
Course: Databases
23.What is the total number of orders placed by each country in the Customers
and Orders tables?
Answer: SELECT c.Country, COUNT(*) AS OrderCount FROM Customers c JOIN
Orders o ON c.CustomerID = o.CustomerID GROUP BY c.Country;
Complexity level: 3
24.What is the total revenue generated by each employee in the year 1997 and
for each product category in the Products, Categories, Employees, Orders, and
Order Details tables?
Answer: SELECT e.FirstName + ' ' + e.LastName AS EmployeeName,
c.CategoryName, SUM(od.UnitPrice * od.Quan�ty) AS TotalRevenue FROM
Employees e JOIN Orders o ON e.EmployeeID = o.EmployeeID JOIN [Order
Details] od ON o.OrderID = od.OrderID JOIN Products p ON od.ProductID =
p.ProductID JOIN Categories c ON p.CategoryID = c.CategoryID WHERE
YEAR(o.OrderDate) = 1997 GROUP BY e.EmployeeID, e.FirstName, e.LastName,
c.CategoryName;
Complexity level: 4
25.What is the name of the customer who placed the highest number of orders
in the year 1997 and has the highest total order amount in the Orders and Order
Details tables?
Answer: SELECT TOP 1 c.CompanyName, SUM(od.UnitPrice * od.Quan�ty) AS
TotalAmount, COUNT() AS OrderCount FROM Customers c JOIN Orders o ON
c.CustomerID = o.CustomerID JOIN [Order Details] od ON o.OrderID =
od.OrderID WHERE YEAR(o.OrderDate) = 1997 GROUP BY c.CustomerID,
c.CompanyName ORDER BY SUM(od.UnitPrice * od.Quan�ty) DESC, COUNT()
DESC;
Complexity level: 5
Akhbar El-Yom Academy
Computer Science Department - First Year
Course: Databases
26.What is the name of the employee who sold the most units in the year 1997
and has the highest total order amount in the Orders and Order Details tables?
Answer: SELECT TOP 1 e.FirstName + ' ' + e.LastName AS EmployeeName, SUM(od.Quan�ty *
od.UnitPrice) AS TotalAmount, COUNT() AS OrderCount FROM Employees e JOIN Orders o ON
e.EmployeeID = o.EmployeeID JOIN [Order Details] od ON o.OrderID = od.OrderID WHERE
YEAR(o.OrderDate) = 1997 GROUP BY e.EmployeeID, e.FirstName, e.LastName ORDER BY
SUM(od.Quan�ty) DESC, SUM(od.Quan�ty * od.UnitPrice) DESC, COUNT() DESC;
Complexity level: 5
27.What is the name of the employee who sold the most units of a specific
product in the Order Details table?
Answer: SELECT TOP 1 e.FirstName + ' ' + e.LastName AS EmployeeName,
SUM(od.Quan�ty) AS TotalSold FROM Employees e JOIN Orders o ON
e.EmployeeID = o.EmployeeID JOIN [Order Details] od ON o.OrderID =
od.OrderID WHERE od.ProductID = 1 GROUP BY e.EmployeeID, e.FirstName,
e.LastName ORDER BY SUM(od.Quan�ty) DESC;
Complexity level: 2
28.What is the name of the customer who placed the highest number of orders
in the Orders table and has a contact �tle of "Owner" in the Customers table?
Answer: SELECT TOP 1 c.CompanyName, COUNT() AS OrderCount FROM
Customers c JOIN Orders o ON c.CustomerID = o.CustomerID WHERE
c.ContactTitle = 'Owner' GROUP BY c.CustomerID, c.CompanyName ORDER BY
COUNT() DESC;
Complexity level: 3
Akhbar El-Yom Academy
Computer Science Department - First Year
Course: Databases
29.What is the name of the employee who sold the most units of a specific
product category in the Order Details table?
Answer: SELECT TOP 1 e.FirstName + ' ' + e.LastName AS EmployeeName,
SUM(od.Quan�ty) AS TotalSold FROM Employees e JOIN Orders o ON
e.EmployeeID = o.EmployeeID JOIN [Order Details] od ON o.OrderID =
od.OrderID JOIN Products p ON od.ProductID = p.ProductID JOIN Categories c
ON p.CategoryID = c.CategoryID WHERE c.CategoryID = 1 GROUP BY
e.EmployeeID, e.FirstName, e.LastName ORDER BY SUM(od.Quan�ty) DESC;
Complexity level: 4
30.What is the name of the customer who placed the highest number of orders
in the year 1997 and has the highest total order amount in the Orders and Order
Details tables, and the customer's city is London?
Answer: SELECT TOP 1 c.CompanyName, SUM(od.UnitPrice * od.Quan�ty) AS
TotalAmount, COUNT() AS OrderCount FROM Customers c JOIN Orders o ON
c.CustomerID = o.CustomerID JOIN [Order Details] od ON o.OrderID =
od.OrderID WHERE YEAR(o.OrderDate) = 1997 AND c.City = 'London' GROUP BY
c.CustomerID, c.CompanyName ORDER BY SUM(od.UnitPrice * od.Quan�ty)
DESC, COUNT() DESC;
Complexity level: 5
31.What is the name of the employee who sold the most units in the year 1997
and has the highest total order amount in the Orders and Order Details tables,
and the employee's city is Seatle?
Answer: SELECT TOP 1 e.FirstName + ' ' + e.LastName AS EmployeeName,
SUM(od.Quan�ty * od.UnitPrice) AS TotalAmount, COUNT() AS OrderCount
FROM Employees e JOIN Orders o ON e.EmployeeID = o.EmployeeID JOIN [Order
Details] od ON o.OrderID = od.OrderID WHERE YEAR(o.OrderDate) = 1997 AND
e.City = 'Seatle' GROUP BY e.EmployeeID, e.FirstName, e.LastName ORDER BY
SUM(od.Quan�ty) DESC, SUM(od.Quan�ty * od.UnitPrice) DESC, COUNT() DESC;
Complexity level: 5
Akhbar El-Yom Academy
Computer Science Department - First Year
Course: Databases
32.What is the name of the employee who sold the most units of a specific
product category in a specific month and year in the Order Details table?
Answer: SELECT TOP 1 e.FirstName + ' ' + e.LastName AS EmployeeName,
SUM(od.Quan�ty) AS TotalSold FROM Employees e JOIN Orders o ON
e.EmployeeID = o.EmployeeID JOIN [Order Details] od ON o.OrderID =
od.OrderID JOIN Products p ON od.ProductID = p.ProductID JOIN Categories c
ON p.CategoryID = c.CategoryID WHERE c.CategoryID = 1 AND
MONTH(o.OrderDate) = 1 AND YEAR(o.OrderDate) = 1997 GROUP BY
e.EmployeeID, e.FirstName, e.LastName ORDER BY SUM(od.Quan�ty) DESC;
Complexity level: 4
33.What is the name of the customer who placed the highest number of orders
in the Orders table and has the highest total order amount in the Orders and
Order Details tables, and the customer's region is Western Europe?
Answer: SELECT TOP 1 c.CompanyName, SUM(od.UnitPrice * od.Quan�ty) AS
TotalAmount, COUNT() AS OrderCount FROM Customers c JOIN Orders o ON
c.CustomerID = o.CustomerID JOIN [Order Details] od ON o.OrderID =
od.OrderID WHERE c.Region = 'Western Europe' GROUP BY c.CustomerID,
c.CompanyName ORDER BY SUM(od.UnitPrice * od.Quan�ty) DESC, COUNT()
DESC;
Complexity level: 5
Akhbar El-Yom Academy
Computer Science Department - First Year
Course: Databases
34.What is the name of the employee who sold the most units of a specific
product in a specific month and year in the Order Details table, and the
employee's country is USA?
Answer: SELECT TOP 1 e.FirstName + ' ' + e.LastName AS EmployeeName,
SUM(od.Quan�ty) AS TotalSold FROM Employees e JOIN Orders o ON
e.EmployeeID = o.EmployeeID JOIN [Order Details] od ON o.OrderID =
od.OrderID WHERE od.ProductID = 1 AND MONTH(o.OrderDate) = 1 AND
YEAR(o.OrderDate) = 1997 AND e.Country = 'USA' GROUP BY e.EmployeeID,
e.FirstName, e.LastName ORDER BY SUM(od.Quan�ty) DESC;
Complexity level: 4
35.What is the name of the customer who placed the highest number of orders
in the Orders table and has the highest total order amount in the Orders and
Order Details tables, and the customer's contact �tle is "Sales Representa�ve"?
Answer: SELECT TOP 1 c.CompanyName, SUM(od.UnitPrice * od.Quan�ty) AS
TotalAmount, COUNT() AS OrderCount FROM Customers c JOIN Orders o ON
c.CustomerID = o.CustomerID JOIN [Order Details] od ON o.OrderID =
od.OrderID WHERE c.ContactTitle = 'Sales Representa�ve' GROUP BY
c.CustomerID, c.CompanyName ORDER BY SUM(od.UnitPrice * od.Quan�ty)
DESC, COUNT() DESC;
Complexity level: 5
Akhbar El-Yom Academy
Computer Science Department - First Year
Course: Databases
36.What is the name of the employee who sold the most units of a specific
product in the year 1998 and has the highest total order amount in the Orders
and Order Details tables, and the employee's region is North America?
Answer: SELECT TOP 1 e.FirstName + ' ' + e.LastName AS EmployeeName,
SUM(od.Quan�ty) AS TotalSold, SUM(od.Quan�ty * od.UnitPrice) AS
TotalAmount FROM Employees e JOIN Orders o ON e.EmployeeID =
o.EmployeeID JOIN [Order Details] od ON o.OrderID = od.OrderID WHERE
od.ProductID = 3 AND YEAR(o.OrderDate) = 1998 AND e.Region = 'North
America' GROUP BY e.EmployeeID, e.FirstName, e.LastName ORDER BY
SUM(od.Quan�ty) DESC, SUM(od.Quan�ty * od.UnitPrice) DESC;
Complexity level: 5
37.What is the name of the customer who placed the highest number of orders
in the Orders table and has the highest total order amount in the Orders and
Order Details tables, and the customer's contact name contains the word
"Smith"?
Answer: SELECT TOP 1 c.CompanyName, SUM(od.UnitPrice * od.Quan�ty) AS
TotalAmount, COUNT() AS OrderCount FROM Customers c JOIN Orders o ON
c.CustomerID = o.CustomerID JOIN [Order Details] od ON o.OrderID =
od.OrderID WHERE c.ContactName LIKE '%Smith%' GROUP BY c.CustomerID,
c.CompanyName ORDER BY SUM(od.UnitPrice * od.Quan�ty) DESC, COUNT()
DESC;
Complexity level: 5
Akhbar El-Yom Academy
Computer Science Department - First Year
Course: Databases
38.What is the name of the employee who sold the most units of a specific
product in the year 1996 and has the highest total order amount in the Orders
and Order Details tables, and the employee's city is London?
Answer: SELECT TOP 1 e.FirstName + ' ' + e.LastName AS EmployeeName,
SUM(od.Quan�ty) AS TotalSold, SUM(od.Quan�ty * od.UnitPrice) AS
TotalAmount FROM Employees e JOIN Orders o ON e.EmployeeID =
o.EmployeeID JOIN [Order Details] od ON o.OrderID = od.OrderID WHERE
od.ProductID = 2 AND YEAR(o.OrderDate) = 1996 AND e.City = 'London' GROUP
BY e.EmployeeID, e.FirstName, e.LastName ORDER BY SUM(od.Quan�ty) DESC,
SUM(od.Quan�ty * od.UnitPrice) DESC;
Complexity level: 5
39.What is the name of the customer who placed the highest number of orders
in the Orders table and has the highest total order amount in the Orders and
Order Details tables, and the customer's region is Eastern Asia?
Answer: SELECT TOP 1 c.CompanyName, SUM(od.UnitPrice * od.Quan�ty) AS
TotalAmount, COUNT() AS OrderCount FROM Customers c JOIN Orders o ON
c.CustomerID = o.CustomerID JOIN [Order Details] od ON o.OrderID =
od.OrderID WHERE c.Region = 'Eastern Asia' GROUP BY c.CustomerID,
c.CompanyName ORDER BY SUM(od.UnitPrice * od.Quan�ty) DESC, COUNT()
DESC;
Complexity level: 5
Akhbar El-Yom Academy
Computer Science Department - First Year
Course: Databases
40.What is the name of the employee who sold the most units of a specific
product in the Orders table, and the employee's city is Seatle?
Answer: SELECT TOP 1 e.FirstName + ' ' + e.LastName AS EmployeeName,
SUM(od.Quan�ty) AS TotalSold FROM Employees e JOIN Orders o ON
e.EmployeeID = o.EmployeeID JOIN [Order Details] od ON o.OrderID =
od.OrderID WHERE od.ProductID = 4 AND e.City = 'Seatle' GROUP BY
e.EmployeeID, e.FirstName, e.LastName ORDER BY SUM(od.Quan�ty) DESC;
Complexity level: 3
41.What is the name of the customer who placed the highest number of orders
in the Orders table and has the highest total order amount in the Orders and
Order Details tables, and the customer's country is Brazil?
Answer: SELECT TOP 1 c.CompanyName, SUM(od.UnitPrice * od.Quan�ty) AS
TotalAmount, COUNT() AS OrderCount FROM Customers c JOIN Orders o ON
c.CustomerID = o.CustomerID JOIN [Order Details] od ON o.OrderID =
od.OrderID WHERE c.Country = 'Brazil' GROUP BY c.CustomerID,
c.CompanyName ORDER BY SUM(od.UnitPrice * od.Quan�ty) DESC, COUNT()
DESC;
Complexity level: 4
42.What is the name of the employee who sold the most units of a specific
product category in the year 1997 and has the highest total order amount in the
Orders and Order Details tables, and the employee's country is Germany?
Answer: SELECT TOP 1 e.FirstName + ' ' + e.LastName AS EmployeeName,
SUM(od.Quan�ty) AS TotalSold, SUM(od.Quan�ty * od.UnitPrice) AS TotalAmount
FROM Employees e JOIN Orders o ON e.EmployeeID = o.EmployeeID JOIN [Order
Details] od ON o.OrderID = od.OrderID JOIN Products p ON od.ProductID = p.ProductID
JOIN Categories c ON p.CategoryID = c.CategoryID WHERE c.CategoryID = 4 AND
YEAR(o.OrderDate) = 1997 AND e.Country = 'Germany' GROUP BY e.EmployeeID,
e.FirstName, e.LastName ORDER BY SUM(od.Quan�ty) DESC, SUM(od.Quan�ty *
od.UnitPrice) DESC;
Akhbar El-Yom Academy
Computer Science Department - First Year
Course: Databases
Complexity level: 5
43.What is the name of the customer who placed the highest number of orders
in the Orders table and has the highest total order amount in the Orders and
Order Details tables, and the customer's contact �tle is "Marke�ng Manager"?
Answer: SELECT TOP 1 c.CompanyName, SUM(od.UnitPrice * od.Quan�ty) AS
TotalAmount, COUNT() AS OrderCount FROM Customers c JOIN Orders o ON
c.CustomerID = o.CustomerID JOIN [Order Details] od ON o.OrderID =
od.OrderID WHERE c.ContactTitle = 'Marke�ng Manager' GROUP BY
c.CustomerID, c.CompanyName ORDER BY SUM(od.UnitPrice * od.Quan�ty)
DESC, COUNT() DESC;
Complexity level: 5
44.What is the name of the customer who placed the highest number of orders
in the Orders table and has the highest total order amount in the Orders and
Order Details tables, and the customer's region is South America?
Answer: SELECT TOP 1 c.CompanyName, SUM(od.UnitPrice * od.Quan�ty) AS
TotalAmount, COUNT() AS OrderCount FROM Customers c JOIN Orders o ON
c.CustomerID = o.CustomerID JOIN [Order Details] od ON o.OrderID =
od.OrderID WHERE c.Region = 'South America' GROUP BY c.CustomerID,
c.CompanyName ORDER BY SUM(od.UnitPrice * od.Quan�ty) DESC, COUNT()
DESC;
Complexity level: 4
Akhbar El-Yom Academy
Computer Science Department - First Year
Course: Databases
45.What is the name of the employee who sold the most units of a specific
product category in the year 1997 and has the highest total order amount in the
Orders and Order Details tables, and the employee's city is Redmond?
Answer: SELECT TOP 1 e.FirstName + ' ' + e.LastName AS EmployeeName,
SUM(od.Quan�ty) AS TotalSold, SUM(od.Quan�ty * od.UnitPrice) AS
TotalAmount FROM Employees e JOIN Orders o ON e.EmployeeID =
o.EmployeeID JOIN [Order Details] od ON o.OrderID = od.OrderID JOIN Products
p ON od.ProductID = p.ProductID JOIN Categories c ON p.CategoryID =
c.CategoryID WHERE c.CategoryID = 3 AND YEAR(o.OrderDate) = 1997 AND
e.City = 'Redmond' GROUP BY e.EmployeeID, e.FirstName, e.LastName ORDER
BY SUM(od.Quan�ty) DESC, SUM(od.Quan�ty * od.UnitPrice) DESC;
Complexity level: 5
46.What is the name of the employee who sold the most units of a specific
product in the year 1996 and has the highest total order amount in the Orders
and Order Details tables, and the employee's region is Central America?
Answer: SELECT TOP 1 e.FirstName + ' ' + e.LastName AS EmployeeName,
SUM(od.Quan�ty) AS TotalSold, SUM(od.Quan�ty * od.UnitPrice) AS
TotalAmount FROM Employees e JOIN Orders o ON e.EmployeeID =
o.EmployeeID JOIN [Order Details] od ON o.OrderID = od.OrderID WHERE
od.ProductID = 2 AND YEAR(o.OrderDate) = 1996 AND e.Region = 'Central
America' GROUP BY e.EmployeeID, e.FirstName, e.LastName ORDER BY
SUM(od.Quan�ty) DESC, SUM(od.Quan�ty * od.UnitPrice) DESC;
Complexity level: 5
Akhbar El-Yom Academy
Computer Science Department - First Year
Course: Databases
47.What is the name of the customer who placed the highest number of orders
in the Orders table and has the highest total order amount in the Orders and
Order Details tables, and the customer's contact name starts with "M" and ends
with "s"?
Answer: SELECT TOP 1 c.CompanyName, SUM(od.UnitPrice * od.Quan�ty) AS
TotalAmount, COUNT() AS OrderCount FROM Customers c JOIN Orders o ON
c.CustomerID = o.CustomerID JOIN [Order Details] od ON o.OrderID =
od.OrderID WHERE c.ContactName LIKE 'M% s' GROUP BY c.CustomerID,
c.CompanyName ORDER BY SUM(od.UnitPrice * od.Quan�ty) DESC, COUNT()
DESC;
Complexity level: 5
48.How many products are there in the Products table with a unit price between
$10 and $20?
Answer: SELECT COUNT(*) AS ProductCount FROM Products WHERE UnitPrice
BETWEEN 10 AND 20;
Complexity level: 2
49.What is the total number of orders placed by customers located in the USA in
the Orders table?
Answer: SELECT COUNT(*) AS OrderCount FROM Orders o JOIN Customers c ON
o.CustomerID = c.CustomerID WHERE c.Country = 'USA';
Complexity level: 2
Akhbar El-Yom Academy
Computer Science Department - First Year
Course: Databases
50.What is the total number of orders placed by customers located in the UK in
the Orders table?
Answer: SELECT COUNT(*) AS OrderCount FROM Orders o JOIN Customers c ON
o.CustomerID = c.CustomerID WHERE c.Country = 'UK';
Complexity level: 2
51.What is the average unit price of products in the Products table?
Answer: SELECT AVG(UnitPrice) AS AvgUnitPrice FROM Products;
Complexity level: 2
52.What is the name of the customer with the highest total order amount in the
Orders and Order Details tables?
Answer: SELECT TOP 1 c.CompanyName, SUM(od.UnitPrice * od.Quan�ty) AS
TotalAmount FROM Customers c JOIN Orders o ON c.CustomerID = o.CustomerID
JOIN [Order Details] od ON o.OrderID = od.OrderID GROUP BY c.CustomerID,
c.CompanyName ORDER BY SUM(od.UnitPrice * od.Quan�ty) DESC;
Complexity level: 3
53.What is the name of the employee who sold the most units of a specific
product in the Orders table, and the employee's region is Western Europe?
Answer: SELECT TOP 1 e.FirstName + ' ' + e.LastName AS EmployeeName,
SUM(od.Quan�ty) AS TotalSold FROM Employees e JOIN Orders o ON
e.EmployeeID = o.EmployeeID JOIN [Order Details] od ON o.OrderID =
od.OrderID WHERE od.ProductID = 1 AND e.Region = 'Western Europe' GROUP
BY e.EmployeeID, e.FirstName, e.LastName ORDER BY SUM(od.Quan�ty) DESC;
Complexity level: 3
Akhbar El-Yom Academy
Computer Science Department - First Year
Course: Databases
54.What is the name of the customer who placed the highest number of orders
in the Orders table and has the highest total order amount in the Orders and
Order Details tables?
Answer: SELECT TOP 1 c.CompanyName, SUM(od.UnitPrice * od.Quan�ty) AS
TotalAmount, COUNT() AS OrderCount FROM Customers c JOIN Orders o ON
c.CustomerID = o.CustomerID JOIN [Order Details] od ON o.OrderID =
od.OrderID GROUP BY c.CustomerID, c.CompanyName ORDER BY
SUM(od.UnitPrice * od.Quan�ty) DESC, COUNT() DESC;
Complexity level: 3
55.What is the name of the employee who sold the most units of a specific
product category in the year 1998 and has the highest total order amount in the
Orders and Order Details tables?
Answer: SELECT TOP 1 e.FirstName + ' ' + e.LastName AS EmployeeName,
SUM(od.Quan�ty) AS TotalSold, SUM(od.Quan�ty * od.UnitPrice) AS
TotalAmount FROM Employees e JOIN Orders o ON e.EmployeeID =
o.EmployeeID JOIN [Order Details] od ON o.OrderID = od.OrderID JOIN Products
p ON od.ProductID = p.ProductID JOIN Categories c ON p.CategoryID =
c.CategoryID WHERE c.CategoryID = 2 AND YEAR(o.OrderDate) = 1998 GROUP BY
e.EmployeeID, e.FirstName, e.LastName ORDER BY SUM(od.Quan�ty) DESC,
SUM(od.Quan�ty * od.UnitPrice) DESC;
Complexity level: 3
56.What is the name of the customer with the highest total order amount in the
Orders and Order Details tables, and the customer's country is Germany?
Answer: SELECT TOP 1 c.CompanyName, SUM(od.UnitPrice * od.Quan�ty) AS
TotalAmount FROM Customers c JOIN Orders o ON c.CustomerID = o.CustomerID
JOIN [Order Details] od ON o.OrderID = od.OrderID WHERE c.Country =
'Germany' GROUP BY c.CustomerID, c.CompanyName ORDER BY
SUM(od.UnitPrice * od.Quan�ty) DESC; Complexity level: 3
Akhbar El-Yom Academy
Computer Science Department - First Year
Course: Databases
57.What is the total number of orders placed in the Orders table for the year
1997?
Answer: SELECT COUNT(*) AS OrderCount FROM Orders WHERE
YEAR(OrderDate) = 1997;
Complexity level: 2
58.What is the total number of orders placed in the Orders table for the year
1998?
Answer: SELECT COUNT(*) AS OrderCount FROM Orders WHERE
YEAR(OrderDate) = 1998;
Complexity level: 2
59.What is the total revenue generated by each category in the Order Details
table?
Answer: SELECT c.CategoryName, SUM(od.UnitPrice * od.Quan�ty) AS
TotalRevenue FROM Categories c JOIN Products p ON c.CategoryID =
p.CategoryID JOIN [Order Details] od ON p.ProductID = od.ProductID GROUP BY
c.CategoryName;
Complexity level: 3
60.What is the total revenue generated by each country in the Orders and Order
Details tables?
Answer: SELECT c.Country, SUM(od.UnitPrice * od.Quan�ty) AS TotalRevenue
FROM Customers c JOIN Orders o ON c.CustomerID = o.CustomerID JOIN [Order
Details] od ON o.OrderID = od.OrderID GROUP BY c.Country;
Complexity level: 3
Akhbar El-Yom Academy
Computer Science Department - First Year
Course: Databases
61.What is the name of the employee who sold the most units of a specific
product in the Orders table, and the employee's city is Seatle?
Answer: SELECT TOP 1 e.FirstName + ' ' + e.LastName AS EmployeeName,
SUM(od.Quan�ty) AS TotalSold FROM Employees e JOIN Orders o ON
e.EmployeeID = o.EmployeeID JOIN [Order Details] od ON o.OrderID =
od.OrderID WHERE od.ProductID = 3 AND e.City = 'Seatle' GROUP BY
e.EmployeeID, e.FirstName, e.LastName ORDER BY SUM(od.Quan�ty) DESC;
Complexity level: 3
62.What is the name of the customer who placed the highest number of orders
in the Orders table and has the highest total order amount in the Orders and
Order Details tables, and the customer's city is London?
Answer: SELECT TOP 1 c.CompanyName, SUM(od.UnitPrice * od.Quan�ty) AS
TotalAmount, COUNT() AS OrderCount FROM Customers c JOIN Orders o ON
c.CustomerID = o.CustomerID JOIN [Order Details] od ON o.OrderID =
od.OrderID WHERE c.City = 'London' GROUP BY c.CustomerID, c.CompanyName
ORDER BY SUM(od.UnitPrice * od.Quan�ty) DESC, COUNT() DESC;
Complexity level: 3
Download