Write a SQL query to find the common records between two tables?
Question
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
It will take less than 1 minute to register for lifetime. Bonus Tip - We don't send OTP to your email id Make Sure to use your own email id for free books and giveaways
Answers ( 3 )
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
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
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;