SQL Tables, Questions, and Answers

1. Sales, Customers, and Products Tables

Sales

SaleIDProductIDCustomerIDQuantitySaleDate
1110122023-01-01
2210232023-01-02
3310312023-01-03
4110112023-01-04

Customers

CustomerIDNameCountry
101Alice JohnsonUSA
102Bob SmithCanada
103Carol WhiteUK

Products

ProductIDProductNamePrice
1Laptop1000
2Tablet500
3Smartphone800

Question:

Find the total revenue generated by each customer.

SQL Query:

SELECT c.Name, SUM(s.Quantity * p.Price) AS TotalRevenue
FROM Sales s
JOIN Customers c ON s.CustomerID = c.CustomerID
JOIN Products p ON s.ProductID = p.ProductID
GROUP BY c.Name;

2. Employees, Projects, and Assignments Tables

Employees

EmployeeIDNameDepartment
1John SmithIT
2Jane DoeHR
3Alice BrownFinance

Projects

ProjectIDProjectNameBudget
1Project A50000
2Project B75000
3Project C100000

Assignments

AssignmentIDEmployeeIDProjectIDHoursWorked
111100
222150
333200
412120

Question:

Calculate the total hours worked by each employee across all projects.

SQL Query:

SELECT e.Name, SUM(a.HoursWorked) AS TotalHours
FROM Assignments a
JOIN Employees e ON a.EmployeeID = e.EmployeeID
GROUP BY e.Name;

3. Orders, Customers, and Products Tables

Orders

OrderIDCustomerIDOrderDate
11012023-02-01
21022023-02-02
31032023-02-03
41012023-02-04

Customers

CustomerIDNameCountry
101Michael ScottUSA
102Jim HalpertCanada
103Dwight SchruteUSA

Products

ProductIDProductNamePrice
1Laptop1200
2Monitor200
3Keyboard50

OrderDetails

OrderIDProductIDQuantity
112
121
233
311
422

Question:

Retrieve the total amount spent by each customer on their orders.

SQL Query:

SELECT c.Name, SUM(od.Quantity * p.Price) AS TotalSpent
FROM Orders o
JOIN OrderDetails od ON o.OrderID = od.OrderID
JOIN Products p ON od.ProductID = p.ProductID
JOIN Customers c ON o.CustomerID = c.CustomerID
GROUP BY c.Name;

4. Flights, Airlines, and Passengers Tables

Flights

FlightIDAirlineIDDestinationDepartureDate
11New York2023-05-01
22London2023-05-02
31Paris2023-05-03
43Tokyo2023-05-04

Airlines

AirlineIDAirlineNameCountry
1DeltaUSA
2British AirwaysUK
3Japan AirlinesJapan

Passengers

PassengerIDNameFlightIDSeatNumber
1Emma Johnson112A
2Liam Brown210B
3Olivia Smith38C
4Noah Davis414D

Question:

Find the number of passengers each airline has flown to their destinations.

SQL Query:

SELECT a.AirlineName, COUNT(p.PassengerID) AS NumberOfPassengers
FROM Passengers p
JOIN Flights f ON p.FlightID = f.FlightID
JOIN Airlines a ON f.AirlineID = a.AirlineID
GROUP BY a.AirlineName;

5. Students, Courses, and Enrollments Tables

Students

StudentIDNameMajor
1AliceComputer Science
2BobMathematics
3CharlieBiology
4DavidPhysics

Courses

CourseIDCourseNameCredits
1Algorithms3
2Linear Algebra4
3Genetics3
4Quantum Mechanics4

Enrollments

EnrollmentIDStudentIDCourseIDGrade
111A
222B
333A
444C
512B

Question:

Calculate the average grade of each student across all their courses.

SQL Query:

SELECT s.Name, AVG(
    CASE e.Grade
        WHEN 'A' THEN 4
        WHEN 'B' THEN 3
        WHEN 'C' THEN 2
        WHEN 'D' THEN 1
        ELSE 0
    END
) AS AverageGrade
FROM Enrollments e
JOIN Students s ON e.StudentID = s.StudentID
GROUP BY s.Name;