Sobes.tech
Junior

What is the idea behind the JOIN operator in SQL?

sobes.tech AI

Answer from AI

The JOIN operator combines rows from two or more tables based on a related column between them, allowing to query data from multiple tables simultaneously.

Types of JOIN:

  • INNER JOIN: Returns records with matching values in both tables.
  • LEFT JOIN (or LEFT OUTER JOIN): Returns all records from the left table and matching records from the right table. If there is no match, the result for the right table will be NULL.
  • RIGHT JOIN (or RIGHT OUTER JOIN): Returns all records from the right table and matching records from the left table. If there is no match, the result for the left table will be NULL.
  • FULL JOIN (or FULL OUTER JOIN): Returns all records when there is a match in either table. If there is no match, the result will have NULL in the table where there is no match.

Example of INNER JOIN:

SELECT orders.OrderID, customers.CustomerName
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;

Example of LEFT JOIN:

SELECT customers.CustomerName, orders.OrderID
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;