SQL Practice set
SQL Practice set
Set B
1. Consider the following relational database: 10 x 1 = 10 Marks
Students (sID, sName, Age, Major, Grade)
Courses (cID, cName, Credits)
Write SQL Statements for the following:
a) Create Students and Courses tables per the above schema. For the Students table, set
18 as a default value for age and ensure that the grade should be in the range of 0 to 4.
Create table students(
sID int primary key,
sName varchar(20),
Age int Default 18,
Major varchar(20),
Grade int check (grade>=0 AND grade<=4)
);
Create table Courses (
cID int primary key,
cName varchar(20),
Credits int
);
b) Insert a record into the student’s table: sID = 1, sName = “Shyam Karki”, Major =
“CS”.
Insert into students (sID, sName,Major) Values (1, ‘Shyam Karki’, ‘CS’);
c) Insert a record into the Courses table: cID = 101, cName = ‘Database Management
System’, Credits = 3.
Insert into courses (cID, cName, Credits) values (101, ‘Database Management
System’, 3);
d) Update the major of the student with Student ID 1 to ‘IT’.
Update table Students set major= ‘IT’ where sID = 1;
e) Retrieve all students whose names start with ‘A’ and end with ‘t’ and are at least 3
characters long.
Select * from students where sName like ‘A__%t’;
f) Display all students who have a 3 GPA.
Select * from students where grade=3;
g) Display the total number of students grouped by Major.
Select Major, Count(*) as total_number_of_students from students group by Major.
h) Delete a course where the course ID is 102.
Delete from Courses where cID=102;
i) Delete the Age column from the student’s table.
Alter table Students
Drop Column Age;
j) Drop the Courses table.
Drop table Courses;
Set C
1. Consider the following relational database: 10 x 1 = 10 Marks
Customers (cID, cName, City, Phone)
Orders (oID, cID, oAmount, oDate)
Products (pID, pName, Price)
Write SQL Statements for the following:
a) Create Customers and Orders tables.
Create table Customers (
cID int primary key,
cName varchar(20),
City Varchar(30),
Phone varchar(10)
);
Create table Orders (
oID int primary key,
cID int,
oAmount int,
oDate date,
foreging key (cID) references Customers(cID)
);
b) Insert a new record into the Customers table with cID = 1, cName = ‘Sita BK’, City =
‘BTL’, Phone = ‘9862030120’.
Insert into customers (cID, cName, City, Phone) values (1, ‘Sita BK’, ‘BTL’,
‘9862030120’);
c) The price of pID = 203 has decreased by 2%. Update the price of the product in the
Products table accordingly.
Update table Products set price = 0.98 * price where pID=203;
d) Alter the data type of the Phone column to NUMBER from VARCHAR in the
Customers table.
Alter table Customers
Modify column phone int;
e) Display the highest order amount.
Select Max(oAmount) from Orders;
f) Display the customer’s name where the second letter is ‘a’ and the name is exactly 4
characters long.
Select cName from Customers where cName like ‘_a__’;
g) Display all orders, sorting them by order amount in descending order.
Select * from Orders order by oAmount ASC;
h) Calculate the total amount of orders.
Select SUM (oAmount) as TotalAmount from Orders;
i) Display the customer’s name and order amount.
Select c.cName, o.oAmount from Customers c, Orders o where c.cID= o.cID;
j) Display the product names along with the number of times each product repeats.
Display the product names along with the number of times each product repeats.
Select pName, Count(*) as ProudctCount from Products Group by pName;
Set D
1. Consider the following relational database: 10 x 1 = 10 Marks
Patients (pID, pName, Age, Disease)
Doctors (dID, dName, Specialization)
Appointments (aID, pID, dID, aDate)
Write SQL Statements for the following:
a) Create the Appointments table as per the above schema.
Create table Appointments(
aID int primary key,
pID int,
dID int,
aDate date,
foreign key (pID) references Patients(pID),
Foreign Key (dID) references Doctors(dID)
);
b) Add a column for Address in the Patients table.
Alter table Patients
Add column Address varchar(30);
c) Insert a new record into the Doctors table.
Insert into Doctors (dID, dName, Specialization) Values (101, ‘Ram Charan’,
‘Cardiology’);
d) Delete the record of Dr. Shiv Narayan Chaudhary.
Delete from Doctors where dName= ‘Dr. Shiv Narayan Chaudhary’;
e) Display the patient name whose name ends with ‘t’ and is at least 4 characters long.
Select pName from Patients where pName like ‘___%t’;
f) List all doctors ordered by doctor name in alphabetical order.
Select * from Doctors order by dName ASC;
g) Display the total count of appointments for each doctor grouped by the doctor ID.
Select dID, Count(*) as Number_of_appointments from Appointments group by dID;
h) Display the patient’s name and appointment date.
Select p.pName, a.aDate from Patients p, Appointments a where p.pID=a.pID;
i) Display all patients who have ‘Chronic’.
Select * from patients where Disease= ‘Chronic’;
j) Drop the Appointments table.
Drop table Appointments;
Set E
1. Consider the following relational database: 10 x 1 = 10 Marks
Books (bID, Title, Author, Price)
Members (mID, mName, MembershipDate)
BorrowRecords (RecordID, mID, bID, BorrowDate, ReturnDate)
Write SQL Statements for the following:
a) Create the BorrowRecords table as per the above schema.
Create table BorrowRecords(
RecordID int primary key,
mID int,
bID int,
BorrowDate Date,
ReturnDate Date,
Foreign Key (mID) references Members(mID),
Foreign Key (bID) references Books(bID)
);
b) Insert a new record into the Books table: bID = 101, Title = ‘Accounting Principle’,
Author = ‘Smith John’, Price = 2000.
Insert into Books (bID, Title, Author, Price) values (101, ‘Accounting Principle’,
‘Smith John’, 2000);
c) Change the data type of bID to VARCHAR from INT.
Alter table Books
Modify Column bID varchar(20);
d) Display all books whose title contains the word ‘Great’.
Select bName from Books where Title = ‘Great’;
e) List all members, ordering them by MembershipDate in ascending order.
SELECT * FROM Members
ORDER BY MembershipDate ASC;
f) Display the member’s name and membership date for the member whose membership
ID is 30001.
SELECT mName, membershipDate
FROM Members
WHERE mID = 30001;
g) Display the unique name from books table that occur only once.
SELECT bName
FROM Books
GROUP BY bName
HAVING COUNT(*) = 1; or logic can be COUNT(*) <2;
h) Delete the details of member ‘Hari Kumar Bhandari’.
DELETE FROM Members
WHERE mName = 'Hari Kumar Bhandari';
i) Display the record id and member name by joining the members and BorrowRecords
tables.
SELECT m.memberID, m.memberName
FROM Members m
JOIN BorrowRecords b ON m.memberID = b.memberID;
j) Drop the table members.
Drop table Members;
Set F
1. Consider the following relational database: 10 x 1 = 10 Marks
Guests (gID, gName, Phone, Email)
Rooms (rID, RoomType, PricePerNight)
Bookings (bID, gID, rID, CheckInDate, CheckOutDate)
Write SQL Statements for the following:
a) Create the Bookings table as per the above schema.
CREATE TABLE Bookings (
bID INT PRIMARY KEY,
gID INT,
rID INT,
CheckInDate DATE,
CheckOutDate DATE,
FOREIGN KEY (gID) REFERENCES Guests(gID),
FOREIGN KEY (rID) REFERENCES Rooms(rID)
);
b) Insert a new record into the Bookings table: BookingID = '200A', GuestID = 1001,
RoomID = 405, CheckInDate = ‘2024-04-21’, and CheckOutDate = ‘2024-04-22’.
INSERT INTO Bookings (bID, gID, rID, CheckInDate, CheckOutDate)
VALUES ('200A', 1001, 405, '2024-04-21', '2024-04-22');
c) Change the data type of PricePerNight to NUMERIC from INT.
ALTER TABLE Rooms
MODIFY COLUMN PricePerNight NUMERIC(10,2);
d) Display all guest names whose names are exactly 4 characters long.
SELECT gName
FROM Guests
WHERE LENGTH(gName) = 4;
OR
SELECT gName
FROM Guests
WHERE gName LIKE ‘____’;
e) List all rooms, ordering them by price in descending order.
SELECT *
FROM Rooms
ORDER BY PricePerNight DESC;
f) Display the email of the guest whose guest ID is 1010.
SELECT Email
FROM Guests
WHERE gID = 1010;
g) Drop the Rooms table.
DROP TABLE Rooms;
h) Display the room type and check-in date from the Bookings and Rooms tables.
SELECT r.RoomType, b.CheckInDate
FROM Bookings b
JOIN Rooms r ON b.rID = r.rID;
i) Delete the booking record whose guest id is 1030.
DELETE FROM Bookings
WHERE gID = 1030;
j) Display the total number of bookings for each guest grouped by guest id.
SELECT gID, COUNT(*) AS TotalBookings
FROM Bookings
GROUP BY gID;