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

Practice-set-SQL

SQL

Uploaded by

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

Practice-set-SQL

SQL

Uploaded by

roshannath214
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Set 1: Basic Operations with INSERT, DELETE, and UPDATE

Question 1

Consider the following tables for a bookstore database:

• Books: BookID, Title, AuthorID, Price, Quantity


• Authors: AuthorID, AuthorName, Country

Using this schema, perform the following tasks:

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

1. Insert a new book:

INSERT INTO Books (BookID, Title, AuthorID, Price,


Quantity)
VALUES (new_id, 'SQL Basics', 3, 500, 10);

2. Delete all books with quantity zero:

DELETE FROM Books


WHERE Quantity = 0;

3. Update the price of "Advanced SQL":

UPDATE Books
SET Price = 750
WHERE Title = 'Advanced SQL';

Set 2: Aggregate Functions with COUNT, SUM, and AVG

Question 2

Using the same bookstore schema from Set 1, answer the following:

1. Find the total number of books in the store.


2. Calculate the total value of all books (i.e., Price * Quantity for each book).
3. Find the average price of books written by the author with AuthorID 2.

Solution
1. Total number of books:

SELECT COUNT(*) AS TotalBooks


FROM Books;

2. Total value of all books:

SELECT SUM(Price * Quantity) AS TotalValue


FROM Books;

3. Average price of books by AuthorID 2:

SELECT AVG(Price) AS AveragePrice


FROM Books
WHERE AuthorID = 2;

Set 3: Basic JOIN Queries

Question 3

Using the bookstore schema, answer the following:

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

1. List of books with author names:

SELECT Books.Title, Authors.AuthorName


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

2. Total number of books by each author:

SELECT Authors.AuthorName, COUNT(Books.BookID) AS


TotalBooks
FROM Authors
LEFT JOIN Books ON Authors.AuthorID = Books.AuthorID
GROUP BY Authors.AuthorName;

3. Books by authors from the United States:

SELECT Books.Title
FROM Books
JOIN Authors ON Books.AuthorID = Authors.AuthorID
WHERE Authors.Country = 'United States';

Set 4: Complex Queries with Subqueries and Conditional Aggregates

Question 4

Consider a modified bookstore schema with an additional Sales table:

• Sales: SaleID, BookID, QuantitySold, SaleDate

Using this schema, answer the following:

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

1. Title and total sales quantity of each book:

SELECT Books.Title, SUM(Sales.QuantitySold) AS TotalSales


FROM Books
JOIN Sales ON Books.BookID = Sales.BookID
GROUP BY Books.Title;

2. Authors with more than 50 copies sold of any book:

SELECT DISTINCT Authors.AuthorName


FROM Authors
JOIN Books ON Authors.AuthorID = Books.AuthorID
JOIN Sales ON Books.BookID = Sales.BookID
GROUP BY Authors.AuthorName, Books.BookID
HAVING SUM(Sales.QuantitySold) > 50;

3. Book with the highest sales quantity:

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

Using the same bookstore schema (Books, Authors, Sales):

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

1. Author with the highest revenue:

SELECT Authors.AuthorName, SUM(Books.Price *


Sales.QuantitySold) AS TotalRevenue
FROM Authors
JOIN Books ON Authors.AuthorID = Books.AuthorID
JOIN Sales ON Books.BookID = Sales.BookID
GROUP BY Authors.AuthorName
ORDER BY TotalRevenue DESC
LIMIT 1;

2. Books sold after January 1, 2023:

SELECT DISTINCT Books.Title


FROM Books
JOIN Sales ON Books.BookID = Sales.BookID
WHERE Sales.SaleDate > '2023-01-01';

3. Average price of books for each author with more than one book:

SELECT Authors.AuthorName, AVG(Books.Price) AS AveragePrice


FROM Authors
JOIN Books ON Authors.AuthorID = Books.AuthorID
GROUP BY Authors.AuthorName
HAVING COUNT(Books.BookID) > 1;

Set 6: Inventory Management System


Consider the following tables for an inventory management system:

• Products: ProductID, ProductName, CategoryID, Price, Stock


• Categories: CategoryID, CategoryName
• Suppliers: SupplierID, SupplierName, Contact

Using this schema, answer the following:

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

1. Insert a new product:

