Geeks Talk

Prepare for your Next Interview


Welcome to the Geeks Talk forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

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 Blogs FAQ Tag Cloud 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,933
Thanks: 10
Thanked 179 Times in 149 Posts
debasisdas has a spectacular aura aboutdebasisdas has a spectacular aura about
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: 530
Thanks: 30
Thanked 63 Times in 61 Posts
susarlasireesha will become famous soon enough
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: 738
Thanks: 22
Thanked 67 Times in 65 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: 43
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: 9
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: 96
Thanks: 2
Thanked 13 Times in 9 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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are Off


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 11:23 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.3.1
Copyright © 2005 - 2010 GeekInterview.com. All Rights Reserved