Geeks Talk

Prepare for your Next Interview




Select within select

This is a discussion on Select within select within the SQL forums, part of the Databases category; Is there a way to write select statement within select statement?...


Go Back   Geeks Talk > Databases > SQL

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 11-03-2007
Junior Member
 
Join Date: Nov 2007
Location: Hyderabad
Posts: 1
Thanks: 0
Thanked 1 Time in 1 Post
neeraj_sigh is on a distinguished road
Select within select

Is there a way to write select statement within select statement?
Reply With Quote
The Following User Says Thank You to neeraj_sigh For This Useful Post:
Sponsored Links
  #2 (permalink)  
Old 11-04-2007
Moderator
 
Join Date: Jun 2007
Location: Bangalore,India
Posts: 1,365
Thanks: 7
Thanked 123 Times in 111 Posts
debasisdas will become famous soon enoughdebasisdas will become famous soon enough
Re: Select within select

Yes you can write that.
This is called nested sub query .
Try the following sample to find out 3rd heighest sal form emp table.
Code:
 select max(sal) from emp where sal< (select max(sal) from emp where sal<(select max(sal) from emp))
Reply With Quote
The Following User Says Thank You to debasisdas For This Useful Post:
  #3 (permalink)  
Old 11-04-2007
Expert Member
 
Join Date: Apr 2007
Location: Bangalore
Posts: 427
Thanks: 17
Thanked 53 Times in 53 Posts
susarlasireesha is on a distinguished road
Re: Select within select

Subqueries are similar to SELECT chaining. While SELECT chaining combines SELECTs on the same level in a query, however, subqueries allow SELECTs to be embedded inside other queries. They can perform several functions:


They can take the place of a constant.
They can take the place of a constant yet vary based on the row being processed.
They can return a list of values for use in a comparison.

Examples
SELECT name
FROM customer
WHERE customer.customer_id = (
SELECT salesorder.customer_id
FROM salesorder
WHERE order_id = 14673
);

Subqueries as Correlated Values

SELECT f1.firstname, f1.lastname, f1.age
FROM friend f1
WHERE age = (
SELECT MAX(f2.age)
FROM friend f2
WHERE f1.state = f2.state
)
ORDER BY firstname, lastname;

Subqueries as Lists of Values

SELECT name
FROM employee
WHERE employee_id IN (
SELECT employee_id
FROM salesorder
WHERE order_date = '7/19/1994'
);

other examples

SELECT name
FROM employee
WHERE employee_id IN (
SELECT employee_id
FROM salesorder
WHERE order_date = '7/19/1994'
);

SELECT name
FROM employee
WHERE employee_id = ANY (
SELECT employee_id
FROM salesorder
WHERE order_date = '7/19/1994'
);

SELECT name
FROM employee
WHERE EXISTS (
SELECT employee_id
FROM salesorder
WHERE salesorder.employee_id
=employee.employee_id AND
order_date = '7/19/1994'
);
Reply With Quote
  #4 (permalink)  
Old 11-05-2007
Junior Member
 
Join Date: Nov 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
purnimamca is on a distinguished road
Re: Select within select

to find nth highest salary

select distinct (a.sal) from emp a where &n=select (count(distinct b.sal) from emp b where a.sal<=b.sal);
Reply With Quote
  #5 (permalink)  
Old 11-13-2007
Expert Member
 
Join Date: Sep 2007
Posts: 754
Thanks: 22
Thanked 62 Times in 61 Posts
krishnaindia2007 is on a distinguished road
Re: Select within select

You can write in this way also

SELECT ename,job,sal
FROM
(
SELECT *
FROM emp
ORDEY BY sal DESC
)
WHERE ROWNUM <=10;
Reply With Quote
  #6 (permalink)  
Old 12-08-2007
Contributing Member
 
Join Date: Jul 2007
Posts: 42
Thanks: 1
Thanked 1 Time in 1 Post
priyasp_msit is on a distinguished road
Re: Select within select

Quote:
Originally Posted by krishnaindia2007 View Post
You can write in this way also

SELECT ename,job,sal
FROM
(
SELECT *
FROM emp
ORDEY BY sal DESC
)
WHERE ROWNUM <=10;
this is called inlineview.
Reply With Quote
  #7 (permalink)  
Old 12-08-2007
Junior Member
 
Join Date: Nov 2007
Posts: 7
Thanks: 0
Thanked 1 Time in 1 Post
prashanth.n is on a distinguished road
Re: Select within select

Quote:
Originally Posted by neeraj_sigh View Post
Is there a way to write select statement within select statement?
Yes, we can write Select statement within select statement.
For instance, To get 2nd max sal we can use below query.
Select max(sal) from emp where sal < (select max(sal) from emp).
Reply With Quote
  #8 (permalink)  
Old 12-09-2007
Contributing Member
 
Join Date: Oct 2007
Location: trichy
Posts: 92
Thanks: 2
Thanked 10 Times in 8 Posts
sarathi trichy is on a distinguished road
Smile Re: Select within select

Quote:
Originally Posted by neeraj_sigh View Post
Is there a way to write select statement within select statement?
this is basic of sql ...it is nested query
eX:select count(sname) from supply where pno in(select pno from pro where pname='pencil';
Reply With Quote
Reply

  Geeks Talk > Databases > SQL


Thread Tools
Display Modes


Similar Threads

Thread Thread Starter Forum Replies Last Post
Which course to select fo IC engines Geek_Guest Career Advice 0 09-20-2007 02:30 AM
Singleton select sdresh MainFrame 2 09-11-2007 02:20 AM
using (top) along with select stmt is possible in MySQL??? Ammu_R MY SQL 2 08-13-2007 03:03 AM
I am not able to select the pop-up window Geek_Guest QTP 0 07-12-2007 05:20 AM
Select a different value from the combo box JobHelper WinRunner 1 12-26-2006 10:17 AM


All times are GMT -4. The time now is 03:31 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Copyright © 2008 GeekInterview.com. All Rights Reserved