Activity #1 - Advanced SQL
Activity #1 - Advanced SQL
Description:
You are working with the database of an online learning platform that offers various courses to
students worldwide. The platform tracks student and instructor details, course information,
enrollments, and payments. Students can enroll in multiple courses, and instructors can teach
several courses within a specific category (e.g., "Programming", "Design", or "Marketing"). Each
course is associated with a price, and students must make a payment to access the course. The
database also stores information about the student’s progress through the course.
This scenario will challenge students to analyze course enrollments, instructor assignments,
payment histories, and student progress.
Database Schema:
1. Students:
○ id: Unique identifier for each student (Primary Key).
○ first_name: Student's first name.
○ last_name: Student's last name.
○ email: Student's email address.
○ registration_date: The date when the student registered on the platform.
2. Instructors:
○ id: Unique identifier for each instructor (Primary Key).
○ first_name: Instructor's first name.
○ last_name: Instructor's last name.
○ email: Instructor's email address.
○ hire_date: The date the instructor was hired.
○ department: The department or specialization the instructor belongs to (e.g.,
"Technology", "Design").
3. Courses:
○ id: Unique identifier for each course (Primary Key).
○ title: Name of the course.
○ category: The category or subject area of the course (e.g., "Web
Development", "Graphic Design").
○ instructor_id: Reference to the instructor who teaches the course (Foreign
Key from Instructors).
○ price: Price of the course in USD.
○ start_date: The date when the course begins.
4. Enrollments:
○ id: Unique identifier for each enrollment (Primary Key).
○ student_id: Reference to the student who enrolled (Foreign Key from
Students).
○ course_id: Reference to the course being enrolled in (Foreign Key from
Courses).
○ enrollment_date: The date the student enrolled in the course.
○ progress_percent: A percentage value (0–100%) indicating the student’s
progress in the course.
5. Payments:
○ id: Unique identifier for each payment (Primary Key).
○ student_id: Reference to the student making the payment (Foreign Key from
Students).
○ course_id: Reference to the course being paid for (Foreign Key from
Courses).
○ payment_amount: The amount paid for the course.
○ payment_date: The date the payment was made.
Exercises:
Description:
You are working with a database designed for a hotel chain’s management system. This system
keeps track of guest information, room details, reservations, payments, and amenities used
during the guests’ stay. Each guest can make multiple reservations, and each reservation can
be associated with multiple room types. The hotel also offers various amenities (like spa
services, gym access, etc.), and guests can be charged for these additional services.
In this scenario, students will work on analyzing guest behavior, booking patterns, and revenue
generation from room reservations and amenities usage.
Database Schema:
1. Guests:
○ id: Unique identifier for each guest (Primary Key).
○ first_name: Guest's first name.
○ last_name: Guest's last name.
○ email: Guest’s email address.
○ signup_date: Date when the guest registered in the system.
2. Rooms:
○ id: Unique identifier for each room (Primary Key).
○ room_number: Room number.
○ type: Type of the room (e.g., "Single", "Double", "Suite").
○ price_per_night: Cost per night for the room.
3. Reservations:
○ id: Unique identifier for each reservation (Primary Key).
○ guest_id: Reference to the guest making the reservation (Foreign Key from
Guests).
○ room_id: Reference to the room being reserved (Foreign Key from Rooms).
○ check_in_date: The date the guest checks in.
○ check_out_date: The date the guest checks out.
4. Payments:
○ id: Unique identifier for each payment (Primary Key).
○ reservation_id: Reference to the reservation (Foreign Key from
Reservations).
○ amount: The total amount paid for the reservation.
○ payment_date: The date the payment was made.
5. Amenities:
○ id: Unique identifier for each amenity (Primary Key).
○ name: Name of the amenity (e.g., "Gym", "Spa", "Wi-Fi").
○ price: Price charged for using the amenity.
6. Guest_Amenities:
○ id: Unique identifier for each guest amenity usage (Primary Key).
○ guest_id: Reference to the guest using the amenity (Foreign Key from
Guests).
○ amenity_id: Reference to the amenity being used (Foreign Key from
Amenities).
○ usage_date: Date the amenity was used.
Exercises:
1. Exercise 1: Guests Who Have Used the Spa But Haven't Made Reservations
List the first_name and last_name of guests who have used the spa but haven't
made any room reservations.
2. Exercise 2: Most Popular Room Types by Total Nights Booked
Display the room types that have been booked for the most total nights across all
reservations. Calculate the total nights for each reservation using the difference between
check_in_date and check_out_date. Display the room type and total nights.
3. Exercise 3: Total Revenue from Room Bookings and Amenities by Guest
Calculate the total revenue generated from room bookings and amenities used by each
guest. Display the first_name, last_name, and total revenue for each guest.
4. Exercise 4: Guests Who Have Booked All Room Types
Find the guests who have made a reservation in every room type offered by the hotel.
Display their first_name and last_name.
5. Exercise 5: Average Nights Stayed by Room Type
Display the average number of nights stayed for each room type. Show the room type
and the average nights stayed.
Scenario 3: Retail Store Database
Description:
You are working with the database of a retail store that sells products both in physical stores
and online. The system tracks customer information, product details, orders, and payments.
Each order may contain multiple products, and each product belongs to a specific category
(such as electronics, clothing, or home goods). Customers can make multiple orders, and the
store keeps track of when the order was placed and the total amount paid for each order. The
store also tracks product inventory levels, helping managers monitor stock availability.
In this scenario, students will work on querying sales data, product inventory, and customer
orders.
Database Schema:
1. Customers:
○ id: Unique identifier for each customer (Primary Key).
○ first_name: Customer’s first name.
○ last_name: Customer’s last name.
○ email: Customer's email address.
○ created_at: Date when the customer created their account.
2. Products:
○ id: Unique identifier for each product (Primary Key).
○ name: Name of the product.
○ category: The category the product belongs to (e.g., "Electronics", "Clothing").
○ price: The price of the product.
○ stock: The number of units available in stock.
3. Orders:
○ id: Unique identifier for each order (Primary Key).
○ customer_id: Reference to the customer who placed the order (Foreign Key
from Customers).
○ order_date: The date the order was placed.
○ total_amount: The total amount paid for the order.
4. Order_Details:
○ id: Unique identifier for each order detail (Primary Key).
○ order_id: Reference to the order (Foreign Key from Orders).
○ product_id: Reference to the product being ordered (Foreign Key from
Products).
○ quantity: The number of units ordered for the product.
○ line_total: Total cost for this specific product (price × quantity).
5. Payments:
○ id: Unique identifier for each payment (Primary Key).
○ order_id: Reference to the order (Foreign Key from Orders).
○ payment_date: The date the payment was made.
○ amount: The amount paid.
Exercises: