Search code examples
sqloracle-databasesqlplusddldml

oracle sql invalid identifier error


I am trying to run the following statement in the oracle database:

select nfl_player.first_name, nfl_player.last_name
from nfl_player
where player_id IN 
    (select nfl_player.player_id as pid
        from nfl_player
        where pid=nfl_team_roster.player_id
        and nfl_team_roster.team_id= 4
    );

For some reason when I run it, I get the following message:

and nfl_team_roster.team_id= 4
    *
ERROR at line 7:
ORA-00904: "NFL_TEAM_ROSTER"."TEAM_ID": invalid identifier

I have double checked and I have my syntax right (or at least I believe so). The column exists. I have included my schema for that table below. What would be giving me this error?

CREATE TABLE NBA_Team_Roster(
roster_ID number primary key,
team_id number,
player_id number unique,
foreign key (team_id) references NBA_Team(team_id),
foreign key (player_id) references NBA_Player(player_id)
);

Any help would be greatly appreciated.


Solution

  • nfl_team_roster table is not used in your query. I think you misplaced the table name in sub-query

    SELECT first_name, 
           last_name 
    FROM   nfl_player nf 
    WHERE  player_id IN (SELECT ntr.player_id AS pid 
                         FROM   NBA_Team_Roster ntr --Here 
                         WHERE  nf.pid = ntr.player_id 
                                AND ntr.team_id = 4); 
    

    Use Alias name instead of full table name to refer columns it makes the query more readable.