Prepare for your Next Interview
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?...
|
|||
|
Select within select
Is there a way to write select statement within select statement?
|
| The Following User Says Thank You to neeraj_sigh For This Useful Post: | ||
| Sponsored Links |
|
|||
|
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)) |
| The Following User Says Thank You to debasisdas For This Useful Post: | ||
|
|||
|
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' ); |
|
|||
|
Re: Select within select
this is called inlineview.
|
|
|||
|
Re: Select within select
Quote:
For instance, To get 2nd max sal we can use below query. Select max(sal) from emp where sal < (select max(sal) from emp). |
|
|||
|
Quote:
eX:select count(sname) from supply where pno in(select pno from pro where pname='pencil'; |
![]() |
|
| Thread Tools | |
| Display Modes | |
|
|
|
||||
| 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 |