What is the sub-query

Editorial / Best Answer

Answered by: Sailaja Pasupuleti

  • Dec 3rd, 2005


    A sub-query is a part of query.  Sub-queries are useful to find unknown value(s).

   Ex:  To find details of employees who are getting maximum salary.

         In this requirement, the maximum salary not mentioned.

Solution:    

To find maximum salary, 

          select max(sal) from emp;

For that maximum salary the employee details, 

          select * from emp where  sal = (select max(sal) from emp);

So, in the above query, "select max(sal) from emp"  is also a query but it returns a value (unknown or not given) and compares with "sal" column.  This query is called sub-query.

Showing Answers 1 - 9 of 9 Answers

nalini

  • Mar 22nd, 2005
 

subquery is inner query executes before the the main query.the result of the subquery used in main query

  Was this answer useful?  Yes

srinivas.T.T

  • Jul 20th, 2005
 

Query with in Query is nothing but subquery. Subquery execute only once per table

  Was this answer useful?  Yes

prathima

  • Sep 9th, 2005
 

a subquery is a SELECT statement that is written inside another SQL statement (which is often, but does not have to be, another SELECT). To distinguish the subquery (or inner query) from its enclosing query (or outer query), it must be enclosed within parentheses. Here is an example: 
 
SELECT * FROM clients WHERE clno IN -- outer query 
(SELECT clno FROM firms WHERE city = 'leduc'); -- inner query 
 

  Was this answer useful?  Yes

annathurai

  • Oct 15th, 2005
 

sub-query nothing but Queries within the queries is called the sub -query.example:select empno,empname frome employee where empno=(select empno from empdetails)

  Was this answer useful?  Yes

Sailaja Pasupuleti

  • Dec 3rd, 2005
 

    A sub-query is a part of query.  Sub-queries are useful to find unknown value(s).

   Ex:  To find details of employees who are getting maximum salary.

         In this requirement, the maximum salary not mentioned.

Solution:    

To find maximum salary, 

          select max(sal) from emp;

For that maximum salary the employee details, 

          select * from emp where  sal = (select max(sal) from emp);

So, in the above query, "select max(sal) from emp"  is also a query but it returns a value (unknown or not given) and compares with "sal" column.  This query is called sub-query.

shobana

  • Aug 28th, 2006
 

"In" is used to find in a range of values.

Eg: select empno, ename from employee where department in ("SALES","ACCOUNTS","FINANCE","HR");

Records from every department are fetched. Checks department whether in SALES or in ACCOUNTS or in FINANCE or in HR.

Exists just checks for the condition and returns true or false.

Eg: select empno, ename from employee where exists(sal>0);

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Answer Question

Click here to Login / Register your free account


 
Send   Reset

 

Related Answered Questions

 

Related Open Questions