0% found this document useful (0 votes)
14 views4 pages

Dbms 5

this document is a file that and answers including in depth answering of creation of files using mysql
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views4 pages

Dbms 5

this document is a file that and answers including in depth answering of creation of files using mysql
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

--------Shwetank Pandey , 2301420042 ,b.

tech cse-------
create database lib;
use lib;
create table books (bookid int primary key
,title varchar(25), author varchar(20)
, price int);

INSERT INTO books (bookid, title, author, price) VALUES


(1, 'Book 1', 'Author 1', 10.99),
(2, 'Book 2', 'Author 2', 12.99),
(3, 'Book 3', 'Author 3', 9.99),
(4, 'Book 4', 'Author 4', 15.99),
(5, 'Book 5', 'Author 5', 8.99);

create table customers (customerid int primary key


,name varchar (25)
,email varchar (25)
,address varchar (25));

INSERT INTO customers (customerid, name , email, address)


VALUES
(1, 'Customer 1', 'customer1@example.com', 'Address 1'),
(2, 'Customer 2', 'customer2@example.com', 'Address 2'),
(3, 'Customer 3', 'customer3@example.com', 'Address 3'),
(4, 'Customer 4', 'customer4@example.com', 'Address 4'),
(5, 'Customer 5', 'customer5@example.com', 'Address 5');

create table orders (orderid int primary key


,customerid int,foreign key (customerid) references
customers(customerid)
,bookid int, foreign key (bookid) references books(bookid)
,quantity int
,orderdate date);

INSERT INTO orders (orderid, customerid, bookid, quantity,


orderdate) VALUES
(1, 1, 1, 2, '2023-06-01'),
(2, 2, 3, 1, '2023-06-02'),
(3, 3, 2, 3, '2023-06-03'),
(4, 4, 4, 2, '2023-06-04'),
(5, 5, 5, 1, '2023-06-05');

create table payments (paymentid int primary key


,orderid int, foreign key (orderid) references orders(orderid)
,paymentdate date,amount int);
INSERT INTO payments (paymentid, orderid, paymentdate,
amount) VALUES
(1, 1, '2023-06-02', 21.98),
(2, 2, '2023-06-03', 9.99),
(3, 3, '2023-06-04', 38.97),
(4, 4, '2023-06-05', 31.98),
(5, 5, '2023-06-06', 8.99);

Solution-----------
select * from customers;

SELECT title, author FROM books;

SELECT SUM(bookid) AS total_books_sold FROM books;

SELECT DISTINCT name


FROM customers
JOIN orders ON c.customerid = o.customerid;

SELECT SUM(amount * orderid) AS total_revenue FROM


payments;

SELECT title, price FROM books ORDER BY price DESC


LIMIT 1;

SELECT customerid ,name, SUM(s.quantity) AS


total_books_sold
FROM customers
JOIN sales ON c.customerid = s.customerid
GROUP BY c.customerid, name;

SELECT c.customer_id, c.first_name, c.last_name


FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id
WHERE o.order_id IS NULL;

SELECT c.customerid, c.name


FROM customers c
LEFT JOIN orders o ON c.customerid = o.customerid
WHERE o.orderid IS NULL;

SELECT c.customerid, c.name, COUNT(o.orderid) AS


total_orders
FROM customers c
JOIN orders o ON c.customerid = o.customerid
GROUP BY c.customerid, c.name
ORDER BY total_orders DESC
LIMIT 1;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy