-
Notifications
You must be signed in to change notification settings - Fork 14
Description
Database Schema
Use the following tables for all questions:
Authors (author_id, first_name, last_name, birth_year, nationality)
Books (book_id, title, author_id, publication_year, genre, price, stock)
Customers (customer_id, first_name, last_name, email, join_date)
Orders (order_id, customer_id, book_id, order_date, quantity, total_amount)
Basic Questions (CREATE and INSERT)
-
Create the Authors table with appropriate data types and constraints. Make author_id the primary key and ensure first_name and last_name cannot be null.
-
Create the Books table with:
- A primary key for book_id
- A foreign key referencing Authors table
- A constraint ensuring price is greater than 0
- A default value of 0 for stock
-
Insert three authors into the Authors table with different nationalities.
-
Insert five books into the Books table, ensuring at least two books are by the same author.
Intermediate Questions (SELECT and WHERE)
-
Write a query to find all books priced between $15 and $25.
-
Find all books with either "Python" or "Java" in their title.
-
List all authors who were born after 1980 AND are either American or British.
-
Display all customers who joined in 2023 but haven't placed any orders.
Advanced Questions (JOIN and GROUP BY)
-
Show the total number of books sold for each author, including authors who haven't sold any books.
-
Find the top 3 customers who have spent the most money on books, showing their name and total spending.
-
List all books along with their authors' names where the book's price is above the average book price.
-
Show each genre and the number of books in that genre, but only for genres with more than 3 books.
Expert Level Questions (Complex Operations)
-
Update the prices of all books in a specific genre by increasing them by 10%, but only if the current stock is less than 5.
-
Delete all orders that are more than a year old AND have a total_amount less than $10.
-
Find authors who have written books in at least 3 different genres.
-
Create a summary showing for each month of 2023:
- Total number of orders
- Total revenue
- Best-selling book (by quantity)
- Number of unique customers
NULL and Constraints Questions
-
Find all books where either the genre is NULL or the price is NULL, but not both.
-
List customers who have placed orders but have NULL email addresses.
-
Show all authors who have no books in the Books table.
Pattern Matching and Ordering Questions
-
Find all books whose titles start with 'The' and end with either 'Guide' or 'Tutorial'.
-
List authors' full names (combined first and last name) ordered by last name, then first name.
-
Show the top 5 months with the highest average order amounts.
Bonus Challenge Questions
-
Write a query to identify potential duplicate customer records based on similar names and email patterns.
-
Create a customer ranking system based on:
- Total money spent (50% weight)
- Number of orders (30% weight)
- Account age (20% weight)
-
Find books that have never been ordered together in the same order (pairs of books that have never appeared together).
Requirements for Submission:
- All queries must run without errors
- Include comments explaining your approach
- For update/delete operations, include a SELECT statement to verify the changes
- Use appropriate aliases for better readability
- Follow SQL best practices for formatting and naming conventions
Scoring Guide:
- Basic Questions (1-4): 20%
- Intermediate Questions (5-8): 20%
- Advanced Questions (9-12): 25%
- Expert Level Questions (13-16): 20%
- Remaining Questions (17-25): 15%