Assigment No:5
1) List all the books that are written by Author Loni and has price less then 600.
Enter password: ****
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.35 MySQL Community Server - GPL
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use sample;
Database changed
mysql> desc books;
+-------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| book_no | int | NO | PRI | NULL | |
| book_name | varchar(30) | NO | | NULL | |
| author_name | varchar(30) | YES | | NULL | |
| cost | decimal(7,2) | YES | | NULL | |
| category | varchar(30) | YES | | NULL | |
+-------------+--------------+------+-----+---------+-------+
5 rows in set (0.45 sec)
mysql> select *from books;
+---------+---------------------------+---------------+--------+----------+
| book_no | book_name | author_name | cost | category |
+---------+---------------------------+---------------+--------+----------+
| 101 | Let us C | Denis Ritchie | 450.00 | system |
| 102 | Oracle - Complete Ref | Loni | 550.00 | database |
| 103 | Mastering SQL | Loni | 250.00 | database |
| 104 | PL SQL-Ref | Scott Urman | 750.00 | database |
| 107 | Introduction to Databases | Eva Miller | 280.00 | Database |
+---------+---------------------------+---------------+--------+----------+
5 rows in set (0.25 sec)
mysql> select * from books where author_name = 'Loni' and cost < 600.00
-> ;
+---------+-----------------------+-------------+--------+----------+
| book_no | book_name | author_name | cost | category |
+---------+-----------------------+-------------+--------+----------+
| 102 | Oracle - Complete Ref | Loni | 550.00 | database |
| 103 | Mastering SQL | Loni | 250.00 | database |
+---------+-----------------------+-------------+--------+----------+
2 rows in set (0.11 sec)
2) List the Issue details for the books that are not returned yet.
mysql> select * from issue where return_date is null;
+--------------+---------+-----------+------------+-------------+
| lib_issue_id | book_no | member_id | issue_date | return_date |
+--------------+---------+-----------+------------+-------------+
| 7002 | 102 | 2 | 2006-12-25 | NULL |
+--------------+---------+-----------+------------+-------------+
1 row in set (0.28 sec)
3) Update all the blank return_date with 31-Dec-06 excluding 7005 and 7006.
mysql> update issue
-> set return_date = '2006-12-31'
-> where return_date is null
-> and lib_issue_id not in (7005, 7006);
Query OK, 1 row affected (0.42 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select *from issue;
+--------------+---------+-----------+------------+-------------+
| lib_issue_id | book_no | member_id | issue_date | return_date |
+--------------+---------+-----------+------------+-------------+
| 7002 | 102 | 2 | 2006-12-25 | 2006-12-31 |
| 7005 | 104 | 2 | 2006-11-15 | 2006-11-30 |
+--------------+---------+-----------+------------+-------------+
2 rows in set (0.00 sec)
mysql>
4) List all the Issue details that have books issued for more then 30 days.
mysql> select *
-> from issue
-> where datediff(current_date, issue_date) > 30;
+--------------+---------+-----------+------------+-------------+
| lib_issue_id | book_no | member_id | issue_date | return_date |
+--------------+---------+-----------+------------+-------------+
| 7002 | 102 | 2 | 2006-12-25 | 2006-12-31 |
| 7005 | 104 | 2 | 2006-11-15 | 2006-11-30 |
+--------------+---------+-----------+------------+-------------+
2 rows in set (0.09 sec)
mysql>
5) List all the books that have price in range of 500 to 750 and has category as Database.
mysql> desc books;
ERROR 1146 (42S02): Table 'sample.books' doesn't exist
mysql> create table books (
-> book_no int primary key,
-> book_name varchar(30) not null,
->
-> author_name varchar(30),
-> cost decimal(7,2),
-> category varchar(10) check (category in ('system', 'fiction', 'database', 'rdbms', 'others'))
-> );
Query OK, 0 rows affected (0.92 sec)
mysql> desc books;
+-------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| book_no | int | NO | PRI | NULL | |
| book_name | varchar(30) | NO | | NULL | |
| author_name | varchar(30) | YES | | NULL | |
| cost | decimal(7,2) | YES | | NULL | |
| category | varchar(10) | YES | | NULL | |
+-------------+--------------+------+-----+---------+-------+
5 rows in set (0.15 sec)
mysql> insert into books(book_no,book_name,author_name,cost,category)
-> values(101,'Let us C','denis ritchie',450,'system'),
-> (102,'oracle-complete Ref','loni',550,'database'),
-> (103,'mastering SQL','loni',250,'database'),
-> (104,'PL SQL-Ref','Scott urman',750,'database'),
-> (105, 'Java Programming', 'James Gosling', 600, 'System'),
-> (106, 'The Great Gatsby', 'F. Scott Fitzgerald', 300, 'Fiction'),
-> (107, 'Introduction to MySQL', 'Michael McLaughlin', 200, 'Database'),
-> (108, 'Data Modeling', 'Graeme Simsion', 400, 'RDBMS'),
-> (109, 'The Hitchhiker''s Guide', 'Douglas Adams', 350, 'Others');
Query OK, 9 rows affected (0.19 sec)
Records: 9 Duplicates: 0 Warnings: 0
mysql> select*from books;
+---------+------------------------+---------------------+--------+----------+
| book_no | book_name | author_name | cost | category |
+---------+------------------------+---------------------+--------+----------+
| 101 | Let us C | denis ritchie | 450.00 | system |
| 102 | oracle-complete Ref | loni | 550.00 | database |
| 103 | mastering SQL | loni | 250.00 | database |
| 104 | PL SQL-Ref | Scott urman | 750.00 | database |
| 105 | Java Programming | James Gosling | 600.00 | System |
| 106 | The Great Gatsby | F. Scott Fitzgerald | 300.00 | Fiction |
| 107 | Introduction to MySQL | Michael McLaughlin | 200.00 | Database |
| 108 | Data Modeling | Graeme Simsion | 400.00 | RDBMS |
| 109 | The Hitchhiker's Guide | Douglas Adams | 350.00 | Others |
+---------+------------------------+---------------------+--------+----------+
9 rows in set (0.00 sec)
mysql> select *
-> from books
-> where cost between 500 and 750
-> and category = 'Database';
+---------+---------------------+-------------+--------+----------+
| book_no | book_name | author_name | cost | category |
+---------+---------------------+-------------+--------+----------+
| 102 | oracle-complete Ref | loni | 550.00 | database |
| 104 | PL SQL-Ref | Scott urman | 750.00 | database |
+---------+---------------------+-------------+--------+----------+
2 rows in set (0.02 sec)
mysql>
6) List all the books that belong to any one of the following categories Science, Database, Fiction,
Management.
mysql> select *
-> from books
-> where category in ('Science', 'Database', 'Fiction', 'Management');
+---------+-----------------------+---------------------+--------+----------+
| book_no | book_name | author_name | cost | category |
+---------+-----------------------+---------------------+--------+----------+
| 102 | oracle-complete Ref | loni | 550.00 | database |
| 103 | mastering SQL | loni | 250.00 | database |
| 104 | PL SQL-Ref | Scott urman | 750.00 | database |
| 106 | The Great Gatsby | F. Scott Fitzgerald | 300.00 | Fiction |
| 107 | Introduction to MySQL | Michael McLaughlin | 200.00 | Database |
+---------+-----------------------+---------------------+--------+----------+
5 rows in set (0.00 sec)
7) List all the members in the descending order of Penalty due on them.
mysql> desc member;
ERROR 1146 (42S02): Table 'sample.member' doesn't exist
mysql> create table member
->
-> (member_id int primary key,
-> member_name varchar(30),
-> member_address varchar(50),
-> acc_open_date date,
-> membership_type VARCHAR(20) CHECK (membership_type IN ('Lifetime', 'Annual', 'Half Yearly',
'Quarterly')),
-> fees_paid int,
-> max_books_allowed int,
-> penalty_amount decimal(7,2));
Query OK, 0 rows affected (0.52 sec)
mysql> desc member;
+-------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+-------+
| member_id | int | NO | PRI | NULL | |
| member_name | varchar(30) | YES | | NULL | |
| member_address | varchar(50) | YES | | NULL | |
| acc_open_date | date | YES | | NULL | |
| membership_type | varchar(20) | YES | | NULL | |
| fees_paid | int | YES | | NULL | |
| max_books_allowed | int | YES | | NULL | |
| penalty_amount | decimal(7,2) | YES | | NULL | |
+-------------------+--------------+------+-----+---------+-------+
8 rows in set (0.02 sec)
mysql> INSERT INTO Member (Member_Id, Member_Name, Member_Address, Acc_Open_Date,
Membership_type, Fees_Paid, Max_Books_Allowed, Penalty_Amount)
-> VALUES (1, 'Richa Sharma', 'Pune', '2005-12-10', 'Lifetime', 25000, 5, 50);
Query OK, 1 row affected (0.21 sec)
mysql> INSERT INTO Member (Member_Id, Member_Name, Member_Address, Acc_Open_Date,
Membership_type, Fees_Paid, Max_Books_Allowed, Penalty_Amount)
-> VALUES (3, 'Nina Dobrev', 'New York', '2021-01-15', 'Annual', 500, 3, 10),
-> (4, 'Alice watson', 'Los Angeles', '2020-05-20', 'Lifetime', 10000, 5, 20),
-> (5,"Nidhi sharma",'Chicago', '2022-03-08', 'Half Yearly', 3000, 4, 15),
-> (6, 'stefan salvator', 'Miami', '2021-12-01', 'Quarterly', 1500, 2, 5),
-> (7,'damon salvator','San Francisco', '2022-06-10', 'Annual', 2000, 3, NULL);
Query OK, 5 rows affected (0.12 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> select*from member;
+-----------+-----------------+----------------+---------------+-----------------+-----------+-------------------+--------------
--+
| member_id | member_name | member_address | acc_open_date | membership_type |
fees_paid | max_books_allowed | penalty_amount |
+-----------+-----------------+----------------+---------------+-----------------+-----------+-------------------+--------------
--+
| 1 | Richa Sharma | Pune | 2005-12-10 | Lifetime | 25000 | 5|
50.00 |
| 3 | Nina Dobrev | New York | 2021-01-15 | Annual | 500 | 3|
10.00 |
| 4 | Alice watson | Los Angeles | 2020-05-20 | Lifetime | 10000 | 5|
20.00 |
| 5 | Nidhi sharma | Chicago | 2022-03-08 | Half Yearly | 3000 | 4|
15.00 |
| 6 | stefan salvator | Miami | 2021-12-01 | Quarterly | 1500 | 2|
5.00 |
| 7 | damon salvator | San Francisco | 2022-06-10 | Annual | 2000 | 3|
NULL |
+-----------+-----------------+----------------+---------------+-----------------+-----------+-------------------+--------------
--+
6 rows in set (0.00 sec)
mysql> select *
-> from member
-> order by penalty_amount desc;
+-----------+-----------------+----------------+---------------+-----------------+-----------+-------------------+--------------
--+
| member_id | member_name | member_address | acc_open_date | membership_type |
fees_paid | max_books_allowed | penalty_amount |
+-----------+-----------------+----------------+---------------+-----------------+-----------+-------------------+--------------
--+
| 1 | Richa Sharma | Pune | 2005-12-10 | Lifetime | 25000 | 5|
50.00 |
| 4 | Alice watson | Los Angeles | 2020-05-20 | Lifetime | 10000 | 5|
20.00 |
| 5 | Nidhi sharma | Chicago | 2022-03-08 | Half Yearly | 3000 | 4|
15.00 |
| 3 | Nina Dobrev | New York | 2021-01-15 | Annual | 500 | 3|
10.00 |
| 6 | stefan salvator | Miami | 2021-12-01 | Quarterly | 1500 | 2|
5.00 |
| 7 | damon salvator | San Francisco | 2022-06-10 | Annual | 2000 | 3|
NULL |
+-----------+-----------------+----------------+---------------+-----------------+-----------+-------------------+--------------
--+
6 rows in set (0.00 sec)
mysql>
8) Modify the price of book with id 103 to Rs 300 and category to RDBMS.
mysql> update books
-> set cost = 300.00,
-> category = 'RDBMS'
-> where book_no = 103;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0
mysql> select*from books;
+---------+------------------------+---------------------+--------+----------+
| book_no | book_name | author_name | cost | category |
+---------+------------------------+---------------------+--------+----------+
| 101 | Let us C | denis ritchie | 450.00 | system |
| 102 | oracle-complete Ref | loni | 550.00 | database |
| 103 | mastering SQL | loni | 300.00 | RDBMS |
| 104 | PL SQL-Ref | Scott urman | 750.00 | database |
| 105 | Java Programming | James Gosling | 600.00 | System |
| 106 | The Great Gatsby | F. Scott Fitzgerald | 300.00 | Fiction |
| 107 | Introduction to MySQL | Michael McLaughlin | 200.00 | Database |
| 108 | Data Modeling | Graeme Simsion | 400.00 | RDBMS |
| 109 | The Hitchhiker's Guide | Douglas Adams | 350.00 | Others |
+---------+------------------------+---------------------+--------+----------+
9 rows in set (0.00 sec)
mysql>
9) List all the books in ascending order of category and descending order of price.
mysql> select *
-> from books
-> order by category asc, cost desc;
+---------+------------------------+---------------------+--------+----------+
| book_no | book_name | author_name | cost | category |
+---------+------------------------+---------------------+--------+----------+
| 104 | PL SQL-Ref | Scott urman | 750.00 | database |
| 102 | oracle-complete Ref | loni | 550.00 | database |
| 107 | Introduction to MySQL | Michael McLaughlin | 200.00 | Database |
| 106 | The Great Gatsby | F. Scott Fitzgerald | 300.00 | Fiction |
| 109 | The Hitchhiker's Guide | Douglas Adams | 350.00 | Others |
| 108 | Data Modeling | Graeme Simsion | 400.00 | RDBMS |
| 103 | mastering SQL | loni | 300.00 | RDBMS |
| 105 | Java Programming | James Gosling | 600.00 | System |
| 101 | Let us C | denis ritchie | 450.00 | system |
+---------+------------------------+---------------------+--------+----------+
9 rows in set (0.02 sec)
mysql>
10) List all the books that contain word SQL in the name of the book.
mysql> select *
-> from books
-> where lower(book_name) like '%sql%';
+---------+-----------------------+--------------------+--------+----------+
| book_no | book_name | author_name | cost | category |
+---------+-----------------------+--------------------+--------+----------+
| 103 | mastering SQL | loni | 300.00 | RDBMS |
| 104 | PL SQL-Ref | Scott urman | 750.00 | database |
| 107 | Introduction to MySQL | Michael McLaughlin | 200.00 | Database |
+---------+-----------------------+--------------------+--------+----------+
3 rows in set (0.06 sec)
mysql>
11) List the Lib_Issue_Id, Issue_Date, Return_Date and No of days Book was issued.
mysql> select lib_issue_id, issue_date, return_date, datediff(return_date, issue_date) as
days_book_issued
-> from issue;
+--------------+------------+-------------+------------------+
| lib_issue_id | issue_date | return_date | days_book_issued |
+--------------+------------+-------------+------------------+
| 7002 | 2006-12-25 | 2006-12-31 | 6|
| 7005 | 2006-11-15 | 2006-11-30 | 15 |
+--------------+------------+-------------+------------------+
2 rows in set (0.00 sec)
12) Find the details of the member of the Library in the order of their joining the library.
mysql> select *from member
-> ORDER BY acc_open_date;;
+-----------+-----------------+----------------+---------------+-----------------+-----------+-------------------+--------------
--+
| member_id | member_name | member_address | acc_open_date | membership_type |
fees_paid | max_books_allowed | penalty_amount |
+-----------+-----------------+----------------+---------------+-----------------+-----------+-------------------+--------------
--+
| 1 | Richa Sharma | Pune | 2005-12-10 | Lifetime | 25000 | 5|
50.00 |
| 4 | Alice watson | Los Angeles | 2020-05-20 | Lifetime | 10000 | 5|
20.00 |
| 3 | Nina Dobrev | New York | 2021-01-15 | Annual | 500 | 3|
10.00 |
| 6 | stefan salvator | Miami | 2021-12-01 | Quarterly | 1500 | 2|
5.00 |
| 5 | Nidhi sharma | Chicago | 2022-03-08 | Half Yearly | 3000 | 4|
15.00 |
| 7 | damon salvator | San Francisco | 2022-06-10 | Annual | 2000 | 3|
NULL |
+-----------+-----------------+----------------+---------------+-----------------+-----------+-------------------+--------------
--+
6 rows in set (0.00 sec)
13) Display the count of total no of books issued to Member 101.
mysql> select*from issue;
+--------------+---------+-----------+------------+-------------+
| lib_issue_id | book_no | member_id | issue_date | return_date |
+--------------+---------+-----------+------------+-------------+
| 7001 | 101 | 1 | 2006-12-10 | NULL |
| 7003 | 104 | 1 | 2006-01-15 | NULL |
| 7004 | 101 | 1 | 2006-07-04 | NULL |
| 7006 | 101 | 3 | 2006-02-18 | NULL |
+--------------+---------+-----------+------------+-------------+
4 rows in set (0.00 sec)
mysql>
14) Display the total penalty due for all the members.
mysql> select count(*) as total_books_issued
-> from issue
-> where member_id = 101;
+--------------------+
| total_books_issued |
+--------------------+
| 0|
+--------------------+
1 row in set (0.00 sec)
mysql>
15) Display the total no of members
mysql> select count(*) as total_members
-> from member;
+---------------+
| total_members |
+---------------+
| 6|
+---------------+
1 row in set (0.25 sec)
mysql>
16) Display the total no of books issued
mysql> SELECT COUNT(*) AS total_books_issued
-> FROM issue;
+--------------------+
| total_books_issued |
+--------------------+
| 4|
+--------------------+
1 row in set (0.00 sec)
mysql>
17) Display the average membership fees paid by all the members
mysql> SELECT AVG(fees_paid) AS average_membership_fees
-> FROM member;
+-------------------------+
| average_membership_fees |
+-------------------------+
| 7000.0000 |
+-------------------------+
1 row in set (0.00 sec)
mysql>
18) List the various categories and count of books in each category.
mysql> SELECT category, COUNT(*) AS book_count
-> FROM books
-> GROUP BY category;
+----------+------------+
| category | book_count |
+----------+------------+
| system | 2|
| database | 3|
| RDBMS | 2|
| Fiction | 1|
| Others | 1|
+----------+------------+
5 rows in set (0.05 sec)
19) List the book_No and the number of times the book is issued in the descending order of count.
mysql> SELECT book_no, COUNT(*) AS issue_count
-> FROM issue
-> GROUP BY book_no
-> ORDER BY issue_count DESC;
+---------+-------------+
| book_no | issue_count |
+---------+-------------+
| 101 | 3|
| 104 | 1|
+---------+-------------+
2 rows in set (0.03 sec)
mysql>
20) Find the maximum, minimum, total and average penalty amount in the member table.
mysql> SELECT
-> MAX(penalty_amount) AS max_penalty,
-> MIN(penalty_amount) AS min_penalty,
-> SUM(penalty_amount) AS total_penalty,
-> AVG(penalty_amount) AS average_penalty
-> FROM member;
+-------------+-------------+---------------+-----------------+
| max_penalty | min_penalty | total_penalty | average_penalty |
+-------------+-------------+---------------+-----------------+
| 50.00 | 5.00 | 100.00 | 20.000000 |
+-------------+-------------+---------------+-----------------+
1 row in set (0.08 sec)
mysql>
21) Display the member id and the no of books for each member that has issued more then 2 books.
mysql> select member_id, count(*) as books_issued
-> from issue
-> group by member_id
-> having count(*) > 2;
+-----------+--------------+
| member_id | books_issued |
+-----------+--------------+
| 1| 3|
+-----------+--------------+
1 row in set (0.00 sec)
mysql>
22) Display the member id, book no and no of times the same book is issued by the member in the
descending order of count.
mysql> select member_id, book_no, count(*) as issue_count
-> from issue
-> group by member_id, book_no
-> order by issue_count desc;
+-----------+---------+-------------+
| member_id | book_no | issue_count |
+-----------+---------+-------------+
| 1| 101 | 2|
| 1| 104 | 1|
| 3| 101 | 1|
+-----------+---------+-------------+
3 rows in set (0.00 sec)
mysql>
23) Display the month and no of books issued each month in the descending order of count.
mysql> select month(issue_date) as issue_month, count(*) as books_issued
-> from issue
-> group by issue_month
-> order by books_issued desc;
+-------------+--------------+
| issue_month | books_issued |
+-------------+--------------+
| 12 | 1|
| 1| 1|
| 7| 1|
| 2| 1|
+-------------+--------------+
4 rows in set (0.03 sec)
24) List the book_no of all the books that are not issued to any member so far.
mysql> select book_no
-> from books
-> where book_no not in (select distinct book_no from issue where book_no is not null);
+---------+
| book_no |
+---------+
| 102 |
| 103 |
| 105 |
| 106 |
| 107 |
| 108 |
| 109 |
+---------+
7 rows in set (0.07 sec)
mysql>
25) List all the member id that exist in member table and has also at least one book issued by them.
mysql> select distinct member_id
-> from issue
-> where member_id is not null;
+-----------+
| member_id |
+-----------+
| 1|
| 3|
+-----------+
2 rows in set (0.05 sec)
mysql>
26) List the member ID with highest and lowest no of books issued.
mysql> (SELECT member_id, COUNT(*) AS books_issued
-> FROM issue
-> GROUP BY member_id
-> ORDER BY books_issued DESC
-> LIMIT 1)
->
-> UNION
->
-> (SELECT member_id, COUNT(*) AS books_issued
-> FROM issue
-> GROUP BY member_id
-> ORDER BY books_issued
-> LIMIT 1);
+-----------+--------------+
| member_id | books_issued |
+-----------+--------------+
| 1| 3|
| 3| 1|
+-----------+--------------+
2 rows in set (0.04 sec)
mysql>
27) List all the Issue_details for books issued in December and July without using any arithmetic,
Logical or comparison operator.
mysql> SELECT *
-> FROM issue
-> WHERE MONTH(issue_date) IN (12, 7);
+--------------+---------+-----------+------------+-------------+
| lib_issue_id | book_no | member_id | issue_date | return_date |
+--------------+---------+-----------+------------+-------------+
| 7001 | 101 | 1 | 2006-12-10 | NULL |
| 7004 | 101 | 1 | 2006-07-04 | NULL |
+--------------+---------+-----------+------------+-------------+
2 rows in set (0.00 sec)
mysql>
28) List the Book_No, Book_Name and Issue_date for all the books that are issued in month of
December and belong to category Database.
mysql> select i.book_no, b.book_name, i.issue_date
-> from issue i
-> join books b on i.book_no = b.book_no
-> where month(i.issue_date) = 12
-> and b.category = 'database';
Empty set (0.04 sec)
29) List the Member Id, Member Name and max books allowed in the descending order of the max
books allowed.
mysql> select member_id, member_name, max_books_allowed
-> from member
-> order by max_books_allowed desc;
+-----------+-----------------+-------------------+
| member_id | member_name | max_books_allowed |
+-----------+-----------------+-------------------+
| 1 | Richa Sharma | 5|
| 4 | Alice watson | 5|
| 5 | Nidhi sharma | 4|
| 3 | Nina Dobrev | 3|
| 7 | damon salvator | 3|
| 6 | stefan salvator | 2|
+-----------+-----------------+-------------------+
6 rows in set (0.00 sec)
mysql>
30) List the Book No, Book Name, Issue_date and Return_Date for all the books issued by Richa
Sharma.
mysql> select i.book_no, b.book_name, i.issue_date, i.return_date
-> from issue i
-> join member m on i.member_id = m.member_id
-> join books b on i.book_no = b.book_no
-> where m.member_name = 'Richa Sharma';
+---------+------------+------------+-------------+
| book_no | book_name | issue_date | return_date |
+---------+------------+------------+-------------+
| 101 | Let us C | 2006-12-10 | NULL |
| 101 | Let us C | 2006-07-04 | NULL |
| 104 | PL SQL-Ref | 2006-01-15 | NULL |
+---------+------------+------------+-------------+
3 rows in set (0.04 sec)
mysql>
31) List the details of all the members that have issued books in Database category.
mysql> select distinct m.member_id, m.member_name, m.acc_open_date, m.membership_type,
m.fees_paid, m.max_books_allowed, m.penalty_amount
-> from member m
-> join issue i on m.member_id = i.member_id
-> join books b on i.book_no = b.book_no
-> where b.category = 'Database';
+-----------+--------------+---------------+-----------------+-----------+-------------------+----------------+
| member_id | member_name | acc_open_date | membership_type | fees_paid |
max_books_allowed | penalty_amount |
+-----------+--------------+---------------+-----------------+-----------+-------------------+----------------+
| 1 | Richa Sharma | 2005-12-10 | Lifetime | 25000 | 5| 50.00 |
+-----------+--------------+---------------+-----------------+-----------+-------------------+----------------+
1 row in set (0.00 sec)
mysql>
32) List all the books that have highest price in their own category.
mysql> SELECT b.*
-> FROM books b
-> JOIN (
-> SELECT category, MAX(cost) AS max_price
-> FROM books
-> GROUP BY category
-> ) max_prices
-> ON b.category = max_prices.category AND b.cost = max_prices.max_price;
+---------+------------------------+---------------------+--------+----------+
| book_no | book_name | author_name | cost | category |
+---------+------------------------+---------------------+--------+----------+
| 104 | PL SQL-Ref | Scott urman | 750.00 | database |
| 105 | Java Programming | James Gosling | 600.00 | System |
| 106 | The Great Gatsby | F. Scott Fitzgerald | 300.00 | Fiction |
| 108 | Data Modeling | Graeme Simsion | 400.00 | RDBMS |
| 109 | The Hitchhiker's Guide | Douglas Adams | 350.00 | Others |
+---------+------------------------+---------------------+--------+----------+
5 rows in set (0.04 sec)
mysql>
33) List all the Issue_Details where Issue_date is not within the Acc_open_date and Return_date for
that member.
mysql> SELECT *
-> FROM issue i
-> JOIN member
-> m ON i.member_id = m.member_id
-> WHERE i.issue_date < m.acc_open_date OR i.return_date > m.acc_open_date;
+--------------+---------+-----------+------------+-------------+-----------+-------------+----------------+---------------+---
--------------+-----------+-------------------+----------------+
| lib_issue_id | book_no | member_id | issue_date | return_date | member_id | member_name |
member_address | acc_open_date | membership_type | fees_paid | max_books_allowed |
penalty_amount |
+--------------+---------+-----------+------------+-------------+-----------+-------------+----------------+---------------+---
--------------+-----------+-------------------+----------------+
| 7006 | 101 | 3 | 2006-02-18 | NULL | 3 | Nina Dobrev | New York | 2021-
01-15 | Annual | 500 | 3| 10.00 |
+--------------+---------+-----------+------------+-------------+-----------+-------------+----------------+---------------+---
--------------+-----------+-------------------+----------------+
1 row in set (0.00 sec)
mysql>
34) List all the members that have not issued a single book so far.
mysql> SELECT m.*FROM member m
-> LEFT JOIN issue i ON m.member_id = i.member_id
-> WHERE i.member_id IS NULL;
+-----------+-----------------+----------------+---------------+-----------------+-----------+-------------------+--------------
--+
| member_id | member_name | member_address | acc_open_date | membership_type |
fees_paid | max_books_allowed | penalty_amount |
+-----------+-----------------+----------------+---------------+-----------------+-----------+-------------------+--------------
--+
| 4 | Alice watson | Los Angeles | 2020-05-20 | Lifetime | 10000 | 5|
20.00 |
| 5 | Nidhi sharma | Chicago | 2022-03-08 | Half Yearly | 3000 | 4|
15.00 |
| 6 | stefan salvator | Miami | 2021-12-01 | Quarterly | 1500 | 2|
5.00 |
| 7 | damon salvator | San Francisco | 2022-06-10 | Annual | 2000 | 3|
NULL |
+-----------+-----------------+----------------+---------------+-----------------+-----------+-------------------+--------------
--+
4 rows in set (0.00 sec)
mysql>
35) List all the members that have issued the same book as issued by Garima.
mysql> SELECT m.*
-> FROM member m
-> JOIN issue i ON m.member_id = i.member_id
-> JOIN issue i_garima ON i.book_no = i_garima.book_no
-> JOIN member garima ON i_garima.member_id = garima.member_id
-> WHERE garima.member_name = 'Garima';
Empty set (0.00 sec)
mysql> select*from member;
+-----------+-----------------+----------------+---------------+-----------------+-----------+-------------------+--------------
--+
| member_id | member_name | member_address | acc_open_date | membership_type |
fees_paid | max_books_allowed | penalty_amount |
+-----------+-----------------+----------------+---------------+-----------------+-----------+-------------------+--------------
--+
| 1 | Richa Sharma | Pune | 2005-12-10 | Lifetime | 25000 | 5|
50.00 |
| 3 | Nina Dobrev | New York | 2021-01-15 | Annual | 500 | 3|
10.00 |
| 4 | Alice watson | Los Angeles | 2020-05-20 | Lifetime | 10000 | 5|
20.00 |
| 5 | Nidhi sharma | Chicago | 2022-03-08 | Half Yearly | 3000 | 4|
15.00 |
| 6 | stefan salvator | Miami | 2021-12-01 | Quarterly | 1500 | 2|
5.00 |
| 7 | damon salvator | San Francisco | 2022-06-10 | Annual | 2000 | 3|
NULL |
+-----------+-----------------+----------------+---------------+-----------------+-----------+-------------------+--------------
--+
6 rows in set (0.00 sec)
mysql>
36) List the Book_Name, Price of all the books that are not returned for more then 30 days.
mysql> SELECT b.book_name, b.cost
-> FROM books b
-> JOIN issue i ON b.book_no = i.book_no
-> WHERE i.return_date IS NULL OR DATEDIFF(CURDATE(), i.issue_date) > 30;
+------------+--------+
| book_name | cost |
+------------+--------+
| Let us C | 450.00 |
| PL SQL-Ref | 750.00 |
| Let us C | 450.00 |
| Let us C | 450.00 |
+------------+--------+
4 rows in set (0.00 sec)
mysql>
37) List all the authors and book_name that has more then 1 book written by them.
mysql> select author_name, book_name
-> from books
-> where author_name in (
-> select author_name
-> from books
-> group by author_name
-> having count(*) > 1
-> );
+-------------+---------------------+
| author_name | book_name |
+-------------+---------------------+
| loni | oracle-complete Ref |
| loni | mastering SQL |
+-------------+---------------------+
2 rows in set (0.03 sec)
mysql>
38) List the Member ID, Member Name of the people that have issued the highest and the lowest no
of books.
mysql> (select member_id, member_name
-> from member
-> order by (select count(*) from issue where issue.member_id = member.member_id) desc
-> limit 1)
->
-> union
->
-> (select member_id, member_name
-> from member
-> order by (select count(*) from issue where issue.member_id = member.member_id) asc
-> limit 1);
+-----------+--------------+
| member_id | member_name |
+-----------+--------------+
| 1 | Richa Sharma |
| 4 | Alice watson |
+-----------+--------------+
2 rows in set (0.00 sec)
mysql>
39) List the details of highest 3 priced books.
mysql> select * from books order by cost desc limit 3;
+---------+---------------------+---------------+--------+----------+
| book_no | book_name | author_name | cost | category |
+---------+---------------------+---------------+--------+----------+
| 104 | PL SQL-Ref | Scott urman | 750.00 | database |
| 105 | Java Programming | James Gosling | 600.00 | System |
| 102 | oracle-complete Ref | loni | 550.00 | database |
+---------+---------------------+---------------+--------+----------+
3 rows in set (0.00 sec)
mysql>
40) List the total cost of all the books that are currently issued but not returned.
mysql> select sum(b.cost) as total_cost
-> from books b
-> join issue i on b.book_no = i.book_no
-> where i.return_date is null;
+------------+
| total_cost |
+------------+
| 2100.00 |
+------------+
1 row in set (0.00 sec)
mysql>
41) List the details of the book that has been issued maximum no of times.
mysql> SELECT b.*
-> FROM books b
-> JOIN issue i ON b.book_no = i.book_no
-> GROUP BY b.book_no
-> ORDER BY COUNT(*) DESC
-> LIMIT 1;
+---------+-----------+---------------+--------+----------+
| book_no | book_name | author_name | cost | category |
+---------+-----------+---------------+--------+----------+
| 101 | Let us C | denis ritchie | 450.00 | system |
+---------+-----------+---------------+--------+----------+
1 row in set (0.00 sec)
mysql>
42) List how many books are issued to lifetime members.
mysql> select count(*) as books_issued_to_lifetime_members
-> from issue i
-> join member m on i.member_id = m.member_id
-> where m.membership_type = 'Lifetime';
+----------------------------------+
| books_issued_to_lifetime_members |
+----------------------------------+
| 3|
+----------------------------------+
1 row in set (0.00 sec)
mysql>
43) List all member types and how many members are there in each type.
mysql> select membership_type, count(*) as members_count from member
-> group by membership_type;
+-----------------+---------------+
| membership_type | members_count |
+-----------------+---------------+
| Lifetime | 2|
| Annual | 2|
| Half Yearly | 1|
| Quarterly | 1|
+-----------------+---------------+
4 rows in set (0.00 sec)
mysql>
44) List first 5 members who had joined library.
mysql> select *
-> from member
-> order by acc_open_date
-> limit 5;
+-----------+-----------------+----------------+---------------+-----------------+-----------+-------------------+--------------
--+
| member_id | member_name | member_address | acc_open_date | membership_type |
fees_paid | max_books_allowed | penalty_amount |
+-----------+-----------------+----------------+---------------+-----------------+-----------+-------------------+--------------
--+
| 1 | Richa Sharma | Pune | 2005-12-10 | Lifetime | 25000 | 5|
50.00 |
| 4 | Alice watson | Los Angeles | 2020-05-20 | Lifetime | 10000 | 5|
20.00 |
| 3 | Nina Dobrev | New York | 2021-01-15 | Annual | 500 | 3|
10.00 |
| 6 | stefan salvator | Miami | 2021-12-01 | Quarterly | 1500 | 2|
5.00 |
| 5 | Nidhi sharma | Chicago | 2022-03-08 | Half Yearly | 3000 | 4|
15.00 |
+-----------+-----------------+----------------+---------------+-----------------+-----------+-------------------+--------------
--+
5 rows in set (0.00 sec)
mysql>
45) List the members with their member type, who have issued books during the period 1st
December to 31st December.
mysql> select m.member_id, m.member_name, m.membership_type
-> from member m
-> join issue i on m.member_id = i.member_id
-> where i.issue_date between '2022-12-01' and '2022-12-31';
Empty set (0.00 sec)
mysql> select*from member;
+-----------+-----------------+----------------+---------------+-----------------+-----------+-------------------+--------------
--+
| member_id | member_name | member_address | acc_open_date | membership_type |
fees_paid | max_books_allowed | penalty_amount |
+-----------+-----------------+----------------+---------------+-----------------+-----------+-------------------+--------------
--+
| 1 | Richa Sharma | Pune | 2005-12-10 | Lifetime | 25000 | 5|
50.00 |
| 3 | Nina Dobrev | New York | 2021-01-15 | Annual | 500 | 3|
10.00 |
| 4 | Alice watson | Los Angeles | 2020-05-20 | Lifetime | 10000 | 5|
20.00 |
| 5 | Nidhi sharma | Chicago | 2022-03-08 | Half Yearly | 3000 | 4|
15.00 |
| 6 | stefan salvator | Miami | 2021-12-01 | Quarterly | 1500 | 2|
5.00 |
| 7 | damon salvator | San Francisco | 2022-06-10 | Annual | 2000 | 3|
NULL |
+-----------+-----------------+----------------+---------------+-----------------+-----------+-------------------+--------------
--+
6 rows in set (0.00 sec)
mysql>
46) List all the members who have not returned books yet.
mysql> select m.*
-> from member m
-> join issue i on m.member_id = i.member_id
-> where i.return_date is null;
+-----------+--------------+----------------+---------------+-----------------+-----------+-------------------+----------------+
| member_id | member_name | member_address | acc_open_date | membership_type |
fees_paid | max_books_allowed | penalty_amount |
+-----------+--------------+----------------+---------------+-----------------+-----------+-------------------+----------------+
| 1 | Richa Sharma | Pune | 2005-12-10 | Lifetime | 25000 | 5|
50.00 |
| 1 | Richa Sharma | Pune | 2005-12-10 | Lifetime | 25000 | 5|
50.00 |
| 1 | Richa Sharma | Pune | 2005-12-10 | Lifetime | 25000 | 5|
50.00 |
| 3 | Nina Dobrev | New York | 2021-01-15 | Annual | 500 | 3|
10.00 |
+-----------+--------------+----------------+---------------+-----------------+-----------+-------------------+----------------+
4 rows in set (0.00 sec)
mysql>
47) List all the members who joined library on the same date Garima joined.
mysql> select *
-> from member
-> where acc_open_date = (select acc_open_date from member where member_name =
'Garima');
Empty set (0.05 sec)
48) List all the members who has issued books from author “Loni” in the month of December
mysql> select*from books;
+---------+------------------------+---------------------+--------+----------+
| book_no | book_name | author_name | cost | category |
+---------+------------------------+---------------------+--------+----------+
| 101 | Let us C | denis ritchie | 450.00 | system |
| 102 | oracle-complete Ref | loni | 550.00 | database |
| 103 | mastering SQL | loni | 300.00 | RDBMS |
| 104 | PL SQL-Ref | Scott urman | 750.00 | database |
| 105 | Java Programming | James Gosling | 600.00 | System |
| 106 | The Great Gatsby | F. Scott Fitzgerald | 300.00 | Fiction |
| 107 | Introduction to MySQL | Michael McLaughlin | 200.00 | Database |
| 108 | Data Modeling | Graeme Simsion | 400.00 | RDBMS |
| 109 | The Hitchhiker's Guide | Douglas Adams | 350.00 | Others |
+---------+------------------------+---------------------+--------+----------+
9 rows in set (0.00 sec)
mysql> select distinct m.*
-> from member m
-> join issue i on m.member_id = i.member_id
-> join books b on i.book_no = b.book_no
-> where b.author_name = 'loni' and month(i.issue_date) = 12;
Empty set (0.00 sec)
49) List names of the authors whose books are least issued by lifetime members.
mysql> select b.author_name
-> from books b
-> join issue i on b.book_no = i.book_no
-> join member m on i.member_id = m.member_id
-> where m.membership_type = 'Lifetime'
-> group by b.author_name
-> order by count(*) asc
-> limit 1;
+-------------+
| author_name |
+-------------+
| Scott urman |
+-------------+
1 row in set (0.00 sec)
mysql>
50) List the names of members who has issued the books whose cost is more than 300 rupees and
whose author is “Scott Urman”
mysql> select distinct m.member_name
-> from member m
-> join issue i on m.member_id = i.member_id
-> join books b on i.book_no = b.book_no
-> where b.author_name = 'Scott Urman' and b.cost > 300;
+--------------+
| member_name |
+--------------+
| Richa Sharma |
+--------------+
1 row in set (0.04 sec)
51) List all lifetime members who joined library during 1st January 2006 to 31st December 2006 but
issued only one book.
mysql> select m.*
-> from member m
-> join issue i on m.member_id = i.member_id
-> where m.membership_type = 'Lifetime'
-> and m.acc_open_date between '2006-01-01' and '2006-12-31'
-> and (select count(*) from issue where member_id = m.member_id) = 1;
Empty set (0.00 sec)
52) Modify the Penalty_Amount for Garima Sen to Rs 100
mysql> update member
-> set penalty_amount = 100
-> where member_name = 'Garima Sen';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0 Changed: 0 Warnings: 0
mysql> select *from member;
+-----------+-----------------+----------------+---------------+-----------------+-----------+-------------------+--------------
--+
| member_id | member_name | member_address | acc_open_date | membership_type |
fees_paid | max_books_allowed | penalty_amount |
+-----------+-----------------+----------------+---------------+-----------------+-----------+-------------------+--------------
--+
| 1 | Richa Sharma | Pune | 2005-12-10 | Lifetime | 25000 | 5|
50.00 |
| 3 | Nina Dobrev | New York | 2021-01-15 | Annual | 500 | 3|
10.00 |
| 4 | Alice watson | Los Angeles | 2020-05-20 | Lifetime | 10000 | 5|
20.00 |
| 5 | Nidhi sharma | Chicago | 2022-03-08 | Half Yearly | 3000 | 4|
15.00 |
| 6 | stefan salvator | Miami | 2021-12-01 | Quarterly | 1500 | 2|
5.00 |
| 7 | damon salvator | San Francisco | 2022-06-10 | Annual | 2000 | 3|
NULL |
+-----------+-----------------+----------------+---------------+-----------------+-----------+-------------------+--------------
--+
6 rows in set (0.00 sec)