INSERT INTO Products (ProductID, ProductName, CategoryID,


Price, Stock)
VALUES (new_id, 'Wireless Mouse', (SELECT CategoryID FROM
Categories WHERE CategoryName = 'Electronics'), 250, 50);

2. Delete all out-of-stock products:

DELETE FROM Products


WHERE Stock = 0;

3. Update the price of products in "Electronics" category by 10%:

UPDATE Products
SET Price = Price * 1.1
WHERE CategoryID = (SELECT CategoryID FROM Categories WHERE
CategoryName = 'Electronics');

4. Total number of products in each category:

SELECT Categories.CategoryName, COUNT(Products.ProductID)


AS TotalProducts
FROM Products
JOIN Categories ON Products.CategoryID =
Categories.CategoryID
GROUP BY Categories.CategoryName;
5. Names of suppliers who supply products in "Office Supplies" category:

SELECT DISTINCT Suppliers.SupplierName


FROM Suppliers
JOIN Products ON Suppliers.SupplierID = Products.SupplierID
JOIN Categories ON Products.CategoryID =
Categories.CategoryID
WHERE Categories.CategoryName = 'Office Supplies';

Set 7: Employee Management System

Consider the following tables for an employee management system:

• Employees: EmployeeID, Name, Position, Salary, DepartmentID


• Departments: DepartmentID, DepartmentName, ManagerID
• Projects: ProjectID, ProjectName, DepartmentID, Budget

Using this schema, answer the following:

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

1. Insert a new employee:

INSERT INTO Employees (EmployeeID, Name, Position, Salary,


DepartmentID)
VALUES (new_id, 'John Doe', 'Developer', 60000, (SELECT
DepartmentID FROM Departments WHERE DepartmentName =
'IT'));

2. Delete employees with salary less than 30000:

DELETE FROM Employees


WHERE Salary < 30000;

3. Update salary of employees in "Marketing" department by 5%:

UPDATE Employees
SET Salary = Salary * 1.05
WHERE DepartmentID = (SELECT DepartmentID FROM Departments
WHERE DepartmentName = 'Marketing');

4. Average salary of employees in each department:

SELECT Departments.DepartmentName, AVG(Employees.Salary) AS


AverageSalary
FROM Employees
JOIN Departments ON Employees.DepartmentID =
Departments.DepartmentID
GROUP BY Departments.DepartmentName;

5. Projects managed by departments with budgets over 500000:

SELECT Projects.ProjectName
FROM Projects
JOIN Departments ON Projects.DepartmentID =
Departments.DepartmentID
WHERE Projects.Budget > 500000;

Set 8: Hotel Booking System

Consider the following tables for a hotel booking system:

• Guests: GuestID, GuestName, Phone, Email


• Rooms: RoomID, RoomType, Price, Status
• Bookings: BookingID, GuestID, RoomID, CheckInDate, CheckOutDate

Using this schema, answer the following:

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

1. Insert a new booking:

INSERT INTO Bookings (BookingID, GuestID, RoomID,


CheckInDate, CheckOutDate)
VALUES (new_id, 101, (SELECT RoomID FROM Rooms WHERE
RoomType = 'Deluxe' AND Status = 'Available' LIMIT 1),
'2024-12-01', '2024-12-10');

2. Delete bookings with check-out dates before "2024-01-01":

DELETE FROM Bookings


WHERE CheckOutDate < '2024-01-01';

3. Update status of rooms with active bookings to "Occupied":

UPDATE Rooms
SET Status = 'Occupied'
WHERE RoomID IN (SELECT RoomID FROM Bookings WHERE
CheckOutDate >= CURRENT_DATE);

4. Total revenue generated from room bookings:

SELECT SUM(Rooms.Price * DATEDIFF(CheckOutDate,


CheckInDate)) AS TotalRevenue
FROM Bookings
JOIN Rooms ON Bookings.RoomID = Rooms.RoomID;

5. Names and emails of guests who booked "Suite" rooms:

SELECT Guests.GuestName, Guests.Email


FROM Guests
JOIN Bookings ON Guests.GuestID = Bookings.GuestID
JOIN Rooms ON Bookings.RoomID = Rooms.RoomID
WHERE Rooms.RoomType = 'Suite';

You might also like