DBMS Lab EXP 4 (1)
DBMS Lab EXP 4 (1)
Experiment No. 4
AIM: WRITE SQL QUERIES TO IMPLEMENT JOINS OPERATION
In SQL, a join is a fundamental operation that allows you to combine rows from two or more
tables based on a related column between them. The primary purpose of a join is to extract
and present data that is spread across multiple tables, reflecting the relationships defined in
the relational database schema. By using joins, you can retrieve a unified result set that
integrates data from different sources.
Sailors
Boats
1. Find all information of sailors who have reserved boat number 101.
3. Find the names of sailors who have reserved a red boat, and list in the order of age.
4. Find the names of sailors who have reserved at least one boat.
5. Find the ids and names of sailors who have reserved two different boats on the same
day.
6. Find the ids of sailors who have reserved a red boat or a green boat.
10. Find the average age of sailors for each rating level that has at least two sailors.
1. Find all information of sailors who have reserved boat number 101.
SELECT *
FROM Sailors
WHERE sid IN (
SELECT sid
FROM Reserves
);
SELECT bname
FROM Boats
WHERE bid IN (
SELECT bid
FROM Reserves
WHERE sid = (
SELECT sid
FROM Sailors
);
3. Find the names of sailors who have reserved a red boat, and list in the order of age.
SELECT sname
FROM Sailors
WHERE sid IN (
SELECT r.sid
FROM Reserves r
ORDER BY age;
4. Find the names of sailors who have reserved at least one boat.
FROM Sailors
WHERE sid IN (
SELECT sid
FROM Reserves
);
5. Find the ids and names of sailors who have reserved two different boats on the same day.
FROM Sailors s
JOIN (
FROM Reserves
) r ON s.sid = r.sid;
6. Find the ids of sailors who have reserved a red boat or a green boat.
WHERE bid IN (
SELECT bid
FROM Boats
);
FROM Sailors
LIMIT 1;
FROM Sailors;
FROM Sailors
GROUP BY rating;
10. Find the average age of sailors for each rating level that has at least two sailors.
GROUP BY rating
Books
published_y
book_id title author genre
ear
1 "The Great Gatsby" F. Scott Fitzgerald Fiction 1925
2 "Dune" Frank Herbert Science Fiction 1965
3 "Neuromancer" William Gibson Science Fiction 1984
4 "1984" George Orwell Dystopian 1949
5 "The Catcher in the Rye" J.D. Salinger Fiction 1951
6 "The Martian" Andy Weir Science Fiction 2011
Members
1. Find all information of members who have borrowed the book titled "The Great Gatsby".
2. Find the name of the author of the book borrowed by John Doe.
3. Find the names of members who have borrowed a book of genre 'Science Fiction', and list
in the order of age.
4. Find the names of members who have borrowed at least one book.
5. Find the ids and names of members who have borrowed two different books on the same
day.
6. Find the ids of members who have borrowed a book published in 2020 or 2021.
10. Find the average age of members for each city that has at least two members.
Queries:
1. Find all information of members who have borrowed the book titled "The Great Gatsby".
SELECT *
FROM Members
WHERE member_id IN (
SELECT member_id
FROM Borrowings
WHERE book_id IN (
SELECT book_id
FROM Books
);
2. Find the name of the author of the book borrowed by John Doe.
SELECT author
FROM Books
WHERE book_id IN (
SELECT book_id
FROM Borrowings
WHERE member_id = (
SELECT member_id
FROM Members
);
3. Find the names of members who have borrowed a book of genre 'Science Fiction', and list
in the order of age.
SELECT name
FROM Members
WHERE member_id IN (
SELECT b.member_id
FROM Borrowings b
ORDER BY age;
4. Find the names of members who have borrowed at least one book.
FROM Members
WHERE member_id IN (
SELECT member_id
FROM Borrowings
);
5. Find the ids and names of members who have borrowed two different books on the same
day.
FROM Members m
JOIN (
FROM Borrowings
) b ON m.member_id = b.member_id;
6. Find the ids of members who have borrowed a book published in 2020 or 2021.
FROM Borrowings
WHERE book_id IN (
SELECT book_id
FROM Books
);
FROM Members
LIMIT 1;
FROM Members
GROUP BY city;
10. Find the average age of members for each city that has at least two members.
FROM Members
GROUP BY city
Courses
Enrollments
1. Find all information of students who are enrolled in the course "Introduction to Computer
Science".
3. Find the names of students who are enrolled in courses offered by the "Computer Science"
department, and list them in the order of age.
4. Find the names of students who are enrolled in at least one course.
5. Find the student ids and names of students who are enrolled in two different courses in the
same semester.
6. Find the ids of students who are enrolled in courses with more than 3 credits.
10. Find the average age of students for each major where there are at least two students.
Queries:
1. Find all information of students who are enrolled in the course "Introduction to Computer
Science".
SELECT s.*
FROM Students s
FROM Students s
SELECT s.student_name
FROM Students s
ORDER BY s.age;
4. Find the names of students who are enrolled in at least one course.
FROM Students s
5. Find the student ids and names of students who are enrolled in two different courses in the
same semester.
FROM Students s
JOIN (
FROM Enrollments
) e ON s.student_id = e.student_id;
6. Find the ids of students who are enrolled in courses with more than 3 credits.
FROM Students s
FROM Students s
ORDER BY s.age
LIMIT 1;
FROM Students s;
FROM Students s
GROUP BY s.major;
10. Find the average age of students for each major where there are at least two students.
GROUP BY s.major