0% found this document useful (0 votes)
6 views

Step 3

The document outlines steps for inserting data into a database, including authors, books, members, and borrowed books, using SQL commands. It also describes querying data to extract and analyze information, demonstrating various SQL queries such as listing authors with their books and counting books by genre. Additionally, it includes bonus tasks for finding members who joined after a specific date and deleting books that were never borrowed.

Uploaded by

Junior Asong
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)
6 views

Step 3

The document outlines steps for inserting data into a database, including authors, books, members, and borrowed books, using SQL commands. It also describes querying data to extract and analyze information, demonstrating various SQL queries such as listing authors with their books and counting books by genre. Additionally, it includes bonus tasks for finding members who joined after a specific date and deleting books that were never borrowed.

Uploaded by

Junior Asong
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/ 3

Step 3: Insert Data

Why?
Inserting data populates the database, making it ready for queries. This step demonstrates
how to add rows to a table.

Inserting Authors

SQL Code:

INSERT INTO Authors (Name, Nationality) VALUES


('Chinua Achebe', 'Nigerian'),
('J.K. Rowling', 'British'),
('George Orwell', 'British'),
('Gabriel Garcia Marquez', 'Colombian');

Explanation:

 INSERT INTO specifies the table where data is added.


 Values are inserted in the order of columns (Name, Nationality).

Inserting Books

SQL Code:

INSERT INTO Books (Title, AuthorID, Genre, PublishedYear) VALUES


('Things Fall Apart', 1, 'Fiction', 1958),
('Harry Potter and the Philosopher\'s Stone', 2, 'Fantasy', 1997),
('1984', 3, 'Dystopian', 1949),
('One Hundred Years of Solitude', 4, 'Magical Realism', 1967);

Explanation:

 The AuthorID links each book to its author in the Authors table.
 Backslashes (\') escape special characters like single quotes.

Inserting Members

SQL Code:

INSERT INTO Members (Name, PhoneNumber, JoinDate) VALUES


('Alice Johnson', '1234567890', '2025-01-01'),
('John Doe', '0987654321', '2025-01-02'),
('Mary Smith', '1122334455', '2025-01-03');

Inserting Borrowed Books


SQL Code:

INSERT INTO BorrowedBooks (BookID, MemberID, BorrowDate, ReturnDate) VALUES


(1, 1, '2025-01-05', NULL),
(2, 2, '2025-01-06', '2025-01-10'),
(3, 3, '2025-01-07', NULL);

Explanation:

 NULL is used for ReturnDate if the book hasn’t been returned.

Step 4: Querying Data

Why?
Queries allow extracting and analyzing data. These tasks test students' understanding of
joins, conditions, and aggregates.

Query 1: List all authors and their books.

SQL Code:

sql
Copy code
SELECT Authors.Name AS Author, Books.Title AS Book
FROM Authors
INNER JOIN Books ON Authors.AuthorID = Books.AuthorID;

Explanation:

 INNER JOIN links Authors and Books tables using the AuthorID.
 AS renames columns for readability.

Query 2: List all members who borrowed books, with book titles.

SQL Code:

sql
Copy code
SELECT Members.Name AS Member, Books.Title AS Book
FROM BorrowedBooks
INNER JOIN Members ON BorrowedBooks.MemberID = Members.MemberID
INNER JOIN Books ON BorrowedBooks.BookID = Books.BookID;

Query 3: Find books not returned.

SQL Code:
sql
Copy code
SELECT Books.Title AS Book, Members.Name AS Borrower
FROM BorrowedBooks
INNER JOIN Books ON BorrowedBooks.BookID = Books.BookID
INNER JOIN Members ON BorrowedBooks.MemberID = Members.MemberID
WHERE BorrowedBooks.ReturnDate IS NULL;

Explanation:

 The WHERE clause filters rows where ReturnDate is NULL.

Query 4: Count books by genre.

SQL Code:

SELECT Genre, COUNT(*) AS TotalBooks


FROM Books
GROUP BY Genre;

Explanation:

 GROUP BY organizes rows by Genre.


 COUNT(*) calculates the number of books in each genre.

Step 5: Bonus Tasks

1. Find members who joined after January 1, 2025.

SELECT * FROM Members WHERE JoinDate > '2025-01-01';

2. Delete books never borrowed.

sql
Copy code
DELETE FROM Books
WHERE BookID NOT IN (SELECT DISTINCT BookID FROM BorrowedBooks);

Deliverables

 SQL scripts for each task.


 Screenshots of query results.
 Reflection on how relationships and joins were used.

You might also like