{"id":82,"date":"2026-03-06T06:35:08","date_gmt":"2026-03-06T06:35:08","guid":{"rendered":"https:\/\/gigz.pk\/sql\/?post_type=lesson&#038;p=82"},"modified":"2026-03-16T18:51:12","modified_gmt":"2026-03-16T18:51:12","slug":"correlated-subqueries","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/sql\/lesson\/correlated-subqueries\/","title":{"rendered":"Correlated Subqueries"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">A <strong>correlated subquery<\/strong> is a type of subquery that depends on the outer query for its values. Unlike a regular subquery, which can run independently, a correlated subquery executes once for each row selected by the outer query.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Key Features<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The inner query references columns from the outer query.<\/li>\n\n\n\n<li>Evaluates row by row, making it dependent on the outer query.<\/li>\n\n\n\n<li>Useful for comparisons, filtering, and conditional aggregation.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT column1, column2<br>FROM table1<br>WHERE column3 = (SELECT column4<br>                 FROM table2<br>                 WHERE table2.column5 = table1.column5);<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>table1<\/code> is the outer query table.<\/li>\n\n\n\n<li><code>table2<\/code> is the inner query table.<\/li>\n\n\n\n<li><code>table2.column5 = table1.column5<\/code> shows the dependency of the inner query on the outer query.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Example 1: Finding Employees with Above Average Salary in Their Department<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT e1.employee_id, e1.name, e1.salary, e1.department_id<br>FROM employees e1<br>WHERE e1.salary &gt; (SELECT AVG(e2.salary)<br>                   FROM employees e2<br>                   WHERE e2.department_id = e1.department_id);<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The inner query calculates the average salary for each department.<\/li>\n\n\n\n<li>The outer query retrieves employees earning more than their department\u2019s average.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Example 2: Departments with the Highest Paid Employee<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT d.department_name, e.name, e.salary<br>FROM departments d<br>JOIN employees e ON d.department_id = e.department_id<br>WHERE e.salary = (SELECT MAX(salary)<br>                  FROM employees<br>                  WHERE department_id = d.department_id);<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The inner query finds the maximum salary for each department.<\/li>\n\n\n\n<li>The outer query lists employees earning that maximum salary.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Advantages<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Allows complex row-by-row comparisons.<\/li>\n\n\n\n<li>Can perform conditional aggregations and ranking.<\/li>\n\n\n\n<li>Useful when joining alone cannot achieve the required logic.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Limitations<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Can be slower for large datasets because the inner query executes multiple times.<\/li>\n\n\n\n<li>Requires careful indexing for performance optimization.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Tips<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use indexes on the columns referenced in the correlated subquery.<\/li>\n\n\n\n<li>Test performance on large tables; sometimes joins or window functions are faster alternatives.<\/li>\n\n\n\n<li>Understand the dependency of inner query on outer query to avoid errors.<\/li>\n<\/ul>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/sql\/\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">Intermediate SQL for Data Professionals (SQL-201) > Advanced Joins &#038; Subqueries > Correlated Subqueries<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773569170017\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":30,"template":"","class_list":["post-82","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Correlated Subqueries - SQL Learning Hub<\/title>\n<meta name=\"description\" content=\"&quot;Learn SQL correlated subqueries with examples, advantages, and performance tips to write efficient row-by-row queries.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/sql\/lesson\/correlated-subqueries\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Correlated Subqueries - SQL Learning Hub\" \/>\n<meta property=\"og:description\" content=\"&quot;Learn SQL correlated subqueries with examples, advantages, and performance tips to write efficient row-by-row queries.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/sql\/lesson\/correlated-subqueries\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL Learning Hub\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-16T18:51:12+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/lesson\\\/correlated-subqueries\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/lesson\\\/correlated-subqueries\\\/\",\"name\":\"Correlated Subqueries - SQL Learning Hub\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/#website\"},\"datePublished\":\"2026-03-06T06:35:08+00:00\",\"dateModified\":\"2026-03-16T18:51:12+00:00\",\"description\":\"\\\"Learn SQL correlated subqueries with examples, advantages, and performance tips to write efficient row-by-row queries.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/lesson\\\/correlated-subqueries\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/sql\\\/lesson\\\/correlated-subqueries\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/lesson\\\/correlated-subqueries\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Intermediate SQL for Data Professionals (SQL-201) > Advanced Joins & Subqueries > Correlated Subqueries\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/\",\"name\":\"SQL Learning Hub\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Correlated Subqueries - SQL Learning Hub","description":"\"Learn SQL correlated subqueries with examples, advantages, and performance tips to write efficient row-by-row queries.","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:\/\/gigz.pk\/sql\/lesson\/correlated-subqueries\/","og_locale":"en_US","og_type":"article","og_title":"Correlated Subqueries - SQL Learning Hub","og_description":"\"Learn SQL correlated subqueries with examples, advantages, and performance tips to write efficient row-by-row queries.","og_url":"https:\/\/gigz.pk\/sql\/lesson\/correlated-subqueries\/","og_site_name":"SQL Learning Hub","article_modified_time":"2026-03-16T18:51:12+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/sql\/lesson\/correlated-subqueries\/","url":"https:\/\/gigz.pk\/sql\/lesson\/correlated-subqueries\/","name":"Correlated Subqueries - SQL Learning Hub","isPartOf":{"@id":"https:\/\/gigz.pk\/sql\/#website"},"datePublished":"2026-03-06T06:35:08+00:00","dateModified":"2026-03-16T18:51:12+00:00","description":"\"Learn SQL correlated subqueries with examples, advantages, and performance tips to write efficient row-by-row queries.","breadcrumb":{"@id":"https:\/\/gigz.pk\/sql\/lesson\/correlated-subqueries\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/sql\/lesson\/correlated-subqueries\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/sql\/lesson\/correlated-subqueries\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/sql\/"},{"@type":"ListItem","position":2,"name":"Intermediate SQL for Data Professionals (SQL-201) > Advanced Joins & Subqueries > Correlated Subqueries"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/sql\/#website","url":"https:\/\/gigz.pk\/sql\/","name":"SQL Learning Hub","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/sql\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/sql\/wp-json\/wp\/v2\/lesson\/82","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/sql\/wp-json\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/sql\/wp-json\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/sql\/wp-json\/wp\/v2\/media?parent=82"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}