0

I have been working as a data analyst for about 4 months now and the above is a very real question for me. The most recent way I've been taught to join is with the left join with the following example.

left join table1
on
table2.id = table1.id

left join table2
on
table3.table_id = table2.table_id

left join table4
on
table1.tablekey_id = table4.tablekey_id

Looking for the most efficient way to connect multiple tables to save time, if possible.

Thanks in Advance!

6
  • 2
    What you have is perfectly fine and uses the appropriate modern syntax for joining tables. Tuning will depend entirely on specific cases.
    – Error_2646
    Oct 21, 2022 at 19:37
  • 4
    What do you mean by "simplest way to join"? If you have to JOIN a table, there's no getting around a JOIN expression.
    – squillman
    Oct 21, 2022 at 19:39
  • What I meant was, is there a shorter syntax available? @squillman My aim is to avoid writing out the code as shown in the example. So, if I can use some combination of left/right/full/outer/inner to join 3 or more tables, that is what I'm looking to ascertain.
    – elangley
    Oct 21, 2022 at 19:46
  • 1
    @elangley - "shorter" is not always "more elegant". Even if there were a way to smash them into a one-liner, having the separate JOIN statements clarifies the intent and is more-maintainable for future onlookers. :) Oct 21, 2022 at 19:58
  • Considering the information required to specify a join I think it fairly obvious you couldn't get anything much simpler, because all the information shown is required to specify the joins. You could use table alises to save some typing
    – Dale K
    Oct 21, 2022 at 19:59

2 Answers 2

0

You could certainly use a shorter alias for the table names and table fields.

Example:
Table 1 = alias t1
tablekey_id = tkid

So then it would t1.tkid instead of having to type out table1.tablekey_id.

0
  1. If it is only two tables of same columns and same datatypes, you can use union concept to join the tables.
  2. You can also join tables like this:

Tables: TableA, TableB

SELECT
    column1,
    column2,
    column3
FROM TableA, TableB
WHERE TableA.id = TableB.id;
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.