Skip to content

SQL Practice Questions Assignment #10

@Pankaj-Str

Description

@Pankaj-Str

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)

  1. 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.

  2. 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
  3. Insert three authors into the Authors table with different nationalities.

  4. Insert five books into the Books table, ensuring at least two books are by the same author.

Intermediate Questions (SELECT and WHERE)

  1. Write a query to find all books priced between $15 and $25.

  2. Find all books with either "Python" or "Java" in their title.

  3. List all authors who were born after 1980 AND are either American or British.

  4. Display all customers who joined in 2023 but haven't placed any orders.

Advanced Questions (JOIN and GROUP BY)

  1. Show the total number of books sold for each author, including authors who haven't sold any books.

  2. Find the top 3 customers who have spent the most money on books, showing their name and total spending.

  3. List all books along with their authors' names where the book's price is above the average book price.

  4. 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)

  1. 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.

  2. Delete all orders that are more than a year old AND have a total_amount less than $10.

  3. Find authors who have written books in at least 3 different genres.

  4. 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

  1. Find all books where either the genre is NULL or the price is NULL, but not both.

  2. List customers who have placed orders but have NULL email addresses.

  3. Show all authors who have no books in the Books table.

Pattern Matching and Ordering Questions

  1. Find all books whose titles start with 'The' and end with either 'Guide' or 'Tutorial'.

  2. List authors' full names (combined first and last name) ordered by last name, then first name.

  3. Show the top 5 months with the highest average order amounts.

Bonus Challenge Questions

  1. Write a query to identify potential duplicate customer records based on similar names and email patterns.

  2. Create a customer ranking system based on:

    • Total money spent (50% weight)
    • Number of orders (30% weight)
    • Account age (20% weight)
  3. Find books that have never been ordered together in the same order (pairs of books that have never appeared together).

Requirements for Submission:

  1. All queries must run without errors
  2. Include comments explaining your approach
  3. For update/delete operations, include a SELECT statement to verify the changes
  4. Use appropriate aliases for better readability
  5. 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%

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions