How to select common records from tables in SQL Server
Loading
How to select common records from tables in SQL Server
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Aaron BlakePosted Oct 25, 2025, 8:33 AM
This is a really helpful overview of selecting common records in SQL Server! I especially appreciate the clear examples using INNER JOIN and INTERSECT. Sometimes I find myself reaching for subqueries when a simple join would do the trick. Thinking about it, perhaps a custom function, something akin to a Sprunki function, that encapsulates the common record selection logic could also be a neat way to streamline queries if you're doing this repeatedly across multiple tables. Thanks for sharing!
Prasad RaveendranPosted Oct 25, 2025, 1:20 AM
I would appreciate if you could be more specific on the query and your use case
Sandhiya PriyaPosted Oct 24, 2025, 4:18 AM
In SQL Server, to select common records (i.e., rows that exist in both tables), you can use a few different methods depending on what you need.
Let’s go through them one by one
1. Using INNER JOIN (Most Common Method)
This method returns only the matching rows from both tables based on a common column.
Example:
Explanation:
INNER JOINreturns only rows that exist in both tables (AandB).Here, common records are identified using the
EmployeeID.2. Using INTERSECT Operator (Simplest Way)
The
INTERSECToperator directly returns common rows between twoSELECTqueries.Example:
Explanation:
INTERSECTautomatically removes duplicates (likeDISTINCT).It works only when both
SELECTstatements have the same number of columns and compatible data types.3. Using EXISTS (For Existence Checking)
The
EXISTSkeyword checks whether a record from one table exists in another.Example:
Explanation:
EXISTSreturns rows fromAwhere the sameEmployeeIDexists inB.Efficient for large datasets.
4. Using IN Operator (Simple Alternative)
If you just need to match one column (like an ID), use
IN.Example:
Explanation:
The subquery inside
INreturns allEmployeeIDs fromEmployees_Archive.Outer query picks rows with those IDs from
Employees.5. Using Common Table Expressions (CTE) + INNER JOIN
CTE can make your query more readable, especially when joining multiple tables.
Example:
Explanation:
CTE stores the common result for reuse or for additional filtering.
6. Using Temporary Table Comparison
If you have temporary or derived data:
Summary of Methods
INNER JOININTERSECTEXISTSINCTE + JOINAjay BansodePosted Aug 5, 2025, 2:15 PM
To select common records (also called intersection) from two tables in SQL Server, you can use:
1.
INNER JOINWhen you want to get matching rows based on a common column(s).
Example:
This returns rows from both tables where
IDexists in both.2.
INTERSECTWhen you want to get exact matching rows between two result sets (all columns must match).
Example:
This returns common rows (duplicates removed) between the two tables where all selected columns match.
3.
EXISTSWhen you want rows from one table that also exist in another.
Example:
4.
INA simple way to find common values in a single column.
Example:
Choose the method based on your goal:
Jignesh KumarPosted Aug 5, 2025, 3:47 AM
Hi Kiran,
To retrieve records that are present in both tables in SQL Server, you can use one of the following methods:
Cynthia SathuragiriPosted Aug 4, 2025, 5:51 AM
To get common records from two tables in SQL Server,you can use INNER JOIN if the tables share a common column or you can use INTERSECT if both tables have the same columns
SELECT a.*
FROM TableA a
INNER JOIN TableB b ON a.ID = b.ID
SELECT ID, Name FROM TableA
INTERSECT
SELECT ID, Name FROM TableB