100 Days of SQL

sql

Day 18 – SQL INNER JOIN Keyword

In SQL, the INNER JOIN keyword is used to combine rows from two or more tables based on a related column between them. The INNER JOIN returns only the rows that have matching values in both tables.

Here’s an example of how to use the INNER JOIN keyword:


SELECT *
FROM table1
INNER JOIN table2
ON table1.column = table2.column;

In this example, table1 and table2 are the names of the two tables being joined, and column is the related column between the two tables. The ON keyword specifies the condition for the join.

The result of the INNER JOIN operation is a new table that includes all the columns from both tables where the join condition is true. The columns that are not used for the join will be duplicated in the result table. You can use column aliases to differentiate between the duplicated columns.

Here’s an example of how to use column aliases with an INNER JOIN:


SELECT table1.column1 AS column1_alias, table2.column2 AS column2_alias
FROM table1
INNER JOIN table2
ON table1.column = table2.column;

In this example, column1_alias and column2_alias are the aliases given to the duplicated columns in the result table. This makes it easier to refer to the columns in the SELECT statement and the result table.