โซJoin Clauses
Different Types of SQL JOINs
Here are the different types of the JOINs in SQL:
(INNER) JOIN
: Returns records that have matching values in both tablesLEFT (OUTER) JOIN
: Returns all records from the left table, and the matched records from the right tableRIGHT (OUTER) JOIN
: Returns all records from the right table, and the matched records from the left tableFULL (OUTER) JOIN
: Returns all records when there is a match in either left or right table
![](https://qatesting.gitbook.io/~gitbook/image?url=https%3A%2F%2Fwww.w3schools.com%2Fsql%2Fimg_innerjoin.gif&width=768&dpr=4&quality=100&sign=cd49b5a3&sv=1)
![](https://qatesting.gitbook.io/~gitbook/image?url=https%3A%2F%2Fwww.w3schools.com%2Fsql%2Fimg_leftjoin.gif&width=768&dpr=4&quality=100&sign=6285e9f6&sv=1)
![](https://qatesting.gitbook.io/~gitbook/image?url=https%3A%2F%2Fwww.w3schools.com%2Fsql%2Fimg_rightjoin.gif&width=768&dpr=4&quality=100&sign=39dc9575&sv=1)
![](https://qatesting.gitbook.io/~gitbook/image?url=https%3A%2F%2Fwww.w3schools.com%2Fsql%2Fimg_fulljoin.gif&width=768&dpr=4&quality=100&sign=d2d41252&sv=1)
QUERYING MULTIPLE TABLES
INNER JOIN
JOIN
(or explicitly INNER JOIN
) returns rows that have matching values in both tables.
![](https://qatesting.gitbook.io/~gitbook/image?url=https%3A%2F%2Flearnsql.com%2Fblog%2Fsql-basics-cheat-sheet%2F02-inner-join.png&width=768&dpr=4&quality=100&sign=d1e093d9&sv=1)
LEFT JOIN
LEFT JOIN
returns all rows from the left table with corresponding rows from the right table. If there's no matching row, NULL
s are returned as values from the second table.
![](https://qatesting.gitbook.io/~gitbook/image?url=https%3A%2F%2Flearnsql.com%2Fblog%2Fsql-basics-cheat-sheet%2F03-left-join.png&width=768&dpr=4&quality=100&sign=9990e0f7&sv=1)
RIGHT JOIN
RIGHT JOIN
returns all rows from the right table with corresponding rows from the left table. If there's no matching row, NULL
s are returned as values from the left table.
![](https://qatesting.gitbook.io/~gitbook/image?url=https%3A%2F%2Flearnsql.com%2Fblog%2Fsql-basics-cheat-sheet%2F04-right-join.png&width=768&dpr=4&quality=100&sign=1ebc445&sv=1)
FULL JOIN
FULL JOIN
(or explicitly FULL OUTER JOIN
) returns all rows from both tables โ if there's no matching row in the second table, NULL
s are returned.
![](https://qatesting.gitbook.io/~gitbook/image?url=https%3A%2F%2Flearnsql.com%2Fblog%2Fsql-basics-cheat-sheet%2F05-full-join.png&width=768&dpr=4&quality=100&sign=9bc0cab5&sv=1)
CROSS JOIN
CROSS JOIN
returns all possible combinations of rows from both tables. There are two syntaxes available.
![](https://qatesting.gitbook.io/~gitbook/image?url=https%3A%2F%2Flearnsql.com%2Fblog%2Fsql-basics-cheat-sheet%2F06-cross-join.png&width=768&dpr=4&quality=100&sign=dad044d9&sv=1)
NATURAL JOIN
NATURAL JOIN
will join tables by all columns with the same name.
NATURAL JOIN
used these columns to match rows:
city.id
, city.name
, country.id
, country.name
.
NATURAL JOIN
is very rarely used in practice.
Last updated