Usually with WHERE
clause in SQL queries, in WHERE we check from the table if a condition is met, but with the following code:
SELECT name
FROM teacher as T1
WHERE (
SELECT COUNT (DISTINCT T2.salary)
FROM teacher as T2
WHERE T2.salary > T1.salary
) = 3;
This following sub-query Sub
is returning a number.
(SELECT COUNT (DISTINCT T2.salary)
FROM teacher as T2
WHERE T2.salary > T1.salary)
but I don’t understand how we check WHERE Sub = 3
, it doesn’t make sense to me. Can you explain?
The entire query finds the 4th highest salary of teacher.
2
Answers
You can use a subquery anywhere where you can use an expression, so why not on the left side of the equality operator?
But this way to solve the problem is rather ugly. Why not
Perhaps more comprehensive with this query :