Skip to content
Advertisement

Filter by a persons name only

I am trying to filter my results to only show a person’s name who is staying at a hotel in a specific city. What I have so far is

select guestName
from Guest
join Hotels on hotelNo=1

However, when I do this, the result page lists every guest name in the database, rather than the ones that are staying in hotelNo 1.

Advertisement

Answer

You should put your condition in Where clause instead.

And Join should look like this

select guestName
from Guest g
join Hotels h on g.HotelId = h.HotelId -- Primary Key of Hotels equals Foreign Key of Guest 
where hotelNo = 1

Explanation

In Join clause, You should address the condition between 2 tables (It is often between Primary key (Hotel table) and Foreign Key (Guest table))

enter image description here

More details in https://www.w3schools.com/sql/sql_join_inner.asp

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement