Home > other >  What is the simplest way to join multiple tables in SQL?
What is the simplest way to join multiple tables in SQL?

Time:10-22

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!

CodePudding user response:

  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;

CodePudding user response:

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.

  • Related