Raj DBMS 5
Raj DBMS 5
Aim
To execute the given SQL queries to retrieve consolidated data by using sub-queries and correlated sub
queries.
Description
Subqueries
A subquery is a query that's nested within another query. Subqueries can be used in the
SELECT, WHERE, or HAVING clauses. They can return a single value or multiple rows or columns.
Subqueries can be used to build complex statements from simpler ones.
Correlated subqueries
A correlated subquery is a subquery that uses values from the outer query in the inner query.
Correlated subqueries are used when a subquery needs to return a different result for each row
considered by the main query. For example, you can use a correlated subquery to find employees
who earn more than their department's average salary.
Syntax for Subqueries and Correlated Subqueries
1. Subqueries:
A subquery is a query inside another query. It can be used in the SELECT, WHERE, or
HAVING clauses.
1: Subquery in SELECT clause
<table_name1>.<column_name>) AS <alias_name>
FROM <table_name1>;
FROM <table_name1>
GROUP BY <column_name>
2. Correlated Subqueries:
A correlated subquery is a subquery that references a column from the outer query. It is s
FROM <table_name1> t1
FROM <table_name2>
WHERE <column_name2> = t1.<column_name>);
FROM <table_name1> t1
GROUP BY <column_name>
Questions
Sub Query
1. List all users who have made reservations for events that are taking place in a specific venue
(e.g., "USA").
4. List the users who have made reservations with a total cost exceeding a certain amount (e.g.,
50).
23CS2014 Database Management Systems Lab URK23CS7040
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.
7. List all events where the total ticket price of reservations exceeds a certain amount.
8. Find the users who have made reservations for more than one event.
10. For each event, find the number of reservations made by users
23CS2014 Database Management Systems Lab URK23CS7040
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:
Thus the Above Query to Understand sub-queries and correlated sub queries is Written , Executed and the
Output is Verified.