• Home
  • SQL INNER JOIN Keyword

    In SQL, the INNER JOIN is used to combine rows from two or more tables based on a related column between them. It returns only the matching rows that satisfy the join condition. The basic syntax of an INNER JOIN is as follows:

    sql
    SELECT columns
    FROM table1
    INNER JOIN table2 ON join_condition;

    Here's an example to illustrate the usage of INNER JOIN:


    Suppose we have two tables, "employees" and "departments", with a common column "department_id" that establishes the relationship between them.

    Table: employees
    `
    +----+----------+---------------+
    | id | name | department_id |
    +----+----------+---------------+
    | 1 | John Doe | 1 |
    | 2 | Jane Smith | 2 |
    | 3 | Mike Johnson | 1 |
    +----+----------+---------------+

    Table: departments

    +----+-------------+
    | id | department |
    +----+-------------+
    | 1 | Sales |
    | 2 | Marketing |
    | 3 | IT |
    +----+-------------+

    To retrieve the employee names along with their corresponding departments, you can use the INNER JOIN as follows:

    sql
    SELECT employees.name, departments.department
    FROM employees
    INNER JOIN departments ON employees.department_id = departments.id;

    The result of the query will be:

    +----------+-------------+
    | name | department |
    +----------+-------------+
    | John Doe | Sales |
    | Jane Smith | Marketing |
    | Mike Johnson | Sales |
    +----------+-------------+

    In the query above, we specified the columns we want to retrieve (`employees.name` and `departments.department`) and used the INNER JOIN clause to join the "employees" and "departments" tables. The `join_condition` (`employees.department_id = departments.id`) specifies the relationship between the two tables based on the "department_id" column in the "employees" table and the "id" column in the "departments" table.


    By using INNER JOIN, only the matching rows from both tables are returned. Rows that don't have a match in the other table will be excluded from the result set.

    You can join multiple tables by extending the INNER JOIN syntax, adding more tables and join conditions as needed.

    Remember to replace "table1" and "table2" with the actual names of your tables, and adjust the column names and join conditions according to your specific database structure and requirements.



    About the Author



    Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

    We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





     PreviousNext