Dbms Ex5-1
Dbms Ex5-1
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's department.
SELECT employee_number, name FROM employee AS e1
WHERE salary > (SELECT avg(salary)FROM employee
WHERE department = e1.department);
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.
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
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
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.
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