I am doing practice queries to improve my SQL skills (I'm a beginner) and I ran into this problem that I need help with using the Northwind database. The requested query should:
Give the name of employees and the city where they live for employees who have sold to customers in the same city.
What I wrote for this was:
USE Northwind;
SELECT DISTINCT FirstName, LastName, e.City
FROM Employees e
INNER JOIN Orders o ON e.EmployeeID = O.EmployeeID
INNER JOIN Customers c ON o.CustomerID = c.CustomerID
INNER JOIN Customers ON c.City = e.City
WHERE e.City = ANY(SELECT Customers.City FROM Customers);
I am returned 6 employees but I am not sure that they are correct and I believe that my WHERE statement is the wrong one as well.
Basically my question is, am I using the correct join statements and how do I go about filtering results with the WHERE statement? I'm not exactly sure how to compare one specific record with other records. Coming from a Java background I am used to for-loops that can check each individual "object" (record) with a specific field from another "object". In this case I am wondering how I can check the City attribute of each record from the Employees table with the City attribute of the records on the Customers table. Any and all advice is appreciated, thanks!