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

Database Lab Paper

Lab paper
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Database Lab Paper

Lab paper
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Answers Of Database Lab Paper

1. Create the Authors Table:

CREATE TABLE Authors (


AuthorID INT PRIMARY KEY,
AuthorName VARCHAR(255) NOT NULL,
Country VARCHAR(100)
);

2. Create the Books Table:

CREATE TABLE Books (


BookID INT PRIMARY KEY,
Title VARCHAR(255) NOT NULL,
AuthorID INT,
Genre VARCHAR(100),
Price DECIMAL(10, 2),
FOREIGN KEY (AuthorID) REFERENCES Authors(AuthorID)
);

3. Insert Data into the Books Table:

INSERT INTO Books (BookID, Title, AuthorID, Genre, Price) VALUES


(1, 'Fundamentals', 101, 'Technology', 50.00),
(2, 'Modern Java', 102, 'Technology', 60.00),
(3, 'Data Science 101', 103, 'Science', 70.00),
(4, 'Introduction to AI', 101, 'Technology', 55.00),
(5, 'Fictional Worlds', 104, 'Fiction', 40.00),
(6, 'Advanced Python', 102, 'Technology', 75.00),
(7, 'Chemistry Basics', 105, 'Science', 45.00),
(8, 'The Art of War', 106, 'History', 30.00),
(9, 'Great Expectations', 104, 'Fiction', 35.00);

4. Insert Data into the Authors Table:

INSERT INTO Authors (AuthorID, AuthorName, Country) VALUES


(101, 'Alice Thompson', 'USA'),
(102, 'Bob Johnson', 'UK'),
(103, 'Carol King', 'Canada'),
(104, 'David Wright', 'Australia'),
(105, 'Eve Martin', 'Germany'),
(106, 'Frank Lee', 'China');
Part 2: Queries

5. Retrieve all books, ordered by Price in ascending order:

SELECT * FROM Books


ORDER BY Price ASC;

6. Retrieve all books, ordered by Genre in ascending order and Price in descending
SELECT * FROM Books

ORDER BY Genre ASC, Price DESC;

7. Group books by Genre and count the number of books in each genre:

SELECT Genre, COUNT(*) AS NumberOfBooks


FROM Books
GROUP BY Genre;

8. Retrieve all books where the Title contains the word 'Data':

SELECT * FROM Books


WHERE Title LIKE '%Data%';

9. Retrieve the first 5 books with the highest price:

SELECT * FROM Books


ORDER BY Price DESC
LIMIT 5;

10. Retrieve all books with AuthorID IN (101, 102, 103):

SELECT * FROM Books


WHERE AuthorID IN (101, 102, 103);

11. Retrieve all books with Price between 40 and 60:

SELECT * FROM Books


WHERE Price BETWEEN 40 AND 60;

12. Retrieve all books, grouped by Genre, having more than 1 book in the genre:

SELECT Genre, COUNT(*) AS NumberOfBooks


FROM Books
GROUP BY Genre
HAVING COUNT(*) > 1;
13. Retrieve all books with Genre as 'Technology' and Price greater than 50, ordered by
Price in descending order:

SELECT * FROM Books


WHERE Genre = 'Technology' AND Price > 50
ORDER BY Price DESC;

14. Retrieve the Title and AuthorName of all books using a join:

SELECT Books.Title, Authors.AuthorName


FROM Books
JOIN Authors ON Books.AuthorID = Authors.AuthorID;

You might also like