Write a SQL query to find the common records between two tables?

Question

Provide multiple ways

in progress 0
TheDataMonk 4 years 3 Answers 4789 views Grand Master 0

Answers ( 3 )

  1. 1) Using Intersect

    (Select * from Employee1)
    Intersect
    (Select * from Employee2)

    2) Using Inner join

    select * from Employee1 e1
    inner join Emplyee2 e2 ON
    e1.id = e2.id

  2. 1. INTERSECT
    SELECT emp_id , Name FROM employee1 E1
    INTERSECT
    SELECT emp_id , Name from employee2 E2

    Note : To use intersect , columns in both the table should be same and written in same order.

    2. JOIN – Equi Join / Inner Join

    select E1.emp_id , E1.Name from Employee1 E1
    INNER JOIN employee1 E2 ON
    E1.emp_id = E2.emp_id
    INNER JOIN employee3 E3
    E2.emp_id = E3.emp_id

    Note : If there are n number of tables , we can use inner join n-1 times to get the records which are common among them

  3. INNER JOIN helps us to get the common records.
    Suppose we want to get the records from two tables who are having the same ID’s
    select * from Table1 T1
    inner join Table2 ON
    T1.id = T2.id;

Leave an answer

Browse
Browse