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

DBMS Lab EXP 4 (1)

The document outlines a series of SQL experiments focused on database management systems, specifically implementing join operations across different schemas including Sailors, Boats, Reserves, Books, Members, and Students. Each experiment includes a set of SQL queries designed to extract and manipulate data from these schemas, demonstrating various join techniques and aggregate functions. The document serves as a practical guide for students to understand and apply SQL joins in real-world database scenarios.

Uploaded by

deviladode2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

DBMS Lab EXP 4 (1)

The document outlines a series of SQL experiments focused on database management systems, specifically implementing join operations across different schemas including Sailors, Boats, Reserves, Books, Members, and Students. Each experiment includes a set of SQL queries designed to extract and manipulate data from these schemas, demonstrating various join techniques and aggregate functions. The document serves as a practical guide for students to understand and apply SQL joins in real-world database scenarios.

Uploaded by

deviladode2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

B.

TECH 5th CSE

DATABASE MANAGEMENT SYSTEM LAB

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.

Experiment No. 4.1

Consider the following schema:

Sailors (sid, sname, rating, age)

Boats (bid, bname, color)

Reserves (sid, bid, day(date))

Sailors

sid sname rating age


1 Alice 10 25
2 Bob 8 30
3 Carol 10 28
4 Dave 7 22
5 Eve 8 35

Boats

bid bname color


101 "Red Racer" Red
102 "Blue Wave" Blue
103 "Green Machine" Green
104 "Yellow Submarine" Yellow
Reserves

sid bid day


1 101 2024-07-01
2 102 2024-07-02
2 103 2024-07-02
3 101 2024-07-03
4 101 2024-07-04
4 104 2024-07-04
5 103 2024-07-05

1. Find all information of sailors who have reserved boat number 101.

2. Find the name of boat reserved by Bob.

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.

7. Find the name and the age of the youngest sailor.

8. Count the number of different sailor names.

9. Find the average age of sailors for each rating level.

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

WHERE bid = 101

);

2. Find the name of the boat reserved by Bob.

SELECT bname

FROM Boats

WHERE bid IN (

SELECT bid

FROM Reserves

WHERE sid = (

SELECT sid

FROM Sailors

WHERE sname = 'Bob'

);

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

JOIN Boats b ON r.bid = b.bid


WHERE b.color = 'red'

ORDER BY age;

4. Find the names of sailors who have reserved at least one boat.

SELECT DISTINCT sname

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.

SELECT s.sid, s.sname

FROM Sailors s

JOIN (

SELECT sid, day(date)

FROM Reserves

GROUP BY sid, day(date)

HAVING COUNT(DISTINCT bid) >= 2

) r ON s.sid = r.sid;

6. Find the ids of sailors who have reserved a red boat or a green boat.

SELECT DISTINCT sid


FROM Reserves

WHERE bid IN (

SELECT bid

FROM Boats

WHERE color IN ('red', 'green')

);

7. Find the name and the age of the youngest sailor.

SELECT sname, age

FROM Sailors

ORDER BY age ASC

LIMIT 1;

8. Count the number of different sailor names.

SELECT COUNT(DISTINCT sname) AS num_sailors

FROM Sailors;

9. Find the average age of sailors for each rating level.

SELECT rating, AVG(age) AS avg_age

FROM Sailors

GROUP BY rating;

10. Find the average age of sailors for each rating level that has at least two sailors.

SELECT rating, AVG(age) AS avg_age


FROM Sailors

GROUP BY rating

HAVING COUNT(*) >= 2;

Experiment No. 4.2


Library Database

Assume the following schema for a library database:

Books (book_id, title, author, genre, published_year)

Members (member_id, name, age, city)

Borrowings (borrow_id, member_id, book_id, borrow_date)

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

member_id name age city


1 John Doe 30 New York
2 Jane Smith 45 Los Angeles
3 Alice Brown 22 New York
4 Bob White 37 Chicago
5 Carol Black 29 New York
6 Eve Davis 31 Los Angeles
Borrowings

borrow_id member_id book_id borrow_date


1 1 1 2024-07-01
2 2 2 2024-07-02
3 1 3 2024-07-03
4 3 4 2024-07-01
5 4 5 2024-07-04
6 2 6 2024-07-05
7 5 2 2024-07-01
8 6 4 2024-07-02

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.

7. Find the name and the age of the youngest member.

8. Count the number of different genres of books in the library.

9. Find the average age of members for each city.

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

WHERE title = 'The Great Gatsby'

);

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

WHERE name = 'John Doe'

);
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

JOIN Books bk ON b.book_id = bk.book_id

WHERE bk.genre = 'Science Fiction'

ORDER BY age;

