Search code examples
databasealgebrarelational

Equijoin when used on relations with attributes of same name


I recently came across a question that has been bugging me for a bit.

If the EquiJoin is used with two relations which have multiple attributes of the same name, with multiple value matches, then what will be the result? For example, consider something like:

R(A,B,C,D) and S(C,D,E,F).

If you apply the equijoin to the above as follows:

R (equi-join)(condition: R.C = S.C) S, how will the resulting relation behave? I am especially interested with the common 'D' attribute of both relations and its presence in the result.

Many thanks in advance for your answers!


Solution

  • This should answer your question:

    table addresses

    first_name  last_name  address
    John        Miller     5 Miller Street
    Jane        Smith      6 Smith Way
    Jane        Becker     7 Becker Street
    

    table jobs

    first_name  last_name  job
    John        Miller     milkman
    Jane        Smith      software engineer
    Jane        Becker     translator
    

    query

    select *
    from addresses
    inner join jobs on jobs.first_name = addresses.first_name
    

    result

    first_name  last_name  address          job
    John        Miller     5 Miller Street  milkman
    Jane        Smith      6 Smith Way      software engineer
    Jane        Smith      6 Smith Way      translator
    Jane        Becker     7 Becker Street  software engineer
    Jane        Becker     7 Becker Street  translator
    

    As we don't join by both first name and last name, but by first name only, we join records that don't belong together.