100% found this document useful (1 vote)
361 views

Nested Queries

Nested Query: A query within a query that has another query embedded within it. The subquery (inner query) executes first and the main query (outer query) uses the subquery results. Subqueries typically appear in the WHERE clause and can use operators like =, <, >, IN, and NOT IN to select rows from the outer query based on the subquery results. Examples demonstrate different types of subqueries to retrieve student names based on criteria like minimum marks, course registrations, and multi-level subqueries.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
361 views

Nested Queries

Nested Query: A query within a query that has another query embedded within it. The subquery (inner query) executes first and the main query (outer query) uses the subquery results. Subqueries typically appear in the WHERE clause and can use operators like =, <, >, IN, and NOT IN to select rows from the outer query based on the subquery results. Examples demonstrate different types of subqueries to retrieve student names based on criteria like minimum marks, course registrations, and multi-level subqueries.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Nested Query

Nested Query: A query within a query that has another query embedded within it

Subquery: An Embedded query inside a query

 The subquery (inner query) executes once before the main query (outer query) executes.

 The main query (outer query) use the subquery result.

 subquery typically appears within the WHERE clause of a query

SQL SUBQUERIES EXAMPLES:

We have the following 3 tables: STUDENTS, COURSES AND FACULTY


SID SNAME SMARKS CID CNAME SID FID FNAME CNAME
---------- ---------- ------ ---------- ------- ------- ---------- ----------- ----------
101 LITHU 98 501 DBMS 101 11201 Mahalakshmi DBMS
301 YASHI 75 502 SE 101 11202 sandhya SE
102 SONU 80 502 SE 102
302 CHETHU 75 502 SE 301
103 AKSHU 85 502 SE 302
303 LALLI 72 503 DAA 103

Syntax:

Example 1: Subquery using an unmodified comparision operator (=, <, >)

Query: waq to select the name of the student who have scored minimum marks

Select sname

from stupk

where smarks=(

select min(smarks)

from stupk);

output:

LALLI

Example 2: subquery using the IN operator


These subqueries can return a group of values, but the values must be from a single column of a
table

Query: waq to select the student names who have registered for the courses

Syntax:

select sname

from stupk

where sid in(

select sid

from coursefk );

Output:

Example3: using NOT IN operator

Query: Waq to select the names of the students who have not registered the courses

Syntax:

select sname

from stupk

where sid not in(

select sid

from coursefk );

output:

Example 4. Using Where clause in subquery

Query: Waq to select the names of the student who have registered for the course ‘DBMS’

Syntax:

select sname

from stupk

where sid in(


select sid

from coursefk

where cname=’dbms’);

output:

Example 5. Multilevel subqueries

Query: select the name of the student who have enrolled the course with dbms if the course
existed in faculty table

Syntax:

Select sname

From stupk

Where sid IN(

Select sid

From coursefk

Where cname IN(

Select cname

From faculty

Where cname=’DBMS’ ));

Output:

LITHU

You might also like