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 belong to. This is good practice, but you only need to do it when there is potential for ambiguity. If there are two different columns with the same name in two different tables, then you have to specify the table names. If the names of the columns are unique, though, you may omit the table names.

SELECT
  Name,
  Model
FROM Person
JOIN Car
  ON Person.Id = OwnerId;

In this case, there is only one column named Name and only one column named Model in the tables Person and Car, so we could provide their names without giving information about the tables they come from. Similarly, there is only one column named OwnerId – it is only in the Car table, so we can omit the name of the table.

When we refer to Id column of the Person table, though, we must write the table name as well (Person.Id) because there is another column with the name Id in the Car table.

Exercise

Select the director name and movie title from the tables Movie and Director such that each movie is shown together with its director. Use as few characters as possible.

Stuck? Here's a hint!

Type:

SELECT
  Title,
  Name
FROM Movie
JOIN Director
  ON DirectorId = Director.Id;