Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server : can't select columns of one table by filtering from another table on same database issue

I am using this statement to fetch the rows from one table by filtering the data from another table.

 SELECT EMPNO 
 FROM EMP 
 WHERE CITY_NAME = 'Los Angeles';

I am using SQL Server 2014 and I get this error:

Msg 207, Level 16, State 1, Line 51
Invalid column name 'CITY_NAME'.

Actually I need to fetch EMPNO FROM EMP by filtering all people from 'Los Angeles'

I also try this statement but it doesn't work either:

SELECT EMPNO 
FROM EMP 
WHERE dbo.LOCATION.CITY_NAME = 'Los Angeles';

IMAGE OF ALL TABLE

NOTE: these are all on the same server and same database

Thank you.

like image 357
Jordan Avatar asked Jan 24 '26 23:01

Jordan


1 Answers

You need JOIN between the tables.

 SELECT EMPNO FROM EMP 
 JOIN DEPT ON EMP.DeptNo = DEPT.DeptNo
 JOIN LOCATION ON LOCATION.City_ID = DEPT.City_ID 
 WHERE LOCATION.CITY_NAME='Los Angeles';
like image 112
Mudassir Hasan Avatar answered Jan 26 '26 14:01

Mudassir Hasan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!