Is there a way to check if two (non-trivial) select are equivalent?
Initially I was hoping for a formally equivalence between two selects, but the answers in proving-sql-query-equivalency stop me.
For my actual need I can just check if the (actual) results of two selects are the same.
By using UNION, UNION ALL, EXCEPT, and INTERSECT, we can compare two queries and get the necessary output as per our requirement. Given examples are simple to follow, we can have complex queries and can apply the mentioned constructs in them and can compare.
Run the queries and compare logical reads for the various tables and execution times. @CombatCaptain You can also stack the comparing queries together in SSMS and press CTRL+M (include actual execution plan) and then F5 .
In SQL, problems require us to compare two columns for equality to achieve certain desired results. This can be achieved through the use of the =(equal to) operator between 2 columns names to be compared.
If you want to compare the query results try the following:
(select * from query1 MINUS select * from query2) UNION ALL (select * from query2 MINUS select * from query1)
This will result in all rows that are returned by only one of the queries.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With