0% found this document useful (0 votes)
6 views3 pages

Exp 7

The document outlines Experiment No. 7 for a DBMS Lab course, focusing on performing nested and complex SQL queries. It explains the concept of subqueries, their rules, and provides examples using a schema with students, teachers, and classes. The conclusion states that nested and complex queries were successfully implemented on the specified system.

Uploaded by

abhisheksali18
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)
6 views3 pages

Exp 7

The document outlines Experiment No. 7 for a DBMS Lab course, focusing on performing nested and complex SQL queries. It explains the concept of subqueries, their rules, and provides examples using a schema with students, teachers, and classes. The conclusion states that nested and complex queries were successfully implemented on the specified system.

Uploaded by

abhisheksali18
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/ 3

Subject: DBMS Lab Class: SE-Data Science

Semester: IV A.Y. 2024-2025


Experiment No. 7

 Aim: To perform Nested and Complex queries on the given schema for a specified
system
 Theory:

Subquery:

A Subquery or Inner query or a Nested query is a query within another SQL query and
embedded within the WHERE clause.

A subquery is used to return data that will be used in the main query as a condition to further
restrict the data to be retrieved.

Subqueries can be used with the SELECT, INSERT, UPDATE, and DELETE statements along
with the operators like =, <, >, >=, <=, IN, BETWEEN, etc.

There are a few rules that subqueries must follow −

 Subqueries must be enclosed within parentheses.


 A subquery can have only one column in the SELECT clause, unless multiple columns
are in the main query for the subquery to compare its selected columns.
 An ORDER BY command cannot be used in a subquery, although the main query can
use an ORDER BY. The GROUP BY command can be used to perform the same
function as the ORDER BY in a subquery.
 Subqueries that return more than one row can only be used with multiple value operators
such as the IN operator.
 A subquery cannot be immediately enclosed in a set function.
 The BETWEEN operator cannot be used with a subquery. However, the BETWEEN
operator can be used within the subquery.

Subqueries with the SELECT Statement

Subqueries are most frequently used with the SELECT statement. The basic syntax is as
follows:

SELECT column_name [, column_name ]


FROM table1 [, table2 ]

WHERE column_name OPERATOR

(SELECT column_name [, column_name ]

FROM table1 [, table2 ]

[WHERE])

A nested SELECT is a query within a query, i.e. when you have a SELECT statement within
the main SELECT. To make the concept clearer, let’s go through an example together.

The database includes three tables: students, teachers, and classes

Students

id name class_id GPA

1 Jack Black 3 3.45

2 Daniel White 1 3.15

3 Kathrine Star 1 3.85

4 Helen Bright 2 3.10

5 Steve May 2 2.40

Teachers

id name subject class_id monthly_salary

1 Elisabeth Grey History 3 2,500

2 Robert Sun Literature [NULL] 2,000

3 John Churchill English 1 2,350

4 Sara Parker Math 2 3,000


Classes

id grade teacher_id number_of_students

1 10 3 21

2 11 4 25

3 12 1 28

SELECT * FROM students

WHERE GPA > ( SELECT AVG(GPA) FROM students);

id name class_id GPA

1 Jack Black 3 3.45

3 Kathrine Star 1 3.85

SELECT AVG(number_of_students)

FROM classes

WHERE teacher_id IN (

SELECT id

FROM teachers

WHERE subject = 'English' OR subject = 'History');

Conclusion:

Hence, we implemented nested and complex queries on a specified system.

You might also like