JOIN
JOIN in SQL is used to combine rows from two or more tables, based on a related column between them. There are several types of JOIN in SQL, including: INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN. They differ in how they handle non-matching rows between the tables.
Example
Output
Explanation
In the given example, the INNER JOIN
keyword is used to select records that have matching values in both tables orders
and customers
. The SELECT statement returns the order_id
from the orders
table and the customer_name
from the customers
table.
Example
Output
Explanation
In the given SQL query, an INNER JOIN
is performed between the orders
and customers
tables. The INNER JOIN
matches rows from both tables where the condition, defined by orders.customer_id = customers.customer_id
, is met. The result is a set of combined rows from both tables where there are matching values in the customer_id
field. The SELECT
clause then specifies that we want to retrieve the order_id
from the orders
table and customer_name
from the customers
table for each of these matched records.
Example
Output
Explanation
The INNER JOIN
keyword selects records that have matching values in both tables. In the provided example, it retrieves the corresponding ‘OrderID’, ‘CustomerName’ and ‘OrderDate’ from the ‘Orders’ and ‘Customers’ tables where the ‘CustomerID’ is the same in both tables.
Example
Output
EMPLOYEE_ID | FIRST_NAME | DEPARTMENT_NAME |
---|---|---|
100 | Steven | Executive |
101 | Neena | Executive |
102 | Lex | Executive |
Explanation
The JOIN command combines rows from two or more tables. In this case, it is used to combine rows from the ‘employees’ and ‘departments’ tables. The ON keyword is used to specify the matching column between the two tables, which is the ‘department_id’. The result of the query is a list of employees’ ID, first names and the names of the departments they work in.
Example
Output
Explanation
In the provided SQL example, a JOIN operation is used to combine rows from two tables, Customers
and Orders
. The ON
keyword defines the condition for the join: matching CustomerID
between the two tables. The SELECT
statement returns a result table with CustomerName
from Customers
and OrderID
from Orders
.