I have created one temp table and inserted 10 records into it. The query against the table gets the 3rd highest salary. This query has a sub query. I want to know which query executes first: the inner query or outer query? Kindly check the query which is written below.
CREATE TABLE #tmpTbl
(
id int,
sal money
)
insert into #tmpTbl values('1','12000')
insert into #tmpTbl values('1','22000')
insert into #tmpTbl values('1','50000')
insert into #tmpTbl values('1','100000')
insert into #tmpTbl values('1','100')
insert into #tmpTbl values('1','5000')
insert into #tmpTbl values('1','15000')
insert into #tmpTbl values('1','18000')
insert into #tmpTbl values('1','19000')
insert into #tmpTbl values('1','21000')
SELECT *
FROM #tmpTbl
order by sal asc
SELECT DISTINCT sal
FROM #tmpTbl t1
WHERE 3 = (SELECT COUNT(DISTINCT SAL)
FROM #tmpTbl t2
WHERE t1.SAL <= t2.SAL)
Kindly run both queires. The first query will give you salary in ascending order and second will give you 22000.00.
Which runs first: the outer query or the inner query?