0% found this document useful (0 votes)
2 views13 pages

Nested Queries

The document contains SQL queries for retrieving various student and hostel information from a database. It includes queries to find students in hostels with vacant rooms, those not in a specific hostel, and those with or without visitors. Additionally, it includes queries related to hostel capacities and student contact numbers.

Uploaded by

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

Nested Queries

The document contains SQL queries for retrieving various student and hostel information from a database. It includes queries to find students in hostels with vacant rooms, those not in a specific hostel, and those with or without visitors. Additionally, it includes queries related to hostel capacities and student contact numbers.

Uploaded by

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

1.

Using IN / NOT IN
✅ Find students who live in hostels that have at least one vacant room
SELECT student_name

FROM student

WHERE room_no IN (

SELECT room_no FROM room WHERE hostel_no IN (

SELECT hostel_no FROM hostel WHERE vacant_rooms_no > 0

);
Find students who live in hostels that are NOT full

SELECT student_name

FROM student
WHERE room_no IN (

SELECT room_no

FROM room

WHERE hostel_no NOT IN (

SELECT hostel_no FROM hostel

WHERE capacity = (SELECT COUNT(*) FROM room WHERE room.hostel_no = hostel.hostel_no)

);
Find students who are NOT in hostel 'AJAD HOSTEL'

SELECT student_name

FROM student

WHERE room_no IN (
SELECT room_no

FROM room

WHERE hostel_no NOT IN (

SELECT hostel_no FROM hostel WHERE hostel_name = 'AJAD HOSTEL'

);
Find hostels that have a capacity greater than at least one hostel in Mumbai
SELECT hostel_name

FROM hostel

WHERE capacity > SOME (

SELECT capacity FROM hostel

WHERE hostel_no IN (

SELECT hostel_no FROM staff WHERE city = 'Mumbai'

);

Find hostels that have a capacity greater than all hostels in Pune

SELECT hostel_name

FROM hostel

WHERE capacity > ALL (

SELECT capacity FROM hostel

WHERE hostel_no IN (
SELECT hostel_no FROM staff WHERE city = 'Pune'

);

Find students who have at least one visitor

SELECT student_name

FROM student S

WHERE EXISTS (

SELECT * FROM visitor V WHERE V.student_id = S.student_id

);
Find students who do NOT have any visitors

SELECT student_name

FROM student S

WHERE NOT EXISTS (

SELECT * FROM visitor V WHERE V.student_id = S.student_id

);
Find all students who have paid rent EXCEPT those staying in AC rooms

SELECT student_name

FROM student

WHERE room_no IN (

SELECT room_no FROM room WHERE rent_status = 'paid'

AND room_no NOT IN (

SELECT room_no FROM room WHERE room_type = 'AC'

);
Find students who have at most one registered contact number

SELECT S.student_name

FROM student S

LEFT JOIN student_contact SC ON S.student_id = SC.student_id

GROUP BY S.student_id

HAVING COUNT(SC.contact_no) <= 1;


Find students who have at least two registered contact numbers
SELECT S.student_name

FROM student S

LEFT JOIN student_contact SC ON S.student_id = SC.student_id

GROUP BY S.student_id

HAVING COUNT(SC.contact_no) >= 2;

You might also like