We wish to join two tables based on specific columns.
This section is organized as follows:
This section is complemented by:
We wish to join two tables based on one column.
Common Column Name
We wish to join two tables based on one column that have the same name in the tables to be joined.
In this example, we wish to join table_1
and table_2
based on a column named col_1
in both
tables.
SELECT t1.col_1,
t1.col_2,
t1.col_3,
t2.col_4
FROM table_1 AS t1
INNER JOIN table_2 AS t2 USING (col_1);
Here is how this works:
INNER JOIN
operation in SQL to combine table_1
and table_2
based on the common
column col_1
.USING
clause which performs an equality comparison
between the col_1
in table_1
and col_1
in table_2
.AS
, in this example table_1
alias is t1
and table_2
alias is t_2
. We recommend using table alias always when performing a join, so we can easily
select the columns we wish to keep from each table.SELECT
statement.
See Selecting By Name.col_1
in
both table_1
and table_2
and all columns from table_1
and col_4
from table_2
.Different Column Name
We wish to join two tables based on one column that has a different name in either table.
In this example, we wish to join table_1
and table_2
based on col_1_x
from table_1
and col_1_y
in table_2
.
SELECT t1.*,
t2.*
FROM table_1 AS t1
INNER JOIN table_2 AS t2 ON t1.col_1_x = t2.col_1_y;
Here is how this works:
INNER JOIN
operation in SQL to combine table_1
and table_2
based on
column col_1_x
from table_1
and col_1_y
from table_2
.ON
clause to specify the join condition which is t1.col_1_x = t2.col_1_y
in this
example.AS
, in this example table_1
alias is t1
and table_2
alias is t_2
. We recommend using table alias always when performing a join, so we can easily
select the columns we wish to keep from each table..*
to
select all columns in a table.
See Selecting By Name.col_1_x
and col_1_y
and
all columns from table_1
and table_2
.We wish to join two tables based on multiple columns.
Common Columns Names
We wish to join two tables based on multiple columns that have the same name in the tables to be joined.
In this example, we wish to join table_1
and table_2
based on the columns named col_1
and col_2
in both tables.
SELECT t1.col_1,
t1.col_2,
t1.col_3,
t2.col_4
FROM table_1 AS t1
INNER JOIN table_2 AS t2 USING (col_1, col_2);
Here is how this works:
USING
clause which performs an equality comparison
between them.Different Columns Names
We wish to join two tables based on multiple columns, while each of the join columns has a different name in either table.
In this example, we wish to join table_1
and table_2
based on col_1_x
and col_2_x
from table_1
and col_1_y
and col_2_y
from table_2
.
SELECT t1.*,
t2.*
FROM table_1 AS t1
INNER JOIN table_2 AS t2 ON
t1.col_1_x = t2.col_1_y AND
t1.col_2_x = t2.col_2_y;
Here is how this works:
ON
clause. We use AND to combine the join conditions
since we want all conditions to be met.Extension: Multiple Join Conditions
We wish to join two tables based on multiple columns and using multiple types of join conditions.
In this example, we wish to combine rows from table_1
and table_2
where:
col_1_x
and col_2_x
have the same value in both tablescol_3_x
from table_1
is bigger than col_3_y
in from table_2
.SELECT t1.*,
t2.*
FROM table_1 AS t1
INNER JOIN table_2 AS t2 ON
t1.col_1_x = t2.col_1_y OR
t1.col_3_x > t2.col_3_y;
Here is how this works:
OR
to separate the multiple join conditions in the ON
clause.ON
clause can take any conditional statement and will combine the two rows if
the statement is true.
General Operations: Conditional.