DEV Community

Achilonu Chinwendu Faustina
Achilonu Chinwendu Faustina

Posted on

DAY 8 CODE CHALLENGE

COMBINE TWO TABLES


Two tables were given to write an SQL query to report the firstName, lastName, city and state of each person in the "person" table. We were asked to report null if the address of a personId was not present in the "Address" table. Also, we were permitted to return result table in any order.

DIAGRAMS OF THE TABLES

Image description

Image description

PROCEDURE

  • From the information given above, left join is required for this solution because we are returning every details from the first(left) table and matched records from the second(right) table.

  • Using the select statement, select the column names (firstName, lastName, city, and state) that were stated to appear in our outcome as seen below:
    select firstName, lastName, city, state

  • Select from the first(left) table (that is the one we are returning the all their records) as seen below:
    from Person

  • Left join to the second(right) table using the first and second tables' common field as seen below:
    left join Address
    using(personId)

  • Since we were permitted to return in any order, I would prefer we go by lastName as seen below:
    order by lastName;

OUTCOME
Image description

Top comments (0)