{"id":1509,"date":"2021-01-03T09:24:13","date_gmt":"2021-01-03T09:24:13","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/sql-query-to-find-missing-records-from-tables\/"},"modified":"2025-05-26T17:47:05","modified_gmt":"2025-05-26T12:17:05","slug":"sql-query-to-find-missing-records-from-tables","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/sql-query-to-find-missing-records-from-tables\/","title":{"rendered":"SQL Query to Find Missing Records between Two Related Tables"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Big data requires a proper skill set to be able to make meaning out of it. SQL has become a very important tool in a data specialist\u2019s toolbox since it is critical for accessing, updating, inserting, manipulating, and modifying data. While working with a database, you may require to\u00a0<a href=\"https:\/\/meetanshi.com\/blog\/run-direct-sql-query-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">run direct SQL query in Magento 2<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Furthermore,&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/use-sql-query-for-case-sensitive-data-in-magento-2\/\">Magento 2 SQL Query for Managing Case-Sensitive Data<\/a>&nbsp;holds significance in this context. This feature ensures precise handling of data sensitivity to letter casing, facilitating accurate data manipulation and retrieval within the Magento 2 database environment. While working with tables in the database, you may want to compare records between two related tables and find the missing data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Today, I\u2019ve come up with the&nbsp;SQL query&nbsp;to find missing records between two related tables.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">SQL Query to Find Missing Records between Two Related Tables<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let us take a practical example of two database tables.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>eav_attribute_option table<\/li>\n\n\n\n<li>eav_attribute_option_swatch table<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>eav_attribute_option table<\/strong>&nbsp;that has a primary key on option_id and reference with the<strong>&nbsp;eav_attribute_option swatch table<\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th class=\"has-text-align-center\" data-align=\"center\"><strong>option_id<\/strong><\/th><th class=\"has-text-align-center\" data-align=\"center\"><strong>attribute_id<\/strong><\/th><th class=\"has-text-align-center\" data-align=\"center\"><strong>sort_order<\/strong><\/th><\/tr><\/thead><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\">1<\/td><td class=\"has-text-align-center\" data-align=\"center\">20<\/td><td class=\"has-text-align-center\" data-align=\"center\">0<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">2<\/td><td class=\"has-text-align-center\" data-align=\"center\">128<\/td><td class=\"has-text-align-center\" data-align=\"center\">1<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">3<\/td><td class=\"has-text-align-center\" data-align=\"center\">20<\/td><td class=\"has-text-align-center\" data-align=\"center\">2<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>eav_attribute_option_swatch table<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th class=\"has-text-align-center\" data-align=\"center\"><strong>swatch_id<\/strong><\/th><th class=\"has-text-align-center\" data-align=\"center\"><strong>option_id<\/strong><\/th><th class=\"has-text-align-center\" data-align=\"center\"><strong>store_id<\/strong><\/th><\/tr><\/thead><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\">1<\/td><td class=\"has-text-align-center\" data-align=\"center\">1<\/td><td class=\"has-text-align-center\" data-align=\"center\">0<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">2<\/td><td class=\"has-text-align-center\" data-align=\"center\">2<\/td><td class=\"has-text-align-center\" data-align=\"center\">0<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">3<\/td><td class=\"has-text-align-center\" data-align=\"center\">3<\/td><td class=\"has-text-align-center\" data-align=\"center\">0<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">4<\/td><td class=\"has-text-align-center\" data-align=\"center\">4<\/td><td class=\"has-text-align-center\" data-align=\"center\">0<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">5<\/td><td class=\"has-text-align-center\" data-align=\"center\">5<\/td><td class=\"has-text-align-center\" data-align=\"center\">0<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Here, these two tables have references to each other. Option_id 4 and 5 will not be found in the eav_attribute_option table.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the above example, we just have a small amount of data, but when it comes to a real-life scenario, databases have a large amount of data and the necessity often arrives to find missing data from related tables.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">While migrating the Magento store from Magento 1 to Magento 2, you may observe that some data is missing from the database. Hence, to solve this, the given solution helps you find the missing records between two related tables.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, the below query is a savior.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">SELECT eav_attribute_option_swatch.*\nFROM eav_attribute_option_swatch\nLEFT OUTER JOIN eav_attribute_option\nON eav_attribute_option_swatch.option_id = eav_attribute_option.option_id\nWHERE eav_attribute_option.option_id IS NULL<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here,&nbsp;<strong>eav_attribute_option_swatch<\/strong>&nbsp;and&nbsp;<strong>eav_attribute_option&nbsp;<\/strong>&nbsp;are the default databases of Magento 2.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s all!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Please consider sharing this post with Magento Community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank You<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Big data requires a proper skill set to be able to make meaning out of it. SQL has become a very important tool in a&#8230;<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1509","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1509","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1509"}],"version-history":[{"count":5,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1509\/revisions"}],"predecessor-version":[{"id":16078,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1509\/revisions\/16078"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1509"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}