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

Airlines

AirlineIDAirlineName
1Airline A
2Airline B

Passengers

PassengerIDName
1John Doe
2Jane Smith

Bookings

BookingIDFlightIDPassengerID
111
222
331

Question:

List the names of passengers and the destinations they are flying to.

SQL Query:

SELECT p.Name, f.Destination
FROM Bookings b
JOIN Passengers p ON b.PassengerID = p.PassengerID
JOIN Flights f ON b.FlightID = f.FlightID;

5. Stores, Products, and Sales Tables

Stores

StoreIDStoreNameLocation
1Store ANew York
2Store BLos Angeles
3Store CChicago

Products

ProductIDProductNamePrice
1Laptop1500
2Tablet600
3Smartphone1000

Sales

SaleIDStoreIDProductIDQuantitySaleDate
11122023-06-01
22212023-06-02
33332023-06-03

Question:

Calculate the total sales amount for each store.

SQL Query:

SELECT s.StoreName, SUM(sa.Quantity * p.Price) AS TotalSales
FROM Sales sa
JOIN Stores s ON sa.StoreID = s.StoreID
JOIN Products p ON sa.ProductID = p.ProductID
GROUP BY s.StoreName;

6. Movies, Directors, and MovieDirectors Tables

Movies

MovieIDTitleReleaseYear
1The Godfather1972
2Pulp Fiction1994
3Inception2010

Directors

DirectorIDName
1Francis Ford Coppola
2Quentin Tarantino
3Christopher Nolan

MovieDirectors

MovieIDDirectorID
11
22
33

Question:

List the titles of movies and their directors.

SQL Query:

SELECT m.Title, d.Name AS Director
FROM MovieDirectors md
JOIN Movies m ON md.MovieID = m.MovieID
JOIN Directors d ON md.DirectorID = d.DirectorID;

7. Courses, Students, and Enrollments Tables

Courses

CourseIDCourseName
1Math
2Science
3History

Students

StudentIDName
1Emily
2Jack
3Olivia

Enrollments

EnrollmentIDCourseIDStudentID
111
222
333
412

Question:

Find the number of students enrolled in each course.

SQL Query:

SELECT c.CourseName, COUNT(e.StudentID) AS TotalStudents
FROM Enrollments e
JOIN Courses c ON e.CourseID = c.CourseID
GROUP BY c.CourseName;

8. Authors, Books, and BookAuthors Tables

Authors

AuthorIDName
1J.K. Rowling
2J.R.R. Tolkien
3George R.R. Martin

Books

BookIDTitlePublicationYear
1Harry Potter1997
2The Hobbit1937
3A Game of Thrones1996

BookAuthors

BookIDAuthorID
11
22
33

Question:

Retrieve the titles of books along with their authors.

SQL Query:

SELECT b.Title, a.Name AS Author
FROM BookAuthors ba
JOIN Books b ON ba.BookID = b.BookID
JOIN Authors a ON ba.AuthorID = a.AuthorID;

9. Departments, Employees, and Salaries Tables

Departments

DepartmentIDDepartmentName
1IT
2HR
3Finance

Employees

EmployeeIDNameDepartmentID
1John1
2Jane2
3Michael3

Salaries

SalaryIDEmployeeIDAmount
1170000
2260000
3375000

Question:

Calculate the total salary for each department.

SQL Query:

SELECT d.DepartmentName, SUM(s.Amount) AS TotalSalary
FROM Salaries s
JOIN Employees e ON s.EmployeeID = e.EmployeeID
JOIN Departments d ON e.DepartmentID = d.DepartmentID
GROUP BY d.DepartmentName;

10. Courses, Students, and Grades Tables

Courses

CourseIDCourseName
1Math
2Science
3History

Students

StudentIDName
1Emily
2Jack
3Olivia

Grades

GradeIDCourseIDStudentIDGrade
111A
222B
333A
412C

Question:

List the students who received an 'A' grade in any course.

SQL Query:

SELECT s.Name
FROM Grades g
JOIN Students s ON g.StudentID = s.StudentID
WHERE g.Grade = 'A';