Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7506629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:19:20+00:00 2026-05-29T22:19:20+00:00

Suppose I have 2 tables, each tables has N columns. There are NO duplicate

  • 0

Suppose I have 2 tables, each tables has N columns. There are NO duplicate rows in table1

And now we want to know what datasets in table2 (including duplicates) are also contained in table1.

I tried

select * from table1
intersect
select * from table2

But this only gives me unique rows that are in both tables. But I don’t want unique rows, are want to see all rows in table2 that are in table1…

Keep in mind!! I cannot do

select *
from table1 a, table b
where a.table1col = b.table2col

…because I don’t know the number of columns of the tables at runtime.

Sure I could do something with dynamic SQL and iterate over the column numbers but I’m asking this precisely because it seems too simple a query for that kind of stuff..

Example:

create table table1 (table1col int)
create table table2 (table2col int)

insert into table1 values (8)
insert into table1 values (7)

insert into table2 values (1)
insert into table2 values (8)
insert into table2 values (7)
insert into table2 values (7)
insert into table2 values (2)
insert into table2 values (9)

I want my query then to return:

8
7
7
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T22:19:21+00:00Added an answer on May 29, 2026 at 10:19 pm

    If the amount of columns is not know, you will have to resort to a value computed over a row to make a match.

    One such function is CHECKSUM.

    Returns the checksum value computed over a row of a table, or over a
    list of expressions. CHECKSUM is intended for use in building hash
    indices.

    SQL Statement

    SELECT  tm.*
    FROM    (
              SELECT  CS = CHECKSUM(*)
              FROM    Table2
            ) tm          
            INNER JOIN (          
              SELECT  CS = CHECKSUM(*)
              FROM    Table2          
              INTERSECT
              SELECT  CHECKSUM(*)          
              FROM    Table1
            ) ti ON ti.CS = tm.CS
    

    Note that CHECKSUM might introduce collisions. You will have to test for that before doing any operation on your data.

    Edit

    In case you are using SQL Server 2005, you might make this a bit more robust by throwing in HASH_BYTES.

    The downside of HASH_BYTESis that you need to specify the columns on which you want to operate but for all the columns you do known up-front, you could use this to prevent collisions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have two tables A and B and each one has only 1
Suppose I have a tags table with two columns: tagid and contentid . Each
Suppose I have two tables that are linked (one has a foreign key to
Ok I have a database with about 1800 rows, each has a column lat
Suppose I have two SQL tables: Customers and PhoneNumbers. Suppose that Customers has the
I have a table with multiple rows. Each row has a check button. How
Suppose that I have an n -sided loaded die, where each side k has
Suppose I have a table nodes where I store a tree. Each node has
I have a grid view. Which contains some columns. Suppose, it has 10 columns
I have this schema: There is a table for persons. Each person has one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.