0% found this document useful (0 votes)
10 views8 pages

Dbms Ex5-1

Uploaded by

imviswanthanss
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 views8 pages

Dbms Ex5-1

Uploaded by

imviswanthanss
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/ 8

20CS2016L-Database Management System Lab URK22CS7008

Ex no: 05 SUBQUERIES AND CORRELATED


SUBQUERIES
Date 10-09-2024

AIM:

To execute given query using the concept of subqueries and correlated subqueries.

DESCRIPTION:

Subquery is usually added in the WHERE Clause of the sql statement. Most of
the time, a subquery is used when you know how to search for a value using a SELECT
statement, but do not know the exact value. Subqueries are an alternate way of
returning data from multiple tables.
Subqueries can be used with the following sql statements along with the comparison
operators like =, <, >, <=, >=, etc.
• Update
• Insert
• Select
• Delete

Correlated sub-query
A correlated sub-query is a term used for specific types of queries in SQL in computer
databases. It is a sub-query (a query nested inside another query) that uses values from
the outer query in its WHERE clause. The sub-query is evaluated once for each row
processed by the outer query.

Detailed Procedure:
Here is an example for a typical correlated sub-query. In this example we are finding the
list of employees (employee number and names) having more salary than the average
salary of all employees in that employee&#39;s department.
SELECT employee_number, name FROM employee AS e1
WHERE salary > (SELECT avg(salary)FROM employee
WHERE department = e1.department);

In the above query the outer query is,

SELECT employee_number, name FROM employee AS e1


WHERE salary >
And the inner query is,
(SELECT avg(salary)
FROM employee
WHERE department = e1.department);
In the above nested query the inner query has to be executed for every
23
20CS2016L-Database Management System Lab URK22CS7008

employee as the department will change for every row. Hence the average salary will
also change. The effect of correlated sub-queries can also be obtained using outer
Joins.

QUERIES AND OUTPUT SCREENSHOT:

Sub Query

1. List all users who have made reservations for events that are taking place in a specific venue
(e.g., “USA”).

24
20CS2016L-Database Management System Lab URK22CS7008

2. Find the events with the highest ticket prices.

3. Find the total number of tickets reserved for a specific event.

4. List the users who have made reservations with a total cost exceeding a certain amount (e.g.,
50).

25
20CS2016L-Database Management System Lab
URK22CS7008

5. Retrieve the events where the number of reservations exceeds a certain threshold.

6. Find all users who have made more reservations than the average number of reservations
across all users.

26
20CS2016L-Database Management System Lab
URK22CS7008

Correlated Sub Query

7. List all events where the total ticket price of reservations exceeds a certain amount.

27
20CS2016L-Database Management System Lab
URK22CS7008

8. Find the users who have made reservations for more than one event.

9. Retrieve the events with the highest number of reservations.

28
20CS2016L-Database Management System Lab
URK22CS7008

10. For each event, find the number of reservations made by users

29
20CS2016L-Database Management System Lab URK22CS7008

11. Find the events for which the total ticket price of reservations exceeds the average total ticket
price for all events.

12. List users who have made reservations for multiple events on the same day.

RESULT:
The concept of subqueries and correlated subqueries has been successfully executed for the given questions.

30

You might also like