Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
How to query more than one table
Creating JOINs
Referencing columns
8. Refer to columns without table names
Let's practice

Instruction

Great. In the previous example, we provided column names together with the tables they are a part of. It's good practice, but you only need to do it when there is a chance of confusing them. If there are two different columns with the same name in two different tables, then you have to specify the tables. If the name of the column is unique, though, you may omit the table name.

SELECT
  name,
  model
FROM person
JOIN car
  ON person.id = owner_id;

There is only one column named name and only one column named model in the person and car tables, so we can provide their names without giving information about the tables they come from. Similarly, there is only one column named owner_id – it is only in the car table, so we can omit the name of the table.

When we refer to column id from table person, though, we must write the table name as well (person.id), because there is another column with the name id in table car.

Exercise

Select director name and movie title from the movie and director tables in such a way that a movie is shown together with its director. Don't write table names in the SELECT clause.

Stuck? Here's a hint!

Type:

SELECT
  title,
  name
FROM movie
JOIN director
  ON director_id = director.id;