JOIN
Example
Section titled “Example”SELECT orders.order_id, customers.customer_nameFROM ordersINNER JOIN customers ON orders.customer_id = customers.customer_id;Output
Section titled “Output”| order_id | customer_name ||----------|---------------|| 1 | John Doe || 2 | Jane Smith || 3 | Mary Johnson |Explanation
Section titled “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
Section titled “Example”SELECT orders.order_id, customers.customer_nameFROM ordersINNER JOIN customersON orders.customer_id = customers.customer_id;Output
Section titled “Output” order_id | customer_name----------+--------------- 1 | John Doe 2 | Jane Smith 3 | Bob Johnson 4 | Alice JohnsonExplanation
Section titled “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
Section titled “Example”SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDateFROM OrdersINNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;Output
Section titled “Output”| OrderID | CustomerName | OrderDate ||---------|--------------|-------------|| 1 | Smith | 2017-07-04 || 2 | Jones | 2017-08-05 || 3 | White | 2017-09-06 |Explanation
Section titled “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
Section titled “Example”SELECT e.employee_id, e.first_name, d.department_nameFROM employees eJOIN departments d ON e.department_id = d.department_id;Output
Section titled “Output”| EMPLOYEE_ID | FIRST_NAME | DEPARTMENT_NAME |
|---|---|---|
| 100 | Steven | Executive |
| 101 | Neena | Executive |
| 102 | Lex | Executive |
Explanation
Section titled “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
Section titled “Example”SELECT Customers.CustomerName, Orders.OrderIDFROM CustomersJOIN OrdersON Customers.CustomerID=Orders.CustomerID;Output
Section titled “Output”| CustomerName | OrderID ||----------------|---------|| Alfreds | 10308 || Ana Trujillo | 10309 || Antonio Moreno | 10310 |Explanation
Section titled “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.