{"id":393,"date":"2010-02-17T12:33:17","date_gmt":"2010-02-17T19:33:17","guid":{"rendered":"https:\/\/simpleprogrammer.com\/?p=393"},"modified":"2013-07-30T11:20:32","modified_gmt":"2013-07-30T15:20:32","slug":"using-sql-to-check-complicated-sql","status":"publish","type":"post","link":"https:\/\/simpleprogrammer.com\/using-sql-to-check-complicated-sql\/","title":{"rendered":"Using SQL to Check Complicated SQL"},"content":{"rendered":"<h3>Complicated Queries<\/h3>\n<p>I frequently find myself in the position of having to write fairly complex SQL queries in order to get a list of data that matches a certain criteria. \u00a0One problem with complicated SQL queries is that it is very hard to know if the result you have is correct, especially when your query involves multi-table joins and sub-selects. \u00a0Often, a small error in one part of a complex SQL query can go unnoticed because it doesn&#8217;t greatly change the count of the results.<\/p>\n<p>Unfortunately, sometimes there is just no way to make a simple query to achieve a certain result. \u00a0Often, a large amount of the information applied to the data in order to create the query requires outside knowledge that is not found in the relationships directly specified in the database. \u00a0In situations like these a solid technique for verifying the results of the query is important.<\/p>\n<h3><strong>SQL checking SQL<\/strong><\/h3>\n<p>I have started using a technique of approaching a query from two different directions in order to check my results. \u00a0The idea is that <strong>if you can think of more than one way to get the same set of data you are looking for, and each way returns the same set or results, the\u00a0likelihood\u00a0of that data being correct is greatly increased.<\/strong><\/p>\n<p>There was a scene in <a href=\"http:\/\/www.amazon.com\/gp\/product\/B003BQROIQ\/ref=as_li_ss_tl?ie=UTF8&amp;camp=1789&amp;creative=390957&amp;creativeASIN=B003BQROIQ&amp;linkCode=as2&amp;tag=makithecompsi-20\" target=\"_blank\">Apollo 13<\/a> where <a href=\"http:\/\/www.amazon.com\/gp\/search\/?ie=UTF8&amp;bbn=2625373011&amp;camp=1789&amp;creative=390957&amp;field-lbr_actors_browse-bin=Tom%20Hanks&amp;linkCode=ur2&amp;qid=1375197353&amp;rh=n%3A2625373011&amp;tag=makithecompsi-20&amp;x=-744&amp;y=-197\" target=\"_blank\">Tom Hanks<\/a> manually calculated some trajectory and 3 guys in Houston all checked his math to make sure it was correct. \u00a0When they all came up with the same answer, everyone could be pretty sure the calculation was correct.<\/p>\n<p><a href=\"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/02\/apollo13missioncontrol1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-394\" title=\"Apollo13MissionControl1\" alt=\"\" src=\"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/02\/apollo13missioncontrol1.jpg\" width=\"500\" height=\"375\" srcset=\"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/02\/apollo13missioncontrol1.jpg 640w, https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/02\/apollo13missioncontrol1-300x225.jpg 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p>That is the idea behind SQL checking SQL. \u00a0If you can write a second query to get the same data, but from a different angle, there is a pretty good chance that the query is correct. \u00a0The best way to do this would be to have another person write the second query, knowing nothing about how you are doing the first.<\/p>\n<h3>A Practical Example<\/h3>\n<p>Sounds good, but how can I actually use it? \u00a0Let&#8217;s say that you have a database which stores job applications and resumes from applicants. \u00a0Let&#8217;s say that you want to create a list of all the applicants last week.<\/p>\n<p>We could approach this problem from two angles.<\/p>\n<ul>\n<li>Get a list of all the people who had an application where the application date was last week.<\/li>\n<li>Get a list of all the people who had a resume created.<\/li>\n<\/ul>\n<p>We have to use some outside information here. \u00a0We have to know that in our particular program each applicant for a job can only have one resume submitted and they must have one submitted. \u00a0Then we know that we should see the same list of people who applied for jobs and had resumes created in that time frame.<\/p>\n<p>Of course this is a really simple example and given all the conditions it would be easy to be pretty sure of either of these queries without running both. \u00a0The problem is when you have a much larger set of tables you are interacting with and shakier sets of relations between the tables and data.<\/p>\n<h3>Tips and Tricks<\/h3>\n<p>Once you have created both queries to get the same set of resulting data, how can you easily check to see if they contain the same data? \u00a0The easiest way is to do a difference in SQL. \u00a0Depending on what flavor of SQL you are using, the syntax is slightly different. \u00a0You can look up the set operations for your flavor of SQL. \u00a0Difference is the opposite of union. \u00a0Difference allows you to see what records exist in the first query and not the second. I like to use difference with the 1st query first and with the 2nd query first, so that I can see what records are in either query, but not in the other. \u00a0Any records I find, I closely examine to figure out why they are in one and not the other. \u00a0Each time I do this, I then makes changes to the query that was wrong until all the results match exactly.<\/p>\n<p>This balancing of the two queries helps to highlight problems that could be missed from either viewpoint of the problem. \u00a0After balancing out both queries to have exactly the same results, there is a really good chance the results are correct. \u00a0Certainly the chances of your query being correct are increased vs &#8220;just winging it.&#8221;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Complicated Queries I frequently find myself in the position of having to write fairly complex SQL queries in order to get a list of data that matches a certain criteria. \u00a0One problem with complicated SQL queries is that it is very hard to know if the result you have is correct, especially when your query&#8230;<\/p>\n","protected":false},"author":2,"featured_media":394,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[475,70886,31679,12],"tags":[],"class_list":["post-393","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-database","category-debugging","category-process-improvement","category-testing"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Using SQL to Check Complicated SQL<\/title>\n<meta name=\"description\" content=\"An explanation of why a complicated SQL query should be checked by a second SQL query. When running two different queries the result set should be the same.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/simpleprogrammer.com\/using-sql-to-check-complicated-sql\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Using SQL to Check Complicated SQL\" \/>\n<meta property=\"og:description\" content=\"An explanation of why a complicated SQL query should be checked by a second SQL query. When running two different queries the result set should be the same.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/simpleprogrammer.com\/using-sql-to-check-complicated-sql\/\" \/>\n<meta property=\"og:site_name\" content=\"Simple Programmer\" \/>\n<meta property=\"article:published_time\" content=\"2010-02-17T19:33:17+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2013-07-30T15:20:32+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/02\/apollo13missioncontrol1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"640\" \/>\n\t<meta property=\"og:image:height\" content=\"480\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"John Sonmez\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"John Sonmez\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/using-sql-to-check-complicated-sql\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/using-sql-to-check-complicated-sql\\\/\"},\"author\":{\"name\":\"John Sonmez\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#\\\/schema\\\/person\\\/1abc70edfb189184827740310672de67\"},\"headline\":\"Using SQL to Check Complicated SQL\",\"datePublished\":\"2010-02-17T19:33:17+00:00\",\"dateModified\":\"2013-07-30T15:20:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/using-sql-to-check-complicated-sql\\\/\"},\"wordCount\":739,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/using-sql-to-check-complicated-sql\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/simpleprogrammer.com\\\/wp-content\\\/uploads\\\/2010\\\/02\\\/apollo13missioncontrol1.jpg\",\"articleSection\":[\"Database\",\"Debugging\",\"Process Improvement\",\"Testing\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/simpleprogrammer.com\\\/using-sql-to-check-complicated-sql\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/using-sql-to-check-complicated-sql\\\/\",\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/using-sql-to-check-complicated-sql\\\/\",\"name\":\"Using SQL to Check Complicated SQL\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/using-sql-to-check-complicated-sql\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/using-sql-to-check-complicated-sql\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/simpleprogrammer.com\\\/wp-content\\\/uploads\\\/2010\\\/02\\\/apollo13missioncontrol1.jpg\",\"datePublished\":\"2010-02-17T19:33:17+00:00\",\"dateModified\":\"2013-07-30T15:20:32+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#\\\/schema\\\/person\\\/1abc70edfb189184827740310672de67\"},\"description\":\"An explanation of why a complicated SQL query should be checked by a second SQL query. When running two different queries the result set should be the same.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/using-sql-to-check-complicated-sql\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/simpleprogrammer.com\\\/using-sql-to-check-complicated-sql\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/using-sql-to-check-complicated-sql\\\/#primaryimage\",\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/wp-content\\\/uploads\\\/2010\\\/02\\\/apollo13missioncontrol1.jpg\",\"contentUrl\":\"https:\\\/\\\/simpleprogrammer.com\\\/wp-content\\\/uploads\\\/2010\\\/02\\\/apollo13missioncontrol1.jpg\",\"width\":640,\"height\":480,\"caption\":\"apollo 13 image to show why complicated sql should be checked\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/using-sql-to-check-complicated-sql\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/simpleprogrammer.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Using SQL to Check Complicated SQL\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#website\",\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/\",\"name\":\"Simple Programmer\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/simpleprogrammer.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#\\\/schema\\\/person\\\/1abc70edfb189184827740310672de67\",\"name\":\"John Sonmez\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g\",\"caption\":\"John Sonmez\"},\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/author\\\/jsonmez\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Using SQL to Check Complicated SQL","description":"An explanation of why a complicated SQL query should be checked by a second SQL query. When running two different queries the result set should be the same.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/simpleprogrammer.com\/using-sql-to-check-complicated-sql\/","og_locale":"en_US","og_type":"article","og_title":"Using SQL to Check Complicated SQL","og_description":"An explanation of why a complicated SQL query should be checked by a second SQL query. When running two different queries the result set should be the same.","og_url":"https:\/\/simpleprogrammer.com\/using-sql-to-check-complicated-sql\/","og_site_name":"Simple Programmer","article_published_time":"2010-02-17T19:33:17+00:00","article_modified_time":"2013-07-30T15:20:32+00:00","og_image":[{"width":640,"height":480,"url":"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/02\/apollo13missioncontrol1.jpg","type":"image\/jpeg"}],"author":"John Sonmez","twitter_card":"summary_large_image","twitter_misc":{"Written by":"John Sonmez","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/simpleprogrammer.com\/using-sql-to-check-complicated-sql\/#article","isPartOf":{"@id":"https:\/\/simpleprogrammer.com\/using-sql-to-check-complicated-sql\/"},"author":{"name":"John Sonmez","@id":"https:\/\/simpleprogrammer.com\/#\/schema\/person\/1abc70edfb189184827740310672de67"},"headline":"Using SQL to Check Complicated SQL","datePublished":"2010-02-17T19:33:17+00:00","dateModified":"2013-07-30T15:20:32+00:00","mainEntityOfPage":{"@id":"https:\/\/simpleprogrammer.com\/using-sql-to-check-complicated-sql\/"},"wordCount":739,"commentCount":0,"image":{"@id":"https:\/\/simpleprogrammer.com\/using-sql-to-check-complicated-sql\/#primaryimage"},"thumbnailUrl":"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/02\/apollo13missioncontrol1.jpg","articleSection":["Database","Debugging","Process Improvement","Testing"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/simpleprogrammer.com\/using-sql-to-check-complicated-sql\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/simpleprogrammer.com\/using-sql-to-check-complicated-sql\/","url":"https:\/\/simpleprogrammer.com\/using-sql-to-check-complicated-sql\/","name":"Using SQL to Check Complicated SQL","isPartOf":{"@id":"https:\/\/simpleprogrammer.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/simpleprogrammer.com\/using-sql-to-check-complicated-sql\/#primaryimage"},"image":{"@id":"https:\/\/simpleprogrammer.com\/using-sql-to-check-complicated-sql\/#primaryimage"},"thumbnailUrl":"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/02\/apollo13missioncontrol1.jpg","datePublished":"2010-02-17T19:33:17+00:00","dateModified":"2013-07-30T15:20:32+00:00","author":{"@id":"https:\/\/simpleprogrammer.com\/#\/schema\/person\/1abc70edfb189184827740310672de67"},"description":"An explanation of why a complicated SQL query should be checked by a second SQL query. When running two different queries the result set should be the same.","breadcrumb":{"@id":"https:\/\/simpleprogrammer.com\/using-sql-to-check-complicated-sql\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/simpleprogrammer.com\/using-sql-to-check-complicated-sql\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/simpleprogrammer.com\/using-sql-to-check-complicated-sql\/#primaryimage","url":"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/02\/apollo13missioncontrol1.jpg","contentUrl":"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/02\/apollo13missioncontrol1.jpg","width":640,"height":480,"caption":"apollo 13 image to show why complicated sql should be checked"},{"@type":"BreadcrumbList","@id":"https:\/\/simpleprogrammer.com\/using-sql-to-check-complicated-sql\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/simpleprogrammer.com\/"},{"@type":"ListItem","position":2,"name":"Using SQL to Check Complicated SQL"}]},{"@type":"WebSite","@id":"https:\/\/simpleprogrammer.com\/#website","url":"https:\/\/simpleprogrammer.com\/","name":"Simple Programmer","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/simpleprogrammer.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/simpleprogrammer.com\/#\/schema\/person\/1abc70edfb189184827740310672de67","name":"John Sonmez","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g","caption":"John Sonmez"},"url":"https:\/\/simpleprogrammer.com\/author\/jsonmez\/"}]}},"_links":{"self":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/posts\/393","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/comments?post=393"}],"version-history":[{"count":0,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/posts\/393\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/media\/394"}],"wp:attachment":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/media?parent=393"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/categories?post=393"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/tags?post=393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}