SELECT E1.Department, E1.DeptPhone
FROM EMPLOYEE AS E1
WHERE EXISTS
(SELECT E2.Department
FROM EMPLOYEE AS E2
WHERE E1.Department = E2.Department
AND E1.DeptPhone <> E2.DeptPhone);
So, when I read this query I think that I'm selecting the dertments, from table E2, that are equal to the departments from E1, that also have different phone numbers.
Since EXISTS is a correlated subquery, I'm also selecting the departments (from E开发者_开发技巧1) and the DeptPhone (also from E1), but I'm only selecting the departments work with my subquery.
In other words, I'm displaying the departments (that doen't have the same phone numbers as E1 but do have the same department name as E1), and the DeptPhone from E1. Am I reading that right?
No. You're selecting all rows from E1
if there exists one or more equal department with different phone numbers.
You are displaying all departments where there is another record with the same department name, but a different phone number. I.e., you are displaying all departments with more than one phone number.
精彩评论