Here's a simple SQL database structure with some sample data for a bookstore. I've included tables for books, authors, genres, orders, and order_items. You can use this data to perform various queries.
authors table
sql
CREATE TABLE authors (
id INT PRIMARY KEY AUTO_INCREMENT,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL
);
INSERT INTO authors (first_name, last_name)
VALUES
('George', 'Orwell'),
('Aldous', 'Huxley'),
('Ray', 'Bradbury'),
('Margaret', 'Atwood'),
('William', 'Golding');
genres table
sql
CREATE TABLE genres (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL UNIQUE
);
INSERT INTO genres (name)
VALUES
('Dystopia'),
('Science Fiction'),
('Speculative Fiction'),
('Novel'),
('Young Adult');
books table
sql
CREATE TABLE books (
id INT PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(100) NOT NULL,
author_id INT NOT NULL,
publication_year INT,
FOREIGN KEY (author_id) REFERENCES authors(id)
);
INSERT INTO books (title, author_id, publication_year)
VALUES
('1984', 1, 1949),
('Brave New World', 2, 1932),
('Fahrenheit 451', 3, 1953),
('The Handmaid\'s Tale', 4, 1985),
('Lord of the Flies', 5, 1954);
book_genres table (many-to-many relationship between books and genres)
CREATE TABLE orders (
id INT PRIMARY KEY AUTO_INCREMENT,
customer_name VARCHAR(100) NOT NULL,
order_date DATE NOT NULL
);
INSERT INTO orders (customer_name, order_date)
VALUES
('John Doe', '2022-01-01'),
('Jane Smith', '2022-02-15'),
('Alice Johnson', '2022-03-10');
order_items table
sql
CREATE TABLE order_items (
id INT PRIMARY KEY AUTO_INCREMENT,
order_id INT NOT NULL,
book_id INT NOT NULL,
quantity INT NOT NULL,
FOREIGN KEY (order_id) REFERENCES orders(id),
FOREIGN KEY (book_id) REFERENCES books(id)
);
INSERT INTO order_items (order_id, book_id, quantity)
VALUES
(1, 1, 2),
(1, 3, 1),
(2, 2, 1),
(2, 4, 2),
(3, 5, 3);
Now you can perform various queries using this data, such as:
List all books along with their authors and genres.
Find the most popular genre based on the number of books.
List all orders along with the books ordered and their quantities.
Find the top 3 best-selling books.
List all authors and the number of books they have written.
Find books published after a specific year.
List all orders made by a specific customer.
Find the total sales amount for each book.
User
can you give me another table
Assistant
Certainly! Here's an additional table, reviews, that you can use to store book reviews and ratings. This table will have a one-to-many relationship with the books table.
reviews table
sql
CREATE TABLE reviews (
id INT PRIMARY KEY AUTO_INCREMENT,
book_id INT NOT NULL,
reviewer_name VARCHAR(100) NOT NULL,
rating INT NOT NULL CHECK (rating >= 1 AND rating <= 5),
review_text TEXT,
review_date DATE NOT NULL,
FOREIGN KEY (book_id) REFERENCES books(id)
);
INSERT INTO reviews (book_id, reviewer_name, rating, review_text, review_date)
VALUES
(1, 'John Doe', 4, 'A thought-provoking exploration of totalitarianism.', '2022-01-05'),
(1, 'Jane Smith', 5, 'A classic that everyone should read.', '2022-02-12'),
(2, 'Alice Johnson', 3, 'An interesting concept, but the pacing was slow.', '2022-03-08'),
(3, 'Bob Brown', 5, 'A captivating story with a powerful message.', '2022-04-20'),
(4, 'Eve Green', 4, 'A chilling and relevant dystopia.', '2022-05-15'),
(5, 'Frank Blue', 5, 'A gripping tale of survival and morality.', '2022-06-10');
With this reviews table, you can perform queries like:
List all books along with their average rating and the number of reviews.
Find the top 5 highest-rated books.
List all reviews for a specific book, ordered by review date.
Find the most active reviewers (those who have written the most reviews).
List all books that have a rating of 4 or higher and at least 5 reviews.
Find the most recent reviews for each book.
Here's an example query to list all books along with their average rating and the number of reviews:
sql
SELECT
b.id,
b.title,
AVG(r.rating) AS average_rating,
COUNT(r.id) AS num_reviews
FROM
books b
LEFT JOIN
reviews r ON b.id = r.book_id
GROUP BY
b.id
ORDER BY
average_rating DESC;
This query will give you a list of books with their average rating and the number of reviews, ordered by the average rating in descending order. You can adjust the query to fit your specific needs.
User
ok now using these two table give me some meaningful question and solution for practice
Assistant
Here are five meaningful questions that combine data from the order_items and reviews tables, along with their SQL solutions for practice:
Which books have both high sales and high ratings?
Solution:
sql
SELECT
b.id,
b.title,
COUNT(oi.id) AS total_sold,
AVG(r.rating) AS average_rating
FROM
books b
JOIN
order_items oi ON b.id = oi.book_id
LEFT JOIN
reviews r ON b.id = r.book_id
GROUP BY
b.id
HAVING
total_sold > 5 AND average_rating > 4
ORDER BY
total_sold DESC,
average_rating DESC;
Which customers have bought books with an average rating of 4 or higher?
Solution:
sql
SELECT DISTINCT
o.customer_name
FROM
orders o
JOIN
order_items oi ON o.id = oi.order_id
JOIN
books b ON oi.book_id = b.id
JOIN
reviews r ON b.id = r.book_id
GROUP BY
o.id
HAVING
AVG(r.rating) >= 4;
What is the total sales amount for each book, considering the quantity sold and the average rating?
Solution:
sql
SELECT
b.id,
b.title,
SUM(oi.quantity) AS total_quantity_sold,
AVG(r.rating) AS average_rating,
(SUM(oi.quantity) * AVG(r.rating)) AS weighted_total
FROM
books b
JOIN
order_items oi ON b.id = oi.book_id
LEFT JOIN
reviews r ON b.id = r.book_id
GROUP BY
b.id
ORDER BY
weighted_total DESC;
Which books have received at least one review from customers who have made a purchase?
Solution:
sql
SELECT DISTINCT
b.id,
b.title
FROM
books b
JOIN
order_items oi ON b.id = oi.book_id
JOIN
orders o ON oi.order_id = o.id
JOIN
reviews r ON b.id = r.book_id
WHERE
o.customer_name = r.reviewer_name;
What is the overall satisfaction score for each customer, considering the quantity of books they've bought and the average rating of those books?
Solution:
sql
SELECT
o.customer_name,
SUM(oi.quantity * AVG(r.rating)) AS overall_satisfaction_score
FROM
orders o
JOIN
order_items oi ON o.id = oi.order_id
JOIN
books b ON oi.book_id = b.id
LEFT JOIN
reviews r ON b.id = r.book_id
GROUP BY
o.id
ORDER BY
overall_satisfaction_score DESC;
These questions and solutions should provide a good practice opportunity for working with multiple tables and performing aggregate functions, joins, and subqueries. You can adjust the queries to fit your specific needs or database schema.
User
give me 3 more question
Assistant
Here are three more questions that combine data from the order_items, reviews, and other tables for practice:
Which authors have written books that have both high sales and high ratings, and what are the titles of those books?
Solution:
sql
SELECT
a.first_name,
a.last_name,
b.title,
COUNT(oi.id) AS total_sold,
AVG(r.rating) AS average_rating
FROM
authors a
JOIN
books b ON a.id = b.author_id
JOIN
order_items oi ON b.id = oi.book_id
LEFT JOIN
reviews r ON b.id = r.book_id
GROUP BY
a.id,
b.id
HAVING
total_sold > 5 AND average_rating > 4
ORDER BY
total_sold DESC,
average_rating DESC;
What is the most popular genre based on the number of books sold and the average rating of those books?
Solution:
sql
SELECT
g.name AS genre,
COUNT(DISTINCT b.id) AS num_books,
AVG(r.rating) AS average_rating,
SUM(oi.quantity) AS total_quantity_sold
FROM
genres g
JOIN
book_genres bg ON g.id = bg.genre_id
JOIN
books b ON bg.book_id = b.id
JOIN
order_items oi ON b.id = oi.book_id
LEFT JOIN
reviews r ON b.id = r.book_id
GROUP BY
g.id
ORDER BY
total_quantity_sold DESC,
average_rating DESC
LIMIT 1;
Which customers have bought books from multiple genres, and what are the genres they've purchased from?
Solution:
sql
SELECT DISTINCT
o.customer_name,
g.name AS genre
FROM
orders o
JOIN
order_items oi ON o.id = oi.order_id
JOIN
books b ON oi.book_id = b.id
JOIN
book_genres bg ON b.id = bg.book_id
JOIN
genres g ON bg.genre_id = g.id
WHERE
o.customer_name IN (
SELECT
o2.customer_name
FROM
orders o2
JOIN
order_items oi2 ON o2.id = oi2.order_id
JOIN
books b2 ON oi2.book_id = b2.id
JOIN
book_genres bg2 ON b2.id = bg2.book_id
JOIN
genres g2 ON bg2.genre_id = g2.id
GROUP BY
o2.customer_name
HAVING
COUNT(DISTINCT g2.id) > 1
);
These questions and solutions should provide more practice opportunities for working with multiple tables, joins, and aggregate functions. You can adjust the queries to fit your specific needs or database schema.
User
give me 10 aggrigiate function question
Assistant
Here are ten aggregate function questions using the provided tables for practice. I've included a mix of basic and more complex aggregate functions, along with some window functions for added challenge.
What is the total number of books sold?
Solution:
sql
SELECT SUM(quantity) AS total_books_sold
FROM order_items;
What is the total sales amount, assuming each book is sold for $10?
Solution:
sql
SELECT SUM(quantity * 10) AS total_sales_amount
FROM order_items;
What is the average rating for all books?
Solution:
sql
SELECT AVG(rating) AS average_rating
FROM reviews;
What is the total number of reviews for each book?
Solution:
sql
SELECT
book_id,
COUNT(*) AS total_reviews
FROM
reviews
GROUP BY
book_id;
What is the total sales amount for each book?
Solution:
sql
SELECT
book_id,
SUM(quantity) AS total_sold,
SUM(quantity * 10) AS total_sales_amount
FROM
order_items
GROUP BY
book_id;
What is the average rating for each book, along with the number of reviews?
Solution:
sql
SELECT
book_id,
AVG(rating) AS average_rating,
COUNT(*) AS num_reviews
FROM
reviews
GROUP BY
book_id;
What is the total sales amount for each author, considering the books they've written?
User
give me some medium level udf question and answer in sql
Assistant
Here are three medium-level user-defined function (UDF) questions and their solutions in SQL. These examples demonstrate how to create and use UDFs to simplify complex queries and improve readability.
Create a UDF to calculate the total sales amount for each book, considering the quantity sold and the price per book.
Solution:
sql
CREATE FUNCTION total_sales_amount(book_id INT)
RETURNS DECIMAL(10, 2)
BEGIN
DECLARE total_sales DECIMAL(10, 2);
SELECT SUM(quantity * price) INTO total_sales
FROM order_items
JOIN books ON order_items.book_id = books.id
WHERE books.id = book_id;
RETURN total_sales;
END;
-- Usage example:
SELECT book_id, title, total_sales_amount(book_id) AS total_sales
FROM books;
Create a UDF to find the most popular genre based on the number of books sold and the average rating of those books.
Solution:
sql
CREATE FUNCTION most_popular_genre()
RETURNS genres.name
BEGIN
DECLARE most_popular_genre genres.name;
SELECT genres.name
INTO most_popular_genre
FROM genres
JOIN book_genres ON genres.id = book_genres.genre_id
JOIN books ON book_genres.book_id = books.id
JOIN order_items ON books.id = order_items.book_id
GROUP BY genres.name
ORDER BY COUNT(DISTINCT order_items.order_id) DESC, AVG(reviews.rating) DESC
LIMIT 1;
RETURN most_popular_genre;
END;
-- Usage example:
SELECT most_popular_genre();
Create a UDF to find the top 3 best-selling books based on the total sales amount.
Solution:
sql
CREATE FUNCTION top_3_best_selling_books()
RETURNS TABLE(book_id INT, title VARCHAR(100), total_sales DECIMAL(10, 2))
BEGIN
SELECT book_id, title, total_sales_amount(book_id) AS total_sales
FROM books
ORDER BY total_sales_amount(book_id) DESC
LIMIT 3;
END;
-- Usage example:
SELECT * FROM top_3_best_selling_books();
These UDF examples demonstrate how to create reusable functions to simplify complex queries and improve readability. You can adjust the functions to fit your specific needs or database schema.
SELECT
author_id,
SUM(quantity) AS total_sold,
SUM(quantity * 10) AS total_sales_amount
FROM
order_items
JOIN
books ON order_items.book_id = books.id
GROUP BY
author_id;
What is the average rating for each author, along with the number of books they've written and the total number of reviews for those books?
Solution:
sql
SELECT
a.id,
a.first_name,
a.last_name,
COUNT(DISTINCT b.id) AS num_books,
COUNT(r.id) AS total_reviews,
AVG(r.rating) AS average_rating
FROM
authors a
JOIN
books b ON a.id = b.author_id
LEFT JOIN
reviews r ON b.id = r.book_id
GROUP BY
a.id;
What is the running total of books sold for each order?
Solution:
sql
SELECT
order_id,
quantity,
SUM(quantity) OVER (PARTITION BY order_id ORDER BY order_id) AS running_total
FROM
order_items;
What is the average rating for each book, along with the rank of its rating among all books?
Solution:
sql
WITH book_ratings AS (
SELECT
book_id,
AVG(rating) AS average_rating,
DENSE_RANK() OVER (ORDER BY AVG(rating) DESC) AS rating_rank
FROM
reviews
GROUP BY
book_id
)
SELECT
book_id,
average_rating,
rating_rank
FROM
book_ratings;
These questions and solutions should provide a good practice opportunity for working with various aggregate functions and window functions in SQL. You can adjust the queries to fit your specific needs or database schema.