DEV Community

Cover image for Sql Joins
Mohammad Reza
Mohammad Reza

Posted on

Sql Joins

Today I want to speak about Sql Joins and look at the diffrent kinds of joins

INNER JOIN

gets all records that are common between both tables based on the supplied ON clause.

SELECT table1.column1, table2.column2...
 FROM table1
 INNER JOIN table2
    ON table1.common_field = table2.common_field;
Enter fullscreen mode Exit fullscreen mode

LEFT JOIN

gets all records from the LEFT linked and the related record from the right table ,but if you have selected some columns from the RIGHT table, if there is no related records, these columns will contain NULL.

SELECT table1.column1, table2.column2...
  FROM table1
  LEFT JOIN table2
    ON table1.common_field = table2.common_field;
Enter fullscreen mode Exit fullscreen mode

Alt Text

RIGHT JOIN

is like the above but gets all records in the RIGHT table.

SELECT table1.column1, table2.column2...
 FROM table1
 RIGHT JOIN table2
    ON table1.common_field = table2.common_field;
Enter fullscreen mode Exit fullscreen mode

FULL JOIN

gets all records from both tables and puts NULL in the columns where related records do not exist in the opposite table.

SELECT table1.column1, table2.column2...
  FROM table1
  FULL JOIN table2
    ON table1.common_field = table2.common_field;
Enter fullscreen mode Exit fullscreen mode

Done :)
Feel free to ask any questions

Top comments (2)

Collapse
 
linuxguist profile image
Nathan S.R.

Thanks for this very useful post. I just wanted to add that, there is a very easy way now, to test all the SQLs described here, using the free & portable tools, mentioned in my latest post here : dev.to/linuxguist/learn-sql-quickl...

Collapse
 
kathleensavage profile image
KathleenSavage

What are the 4 JOINs in SQL? priere pour faire revenir l être aime