Nick
Nick

Reputation: 500

SQL Server finding same records in two different tables

I have two tables called TableX and TableY. Both are identical table with following columns

ID int
Sname varchar(256)

TableX has following data

enter image description here

TableY has following data

enter image description here

How do I write a SQL Server statement that show me all matching record that in TableX and TableY on the SName?

The results I want to see is

enter image description here

I want to match it only by SName.

thanks community Nick

Upvotes: 0

Views: 47

Answers (1)

M.Ali
M.Ali

Reputation: 69494

SELECT SNAME FROM TABLEX
INTERSECT
SELECT SNAME FROM TABLEY

OR

SELECT X.SNAME
FROM TABLEX X INNER JOIN TABLEY Y
ON X.SNAME = Y.SNAME

OR

SELECT X.SNAME
FROM TABLEX X
WHERE EXISTS (SELECT 1
              FROM TABLEY
              WHERE SNAME = X.SNAME)

Upvotes: 2

Related Questions