Proving SQL query equivalency

后端 未结 9 2427
一整个雨季
一整个雨季 2020-12-15 05:33

How would you go about proving that two queries are functionally equivalent, eg they will always both return the same result set.


As I had a specific query in

9条回答
  •  隐瞒了意图╮
    2020-12-15 06:11

    1) Real equivalency proof with Cosette:
    Cosette checks (with a proof) if 2 SQL query's are equivalent and counter examples when not equivalent. It's the only way to be absolutely sure, well almost ;) You can even throw in 2 query's on their website and check (formal) equivalence right away.

    Link to Cosette: http://cosette.cs.washington.edu/

    Link to article that gives a good explanation of how Cosette works: https://medium.com/@uwdb/introducing-cosette-527898504bd6


    2) Or if you're just looking for a quick practical fix:
    Try this stackoverflow answer: [sql - check if two select's are equal]
    Which comes down to:

    (select * from query1 MINUS select * from query2) 
    UNION ALL
    (select * from query2 MINUS select * from query1)
    

    This query gives you all rows that are returned by only one of the queries.

提交回复
热议问题