The second query is incorrect. This is because of the rules of name resolution. Just like in C# or any other language, when T-SQL sees a column name, it tries to resolve it from inside out. So in your second statement you are comparing dep column from the inner query to itself.SELECT *
FROM student
WHERE salary > (
SELECT AVG(salary)
FROM student e
WHERE dep = e.dep -- This is equivalent to e.dep = e.dep, which is always TRUE
);