Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare entire row of a table with entire row of another table

Is there a way in SQL Server to compare entire row of one table with another table which has same data types.

Example:

CREATE TABLE [DBO].[PRODUCT]
  (
     [PID]  [INT] NULL,
     [NAME] [NCHAR](10) NULL,
     [PDID] [INT] NULL
  )

select * from product p, product c where p.{entirerow} = c.{entirerow}

is there some kind of option here?

like image 241
sqlnewbie Avatar asked Aug 11 '11 10:08

sqlnewbie


People also ask

How do I compare two rows in a table?

Here's the SQL query to compare each row with previous row. In the above query, we join sales table with itself using an INNER JOIN condition g2.id=g1.id + 1 that allows you to compare each row with its previous row. Please note, this condition depends on the fact that our id column has consecutive numbers.

How can I compare two rows by rows in SQL Server?

Comparing the Results of the Two Queries Let us suppose, we have two tables: table1 and table2. Here, we will use UNION ALL to combine the records based on columns that need to compare. If the values in the columns that need to compare are the same, the COUNT(*) returns 2, otherwise the COUNT(*) returns 1.

Can we compare two rows in SQL?

Example 1: Comparing rows of the same table. In the example, we are comparing the immediate rows to calculate the sales made on a day by comparing the amounts of two consecutive days. Syntax for inner join : SELECT column_name(s) FROM table1 t1 INNER JOIN table1 t2 on t1. column1 = t2.

How do you find the difference between two tables in data?

Use the Find Unmatched Query Wizard to compare two tables One the Create tab, in the Queries group, click Query Wizard. In the New Query dialog box, double-click Find Unmatched Query Wizard. On the first page of the wizard, select the table that has unmatched records, and then click Next.


1 Answers

Use SQL INTERSECT(all matching rows) and EXCEPT (all different rows), here for example:

SELECT * FROM Table1 INTERSECT SELECT * FROM Table2
SELECT * FROM Table1 EXCEPT SELECT * FROM Table2

You can compare subsets of columns by replacing * with a column list or subsets of rows by using a WHERE clause.

One caveat is that the schema's of the tables need to be the same.

like image 200
Simon Avatar answered Oct 11 '22 10:10

Simon