stoolpigeon
Registered User.
- Local time
- Today, 13:39
- Joined
- Apr 23, 2013
- Messages
- 12
I understand the following query setup:
SELECT *
FROM tbl1
WHERE field1 = (SELECT field1 FROM tbl2 WHERE field2 = x)
Essentially the nested select statement is executed and returns a value for field1 which is then used in the 'main' select statement.
What I don't understand is the following:
SELECT *
FROM Employee Emp1
WHERE (N-1) = (
SELECT COUNT(DISTINCT(Emp2.Salary))
FROM Employee Emp2
WHERE Emp2.Salary > Emp1.Salary)
I'm struggling with the idea that there's some kind of looping going on? The inner select statement is being executed for each Employee in Emp1 in the outer statement. This seems completely different from the first method of nestling a select statement above.
It seems to me to be similar in structure to when I use, in VBA:
for i = x to y
for j = w to z
*statement*
next j
next i
where i is fixed until j has finished iterating and then we move to the next i etc but I don't really follow how SQL is doing this?
SELECT *
FROM tbl1
WHERE field1 = (SELECT field1 FROM tbl2 WHERE field2 = x)
Essentially the nested select statement is executed and returns a value for field1 which is then used in the 'main' select statement.
What I don't understand is the following:
SELECT *
FROM Employee Emp1
WHERE (N-1) = (
SELECT COUNT(DISTINCT(Emp2.Salary))
FROM Employee Emp2
WHERE Emp2.Salary > Emp1.Salary)
I'm struggling with the idea that there's some kind of looping going on? The inner select statement is being executed for each Employee in Emp1 in the outer statement. This seems completely different from the first method of nestling a select statement above.
It seems to me to be similar in structure to when I use, in VBA:
for i = x to y
for j = w to z
*statement*
next j
next i
where i is fixed until j has finished iterating and then we move to the next i etc but I don't really follow how SQL is doing this?