DEV Community

Cover image for Sql Joins
Mohammad Reza
Mohammad Reza

Posted on

5 1

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

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

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

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

đź‘‹ Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay