0% found this document useful (0 votes)
33 views3 pages

Sheet 2

The document outlines a practice exercise for performing CRUD operations on a Library Management System database using SQL. It includes steps for creating a database and tables, inserting data, querying data, updating records, and deleting entries, along with additional challenges and advanced queries. Each step provides specific tasks to enhance understanding and application of SQL commands related to library management.

Uploaded by

nothinga921
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)
33 views3 pages

Sheet 2

The document outlines a practice exercise for performing CRUD operations on a Library Management System database using SQL. It includes steps for creating a database and tables, inserting data, querying data, updating records, and deleting entries, along with additional challenges and advanced queries. Each step provides specific tasks to enhance understanding and application of SQL commands related to library management.

Uploaded by

nothinga921
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/ 3

Practice Sheet- 2 (SQL)

Structured Query Language


Exercise- 2: Library Management System
Objective: Practice performing CRUD (Create, Read, Update, Delete) operations on a Books
table to manage a library database.

Instructions
For this exercise, you will perform the following tasks on a database named LibraryDB.
Complete each task as described. Write the SQL queries to execute the operations.

Step 1: Create the Database and Tables


1. Create a new database named LibraryDB.
2. Create a table named Books with the following columns:
o BookID: INT, Primary Key, Auto Increment.
o Title: VARCHAR(255), not null.
o Author: VARCHAR(255).
o PublishedYear: INT.
o Genre: VARCHAR(100).
o AvailableCopies: INT.
3. Create a table named Authors with the following columns:
o AuthorID: INT, Primary Key, Auto Increment.
o Name: VARCHAR(255), not null.
o Nationality: VARCHAR(100).
o BirthYear: INT.
o DeathYear: INT.

Step 2: Insert Data (Create)


1. Insert at least 5 books into the Books table with values for all columns.

Page 1 of 7
Practice Sheet- 2 (SQL)

2. Insert at least 5 authors into the Authors table with values for all columns.

Step 3: Query Data (Read)


1. Retrieve all books in the database.
2. Retrieve books published after 1950, displaying only the Title and Author.
3. Find the total number of available copies for each genre.
4. Retrieve the details of books where the number of available copies is greater than 5.
5. List all books with the title containing "The" (case insensitive).

Step 4: Update Data (Update)


1. Update the number of available copies for a specific book by adding 2 more.
2. Change the genre of a specific book to a new genre.
3. Update the PublishedYear of a book to correct an error.
4. Decrease the number of available copies for all books published before 2000 by 1.

Step 5: Delete Data (Delete)


1. Delete a book from the database based on its title.
2. Remove all books with less than 5 available copies.
3. Delete all books from a specific genre.

Step 6: Additional Challenges


1. Add a new column to the Books table to track the ISBN of each book.
2. Insert a new book into the table with all details, including the ISBN field.
3. Write a query to retrieve the books sorted by PublishedYear in descending order.
4. Write a query to find the oldest book in the database (based on PublishedYear).

Step 7: Advanced Queries

Page 2 of 7
Practice Sheet- 2 (SQL)

1. Use the GROUP BY clause to find the number of books in each genre.
2. Use the DISTINCT keyword to find all unique genres in the database.
3. Write a query using AND, OR, and NOT to retrieve books published after 2000 that are
either in the "Fiction" genre or not written by "Jane Austen".
4. Use aggregate functions (e.g., SUM, AVG, MAX, MIN) to calculate:
o The average number of available copies across all books.
o The maximum number of available copies for any book.
5. Write a query using a wildcard (LIKE) to find all books with titles starting with the letter
"P".
6. Retrieve books where the PublishedYear is between 1900 and 2000 using the
BETWEEN operator.
7. Retrieve books with Genre values in a specified list (e.g., "Fiction", "Romance") using
the IN operator.
8. Perform an inner join between two tables (Books and Authors, where Authors
contains additional details about authors) to retrieve books along with author details.
9. Use the HAVING clause to filter genres that have more than 5 books in total.
10. Write a query using the EXISTS operator to check if there are any books with less than
3 available copies.

Page 3 of 7

You might also like