2023-05-10

SQL :: Using Joins (INNER JOIN, LEFT JOIN, RIGHT JOIN)

Understanding joins in SQL can save a lot of pain, the basics of any query is:

SELECT stuff
FROM table

When the table isn't enough, joins start showing up:

SELECT stuff, moreStuff
FROM table
INNER JOIN anotherTable ON table.key = anotherTable.foreignKey


The basics on how joins work:

  • INNER JOIN
    selects all records where table.key = anotherTable.foreignKey

SELECT stuff, moreStuff
FROM table
INNER JOIN anotherTable ON table.key = anotherTable.foreignKey


  • LEFT (OUTER) JOIN
    selects all records from table and any matches from anotherTable where anotherTable.foreignKey = table.key

SELECT stuff, moreStuff
FROM table
LEFT JOIN anotherTable ON table.key = anotherTable.foreignKey


  • RIGHT JOIN
    selects all records from anotherTable and any matches from table where table.key = anotherTable.foreignKey

SELECT stuff, moreStuff
FROM table
LEFT JOIN anotherTable ON table.key = anotherTable.foreignKey





No comments:

Post a Comment

Please provide any feedback or post questions here...

Javascript :: comparison operators, using == vs === and != vs !==

Comparison operators are distinguished following simple logic, whether data type is being included or not. Essentially if it is === or !==, ...