Practice-set-SQL
Practice-set-SQL
Question 1
1. Insert a new book titled "SQL Basics" with AuthorID 3, price of 500, and quantity of 10.
2. Delete all books with a quantity of zero.
3. Update the price of the book titled "Advanced SQL" to 750.
Solution
UPDATE Books
SET Price = 750
WHERE Title = 'Advanced SQL';
Question 2
Using the same bookstore schema from Set 1, answer the following:
Solution
1. Total number of books:
Question 3
1. List the titles of all books along with the name of their authors.
2. Show the total number of books written by each author.
3. Retrieve all books by authors from the United States.
Solution
SELECT Books.Title
FROM Books
JOIN Authors ON Books.AuthorID = Authors.AuthorID
WHERE Authors.Country = 'United States';
Question 4
1. Find the title and total sales quantity of each book sold.
2. List all authors who have sold more than 50 copies of any book.
3. Find the book with the highest sales quantity.
Solution
SELECT Books.Title
FROM Books
JOIN Sales ON Books.BookID = Sales.BookID
GROUP BY Books.Title
ORDER BY SUM(Sales.QuantitySold) DESC
LIMIT 1;
Set 5: Advanced SQL Queries with Nested Subqueries and Filtering
Question 5
1. Find the author who has generated the most revenue from book sales (i.e., Price *
QuantitySold).
2. List all books that were sold after January 1, 2023.
3. Find the average price of books for each author, but only include authors who have more
than one book in the database.
Solution
3. Average price of books for each author with more than one book:
1. Insert a new product named "Wireless Mouse" in the "Electronics" category with a price
of 250 and stock of 50 units.
2. Delete all products that are out of stock.
3. Update the price of all products in the "Electronics" category by increasing it by 10%.
4. Find the total number of products in each category.
5. Retrieve the names of all suppliers who supply products in the "Office Supplies"
category.
Solution
UPDATE Products
SET Price = Price * 1.1
WHERE CategoryID = (SELECT CategoryID FROM Categories WHERE
CategoryName = 'Electronics');
1. Insert a new employee named "John Doe" in the "IT" department as a "Developer" with a
salary of 60000.
2. Delete employees who have a salary less than 30000.
3. Update the salary of all employees in the "Marketing" department by giving a 5%
increase.
4. Find the average salary of employees in each department.
5. Retrieve the names of all projects managed by departments with budgets over 500000.
Solution
UPDATE Employees
SET Salary = Salary * 1.05
WHERE DepartmentID = (SELECT DepartmentID FROM Departments
WHERE DepartmentName = 'Marketing');
SELECT Projects.ProjectName
FROM Projects
JOIN Departments ON Projects.DepartmentID =
Departments.DepartmentID
WHERE Projects.Budget > 500000;
1. Insert a new booking for a guest with GuestID 101, booking a "Deluxe" room from
"2024-12-01" to "2024-12-10".
2. Delete all bookings with check-out dates before "2024-01-01".
3. Update the status of all rooms with active bookings to "Occupied".
4. Calculate the total revenue generated from all room bookings.
5. Retrieve the names and emails of all guests who have booked "Suite" rooms.
Solution
UPDATE Rooms
SET Status = 'Occupied'
WHERE RoomID IN (SELECT RoomID FROM Bookings WHERE
CheckOutDate >= CURRENT_DATE);