0% found this document useful (0 votes)
15 views5 pages

Raj DBMS 5

The document outlines a lab exercise focused on executing SQL queries using subqueries and correlated subqueries to retrieve consolidated data. It explains the definitions, syntax, and applications of both types of queries, providing examples for clarity. Additionally, it lists specific questions for students to answer using these querying techniques.
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)
15 views5 pages

Raj DBMS 5

The document outlines a lab exercise focused on executing SQL queries using subqueries and correlated subqueries to retrieve consolidated data. It explains the definitions, syntax, and applications of both types of queries, providing examples for clarity. Additionally, it lists specific questions for students to answer using these querying techniques.
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/ 5

23CS2014 Database Management Systems Lab URK23CS7040

Ex. No: 05 SUB QUERY AND CORRELATED SUB QUERY


Date

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

SELECT <column1>, <column2>,

(SELECT <column_name> FROM <table_name2> WHERE <column_name2> =

<table_name1>.<column_name>) AS <alias_name>

FROM <table_name1>;

2: Subquery in WHERE clause


SELECT <column1>, <column2>
FROM <table_name1>

WHERE <column_name> = (SELECT <column_name> FROM <table_name2>


WHERE
<column_name2> = '<some_value>');

3: Subquery in HAVING clause


23CS2014 Database Management Systems Lab URK23CS7040

SELECT <column_name>, AVG(<column_name>)

FROM <table_name1>
GROUP BY <column_name>

HAVING AVG(<column_name>) > (SELECT AVG(<column_name>) FROM

<table_name2> WHERE <column_name2> = <some_value>);

2. Correlated Subqueries:

A correlated subquery is a subquery that references a column from the outer query. It is s

evaluated once for each row processed by the outer query.


1: Correlated subquery with WHERE clause

SELECT <column1>, <column2>

FROM <table_name1> t1

WHERE <column_name> > (SELECT AVG(<column_name>)

FROM <table_name2>
WHERE <column_name2> = t1.<column_name>);

2: Correlated subquery with SELECT clause

SELECT <column1>, <column2>,


(SELECT MAX(<column_name>) FROM <table_name2> WHERE
<column_name2> =
t1.<column_name>) AS <alias_name>
FROM <table_name1> t1;

3: Correlated subquery in HAVING clause

SELECT <column_name>, AVG(<column_name>)

FROM <table_name1> t1
GROUP BY <column_name>

HAVING AVG(<column_name>) > (SELECT AVG(<column_name>)


FROM <table_name2>
WHERE <column_name2> = t1.<column_name>);
23CS2014 Database Management Systems Lab URK23CS7040

Questions

Sub Query

1. List all users who have made reservations for events that are taking place in a specific venue

(e.g., "USA").

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

Correlated Sub Query

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.

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

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.

You might also like