{"id":615,"date":"2024-12-04T15:18:40","date_gmt":"2024-12-04T08:18:40","guid":{"rendered":"https:\/\/www.pgtutorial.com\/?page_id=615"},"modified":"2025-01-02T16:05:06","modified_gmt":"2025-01-02T09:05:06","slug":"postgresql-correlated-subquery","status":"publish","type":"page","link":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-correlated-subquery\/","title":{"rendered":"PostgreSQL Correlated Subquery"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use a PostgreSQL correlated subquery to select data that depends on the values of the outer query.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='getting-started-with-postgresql-correlated-subquery'>Getting Started with PostgreSQL Correlated Subquery <a href=\"#getting-started-with-postgresql-correlated-subquery\" class=\"anchor\" id=\"getting-started-with-postgresql-correlated-subquery\" title=\"Anchor for Getting Started with PostgreSQL Correlated Subquery\">#<\/a><\/h2>\n\n\n\n<p>A correlated subquery is a <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-subquery\/\">subquery<\/a> that uses values from an outer query.<\/p>\n\n\n\n<p>Unlike a regular subquery that can execute independently, PostgreSQL may have to execute a correlated subquery for every row in the outer query.<\/p>\n\n\n\n<p>For this reason, you should avoid using the correlated subquery as much as possible to improve the query performance.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='postgresql-correlated-subquery-example'>PostgreSQL Correlated Subquery example <a href=\"#postgresql-correlated-subquery-example\" class=\"anchor\" id=\"postgresql-correlated-subquery-example\" title=\"Anchor for PostgreSQL Correlated Subquery example\">#<\/a><\/h2>\n\n\n\n<p>Suppose we have the following <code>products<\/code> table that include id, name, price, and brand:<\/p>\n\n\n\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>Script for creating the products table and inserting data into it<\/summary><pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> products (\n  id <span class=\"hljs-type\">INT<\/span> <span class=\"hljs-keyword\">GENERATED<\/span> <span class=\"hljs-keyword\">ALWAYS<\/span> <span class=\"hljs-keyword\">AS<\/span> <span class=\"hljs-keyword\">IDENTITY<\/span> <span class=\"hljs-keyword\">PRIMARY KEY<\/span>,\n  <span class=\"hljs-type\">name<\/span> <span class=\"hljs-type\">VARCHAR<\/span>(<span class=\"hljs-number\">50<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">NULL<\/span>,\n  price <span class=\"hljs-type\">DEC<\/span>(<span class=\"hljs-number\">11<\/span>, <span class=\"hljs-number\">2<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">NULL<\/span> <span class=\"hljs-keyword\">CHECK<\/span> (price &gt; <span class=\"hljs-number\">0<\/span>),\n  brand <span class=\"hljs-type\">VARCHAR<\/span>(<span class=\"hljs-number\">50<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">NULL<\/span>\n);\n<span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span>\n  products (<span class=\"hljs-type\">name<\/span>, price, brand)\n<span class=\"hljs-keyword\">VALUES<\/span>\n  (<span class=\"hljs-string\">'Galaxy S24'<\/span>, <span class=\"hljs-number\">799.99<\/span>, <span class=\"hljs-string\">'Samsung'<\/span>),\n  (<span class=\"hljs-string\">'iPhone 16'<\/span>, <span class=\"hljs-number\">1099.99<\/span>, <span class=\"hljs-string\">'Apple'<\/span>),\n  (<span class=\"hljs-string\">'iPhone 16 Pro Max'<\/span>, <span class=\"hljs-number\">1399.99<\/span>, <span class=\"hljs-string\">'Apple'<\/span>),\n  (<span class=\"hljs-string\">'iPhone 16 Plus'<\/span>, <span class=\"hljs-number\">1199.99<\/span>, <span class=\"hljs-string\">'Apple'<\/span>),\n  (<span class=\"hljs-string\">'Galaxy S24 Ultra'<\/span>, <span class=\"hljs-number\">1299.99<\/span>, <span class=\"hljs-string\">'Samsung'<\/span>),\n  (<span class=\"hljs-string\">'Galaxy S24 Plus'<\/span>, <span class=\"hljs-number\">1119.99<\/span>, <span class=\"hljs-string\">'Samsung'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><\/details>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>id<\/th><th>name<\/th><th>price<\/th><th>brand<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>Galaxy S24<\/td><td>799.99<\/td><td>Samsung<\/td><\/tr><tr><td>2<\/td><td>iPhone 16<\/td><td>1099.99<\/td><td>Apple<\/td><\/tr><tr><td>3<\/td><td>iPhone 16 Pro Max<\/td><td>1399.99<\/td><td>Apple<\/td><\/tr><tr><td>4<\/td><td>iPhone 16 Plus<\/td><td>1199.99<\/td><td>Apple<\/td><\/tr><tr><td>5<\/td><td>Galaxy S24 Ultra<\/td><td>1299.99<\/td><td>Samsung<\/td><\/tr><tr><td>6<\/td><td>Galaxy S24 Plus<\/td><td>1119.99<\/td><td>Samsung<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The following example shows how to use a correlated subquery to get the product name, brand, and price of the most expensive product in each brand:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  <span class=\"hljs-type\">name<\/span>,\n  brand,\n  price\n<span class=\"hljs-keyword\">FROM<\/span>\n  products p1\n<span class=\"hljs-keyword\">WHERE<\/span>\n  price = (\n    <span class=\"hljs-keyword\">SELECT<\/span>\n      MAX(price)\n    <span class=\"hljs-keyword\">FROM<\/span>\n      products p2\n    <span class=\"hljs-keyword\">WHERE<\/span>\n      p2.brand = p1.brand\n  );<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/pgtutorial.com\/playground\/?db=subquery&amp;q=U0VMRUNUIG5hbWUsIGJyYW5kLCBwcmljZSBGUk9NIHByb2R1Y3RzIHAxIFdIRVJFIHByaWNlID0gKCBTRUxFQ1QgTUFYKHByaWNlKSBGUk9NIHByb2R1Y3RzIHAyIFdIRVJFIHAyLmJyYW5kID0gcDEuYnJhbmQgKTs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">       name        |  brand  |  price\n-------------------+---------+---------\n iPhone 16 Pro Max | Apple   | 1399.99\n Galaxy S24 Ultra  | Samsung | 1299.99<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, the outer query selects the <code>name<\/code>, <code>brand<\/code>, and <code>price<\/code> from the <code>products<\/code> table with the alias <code>p1<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  <span class=\"hljs-type\">name<\/span>,\n  brand,\n  price\n<span class=\"hljs-keyword\">FROM<\/span>\n  products p1<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, PostgreSQL executes the correlated subquery for each row in the outer query:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  MAX(price)\n<span class=\"hljs-keyword\">FROM<\/span>\n  products p2\n<span class=\"hljs-keyword\">WHERE<\/span>\n  p2.brand = p1.brand<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The correlated subquery selects the maximum price from the <code>products<\/code> table with the alias <code>p2<\/code>, where the brand of <code>p2<\/code> matches the brand of the current row in the outer query (<code>p1.brand<\/code>).<\/p>\n\n\n\n<p>Third, the <code>WHERE<\/code> clause in the outer query checks the price of the current row (<code>p1.price<\/code>) equals the maximum price of the brand of the current row. If yes, PostgreSQL includes the row in the result set.<\/p>\n\n\n\n<p>Here&#8217;s the detail:<\/p>\n\n\n\n<p>Row id 1<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>id<\/th><th>name<\/th><th>price<\/th><th>brand<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>Galaxy S24<\/td><td>799.99<\/td><td>Samsung<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The subquery finds the maximum price of the Samsung brand (<code>1299.99<\/code>).<\/p>\n\n\n\n<p>The <code>WHERE<\/code> clause compares the price (<code>799.99<\/code>) with the maximum price (<code>1299.99<\/code>). It returns false. PostgreSQL does not include row 1 in the result set.<\/p>\n\n\n\n<p>Row id 2:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>id<\/th><th>name<\/th><th>price<\/th><th>brand<\/th><\/tr><\/thead><tbody><tr><td>2<\/td><td>iPhone 16<\/td><td>1099.99<\/td><td>Apple<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The subquery finds the maximum price of the Apple brand (<code>1399.99<\/code>).<\/p>\n\n\n\n<p>The <code>WHERE<\/code> clause compares the price (<code>1099.99<\/code>) with the maximum price (<code>1399.99<\/code>), which returns <code>false<\/code>. Hence, PostgreSQL does not include row id 2 in the result set.<\/p>\n\n\n\n<p>Row id 3<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>id<\/th><th>name<\/th><th>price<\/th><th>brand<\/th><\/tr><\/thead><tbody><tr><td>3<\/td><td>iPhone 16 Pro Max<\/td><td>1399.99<\/td><td>Apple<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The subquery finds the maximum price of the Apple brand (<code>1399.99<\/code>).<\/p>\n\n\n\n<p>The <code>WHERE<\/code> clause compares the price (<code>1399.99<\/code>) with the maximum price (<code>1399.99<\/code>),which returns <code>true<\/code>. PostgreSQL includes row id 3 in the result set.<\/p>\n\n\n\n<p>Row id 4<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>id<\/th><th>name<\/th><th>price<\/th><th>brand<\/th><\/tr><\/thead><tbody><tr><td>4<\/td><td>iPhone 16 Plus<\/td><td>1199.99<\/td><td>Apple<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The subquery finds the maximum price of the Apple brand (<code>1399.99<\/code>).<\/p>\n\n\n\n<p>The <code>WHERE<\/code> clause compares the price (<code>1199.99<\/code>) with the maximum price (<code>1399.99<\/code>). It returns <code>false<\/code>. PostgreSQL does not include row id 4 in the result set.<\/p>\n\n\n\n<p>Row id 5<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>id<\/th><th>name<\/th><th>price<\/th><th>brand<\/th><\/tr><\/thead><tbody><tr><td>5<\/td><td>Galaxy S24 Ultra<\/td><td>1299.99<\/td><td>Samsung<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The subquery finds the maximum price of the Samsung brand (<code>1299.99<\/code>).<\/p>\n\n\n\n<p>The <code>WHERE<\/code> clause compares the price (<code>1299.99<\/code>) with the maximum price (<code>1299.99<\/code>), which returns true. Therefore, PostgreSQL does not include row id 5 in the result set.<\/p>\n\n\n\n<p>Row id 6<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>id<\/th><th>name<\/th><th>price<\/th><th>brand<\/th><\/tr><\/thead><tbody><tr><td>6<\/td><td>Galaxy S24 Plus<\/td><td>1119.99<\/td><td>Samsung<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The subquery finds the maximum price of the Samsung brand (<code>1299.99<\/code>).<\/p>\n\n\n\n<p>The <code>WHERE<\/code> clause compares the price (<code>1119.99<\/code>) with the maximum price (<code>1299.99<\/code>), which returns false. PostgreSQL does not include row id 6 in the result set.<\/p>\n\n\n\n<p>The final result set will be as follows:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>name<\/th><th>price<\/th><th>brand<\/th><\/tr><\/thead><tbody><tr><td>iPhone 16 Pro Max<\/td><td>1399.99<\/td><td>Apple<\/td><\/tr><tr><td>Galaxy S24 Ultra<\/td><td>1299.99<\/td><td>Samsung<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>This example shows that PostgreSQL has to execute the correlated subquery to find the maximum price of Apple brand for every row in the outer query. It is redundant and inefficient.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='rewriting-a-correlated-subquery-using-join'>Rewriting a correlated subquery using join <a href=\"#rewriting-a-correlated-subquery-using-join\" class=\"anchor\" id=\"rewriting-a-correlated-subquery-using-join\" title=\"Anchor for Rewriting a correlated subquery using join\">#<\/a><\/h2>\n\n\n\n<p>The following example uses a <code><code><a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-inner-join\/\">JOIN<\/a><\/code><\/code> with a subquery to make the query faster:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  p1.name,\n  p1.brand,\n  p1.price\n<span class=\"hljs-keyword\">FROM<\/span>\n  products p1\n  <span class=\"hljs-keyword\">JOIN<\/span> (\n    <span class=\"hljs-keyword\">SELECT<\/span>\n      brand,\n      MAX(price) <span class=\"hljs-keyword\">AS<\/span> max_price\n    <span class=\"hljs-keyword\">FROM<\/span>\n      products\n    <span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span>\n      brand\n  ) p2 <span class=\"hljs-keyword\">ON<\/span> p1.brand = p2.brand\n  <span class=\"hljs-keyword\">AND<\/span> p1.price = p2.max_price;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/pgtutorial.com\/playground\/?db=subquery&amp;q=U0VMRUNUIHAxLm5hbWUsIHAxLmJyYW5kLCBwMS5wcmljZSBGUk9NIHByb2R1Y3RzIHAxIEpPSU4gKCBTRUxFQ1QgYnJhbmQsIE1BWChwcmljZSkgQVMgbWF4X3ByaWNlIEZST00gcHJvZHVjdHMgR1JPVVAgQlkgYnJhbmQgKSBwMiBPTiBwMS5icmFuZCA9IHAyLmJyYW5kIEFORCBwMS5wcmljZSA9IHAyLm1heF9wcmljZTs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, select the brand and maximum product price by brand using <code><a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-group-by\/\">GROUP BY<\/a><\/code> with the <code>MAX<\/code> aggregate function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  brand,\n  MAX(price) <span class=\"hljs-keyword\">AS<\/span> max_price\n<span class=\"hljs-keyword\">FROM<\/span>\n  products\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  brand;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/pgtutorial.com\/playground\/?db=subquery&amp;q=U0VMRUNUIGJyYW5kLCBNQVgocHJpY2UpIEFTIG1heF9wcmljZSBGUk9NIHByb2R1Y3RzIEdST1VQIEJZIGJyYW5kOw%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Second, join the <code>products<\/code> table with the result set returned by the subquery on the <code>brand<\/code> and <code>price<\/code> columns. This will retrieve the products with the highest price for each selected brand.<\/p>\n\n\n\n<p>This approach reduces the number of times PostgreSQL executes the subquery, improving the query performance, especially when the <code>products<\/code> table is large.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A correlated subquery is a subquery that depends on the outer query.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=correlated-subquery\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"615\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-correlated-subquery\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL Correlated Subquery\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"615\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-correlated-subquery\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL Correlated Subquery\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn how to use a PostgreSQL correlated subquery to select data that depends on the values of the outer query.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":13,"menu_order":50,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-615","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PostgreSQL Correlated Subquery<\/title>\n<meta name=\"description\" content=\"In this tutorial, you&#039;ll learn how to use a PostgreSQL correlated subquery to select data that depends on the values of the outer query.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-correlated-subquery\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PostgreSQL Correlated Subquery\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you&#039;ll learn how to use a PostgreSQL correlated subquery to select data that depends on the values of the outer query.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-correlated-subquery\/\" \/>\n<meta property=\"og:site_name\" content=\"PostgreSQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2025-01-02T09:05:06+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=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-correlated-subquery\\\/\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-correlated-subquery\\\/\",\"name\":\"PostgreSQL Correlated Subquery\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\"},\"datePublished\":\"2024-12-04T08:18:40+00:00\",\"dateModified\":\"2025-01-02T09:05:06+00:00\",\"description\":\"In this tutorial, you'll learn how to use a PostgreSQL correlated subquery to select data that depends on the values of the outer query.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-correlated-subquery\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-correlated-subquery\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-correlated-subquery\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PostgreSQL Tutorial\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"PostgreSQL Correlated Subquery\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/\",\"name\":\"PostgreSQL Tutorial\",\"description\":\"Learn PostgreSQL from Scratch\",\"alternateName\":\"PostgreSQL\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pgtutorial.com\\\/?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":"PostgreSQL Correlated Subquery","description":"In this tutorial, you'll learn how to use a PostgreSQL correlated subquery to select data that depends on the values of the outer query.","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:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-correlated-subquery\/","og_locale":"en_US","og_type":"article","og_title":"PostgreSQL Correlated Subquery","og_description":"In this tutorial, you'll learn how to use a PostgreSQL correlated subquery to select data that depends on the values of the outer query.","og_url":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-correlated-subquery\/","og_site_name":"PostgreSQL Tutorial","article_modified_time":"2025-01-02T09:05:06+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-correlated-subquery\/","url":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-correlated-subquery\/","name":"PostgreSQL Correlated Subquery","isPartOf":{"@id":"https:\/\/www.pgtutorial.com\/#website"},"datePublished":"2024-12-04T08:18:40+00:00","dateModified":"2025-01-02T09:05:06+00:00","description":"In this tutorial, you'll learn how to use a PostgreSQL correlated subquery to select data that depends on the values of the outer query.","breadcrumb":{"@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-correlated-subquery\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-correlated-subquery\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-correlated-subquery\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pgtutorial.com\/"},{"@type":"ListItem","position":2,"name":"PostgreSQL Tutorial","item":"https:\/\/www.pgtutorial.com\/"},{"@type":"ListItem","position":3,"name":"PostgreSQL Correlated Subquery"}]},{"@type":"WebSite","@id":"https:\/\/www.pgtutorial.com\/#website","url":"https:\/\/www.pgtutorial.com\/","name":"PostgreSQL Tutorial","description":"Learn PostgreSQL from Scratch","alternateName":"PostgreSQL","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pgtutorial.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/615","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/comments?post=615"}],"version-history":[{"count":9,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/615\/revisions"}],"predecessor-version":[{"id":1373,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/615\/revisions\/1373"}],"up":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/13"}],"wp:attachment":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/media?parent=615"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}