The Wayback Machine - https://web.archive.org/web/20240721120606/https://www.geeksforgeeks.org/sql-on-clause/
Open In App

SQL | ON Clause

Last Updated : 13 May, 2018
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

The join condition for the natural join is basically an EQUIJOIN of all columns with same name. To specify arbitrary conditions or specify columns to join, the ON Clause is used.

  1. The join condition is separated from other search conditions.
  2. The ON Clause makes code easy to understand.
  3. ON Clause can be used to join columns that have different names.
  4. We use ON clause to specify a join condition. This lets us specify join conditions separate from any search or filter conditions in the WHERE clause.

EXAMPLES:

We will apply the below mentioned commands on the following base tables:

    Employees Table

    Departments Table

    QUERY 1: Write SQL query to find the working location of the employees. Also give their respective employee_id, last_name and department_id?

    
    Input :SELECT e.employee_id, e.last_name, e.department_id,
    d.department_id, d.location_id
    FROM employees e JOIN departments d
    ON (e.department_id = d.department_id);
    Output : 
    

Explanation: The example shown joins the DEPARTMENT_ID column in the EMPLOYEES and DEPARTMENTS
tables using ON Clause, and thus shows the required data.

We will apply the below mentioned commands on the following base tables:

    Countries Table

    Locations Table

QUERY 2: Write SQL query to find the location_id, street_address, postal_code and their respective country name?


Input : SELECT l.location_id, l.street_address, l.postal_code, c.country_name
FROM locations l JOIN countries c
ON (l.country_id = c.country_id);
Output : 

Explanation: The example shown joins the COUNTRY_ID column in the LOCATIONS and COUNTRIES
tables using the ON Clause, and thus shows the required details.



Similar Reads

Difference between Having clause and Group by clause
1. Having Clause : Having Clause is basically like the aggregate function with the GROUP BY clause. The HAVING clause is used instead of WHERE with aggregate functions. While the GROUP BY Clause groups rows that have the same values into summary rows. The having clause is used with the where clause in order to find rows with certain conditions. The
3 min read
SQL | Intersect & Except clause
1. INTERSECT clause : As the name suggests, the intersect clause is used to provide the result of the intersection of two select statements. This implies the result contains all the rows which are common to both the SELECT statements. Syntax : SELECT column-1, column-2 …… FROM table 1 WHERE….. INTERSECT SELECT column-1, column-2 …… FROM table 2 WHE
1 min read
SQL | USING Clause
If several columns have the same names but the datatypes do not match, the NATURAL JOIN clause can be modified with the USING clause to specify the columns that should be used for an EQUIJOIN. USING Clause is used to match only one column when more than one column matches. NATURAL JOIN and USING Clause are mutually exclusive. It should not have a q
2 min read
SQL | With Ties Clause
This post is a continuation of SQL Offset-Fetch Clause Now, we understand that how to use the Fetch Clause in Oracle Database, along with the Specified Offset and we also understand that Fetch clause is the newly added clause in the Oracle Database 12c or it is the new feature added in the Oracle database 12c. Now consider the below example: Suppos
4 min read
SQL | Sub queries in From Clause
From clause can be used to specify a sub-query expression in SQL. The relation produced by the sub-query is then used as a new relation on which the outer query is applied. Sub queries in the from clause are supported by most of the SQL implementations.The correlation variables from the relations in from clause cannot be used in the sub-queries in
2 min read
Combining aggregate and non-aggregate values in SQL using Joins and Over clause
Prerequisite - Aggregate functions in SQL, Joins in SQLAggregate functions perform a calculation on a set of values and return a single value. Now, consider an employee table EMP and a department table DEPT with following structure:Table - EMPLOYEE TABLE NameNullTypeEMPNONOT NULLNUMBER(4)ENAME VARCHAR2(10)JOB VARCHAR2(9)MGR NUMBER(4)HIREDATE DATESA
2 min read
SQL Full Outer Join Using Left and Right Outer Join and Union Clause
An SQL join statement is used to combine rows or information from two or more than two tables on the basis of a common attribute or field. There are basically four types of JOINS in SQL. In this article, we will discuss FULL OUTER JOIN using LEFT OUTER Join, RIGHT OUTER JOIN, and UNION clause. Consider the two tables below: Sample Input Table 1: Pu
3 min read
Difference between From and Where Clause in SQL
1. FROM Clause: It is used to select the dataset which will be manipulated using Select, Update or Delete command.It is used in conjunction with SQL statements to manipulate dataset from source table.We can use subqueries in FROM clause to retrieve dataset from table. Syntax of FROM clause: SELECT * FROM TABLE_NAME; 2. WHERE Clause: It is used to a
2 min read
Distinct clause in MS SQL Server
The SELECT DISTINCT statement is used to return only distinct (different) values. Inside a table, a column often contains many duplicate values and sometimes we only want to list the different (distinct) values. Consider a simple database and we will be discussing distinct clauses in MS SQL Server. Suppose a table has a maximum of 1000 rows constit
3 min read
Where clause in MS SQL Server
In this article, where clause will be discussed alongside example. Introduction : To extract the data at times, we need a particular conditions to satisfy. 'where' is a clause used to write the condition in the query. Syntax : select select_list from table_name where condition A example is given below for better clarification - Example : Sample tab
1 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg