1. Employees and Departments Tables
Table: Employees
EmployeeID | Name | DepartmentID | Salary |
1 | John Smith | 1 | 60000 |
2 | Jane Doe | 2 | 75000 |
3 | Alice Brown | 1 | 80000 |
4 | Bob White | 3 | 50000 |
Table: Departments
DepartmentID | DepartmentName |
1 | IT |
2 | HR |
3 | Finance |
Question: Find the total salary expenditure for each department.
SQL Query:
SELECT d.DepartmentName, SUM(e.Salary) AS TotalSalary
FROM Employees e
JOIN Departments d ON e.DepartmentID = d.DepartmentID
GROUP BY d.DepartmentName;
2. Orders and Customers Tables
Table: Orders
OrderID | CustomerID | OrderDate | Amount |
1 | 101 | 2023-01-10 | 250.50 |
2 | 102 | 2023-01-12 | 150.75 |
3 | 103 | 2023-01-15 | 300.20 |
4 | 101 | 2023-01-20 | 220.10 |
Table: Customers
CustomerID | Name | Country |
101 | Michael Scott | USA |
102 | Jim Halpert | Canada |
103 | Dwight Schrute | USA |
Question: Retrieve the total amount spent by each customer, along with their name and country.
SQL Query:
SELECT c.Name, c.Country, SUM(o.Amount) AS TotalSpent
FROM Orders o
JOIN Customers c ON o.CustomerID = c.CustomerID
GROUP BY c.Name, c.Country;
3. Products, Sales, and Categories Tables
Table: Products
ProductID | ProductName | CategoryID | Price |
1 | Laptop | 1 | 800 |
2 | Headphones | 1 | 50 |
3 | Coffee Maker | 2 | 120 |
4 | Mixer | 2 | 150 |
Table: Sales
SaleID | ProductID | Quantity | SaleDate |
1 | 1 | 3 | 2022-11-05 |
2 | 2 | 2 | 2022-11-10 |
3 | 1 | 5 | 2022-11-15 |
4 | 3 | 1 | 2022-11-20 |
Table: Categories
CategoryID | CategoryName |
1 | Electronics |
2 | Home Appliances |
Question: Calculate the total revenue generated by each category.
SQL Query:
SELECT c.CategoryName, SUM(s.Quantity * p.Price) AS TotalRevenue
FROM Sales s
JOIN Products p ON s.ProductID = p.ProductID
JOIN Categories c ON p.CategoryID = c.CategoryID
GROUP BY c.CategoryName;
4. Students, Courses, and Enrollments Tables
Table: Students
StudentID | Name | Age |
1 | Emma | 20 |
2 | Liam | 21 |
3 | Olivia | 22 |
4 | Noah | 23 |
Table: Courses
CourseID | CourseName |
101 | Mathematics |
102 | Physics |
103 | Chemistry |
Table: Enrollments
EnrollmentID | StudentID | CourseID |
1 | 1 | 101 |
2 | 2 | 102 |
3 | 3 | 103 |
4 | 4 | 101 |
5 | 1 | 102 |
Question: List the names of students and the courses they are enrolled in.
SQL Query:
SELECT s.Name, c.CourseName
FROM Enrollments e
JOIN Students s ON e.StudentID = s.StudentID
JOIN Courses c ON e.CourseID = c.CourseID;
5. Books, Authors, and BookAuthors Tables
Table: Books
BookID | Title | Price |
1 | The Alchemist | 15.99 |
2 | 1984 | 12.99 |
3 | To Kill a Mockingbird | 10.99 |
4 | Pride and pre style="font-size: 18px;" style="font-size: 18px;"judice | 8.99 |
Table: Authors
AuthorID | Name |
1 | Paulo Coelho |
2 | George Orwell |
3 | Harper Lee |
4 | Jane Austen |
Table: BookAuthors
BookID | AuthorID |
1 | 1 |
2 | 2 |
3 | 3 |
4 | 4 |
Question: List the titles of books and their authors.
SQL Query:
SELECT b.Title, a.Name
FROM BookAuthors ba
JOIN Books b ON ba.BookID = b.BookID
JOIN Authors a ON ba.AuthorID = a.AuthorID;