{"id":4948,"date":"2015-12-02T10:00:57","date_gmt":"2015-12-02T01:00:57","guid":{"rendered":"http:\/\/www.skyarch.net\/blog\/?p=4948"},"modified":"2015-12-01T17:37:20","modified_gmt":"2015-12-01T08:37:20","slug":"learn-sql-sub-query-in-easy-way","status":"publish","type":"post","link":"https:\/\/www.skyarch.net\/blog\/en\/learn-sql-sub-query-in-easy-way\/","title":{"rendered":"Learn SQL Sub-Query in Easy Way"},"content":{"rendered":"<h1><span style=\"color: #ff6600\"><strong>SQL Sub-Query<\/strong><\/span><\/h1>\n<blockquote style=\"padding: 40px 5px !important\">\n<h3 id=\"loom-header-2\" style=\"padding: 0px;margin-top: 5px\"><span style=\"color: #ff9900;font-size: 18px\">\u27a3<\/span>\u00a0 Sub-queries are queries embedded in queries.<\/h3>\n<h3 id=\"loom-header-3\" style=\"padding: 0px;margin-top: 5px\"><span style=\"color: #ff9900;font-size: 18px\">\u27a3<\/span>\u00a0 They are used to retrieve data from one table based on data in another table.<\/h3>\n<h3 id=\"loom-header-4\" style=\"padding: 0px;margin-top: 5px\"><span style=\"color: #ff9900;font-size: 18px\">\u27a3<\/span>\u00a0 Generally used when tables have some kind of relationship.<\/h3>\n<h3 id=\"loom-header-5\" style=\"padding: 0px;margin-top: 5px\"><span style=\"color: #ff9900;font-size: 18px\">\u27a3<\/span>\u00a0 Meaning a sub-query SELECT statement can standalone and is NOT\u00a0depended on the statement in which it is nested.<\/h3>\n<\/blockquote>\n<p><strong style=\"color: #cc6600;font-size: 17px\">The sub-query can contain any valid SELECT statement, but it must return a single column with the expected number of results.<\/strong><\/p>\n<p><strong style=\"font-size: 15px\">IF<\/strong>\u00a0the sub-query returns only one result, then the main query can check for equality, inequality, greater than, less than, etc.<br \/>\nOn the other hand, if the subquery returns more than one record, the main query must check to see if a field value is (or is <strong>NOT<\/strong> ) <strong>IN<\/strong> the set of values returned.<\/p>\n<p><strong style=\"font-size: 15px\">FOR<\/strong>\u00a0example in database, the <strong><u>Orders Table<\/u><\/strong> has a <i>customer_id<\/i> field, which references a customer in the <strong><u>Customers Table<\/u><\/strong>.\u00a0Thus, retrieving data for a specific order is pretty straightforward.<\/p>\n<p>&nbsp;<\/p>\n<table>\n<thead>\n<tr>\n<th><strong>id<\/strong><\/th>\n<th><strong>first_name<\/strong><\/th>\n<th><strong>last_name<\/strong><\/th>\n<th><strong>age<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>john<\/td>\n<td>wall<\/td>\n<td>24<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>mark<\/td>\n<td>smith<\/td>\n<td>34<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>james<\/td>\n<td>lee<\/td>\n<td>31<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>shiela<\/td>\n<td>maye<\/td>\n<td>22<\/td>\n<\/tr>\n<\/tbody>\n<caption style=\"text-align: left;color: #ff6600\"><strong>CUSTOMERS TABLE<\/strong><\/caption>\n<\/table>\n<table>\n<thead>\n<tr>\n<th><strong>id<\/strong><\/th>\n<th><strong>customer_id<\/strong><\/th>\n<th><strong>product<\/strong><\/th>\n<th><strong>category<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>4<\/td>\n<td>casserole<\/td>\n<td>kitchen<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2<\/td>\n<td>grass cutter<\/td>\n<td>garage<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>1<\/td>\n<td>wiper<\/td>\n<td>general<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>1<\/td>\n<td>rice cooker<\/td>\n<td>electronic<\/td>\n<\/tr>\n<\/tbody>\n<caption style=\"text-align: left;color: #ff6600\"><strong>ORDERS TABLE<\/strong><\/caption>\n<\/table>\n<p><strong>Detailed Examples<\/strong> :<\/p>\n<p><strong>Note:<\/strong><br \/>\n<b>c<\/b> = alias for customers<br \/>\n<b>o<\/b> = alias for orders<\/p>\n<h3 id=\"loom-header-6\"><strong style=\"color: #cc6600;font-size: 16px\">\u2666 Let's start with a simple SQL Left Join Query.<\/strong><\/h3>\n<p style=\"line-height: 25px;background-color: #eee;padding: 5px 10px 5px 10px;font-weight: bold;font-size: 13px\">SELECT *\u00a0FROM\u00a0customers c<br \/>\nLEFT JOIN orders o<br \/>\non c.id = o.customer_id<br \/>\nWHERE\u00a0o.product = 'wiper';<\/p>\n<p><strong>Step by Step Process<\/strong> :<\/p>\n<ol>\n<li>Selecting all the data from Customers Table with <strong>matched<\/strong> data in Orders Table <strong>(see 1-a Table)<\/strong>.<\/li>\n<li>Joining\/merging the data from Orders Table where ID (customers table) is EQUAL to CUSTOMER_ID (orders table) <strong>(see 2-a Table)<\/strong>.<\/li>\n<li>Filter the results by (WHERE) clause, <strong>product<\/strong> (orders table) is <strong>EQUAL<\/strong> to ' <strong>wiper<\/strong> ' <strong>(see 3-a Table)<\/strong>.<\/li>\n<\/ol>\n<table>\n<thead>\n<tr>\n<th><strong>id<\/strong><\/th>\n<th><strong>first_name<\/strong><\/th>\n<th><strong>last_name<\/strong><\/th>\n<th><strong>age<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>john<\/td>\n<td>wall<\/td>\n<td>24<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>mark<\/td>\n<td>smith<\/td>\n<td>34<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>shiela<\/td>\n<td>maye<\/td>\n<td>22<\/td>\n<\/tr>\n<\/tbody>\n<caption style=\"text-align: left\"><strong>1-a Table<\/strong>:<\/caption>\n<\/table>\n<table>\n<thead>\n<tr>\n<th><strong>id<\/strong><\/th>\n<th><strong>first_name<\/strong><\/th>\n<th><strong>last_name<\/strong><\/th>\n<th><strong>age<\/strong><\/th>\n<th><strong>customer_id<\/strong><\/th>\n<th><strong>product<\/strong><\/th>\n<th><strong>category<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>john<\/td>\n<td>wall<\/td>\n<td>24<\/td>\n<td>1<\/td>\n<td>wiper<\/td>\n<td>general<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>john<\/td>\n<td>wall<\/td>\n<td>24<\/td>\n<td>1<\/td>\n<td>rice cooker<\/td>\n<td>electronic<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>mark<\/td>\n<td>smith<\/td>\n<td>34<\/td>\n<td>2<\/td>\n<td>grass cutter<\/td>\n<td>garage<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>shiela<\/td>\n<td>maye<\/td>\n<td>22<\/td>\n<td>4<\/td>\n<td>casserole<\/td>\n<td>kitchen<\/td>\n<\/tr>\n<\/tbody>\n<caption style=\"text-align: left\"><strong>2-a Table<\/strong>:<\/caption>\n<\/table>\n<table>\n<thead>\n<tr>\n<th><strong>id<\/strong><\/th>\n<th><strong>first_name<\/strong><\/th>\n<th><strong>last_name<\/strong><\/th>\n<th><strong>age<\/strong><\/th>\n<th><strong>customer_id<\/strong><\/th>\n<th><strong>product<\/strong><\/th>\n<th><strong>category<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>john<\/td>\n<td>wall<\/td>\n<td>24<\/td>\n<td>1<\/td>\n<td>wiper<\/td>\n<td>general<\/td>\n<\/tr>\n<\/tbody>\n<caption style=\"text-align: left\"><strong>3-a Table - FINAL RESULT<\/strong>:<\/caption>\n<\/table>\n<p style=\"border: 1px dashed #e68a00;height: auto;padding: 5px\"><strong style=\"color: #e68a00\">CONCLUSION<\/strong><b>:<\/b> Our first query gathers all the data first by executing the <strong>SELECT<\/strong>, <strong>JOIN<\/strong> functions, then by setting the <strong>WHERE<\/strong> clause at the end of query will filter the data given by the first executed functions.<\/p>\n<h3><\/h3>\n<h3 id=\"loom-header-6\"><strong style=\"color: #cc6600;font-size: 16px\">\u2666 Next, let's do the SQL Join with Sub-Query.<\/strong><\/h3>\n<p style=\"line-height: 25px;background-color: #eee;padding: 5px 10px 5px 10px;font-weight: bold;font-size: 13px\">SELECT * FROM\u00a0customers c<br \/>\nLEFT JOIN (\u00a0SELECT * FROM\u00a0orders o WHERE\u00a0o.product = 'wiper'\u00a0) as sub_result<br \/>\non c.id = sub_result.customer_id;<\/p>\n<p><strong>Step by Step Process<\/strong> :<\/p>\n<ol>\n<li>Selecting all the data from Customers Table with <strong>matched<\/strong> data in Orders Table <strong>(see 1-b Table)<\/strong>.<\/li>\n<li>Selecting all the data from Orders Table where <strong>product<\/strong> (orders table) is <strong>EQUAL<\/strong> ' <strong>wiper<\/strong> ' <strong>(see 2-b Table)<\/strong>.<\/li>\n<li>Joining\/merging data from Step1 and Step2 with filter clause <strong>(see 3-b Table)<\/strong> .<br \/>\n3-1: Filter the results by (WHERE) clause statement.<br \/>\n3-2. All data from Step1 will remain, appending the data from Step2 if matched, <strong>id<\/strong> (customers table) is <strong>EQUAL<\/strong> to <strong>customer_id<\/strong> (orders table).<br \/>\n3-3. All data from Step1 with NO matched, will append and set to <strong>null value<\/strong> .<\/li>\n<\/ol>\n<table>\n<thead>\n<tr>\n<th><strong>id<\/strong><\/th>\n<th><strong>first_name<\/strong><\/th>\n<th><strong>last_name<\/strong><\/th>\n<th><strong>age<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>john<\/td>\n<td>wall<\/td>\n<td>24<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>mark<\/td>\n<td>smith<\/td>\n<td>34<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>shiela<\/td>\n<td>maye<\/td>\n<td>22<\/td>\n<\/tr>\n<\/tbody>\n<caption style=\"text-align: left\"><strong>1-b Table<\/strong>:<\/caption>\n<\/table>\n<table>\n<thead>\n<tr>\n<th><strong>id<\/strong><\/th>\n<th><strong>customer_id<\/strong><\/th>\n<th><strong>product<\/strong><\/th>\n<th><strong>category<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>3<\/td>\n<td>1<\/td>\n<td>wiper<\/td>\n<td>general<\/td>\n<\/tr>\n<\/tbody>\n<caption style=\"text-align: left\"><strong>2-b Table<\/strong>:<\/caption>\n<\/table>\n<table>\n<thead>\n<tr>\n<th><strong>id<\/strong><\/th>\n<th><strong>first_name<\/strong><\/th>\n<th><strong>last_name<\/strong><\/th>\n<th><strong>age<\/strong><\/th>\n<th><strong>customer_id<\/strong><\/th>\n<th><strong>product<\/strong><\/th>\n<th><strong>category<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>john<\/td>\n<td>wall<\/td>\n<td>24<\/td>\n<td>1<\/td>\n<td>wiper<\/td>\n<td>general<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>mark<\/td>\n<td>smith<\/td>\n<td>34<\/td>\n<td>null<\/td>\n<td>null<\/td>\n<td>null<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>shiela<\/td>\n<td>maye<\/td>\n<td>22<\/td>\n<td>null<\/td>\n<td>null<\/td>\n<td>null<\/td>\n<\/tr>\n<\/tbody>\n<caption style=\"text-align: left\"><strong>3-b Table - FINAL RESULT<\/strong>:<\/caption>\n<\/table>\n<p style=\"border: 1px dashed #e68a00;height: auto;padding: 5px\"><strong style=\"color: #e68a00\">CONCLUSION<\/strong><b>:<\/b> Our second query gets all the data from the Customers Table, but before joining the result from Orders Table there's another function called SUB-QUERY (a independent\/standalone query) which filters the data and sending it to a variable which allows us to be used for further execution like join function.<\/p>\n<p style=\"color: #ff6600;font-size: 16px;letter-spacing: 1px\">Happy Programming Everyone !!!\u00a0<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4962\" src=\"http:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2015\/11\/smiley_skyarch1.jpg\" alt=\"smiley_skyarch\" width=\"25\" height=\"25\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>SQL Sub-Query \u27a3\u00a0 Sub-queries are queries embedded in queries. \u27a3\u00a0 They are used to retrieve data from one table&#8230;<\/p>\n","protected":false},"author":1,"featured_media":4940,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_locale":"en_US","_original_post":"4948","footnotes":""},"categories":[24,23],"tags":[],"class_list":{"0":"post-4948","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-mysql","8":"category-23","9":"en-US"},"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/posts\/4948","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/comments?post=4948"}],"version-history":[{"count":20,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/posts\/4948\/revisions"}],"predecessor-version":[{"id":4974,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/posts\/4948\/revisions\/4974"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/media\/4940"}],"wp:attachment":[{"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/media?parent=4948"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/categories?post=4948"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/tags?post=4948"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}