0% found this document useful (0 votes)
11 views10 pages

Nazrin Lab6

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views10 pages

Nazrin Lab6

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

CREATE TABLE author (

author_ID INT PRIMARY KEY,

name NVARCHAR(100),

surname NVARCHAR(100),

nickname NVARCHAR(100)

);

CREATE TABLE book_author (

book_ID INT,

author_ID INT,

PRIMARY KEY (book_ID, author_ID),

FOREIGN KEY (book_ID) REFERENCES Book(book_ID),

FOREIGN KEY (author_ID) REFERENCES author(author_ID)

);

CREATE TABLE Book (

book_ID INT PRIMARY KEY,

title NVARCHAR(255),

category_ID INT,

FOREIGN KEY (category_ID) REFERENCES category(categoryid)

);

CREATE TABLE book_copy (

book_copyID INT PRIMARY KEY,

published_year YEAR,

book_ID INT,
publisher_id INT,

FOREIGN KEY (book_ID) REFERENCES Book(book_ID),

FOREIGN KEY (publisher_id) REFERENCES publisher(publisher_id)

);

CREATE TABLE publisher (

publisher_id INT PRIMARY KEY,

name NVARCHAR(100)

);

CREATE TABLE category (

categoryid INT PRIMARY KEY,

name_of_category NVARCHAR(100)

);

CREATE TABLE Plan (

plan_id INT PRIMARY KEY,

name_of_plan NVARCHAR(100),

price DECIMAL(10, 2),

availability NVARCHAR(50)

);

CREATE TABLE customer_account (

customer_id INT PRIMARY KEY,

name NVARCHAR(100),

surname NVARCHAR(100),
email NVARCHAR(100),

phone NVARCHAR(15),

status NVARCHAR(50)

);

CREATE TABLE checkout (

checkout_id INT PRIMARY KEY,

valid_time DATETIME,

deadline DATETIME,

book_copyID INT,

customerid INT,

fine_if_late DECIMAL(10, 2),

plan_id INT,

FOREIGN KEY (book_copyID) REFERENCES book_copy(book_copyID),

FOREIGN KEY (customerid) REFERENCES customer_account(customer_id),

FOREIGN KEY (plan_id) REFERENCES Plan(plan_id)

);

INSERT INTO author (author_ID, name, surname, nickname) VALUES

(1, 'Leo', 'Tolstoy', NULL),

(2, 'Jane', 'Austen', NULL),

(3, 'Mark', 'Twain', 'Samuel');

INSERT INTO category (categoryid, name_of_category) VALUES

(1, 'Classic Literature'),

(2, 'Historical Fiction'),


(3, 'Adventure');

INSERT INTO Book (book_ID, title, category_ID) VALUES

(1, 'War and Peace', 2),

(2, 'Pride and Prejudice', 1),

(3, ‘crime and punishment', 3)

INSERT INTO book_author (book_ID, author_ID) VALUES

(1, 1), -- Leo Tolstoy wrote War and Peace

(2, 2), -- Jane Austen wrote Pride and Prejudice

(3, 3); -- Dostayevski wrote crime and punishment

INSERT INTO publisher (publisher_id, name) VALUES

(1, 'Penguin Books'),

(2, 'HarperCollins'),

(3, 'Random House');

INSERT INTO book_copy (book_copyID, published_year, book_ID, publisher_id) VALUES

(1, 1869, 1, 1),

(2, 1813, 2, 2),

(3, 1876, 3, 3);

INSERT INTO Plan (plan_id, name_of_plan, price, availability) VALUES

(1, 'Standard Plan', 9.99, 'Available'),

(2, 'Premium Plan', 19.99, 'Available'),

(3, 'Student Plan', 5.99, 'Available');


INSERT INTO customer_account (customer_id, name, surname, email, phone, status) VALUES

(1, 'Nazrin', 'mammadova', '[email protected]', '9945123456', 'Active'),

(2, 'Acap', 'Osmanli’, '[email protected]', '994676543', 'Active'),

(3, 'Zakra', 'Aslanli', '[email protected]', '994555555', 'Inactive');

INSERT INTO checkout (checkout_id, valid_time, deadline, book_copyID, customerid,


fine_if_late, plan_id) VALUES

(1, '2024-11-10 10:00:00', '2024-11-20 10:00:00', 1, 1, 2.50, 1),

(2, '2024-11-11 15:00:00', '2024-11-21 15:00:00', 2, 2, 3.00, 2),

(3, '2024-11-12 12:00:00', '2024-11-22 12:00:00', 3, 3, 1.50, 3);

1.. Count the total number of books sold for each publisher, including a grand total using
`ROLLUP`.

SELECT
PublisherName,
COUNT(*) AS TotalBooksSold
FROM
BookCopy
INNER JOIN
Publisher ON BookCopy.PublisherlD =
Publisher.PublisherlD
GROUP BY
ROLLUP(PublisherName);
2. Calculate the total revenue generated by each book, grouped by book title.
SELECT
BookTitle,
SUM(SalePrice) AS TotalRevenue
FROM
Checkout
INNER JOIN
Book ON Checkout.BookID =
Book.BookID
GROUP BY BookTitle;

3. Find the average quantity of books purchased per order, grouped by customer, only for
customers who have purchased more than 5 books.
SELECT
CustomerID, AVG(Quantity) AS
AvgBooksPurchased
FROM
Checkout
GROUP BY
CustomerID
HAVING
SUM(Quantity) > 5;
4. Retrieve the total number of books available in stock for each publisher, grouped by publisher
name.
SELECT
PublisherName, SUM(CopiesInStock) AS
TotalBooksInStock
FROM
Book
INNER JOIN
Publisher ON Book.PublisherlD =
Publisher.PublisherlD
GROUP BY
PublisherName;

5. Calculate the average discount offered on books for each genre


SELECT
CategoryName,
AVG(Discount) AS AvgDiscount
FROM
Book
NNER JOIN
Category ON Book.CategorylD =
Category.CategoryID
GROUP BY
CategoryName;
.

6. Find the maximum list price of books grouped by language.


SELECT
Language,
MAX(ListPrice) AS MaxPrice
FROM
Book
GROUP BY
Language;

7. Count the number of reviews written by each customer, grouped by their customer ID.
SELECT
CustomerID,
COUNT (ReviewID) AS TotalReviews
FROM
CustomerAccount
LEFT JOIN
Reviews ON
CustomerAccount.CustomerlD =
Reviews.CustomerlD
GROUP BY CustomerlD;

8. Calculate the total revenue generated for each order, grouped by order ID, including orders
with no revenue.
SELECT
OrderlD,
COALESCE(SUM(SalePrice), 0) AS
TotalRevenue
FROM
Checkout
LEFT JOIN
BookCopy ON Checkout.BookCopyID
= BookCopy.BookCopyID
GROUP BY OrderlD;
9. Retrieve the total number of orders placed by each customer, grouped by city, including cities
with no orders.
SELECT
City,
COUNT(OrderlD) AS TotalOrders
FROM
CustomerAccount
LEFT JOIN
Checkout ON
CustomerAccount.CustomerlD =
Checkout.CustomerID
GROUP BY
City;

10. Find the minimum and maximum list price of books grouped by genre, including genres with no books.

SELECT
Category Name,
MIN (ListPrice) AS MinPrice, MAX(ListPrice) AS MaxPrice
FROM
Book
RIGHT JOIN
Category ON Book.CategoryID =
Category.CategorylD
GROUP BY
CategoryName;

You might also like