0% found this document useful (0 votes)
10 views8 pages

Sub Queries

Uploaded by

Rajitha Natti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views8 pages

Sub Queries

Uploaded by

Rajitha Natti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Sub Queries

A subquery is a SQL query nested inside a larger query.

 A subquery may occur in :

o - A SELECT clause

o - A FROM clause

o - A WHERE clause

 The subquery can be nested inside a SELECT, INSERT, UPDATE, or DELETE


statement or inside another subquery.

 A subquery is usually added within the WHERE Clause of another SQL SELECT
statement.

 You can use the comparison operators, such as >, <, or =. The comparison operator
can also be a multiple-row operator, such as IN, ANY, or ALL.

 A subquery is also called an inner query or inner select, while the statement
containing a subquery is also called an outer query or outer select.

 The inner query executes first before its parent query so that the results of an inner
query can be passed to the outer query.

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

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


Case 1
In this section, you will learn the requirements of using subqueries. We have the following
two tables 'student' and 'marks' with common field 'StudentID'
student marks

Now we want to write a query to identify all students who get better marks than that of the
student who's StudentID is 'V002', but we do not know the marks of 'V002'.
- To solve the problem, we require two queries. One query returns the marks (stored in
Total_marks field) of 'V002' and a second query identifies the students who get better marks
than the result of the first query.

SELECT *
FROM `marks`
WHERE studentid = 'V002';
Query result:

The result of the query is 80.


- Using the result of this query, here we have written another query to identify the students
who get better marks than 80. Here is the query :

SELECT a.studentid, a.name, b.total_marks


FROM student a, marks b
WHERE a.studentid = b.studentid
AND b.total_marks >80;
Query result:
Above two queries identified students who get the better number than the student who's
StudentID is 'V002' (Abhay).

You can combine the above two queries by placing one query inside the other. The subquery
(also called the 'inner query') is the query inside the parentheses. See the following code and
query result :

SELECT a.studentid, a.name, b.total_marks


FROM student a, marks b
WHERE a.studentid = b.studentid AND b.total_marks >
(SELECT total_marks
FROM marks
WHERE studentid = 'V002');
Subqueries: Guidelines

There are some guidelines to consider when using subqueries :

 A subquery must be enclosed in parentheses.

 A subquery must be placed on the right side of the comparison operator.

 Subqueries cannot manipulate their results internally, therefore ORDER BY clause


cannot be added into a subquery. You can use an ORDER BY clause in the main
SELECT statement (outer query) which will be the last clause.

 Use single-row operators with single-row subqueries.

 If a subquery (inner query) returns a null value to the outer query, the outer query will
not return any rows when using certain comparison operators in a WHERE clause.

Case study 2
Correlated sub queries are used for row-by row processing. Each sub query is executed once
for every row of the outer query. Normal subqueries inner queries executed first, and outer
query executed. But in correlated sub queries outer query is always depends inner query so
outer query executed first, inner query executed for each row in outer query.

Case3
Inserting values from other table using select
QL> select * from customer;
New table created
CID NAME SQL> create table cus(id varchar2(5),cname
--- ---------- varchar2(10));
c01 Riya
c02 Jiya Table created.
c03 Diya
c04 Taral SQL> insert into cus(id,cname) select cid,name
c05 Saral from customer;

5 rows created.

SQL> select * from cus;

ID CNAME
----- ----------
c01 Riya
c02 Jiya
c03 Diya
c04 Taral
c05 Saral

Update operation as sub query


SQL> update account set
balance=balance+1000 where ano in (select
ano from account_holder where cid='c02');

2 rows updated.

SQL> select * from account;

ANO BALANCE BNAME


--- ---------- ----------
a01 5000 vvn
a02 7000 ksad
a03 7000 anand
a04 9000 ksad
a05 6000 vvn

SQL> delete from account where ano To delete account of customer Saral from
in(select ano from account_holder where cid account table.
in (select cid from customer where
name='Saral'));
1 row deleted.

SQL> select * from account;

ANO BALANCE BNAME


--- ---------- ----------
a01 5000 vvn
a02 6000 ksad
a03 7000 anand
a04 8000 ksad

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy