ON
ON is a clause in SQL that is used to specify a condition in JOIN operations. It signifies a relationship between tables that is based on the data contents of the respective tables, specifically the columns that are intended to share a relationship.
Example
Output
Explanation
This SQL statement performs an INNER JOIN between the orders
table and the customers
table where the customer_id
of both tables match. The result is a new table that shows the order_id
from the orders
table and the customer_name
from the customers
table for each matching customer_id
.
Example
Output
Explanation
The ON
keyword in PostgreSQL is used in a JOIN clause to specify the conditions that should be met for records being joined. In the example, the employees
table is being joined with the department
table based on dept_id
. This means for each employee, the SQL engine will match it to a department where the dept_id
is the same, displaying the employee’s name (emp_name
) and the corresponding department’s name (dept_name
).
Example
Output
Explanation
In the provided SQL code, the ON
keyword is used to specify the field upon which the INNER JOIN
is to be performed. This code gets the OrderID
, CustomerName
, and OrderDate
from two tables: Orders
and Customers
. The join is performed on the basis of CustomerID
which is common in both tables. The output returns a consolidated view of all orders along with the corresponding customer name and order date.
Example
Output
Explanation
In the example provided, the SQL JOIN statement is utilized in conjunction with the ON keyword to combine rows from both the EMPLOYEES and ORDERS tables based on a common column between them, which, in this case, is the ORDER_ID. The result is a newly formed table that includes the FIRST_NAME from the EMPLOYEES table and ORDER_DATE from the ORDERS table.
Example
Output
Explanation
In this example, we’ve created two tables - Employees
and Departments
. Using the ON
statement, the SELECT
query is able to join the two tables on the DepartmentID
field. This allows us to get the Name
from the Employees
table and DepartmentName
from the Departments
table in a single query.