4. Find the names of members who have borrowed at least one book.

SELECT DISTINCT name

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.

SELECT m.member_id, m.name

FROM Members m
JOIN (

SELECT member_id, borrow_date

FROM Borrowings

GROUP BY member_id, borrow_date

HAVING COUNT(DISTINCT book_id) >= 2

) b ON m.member_id = b.member_id;

6. Find the ids of members who have borrowed a book published in 2020 or 2021.

SELECT DISTINCT member_id

FROM Borrowings

WHERE book_id IN (

SELECT book_id

FROM Books

WHERE published_year IN (2020, 2021)

);

7. Find the name and the age of the youngest member.

SELECT name, age

FROM Members

ORDER BY age ASC

LIMIT 1;

8. Count the number of different genres of books in the library.

SELECT COUNT(DISTINCT genre) AS num_genres


FROM Books;

9. Find the average age of members for each city.

SELECT city, AVG(age) AS avg_age

FROM Members

GROUP BY city;

10. Find the average age of members for each city that has at least two members.

SELECT city, AVG(age) AS avg_age

FROM Members

GROUP BY city

HAVING COUNT(*) >= 2;

Experiment No. 4.3


University Database

Assume the following schema for a university database:

Students (student_id, student_name, age, major, admission_year)

Courses (course_id, course_name, department, credits)

Enrollments (enrollment_id, student_id, course_id, semester, grade)


Students

student_id student_name age major admission_year


1 John Smith 20 Computer Science 2022
2 Jane Doe 22 Mathematics 2021
3 Alice Brown 21 Computer Science 2022
4 Bob White 23 Engineering 2020
5 Carol Black 19 Mathematics 2023
6 Eve Davis 22 Computer Science 2021

Courses

course_id course_name department credits


101 "Introduction to Computer Science" Computer Science 4
102 "Calculus I" Mathematics 3
103 "Data Structures" Computer Science 4
104 "Thermodynamics" Engineering 3
105 "Advanced Calculus" Mathematics 4

Enrollments

enrollment_id student_id course_id semester grade


1 1 101 Fall 2023 A
2 1 103 Fall 2023 B
3 2 102 Spring 2024 A
4 3 101 Fall 2023 C
5 4 105 Spring 2024 B
6 5 102 Fall 2023 A
7 6 103 Fall 2023 B
8 6 101 Fall 2023 A

1. Find all information of students who are enrolled in the course "Introduction to Computer
Science".

2. Find the department of the course enrolled by John Smith.

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.

7. Find the name and the age of the youngest student.

8. Count the number of different majors in the university.

9. Find the average age of students for each major.

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

JOIN Enrollments e ON s.student_id = e.student_id

JOIN Courses c ON e.course_id = c.course_id

WHERE c.course_name = 'Introduction to Computer Science';

2. Find the department of the course enrolled by John Smith.

SELECT DISTINCT c.department

FROM Students s

JOIN Enrollments e ON s.student_id = e.student_id

JOIN Courses c ON e.course_id = c.course_id

WHERE s.student_name = 'John Smith';


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.

SELECT s.student_name

FROM Students s

JOIN Enrollments e ON s.student_id = e.student_id

JOIN Courses c ON e.course_id = c.course_id

WHERE c.department = 'Computer Science'

ORDER BY s.age;

4. Find the names of students who are enrolled in at least one course.

SELECT DISTINCT s.student_name

FROM Students s

JOIN Enrollments e ON s.student_id = e.student_id;

5. Find the student ids and names of students who are enrolled in two different courses in the
same semester.

SELECT s.student_id, s.student_name

FROM Students s

JOIN (

SELECT student_id, semester

FROM Enrollments

GROUP BY student_id, semester

HAVING COUNT(DISTINCT course_id) >= 2

) e ON s.student_id = e.student_id;
6. Find the ids of students who are enrolled in courses with more than 3 credits.

SELECT DISTINCT s.student_id

FROM Students s

JOIN Enrollments e ON s.student_id = e.student_id

JOIN Courses c ON e.course_id = c.course_id

WHERE c.credits > 3;

7. Find the name and the age of the youngest student.

SELECT s.student_name, s.age

FROM Students s

ORDER BY s.age

LIMIT 1;

8. Count the number of different majors in the university.

SELECT COUNT(DISTINCT s.major) AS num_majors

FROM Students s;

9. Find the average age of students for each major.

SELECT s.major, AVG(s.age) AS avg_age

FROM Students s

GROUP BY s.major;

10. Find the average age of students for each major where there are at least two students.

SELECT s.major, AVG(s.age) AS avg_age


FROM Students s

GROUP BY s.major

HAVING COUNT(*) >= 2;

You might also like