How to compare two tables each having 500 columns using PL-SQL - sql

I need to compare two tables in different databases and check whether the data in both tables are matching or not.
The compare should return a result showing rows that don't match using an exact column to column data check.
Is this possible in PL-SQL?

To return all rows in table1 that do not match exactly the rows in table2:
select * from table1 except select * from table2
And to return all rows in table1 that match exactly what is in table2:
select * from table1 intersect select * from table2

Related

DB2 - table contains data but a select statement on specific columns returns empty

The database is DB2.
A table contains data, and "SELECT * FROM TABLE1" statement returns the data correctly.
However the statements like the following return empty:
SELECT COL1 FROM TABLE1
Also SELECT COUNT(COL1) FROM TABLE1 returns ZERO but SELECT COUNT(*) FROM TABLE1 returns the correct result.
The table contains 7 columns, and only 3 columns of them have such a problems, all the other columns are fine.
This is the first time I met such a situation in recent 10 years.

How to Identify matching records in two tables?

I have two tables with same column names. There are a total 40 columns in each table. Both the tables have same unique IDs. If I perform an inner join on the ID columns I get a match on 80% of the data. However, I would like to see if this match has exactly same data in each of the columns.
If there were a few rows like say 50-100 I could have performed a simple union operation ordered by ID and manually checked for the data. But both the tables contain more than 5000 records.
Is a join on each of the columns a valid solution for this or do I need to perform concatenation?
Suppose you have N columns, you can add GROUP BY COL1,COL2,....COLN
select * from table1
union all
select * from table2
group by COL1, COL2, ... , COLN
having count(*)>1;
Reference: link

SQL MINUS showing no difference between first and second while shows difference between second and first

SQL MINUS is used as:
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions]
MINUS
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions];
In case I see no difference between first minus second but see a difference between second minus first, what does this signify? Is there any real difference? If so, then why and how may I get first minus second as no difference?
Please help.
You can refer the docs to understand the MINUS operator:
The Oracle MINUS operator is used to return all rows in the first
SELECT statement that are not returned by the second SELECT statement.
Each SELECT statement will define a dataset. The MINUS operator will
retrieve all records from the first dataset and then remove from the
results all records from the second dataset.
So if all the records of table1 exist in table2 then there will not be any record shown in the output. But when you reverse the tables and if there is a change in records then the same difference of records can be seen.
Of course there is, MINUS is substractring all the records from the first table, that appear on the second table.
Take this example:
TABLE1:
ID
1
2
4
Table2:
ID
1
2
4
5
SELECT * FROM TABLE1
MINUS
SELECT * FROM TABLE2
Will return nothing, since 1,2,4(all of table1 records) appear on table2 although they don't have exactly the same content.
As oppose to:
SELECT * FROM TABLE2
MINUS
SELECT * FROM TABLE1
Will return 5, because its the only value that doesn't appear on table1
So even if you are selecting from the same table, if you select different content(different where conditions) then minus won't work both sides equally .
If I understand you correctly, there are two cases of the problem as follows:
Table1 and Table2 have ame number of rows, but different values: In this case using Table1 Minus Table2 will have the same results as Table2 Minuse table1.
Different number of rows: In this case, Table1 Minus Table2 will only return those rows exists in Table1 and do not exist in Table2.
If you want to return rows exist in Table2, and do not exist in Table1, you have to write Table2 Minus Table1.
If you want to return all the differences, then you can use UNION ALL:
Table1 MINUS Table2
UNION All
Table2 MINUS Table1

SQL queries producing unexpected results

I've got a strange situation with two SQL queries that aren't producing the expected results. Here are the queries:
Query 1:
SELECT DISTINCT SomeCharValue
FROM Table1
JOIN Table2
ON Table1.SomeCharValue = Table2.SomeCharValue
ORDER BY SomeCharValue
Query 2:
SELECT DISTINCT SomeCharValue
FROM Table1
JOIN Table2
ON Table1.SomeCharValue <> Table2.SomeCharValue
ORDER BY SomeCharValue
I have two tables with columns of varchar(15). Table2 is essentially a small subset of the values in Table1, thus Table1 has all values stored in Table2. The problem is, the two queries should never produce the same results, yet they do. Both queries will produce the same result for certain values; for example, if Table1 and Table2 contain the word 'hello', then Query 1 should return it, while Query 2 should not. However, BOTH queries return 'hello'. It doesn't make sense that 'hello' in both tables is equal and not equal at the same time. I ran a length query to test the values, and some were a different size with trailing white spaces, but even after changing these to be an exact match, and verifying the hexadecimal value of the characters to be the same, the same results occur. I can't compare numeric key fields since there is no key relationship between these tables. I can only compare the exact character values in the columns. Any ideas?
Imagine you have table1 containing a and b as separate rows, and table2 has the exact same contents.
Now for your second query, table1's row a will be compared to both the rows in table2. It will pass the ON clause when comparing to row b in table2, and hence a will be in your result set. Similarly for the b row in table1 which will pass the ON clause when compared to the a row in table2.
You could rewrite the query as
SELECT DISTINCT SomeCharValue
FROM TABLE1
WHERE SomeCharValue NOT IN (SELECT DISTINCT SomeCharValue FROM Table2)
ORDER BY SomeCharValue
Did you try to use NOT LIKE instead of <>

Proper way of querying table columns in SQL?

I have about 6 tables where some of the columns are identical. Do I have to know which tables contain the column I'm querying on or is there a way to write an SQL query such that I can reference a column and the database will scan the tables looking for a specific column?
For example, assume table1, table3, and table5 all contain the column 'Population'. Do I have to specify in my query that I want to retrieve information from 'Population' in tables table1, table2, and table3, or can is there a way to only specify that I want information from the 'Population' column without specifying any tables?
select table1.population as pop1, table2.population as pop2, table5.population as pop3
from table1, table2, table5;
This will return 3 columns showing the population from each table.
select population
from table1
union
select population
from table2
union
select population
from table5;
This will return a long list of populations in one column.