{"id":593,"date":"2024-12-03T13:26:47","date_gmt":"2024-12-03T06:26:47","guid":{"rendered":"https:\/\/www.pgtutorial.com\/?page_id=593"},"modified":"2025-01-02T16:17:00","modified_gmt":"2025-01-02T09:17:00","slug":"postgresql-where-in-subquery","status":"publish","type":"page","link":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-where-in-subquery\/","title":{"rendered":"PostgreSQL WHERE IN Subquery"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll explore using the PostgreSQL <code>WHERE IN subquery<\/code> to filter rows based on a list of values returned by the subquery.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='getting-started-with-the-postgresql-where-in-subquery'>Getting Started with the PostgreSQL WHERE IN subquery <a href=\"#getting-started-with-the-postgresql-where-in-subquery\" class=\"anchor\" id=\"getting-started-with-the-postgresql-where-in-subquery\" title=\"Anchor for Getting Started with the PostgreSQL WHERE IN subquery\">#<\/a><\/h2>\n\n\n\n<p>In PostgreSQL, the <code>WHERE IN subquery<\/code> allows you to <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-in\/\">check if a value is in a set of values<\/a> returned by <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-subquery\/\">subquery<\/a>.<\/p>\n\n\n\n<p>Here&#8217;s the syntax for using the <code>WHERE IN subquery<\/code> in the <code><a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-select\/\">SELECT<\/a><\/code> statement:<\/p>\n\n\n<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\">SELECT<\/span> select_list\n<span class=\"hljs-keyword\">FROM<\/span> <span class=\"hljs-built_in\">table_name<\/span>\n<span class=\"hljs-keyword\">WHERE<\/span> <span class=\"hljs-built_in\">column_name<\/span> <span class=\"hljs-keyword\">IN<\/span> (subquery);<\/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>\n\n\n<p>In this syntax:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, specify the column name in the <code>WHERE<\/code> clause with the <code><a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-in\/\">IN<\/a><\/code> operator to filter rows.<\/li>\n\n\n\n<li>Second, place a subquery inside parentheses <code>()<\/code> to supply values to the <code>IN<\/code> operator.<\/li>\n<\/ul>\n\n\n\n<p>The query returns a result set with the values in the <code>column_name<\/code> matching any value returned by the subquery.<\/p>\n\n\n\n<p>If the subquery returns no rows, the outer query will also return an empty result set.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='postgresql-where-in-subquery-example'>PostgreSQL WHERE IN subquery example <a href=\"#postgresql-where-in-subquery-example\" class=\"anchor\" id=\"postgresql-where-in-subquery-example\" title=\"Anchor for PostgreSQL WHERE IN subquery example\">#<\/a><\/h2>\n\n\n\n<p>The following example uses the <code>WHERE IN subquery<\/code> to find all products stored in a warehouse with id 1:<\/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  product_name\n<span class=\"hljs-keyword\">FROM<\/span>\n  products\n<span class=\"hljs-keyword\">WHERE<\/span>\n  product_id <span class=\"hljs-keyword\">IN<\/span> (\n    <span class=\"hljs-keyword\">SELECT<\/span>\n      product_id\n    <span class=\"hljs-keyword\">FROM<\/span>\n      inventories\n    <span class=\"hljs-keyword\">WHERE<\/span>\n      warehouse_id = <span class=\"hljs-number\">1<\/span>\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:\/\/www.pgtutorial.com\/playground\/?q=U0VMRUNUIHByb2R1Y3RfbmFtZSBGUk9NIHByb2R1Y3RzIFdIRVJFIHByb2R1Y3RfaWQgSU4gKCBTRUxFQ1QgcHJvZHVjdF9pZCBGUk9NIGludmVudG9yaWVzIFdIRVJFIHdhcmVob3VzZV9pZCA9IDEgKTs\" 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=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">        product_name\n<span class=\"hljs-comment\">----------------------------<\/span>\n Xiaomi Mi <span class=\"hljs-number\">14<\/span>\n Apple iPhone <span class=\"hljs-number\">15<\/span> Pro Max\n Apple AirPods Pro <span class=\"hljs-number\">3<\/span>\n Samsung Galaxy Watch <span class=\"hljs-number\">6<\/span>\n Samsung QN900C Neo QLED\n Bose SoundLink Max\n Microsoft Surface Laptop <span class=\"hljs-number\">5<\/span>\n Dell Inspiron <span class=\"hljs-number\">27<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><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>In this example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, the subquery retrieves a list of <code>product_id<\/code> stored in the warehouse id <code>1<\/code>.<\/li>\n\n\n\n<li>The outer query selects the products where <code>product_id<\/code> matches any <code>product_id<\/code> returned by the subquery.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='postgresql-where-not-in-subquery-example'>PostgreSQL WHERE NOT IN subquery example <a href=\"#postgresql-where-not-in-subquery-example\" class=\"anchor\" id=\"postgresql-where-not-in-subquery-example\" title=\"Anchor for PostgreSQL WHERE NOT IN subquery example\">#<\/a><\/h2>\n\n\n\n<p>You can use the <code>NOT IN<\/code> operator to filter rows that are not in a list returned by a query.<\/p>\n\n\n\n<p>The following statment uses the <code>WHERE NOT IN subquery<\/code> to find all products that are not stored in a warehouse id 1:<\/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  product_name\n<span class=\"hljs-keyword\">FROM<\/span>\n  products\n<span class=\"hljs-keyword\">WHERE<\/span>\n  product_id <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">IN<\/span> (\n    <span class=\"hljs-keyword\">SELECT<\/span>\n      product_id\n    <span class=\"hljs-keyword\">FROM<\/span>\n      inventories\n    <span class=\"hljs-keyword\">WHERE<\/span>\n      warehouse_id = <span class=\"hljs-number\">1<\/span>\n  );<\/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><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=U0VMRUNUIHByb2R1Y3RfbmFtZSBGUk9NIHByb2R1Y3RzIFdIRVJFIHByb2R1Y3RfaWQgTk9UIElOICggU0VMRUNUIHByb2R1Y3RfaWQgRlJPTSBpbnZlbnRvcmllcyBXSEVSRSB3YXJlaG91c2VfaWQgPSAxICk7\" 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-5\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">       product_name\n<span class=\"hljs-comment\">---------------------------<\/span>\n Sony Xperia <span class=\"hljs-number\">1<\/span> VI\n Samsung Galaxy Z Fold <span class=\"hljs-number\">5<\/span>\n Samsung Galaxy Tab S9\n Apple iPad Pro <span class=\"hljs-number\">12.9<\/span>\n Samsung Galaxy Buds Pro <span class=\"hljs-number\">2<\/span>\n Apple Watch Series <span class=\"hljs-number\">9<\/span>\n LG OLED TV C3\n Sony Bravia XR A95K\n LG G3 OLED\n Sony HT-A7000 Soundbar\n Dell XPS <span class=\"hljs-number\">15<\/span>\n HP Spectre x360\n Lenovo ThinkPad X1 Carbon\n Apple iMac <span class=\"hljs-number\">24<\/span>\"<\/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<h2 class=\"wp-block-heading\" id='using-postgresql-where-not-in-with-the-update-statement'>Using PostgreSQL WHERE NOT IN with the UPDATE statement <a href=\"#using-postgresql-where-not-in-with-the-update-statement\" class=\"anchor\" id=\"using-postgresql-where-not-in-with-the-update-statement\" title=\"Anchor for Using PostgreSQL WHERE NOT IN with the UPDATE statement\">#<\/a><\/h2>\n\n\n\n<p>The following statement shows how to use the <code>WHERE IN subquery<\/code> in the <code><a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-update\/\">UPDATE<\/a><\/code> statement to filter rows for updating:<\/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\">UPDATE<\/span> <span class=\"hljs-built_in\">table_name<\/span>\n<span class=\"hljs-keyword\">SET<\/span> column2 = value2,\n \u00a0 \u00a0column3 = value3,\n \u00a0 \u00a0...\n<span class=\"hljs-keyword\">WHERE<\/span> column1 <span class=\"hljs-keyword\">IN<\/span> (subquery);<\/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>The <code>UPDATE<\/code> statement will update rows whose values in <code>column1<\/code> match any value returned by the subquery.<\/p>\n\n\n\n<p>If there is no match or the subquery returns now row, the <code>UPDATE<\/code> statement will not update any rows.<\/p>\n\n\n\n<p>The following example decreases the prices of products stored in the warehouse id 1 by 10%:<\/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\">UPDATE<\/span> products\n<span class=\"hljs-keyword\">SET<\/span>\n  price = price * <span class=\"hljs-number\">0.9<\/span>\n<span class=\"hljs-keyword\">WHERE<\/span>\n  product_id <span class=\"hljs-keyword\">IN<\/span> (\n    <span class=\"hljs-keyword\">SELECT<\/span>\n      product_id\n    <span class=\"hljs-keyword\">FROM<\/span>\n      inventories\n    <span class=\"hljs-keyword\">WHERE<\/span>\n      warehouse_id = <span class=\"hljs-number\">1<\/span>\n  ) <span class=\"hljs-keyword\">RETURNING<\/span> *;<\/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:\/\/www.pgtutorial.com\/playground\/?q=VVBEQVRFIHByb2R1Y3RzIFNFVCBwcmljZSA9IHByaWNlICogMC45IFdIRVJFIHByb2R1Y3RfaWQgSU4gKCBTRUxFQ1QgcHJvZHVjdF9pZCBGUk9NIGludmVudG9yaWVzIFdIRVJFIHdhcmVob3VzZV9pZCA9IDEgKSBSRVRVUk5JTkcgKjs\" 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-8\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"> product_id |        product_name        |  price  | brand_id | category_id\n<span class=\"hljs-comment\">------------+----------------------------+---------+----------+-------------<\/span>\n          <span class=\"hljs-number\">4<\/span> | Xiaomi Mi <span class=\"hljs-number\">14<\/span>               |  <span class=\"hljs-number\">719.99<\/span> |        <span class=\"hljs-number\">4<\/span> |           <span class=\"hljs-number\">2<\/span>\n          <span class=\"hljs-number\">7<\/span> | Apple iPhone <span class=\"hljs-number\">15<\/span> Pro Max    | <span class=\"hljs-number\">1169.99<\/span> |        <span class=\"hljs-number\">2<\/span> |           <span class=\"hljs-number\">3<\/span>\n         <span class=\"hljs-number\">10<\/span> | Apple AirPods Pro <span class=\"hljs-number\">3<\/span>        |  <span class=\"hljs-number\">224.99<\/span> |        <span class=\"hljs-number\">2<\/span> |           <span class=\"hljs-number\">5<\/span>\n         <span class=\"hljs-number\">13<\/span> | Samsung Galaxy Watch <span class=\"hljs-number\">6<\/span>     |  <span class=\"hljs-number\">314.99<\/span> |        <span class=\"hljs-number\">1<\/span> |           <span class=\"hljs-number\">6<\/span>\n         <span class=\"hljs-number\">16<\/span> | Samsung QN900C Neo QLED    | <span class=\"hljs-number\">2699.99<\/span> |        <span class=\"hljs-number\">1<\/span> |           <span class=\"hljs-number\">8<\/span>\n         <span class=\"hljs-number\">19<\/span> | Bose SoundLink Max         |  <span class=\"hljs-number\">359.99<\/span> |        <span class=\"hljs-number\">7<\/span> |           <span class=\"hljs-number\">9<\/span>\n         <span class=\"hljs-number\">22<\/span> | Microsoft Surface Laptop <span class=\"hljs-number\">5<\/span> | <span class=\"hljs-number\">1169.99<\/span> |        <span class=\"hljs-number\">9<\/span> |          <span class=\"hljs-number\">11<\/span>\n         <span class=\"hljs-number\">25<\/span> | Dell Inspiron <span class=\"hljs-number\">27<\/span>           |  <span class=\"hljs-number\">899.99<\/span> |        <span class=\"hljs-number\">7<\/span> |          <span class=\"hljs-number\">12<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><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>How it works:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The subquery&nbsp;retrieves a list of <code>product_id<\/code> stored in the warehouse id 1.<\/li>\n\n\n\n<li>The&nbsp;<code>UPDATE<\/code>&nbsp;statement then decreases the price of the selected products by 10%.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='using-where-in-subquery-with-the-delete-statement'>Using WHERE IN subquery with the DELETE statement <a href=\"#using-where-in-subquery-with-the-delete-statement\" class=\"anchor\" id=\"using-where-in-subquery-with-the-delete-statement\" title=\"Anchor for Using WHERE IN subquery with the DELETE statement\">#<\/a><\/h2>\n\n\n\n<p>The following shows how to use the <code>WHERE IN subquery<\/code> in the <code><a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-delete\/\">DELETE<\/a><\/code> statement to select rows for deletion:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" 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\">DELETE<\/span> <span class=\"hljs-keyword\">FROM<\/span> <span class=\"hljs-built_in\">table_name<\/span>\n<span class=\"hljs-keyword\">WHERE<\/span> column1 <span class=\"hljs-keyword\">IN<\/span> (subquery);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><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>This <code>DELETE<\/code> statement will delete rows with values in <code>column1<\/code> matching any value returned by the subquery.<\/p>\n\n\n\n<p>If the subquery returns no row or there are no matches, the <code>DELETE<\/code> statement will not delete any rows.<\/p>\n\n\n\n<p>The following example shows how to delete the products stored in the warehouse id 1:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" 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\">DELETE<\/span> <span class=\"hljs-keyword\">FROM<\/span> products\n<span class=\"hljs-keyword\">WHERE<\/span>\n  product_id <span class=\"hljs-keyword\">IN<\/span> (\n    <span class=\"hljs-keyword\">SELECT<\/span>\n      product_id\n    <span class=\"hljs-keyword\">FROM<\/span>\n      inventories\n    <span class=\"hljs-keyword\">WHERE<\/span>\n      warehouse_id = <span class=\"hljs-number\">1<\/span>\n  ) \n<span class=\"hljs-keyword\">RETURNING<\/span> *;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><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:\/\/www.pgtutorial.com\/playground\/?q=REVMRVRFIEZST00gcHJvZHVjdHMgV0hFUkUgcHJvZHVjdF9pZCBJTiAoIFNFTEVDVCBwcm9kdWN0X2lkIEZST00gaW52ZW50b3JpZXMgV0hFUkUgd2FyZWhvdXNlX2lkID0gMSApIFJFVFVSTklORyAqOw%3D\" 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-11\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"> product_id |        product_name        |  price  | brand_id | category_id\n<span class=\"hljs-comment\">------------+----------------------------+---------+----------+-------------<\/span>\n          <span class=\"hljs-number\">4<\/span> | Xiaomi Mi <span class=\"hljs-number\">14<\/span>               |  <span class=\"hljs-number\">719.99<\/span> |        <span class=\"hljs-number\">4<\/span> |           <span class=\"hljs-number\">2<\/span>\n          <span class=\"hljs-number\">7<\/span> | Apple iPhone <span class=\"hljs-number\">15<\/span> Pro Max    | <span class=\"hljs-number\">1169.99<\/span> |        <span class=\"hljs-number\">2<\/span> |           <span class=\"hljs-number\">3<\/span>\n         <span class=\"hljs-number\">10<\/span> | Apple AirPods Pro <span class=\"hljs-number\">3<\/span>        |  <span class=\"hljs-number\">224.99<\/span> |        <span class=\"hljs-number\">2<\/span> |           <span class=\"hljs-number\">5<\/span>\n         <span class=\"hljs-number\">13<\/span> | Samsung Galaxy Watch <span class=\"hljs-number\">6<\/span>     |  <span class=\"hljs-number\">314.99<\/span> |        <span class=\"hljs-number\">1<\/span> |           <span class=\"hljs-number\">6<\/span>\n         <span class=\"hljs-number\">16<\/span> | Samsung QN900C Neo QLED    | <span class=\"hljs-number\">2699.99<\/span> |        <span class=\"hljs-number\">1<\/span> |           <span class=\"hljs-number\">8<\/span>\n         <span class=\"hljs-number\">19<\/span> | Bose SoundLink Max         |  <span class=\"hljs-number\">359.99<\/span> |        <span class=\"hljs-number\">7<\/span> |           <span class=\"hljs-number\">9<\/span>\n         <span class=\"hljs-number\">22<\/span> | Microsoft Surface Laptop <span class=\"hljs-number\">5<\/span> | <span class=\"hljs-number\">1169.99<\/span> |        <span class=\"hljs-number\">9<\/span> |          <span class=\"hljs-number\">11<\/span>\n         <span class=\"hljs-number\">25<\/span> | Dell Inspiron <span class=\"hljs-number\">27<\/span>           |  <span class=\"hljs-number\">899.99<\/span> |        <span class=\"hljs-number\">7<\/span> |          <span class=\"hljs-number\">12<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><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>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The subquery&nbsp;retrieves all&nbsp;<code>product_id<\/code> stored in warehouse <code>1<\/code>.<\/li>\n\n\n\n<li>The&nbsp;<code>DELETE<\/code>&nbsp;statement removes all products with those&nbsp;<code>product_id<\/code> from the&nbsp;<code>products<\/code>&nbsp;table.<\/li>\n<\/ul>\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>Use the <code>WHERE IN&nbsp;subquery<\/code> to filter rows based on dynamic sets of values.<\/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=where-in-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=\"593\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-where-in-subquery\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL WHERE IN 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=\"593\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-where-in-subquery\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL WHERE IN 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>Summary: in this tutorial, you&#8217;ll explore using the PostgreSQL WHERE IN subquery to filter rows based on a list of values returned by the subquery. Getting Started with the PostgreSQL WHERE IN subquery # In PostgreSQL, the WHERE IN subquery allows you to check if a value is in a set of values returned by [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":13,"menu_order":51,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-593","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 WHERE IN Subquery<\/title>\n<meta name=\"description\" content=\"In this tutorial, you&#039;ll explore using the PostgreSQL WHERE IN subquery to filter rows based on a list of values returned by the subquery.\" \/>\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-where-in-subquery\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PostgreSQL WHERE IN Subquery\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you&#039;ll explore using the PostgreSQL WHERE IN subquery to filter rows based on a list of values returned by the subquery.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-where-in-subquery\/\" \/>\n<meta property=\"og:site_name\" content=\"PostgreSQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2025-01-02T09:17:00+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=\"2 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-where-in-subquery\\\/\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-where-in-subquery\\\/\",\"name\":\"PostgreSQL WHERE IN Subquery\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\"},\"datePublished\":\"2024-12-03T06:26:47+00:00\",\"dateModified\":\"2025-01-02T09:17:00+00:00\",\"description\":\"In this tutorial, you'll explore using the PostgreSQL WHERE IN subquery to filter rows based on a list of values returned by the subquery.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-where-in-subquery\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-where-in-subquery\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-where-in-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 WHERE IN 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 WHERE IN Subquery","description":"In this tutorial, you'll explore using the PostgreSQL WHERE IN subquery to filter rows based on a list of values returned by the subquery.","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-where-in-subquery\/","og_locale":"en_US","og_type":"article","og_title":"PostgreSQL WHERE IN Subquery","og_description":"In this tutorial, you'll explore using the PostgreSQL WHERE IN subquery to filter rows based on a list of values returned by the subquery.","og_url":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-where-in-subquery\/","og_site_name":"PostgreSQL Tutorial","article_modified_time":"2025-01-02T09:17:00+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-where-in-subquery\/","url":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-where-in-subquery\/","name":"PostgreSQL WHERE IN Subquery","isPartOf":{"@id":"https:\/\/www.pgtutorial.com\/#website"},"datePublished":"2024-12-03T06:26:47+00:00","dateModified":"2025-01-02T09:17:00+00:00","description":"In this tutorial, you'll explore using the PostgreSQL WHERE IN subquery to filter rows based on a list of values returned by the subquery.","breadcrumb":{"@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-where-in-subquery\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-where-in-subquery\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-where-in-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 WHERE IN 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\/593","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=593"}],"version-history":[{"count":4,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/593\/revisions"}],"predecessor-version":[{"id":1374,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/593\/revisions\/1374"}],"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=593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}