Show
Read these next...
Say you have requirement to compare two tables. You have two tables in same database or server that you wish to compare, and check if any changes in the column values or see if any row is missing in either of tables. Below are some of the methods you can use to compare two tables in SQL. Compare Two Tables using UNION ALLUNION allows you to compare data from two similar tables or data sets. It also handles the NULL values to other NULL values which JOIN or WHERE clause doesn’t handle. It allows quickly checking what are the data missing or changed in either table.
The above query returns the all rows from both tables as old and new. You can quickly verify the differences between two tables. Related reading: Steps to Optimize SQL Query Performance Compare Two Table using MINUSYou can compare the two similar tables or data sets using MINUS operator. It returns all rows in table 1 that do not exist or changed in the other table.
You can quickly check how many records are having mismatch between two tables. The only drawback with using UNION and MINUS is that the tables must have the same number of columns and the data types must match. Compare Two Table using JOINThis is the easiest but user has to do some additional work to get the correct result. In this approach you can join the two tables on the primary key of the two tables and use case statement to check whether particular column is matching between two tables.
The only drawback of using JOIN is, it cannot compare the NULL values, and you should use the NVL on the column that may have null values in it. Compare Two Table using NOT EXISTSThe other faster method is to use the NOT EXISTS in WHERE clause of the query. This method is faster and performs well on the large volume of data.
In this approach also you have to use the NVL on the columns which contains NULL in it. Compare Cells From Two Tables – Cell by Cell ValidationYou can use the CASE statement to compare records from both tables and check if the cells are matching from both tables. For example, following example demonstrates the cell by cell validation.
You can use LEFT OUTER JOIN or INNER JOIN if you know the table count is matching. Get Matched and Unmatched Count from Two TablesYou can use full outer join to get matched and unmatched records or count from two tables which has common columns in it. SELECT Sum(CASE WHEN t1.file_name IS NOT NULL AND t2.file_n IS NOT NULL THEN 1 ELSE 0 END) AS matched_count, Sum( CASE WHEN t1.file_name IS NOT NULL AND t2.file_n IS NOT NULL THEN 0 ELSE 1 END) AS un_matched_count FROM assessment_file_query_mapping AS t1 FULL OUTER JOIN ( SELECT * FROM (SELECT DISTINCT regexp_split_to_table(files, e',') AS file_n FROM job_info) AS a WHERE length(trim(file_n)) <>0 ) AS t2 ON trim(t1.file_name) = trim(file_n); We are checking the matched and unmatched file count from two tables. Example uses regular expression to split the comma separated records into table and then join on common file name column. Related Posts:
How can I compare two tables in Oracle database?Comparing Diffs Between Two Oracle Database Schemas. Select Source and Target.. Select a schema or several schemas (if the tool provides such possibility) for comparison.. Tune comparison process by checking the needed options (an optional step). View comparison results.. How do you compare values in two tables?Compare two tables by using joins. To compare two tables by using joins, you create a select query that includes both tables. If there is not already an existing relationship between the tables on the fields that contain the corresponding data, you create a join on the fields that you want to examine for matches.
How do you compare data between two tables in different databases?Comparing Database Data. On the SQL menu, point to Data Compare, and then click New Data Comparison. ... . Identify the source and target databases. ... . Select the check boxes for the tables and views that you want to compare.. How can I compare two table values in SQL?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.
|