{"id":384,"date":"2019-05-30T01:11:52","date_gmt":"2019-05-30T09:11:52","guid":{"rendered":"https:\/\/db2tutorial.com\/?page_id=384"},"modified":"2020-04-11T06:59:24","modified_gmt":"2020-04-11T14:59:24","slug":"db2-subquery","status":"publish","type":"page","link":"https:\/\/www.db2tutorial.com\/db2-basics\/db2-subquery\/","title":{"rendered":"Db2 Subquery"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn about the Db2 subquery or subselect which is a <code>SELECT<\/code> statement nested inside another statement such as <code>SELECT<\/code>, <code>INSERT<\/code>, <code>UPDATE<\/code>, and <code>DELETE<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to Db2 subquery<\/h2>\n\n\n\n<p>A subquery is a nested SQL statement that contains a <code>SELECT<\/code> statement inside the <code><a href=\"https:\/\/db2tutorial.com\/db2-basics\/db2-where\/\">WHERE<\/a><\/code> or <code><a href=\"https:\/\/db2tutorial.com\/db2-basics\/db2-having\/\">HAVING<\/a><\/code> clause of another SQL statement. A subquery is called a subselect.<\/p>\n\n\n\n<p>The subquery allows you to form a search condition based on the data in another table. For example, you can find all books by publishers whose name contains the keyword <code>Oxford<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n\ttitle, rating, publisher_id \n<span class=\"hljs-keyword\">FROM<\/span> \n\tbooks\n<span class=\"hljs-keyword\">WHERE<\/span> \n\tpublisher_id <span class=\"hljs-keyword\">IN<\/span> (\n\t\t<span class=\"hljs-keyword\">SELECT<\/span> \n\t\t\tpublisher_id \n\t\t<span class=\"hljs-keyword\">FROM<\/span> \n\t\t\tpublishers\n\t\t<span class=\"hljs-keyword\">WHERE<\/span> \n\t\t\t<span class=\"hljs-keyword\">name<\/span> <span class=\"hljs-keyword\">LIKE<\/span> <span class=\"hljs-string\">'%Oxford%'<\/span>\n);\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"715\" height=\"295\" src=\"https:\/\/db2tutorial.com\/wp-content\/uploads\/2019\/05\/Db2-Subquery-Example.png\" alt=\"Db2 Subquery Example\" class=\"wp-image-386\" srcset=\"https:\/\/www.db2tutorial.com\/wp-content\/uploads\/2019\/05\/Db2-Subquery-Example.png 715w, https:\/\/www.db2tutorial.com\/wp-content\/uploads\/2019\/05\/Db2-Subquery-Example-300x124.png 300w\" sizes=\"auto, (max-width: 715px) 100vw, 715px\" \/><\/figure>\n\n\n\n<p>In this example, here is the subquery:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n\tpublisher_id \n<span class=\"hljs-keyword\">FROM<\/span> \n\tpublishers\n<span class=\"hljs-keyword\">WHERE<\/span> \n\t<span class=\"hljs-keyword\">name<\/span> <span class=\"hljs-keyword\">LIKE<\/span> <span class=\"hljs-string\">'%Oxford%'<\/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\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The subquery is always enclosed in parentheses.<\/p>\n\n\n\n<p>A subquery is also known as an inner query or inner select while the query that contains the subquery is known as an outer query or outer select.<\/p>\n\n\n\n<p>To better understand the result of the outer query, you can imagine that Db2 goes through the following process:<\/p>\n\n\n\n<p>1) DB2 first executes the subquery to get a list of publisher id:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n   publisher_id\n<span class=\"hljs-keyword\">FROM<\/span>\n   publishers\n<span class=\"hljs-keyword\">WHERE<\/span>\n   <span class=\"hljs-keyword\">name<\/span> <span class=\"hljs-keyword\">LIKE<\/span> <span class=\"hljs-string\">'%Oxford%'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here is the output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">PUBLISHER_ID\n<span class=\"hljs-comment\">-------------<\/span>\n148\n149\n150\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>2) DB2 then uses this list for the search condition of the outer query:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n\ttitle, \n\trating, \n\tpublisher_id \n<span class=\"hljs-keyword\">FROM<\/span> \n\tbooks\n<span class=\"hljs-keyword\">WHERE<\/span> \n\tpublisher_id <span class=\"hljs-keyword\">IN<\/span> (<span class=\"hljs-number\">148<\/span>,<span class=\"hljs-number\">149<\/span>,<span class=\"hljs-number\">150<\/span>);\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>By using the subquery, you are able to combine steps together. The subquery removes the step of selecting the publisher id list and plugging them into the outer select. On top of it, the result of the query is automatically adjusted whenever the publisher data changes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Nested Subqueries<\/h2>\n\n\n\n<p>Db2 allows you to nest a subquery within another subquery. The relationship between the nested subquery and subquery is the same as the relationship between the subquery and outer query. The maximum level of nesting in Db2 is 15.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Db2 Subquery types<\/h2>\n\n\n\n<p>Db2 allows you to use a subquery in the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>in the place of expression in the <code>SELECT<\/code> clause<\/li><li>in the <code>FROM<\/code> clause<\/li><li>within the <code>IN<\/code> or <code>NOT IN<\/code> operator in the <code>WHERE<\/code> clause.<\/li><li>within the <code>ANY<\/code> or <code>ALL<\/code> operator in the <code>WHERE<\/code> clause<\/li><li>within the <code>EXISTS<\/code> or <code>NOT EXISTS<\/code> operator in the <code>WHERE<\/code> clause.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">1) Using a subquery in place of an expression example<\/h3>\n\n\n\n<p>When a subquery returns a single value, you can place it in place of an expression e.g., in the select list of the <code>SELECT<\/code> clause.<\/p>\n\n\n\n<p>This example uses a subquery to find the average number of pages of all books in the <code>books<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n\ttitle, \n\ttotal_pages, \n\t(<span class=\"hljs-keyword\">SELECT<\/span> \n\t\t<span class=\"hljs-keyword\">ROUND<\/span>(<span class=\"hljs-keyword\">AVG<\/span>(total_pages),<span class=\"hljs-number\">0<\/span>) \n\t<span class=\"hljs-keyword\">FROM<\/span> \n\t\tbooks\n\t) avg_pages\n<span class=\"hljs-keyword\">FROM<\/span> \n\tbooks\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> \n\ttitle;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"734\" height=\"337\" src=\"https:\/\/db2tutorial.com\/wp-content\/uploads\/2019\/05\/Db2-Subquery-in-SELECT-clause-example.png\" alt=\"Db2 Subquery in SELECT clause example\" class=\"wp-image-387\" srcset=\"https:\/\/www.db2tutorial.com\/wp-content\/uploads\/2019\/05\/Db2-Subquery-in-SELECT-clause-example.png 734w, https:\/\/www.db2tutorial.com\/wp-content\/uploads\/2019\/05\/Db2-Subquery-in-SELECT-clause-example-300x138.png 300w\" sizes=\"auto, (max-width: 734px) 100vw, 734px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">2) Using a subquery with IN operator example<\/h3>\n\n\n\n<p>You often use a subquery with the <code><a href=\"https:\/\/db2tutorial.com\/db2-basics\/db2-in\/\">IN<\/a><\/code> operator. In this case, the subquery returns zero or multiple values. The outer query makes use of these values as a filter.<\/p>\n\n\n\n<p>The following statement returns all books from the authors whose first name is <code>Tim<\/code>.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n\ttitle\n<span class=\"hljs-keyword\">FROM<\/span> \n\tbooks b\n<span class=\"hljs-keyword\">INNER<\/span> <span class=\"hljs-keyword\">JOIN<\/span> book_authors A \n\t<span class=\"hljs-keyword\">ON<\/span> A.book_id = b.book_id\n<span class=\"hljs-keyword\">WHERE<\/span> author_id <span class=\"hljs-keyword\">IN<\/span> (\n\t<span class=\"hljs-keyword\">SELECT<\/span> \n\t\tauthor_id \n\t<span class=\"hljs-keyword\">FROM<\/span> \n\t\t<span class=\"hljs-keyword\">authors<\/span> \n\t<span class=\"hljs-keyword\">WHERE<\/span> \n\t\tfirst_name = <span class=\"hljs-string\">'Tim'<\/span>\n);\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"419\" height=\"115\" src=\"https:\/\/db2tutorial.com\/wp-content\/uploads\/2019\/05\/Db2-Subquery-with-the-IN-clause-example.png\" alt=\"Db2 Subquery with the IN clause example\" class=\"wp-image-388\" srcset=\"https:\/\/www.db2tutorial.com\/wp-content\/uploads\/2019\/05\/Db2-Subquery-with-the-IN-clause-example.png 419w, https:\/\/www.db2tutorial.com\/wp-content\/uploads\/2019\/05\/Db2-Subquery-with-the-IN-clause-example-300x82.png 300w\" sizes=\"auto, (max-width: 419px) 100vw, 419px\" \/><\/figure>\n\n\n\n<p>In this example, the subquery returns a list of author ids which are used in the outer query to find their books.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3) Using subquery with ANY operator example<\/h3>\n\n\n\n<p>The following illustrates the syntax of a subquery used with the <code>ANY<\/code> operator:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">expression comparison_operator ANY (subquery)\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Suppose, the subquery returns a list of value v1, v2, &#8230;. The <code>ANY<\/code> operator returns true if one of the following comparison pair evaluates to true:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">(expression, v1)\n(expression, v2)\n(expression, ...)\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Note that the comparison operators are <code>=<\/code>, <code>&gt;<\/code>, <code>&gt;=<\/code>, <code>&lt;<\/code>, <code>&lt;=<\/code>, and <code>&lt;&gt;<\/code>.<\/p>\n\n\n\n<p>The following statement uses a query in the <code>ANY<\/code> operator to find books which have more than five authors.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n\tbook_id, \n\ttitle, \n\trating\n<span class=\"hljs-keyword\">FROM<\/span>\n\tbooks \n<span class=\"hljs-keyword\">WHERE<\/span>\n\tbook_id = <span class=\"hljs-keyword\">ANY<\/span>(\n\t\t<span class=\"hljs-keyword\">SELECT<\/span> \n\t\t\tbook_id\n\t\t<span class=\"hljs-keyword\">FROM<\/span> \n\t\t\tbook_authors\n\t\t<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span> \n\t\t\tbook_id\n\t\t<span class=\"hljs-keyword\">HAVING<\/span> \n\t\t\t<span class=\"hljs-keyword\">COUNT<\/span>(author_id) &gt; <span class=\"hljs-number\">5<\/span>\n\t);\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"476\" height=\"112\" src=\"https:\/\/db2tutorial.com\/wp-content\/uploads\/2019\/05\/Db2-Subquery-with-ANY-operator-example.png\" alt=\"Db2 Subquery with ANY operator example\" class=\"wp-image-389\" srcset=\"https:\/\/www.db2tutorial.com\/wp-content\/uploads\/2019\/05\/Db2-Subquery-with-ANY-operator-example.png 476w, https:\/\/www.db2tutorial.com\/wp-content\/uploads\/2019\/05\/Db2-Subquery-with-ANY-operator-example-300x71.png 300w\" sizes=\"auto, (max-width: 476px) 100vw, 476px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">4) Using a subquery with ALL operator example<\/h3>\n\n\n\n<p>The syntax of using a subquery with the <code>ALL<\/code> operator is similar to the syntax of using the subquery with the <code>ANY<\/code> operator:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">expression comparison_operator ALL (subquery)\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>ALL<\/code> operator returns true if all comparison pairs return to <code>TRUE<\/code>.<\/p>\n\n\n\n<p>This statement uses the ALL operator to find the books whose ratings are greater than the average rating of all books by publishers:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n\ttitle, \n\trating\n<span class=\"hljs-keyword\">FROM<\/span> \n\tbooks \n<span class=\"hljs-keyword\">WHERE<\/span> \n\trating &gt; <span class=\"hljs-keyword\">ALL<\/span>(\n\t\t<span class=\"hljs-keyword\">SELECT<\/span> \n\t\t\t<span class=\"hljs-keyword\">AVG<\/span>(rating)\n\t\t<span class=\"hljs-keyword\">FROM<\/span> \n\t\t\tbooks\n\t\t<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span> publisher_id\n);\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"576\" height=\"77\" src=\"https:\/\/db2tutorial.com\/wp-content\/uploads\/2019\/05\/Db2-Subquery-with-ALL-operator-example.png\" alt=\"Db2 Subquery with ALL operator example\" class=\"wp-image-390\" srcset=\"https:\/\/www.db2tutorial.com\/wp-content\/uploads\/2019\/05\/Db2-Subquery-with-ALL-operator-example.png 576w, https:\/\/www.db2tutorial.com\/wp-content\/uploads\/2019\/05\/Db2-Subquery-with-ALL-operator-example-300x40.png 300w\" sizes=\"auto, (max-width: 576px) 100vw, 576px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">5) Using a subquery with EXISTS operator example<\/h3>\n\n\n\n<p>Here is the syntax of using a subquery with <code>EXISTS<\/code> and <code>NOT EXISTS<\/code> operator:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">WHERE &#91;NOT] EXISTS (subquery)\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>EXISTS<\/code> operator returns true only if the subquery returns a non-empty result set. The <code>NOT<\/code> operator negates the <code>EXISTS<\/code> operator.<\/p>\n\n\n\n<p>The following query finds the authors who have books published in 2019:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n\tfirst_name,\n\tlast_name\n<span class=\"hljs-keyword\">FROM<\/span> \n\t<span class=\"hljs-keyword\">authors<\/span> a\n\t<span class=\"hljs-keyword\">INNER<\/span> <span class=\"hljs-keyword\">JOIN<\/span> book_authors b \n\t\t<span class=\"hljs-keyword\">ON<\/span> b.author_id = a.author_id\n<span class=\"hljs-keyword\">WHERE<\/span> \n\t<span class=\"hljs-keyword\">EXISTS<\/span> (\n\t\t<span class=\"hljs-keyword\">SELECT<\/span> \n\t\t\tbook_id\n\t\t<span class=\"hljs-keyword\">FROM<\/span> \n\t\t\tbooks\n\t\t<span class=\"hljs-keyword\">WHERE<\/span> \n\t\t\t<span class=\"hljs-keyword\">YEAR<\/span>(published_date) = <span class=\"hljs-number\">2018<\/span> <span class=\"hljs-keyword\">AND<\/span> \n        \tbook_id = b.book_id\n);\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"194\" height=\"516\" src=\"https:\/\/db2tutorial.com\/wp-content\/uploads\/2019\/05\/Db2-Subquery-with-the-EXISTS-operator-example.png\" alt=\"Db2 Subquery with the EXISTS operator example\" class=\"wp-image-392\" srcset=\"https:\/\/www.db2tutorial.com\/wp-content\/uploads\/2019\/05\/Db2-Subquery-with-the-EXISTS-operator-example.png 194w, https:\/\/www.db2tutorial.com\/wp-content\/uploads\/2019\/05\/Db2-Subquery-with-the-EXISTS-operator-example-113x300.png 113w\" sizes=\"auto, (max-width: 194px) 100vw, 194px\" \/><\/figure>\n\n\n\n<p>In this tutorial, you have learned about the Db2 subquery and how to use various types of subqueries to query data.<\/p>\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=\"384\"\n\t\t\t\tdata-post-url=\"https:\/\/www.db2tutorial.com\/db2-basics\/db2-subquery\/\"\n\t\t\t\tdata-post-title=\"Db2 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=\"384\"\n\t\t\t\tdata-post-url=\"https:\/\/www.db2tutorial.com\/db2-basics\/db2-subquery\/\"\n\t\t\t\tdata-post-title=\"Db2 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, we will introduce you to the Db2 subquery or subselect which is a select statement nested inside another statement such as SELECT, INSERT, UPDATE, and DELETE.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":141,"menu_order":21,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-384","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Db2 Subquery<\/title>\n<meta name=\"description\" content=\"In this tutorial, you will learn about Db2 subquery or subselect which is a select statement nested inside another statement such as SELECT, INSERT, UPDATE, and DELETE.\" \/>\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.db2tutorial.com\/db2-basics\/db2-subquery\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Db2 Subquery\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you will learn about Db2 subquery or subselect which is a select statement nested inside another statement such as SELECT, INSERT, UPDATE, and DELETE.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.db2tutorial.com\/db2-basics\/db2-subquery\/\" \/>\n<meta property=\"og:site_name\" content=\"DB2 Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2020-04-11T14:59:24+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/db2tutorial.com\/wp-content\/uploads\/2019\/05\/Db2-Subquery-Example.png\" \/>\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.db2tutorial.com\/db2-basics\/db2-subquery\/\",\"url\":\"https:\/\/www.db2tutorial.com\/db2-basics\/db2-subquery\/\",\"name\":\"Db2 Subquery\",\"isPartOf\":{\"@id\":\"https:\/\/www.db2tutorial.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.db2tutorial.com\/db2-basics\/db2-subquery\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.db2tutorial.com\/db2-basics\/db2-subquery\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/db2tutorial.com\/wp-content\/uploads\/2019\/05\/Db2-Subquery-Example.png\",\"datePublished\":\"2019-05-30T09:11:52+00:00\",\"dateModified\":\"2020-04-11T14:59:24+00:00\",\"description\":\"In this tutorial, you will learn about Db2 subquery or subselect which is a select statement nested inside another statement such as SELECT, INSERT, UPDATE, and DELETE.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.db2tutorial.com\/db2-basics\/db2-subquery\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.db2tutorial.com\/db2-basics\/db2-subquery\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.db2tutorial.com\/db2-basics\/db2-subquery\/#primaryimage\",\"url\":\"https:\/\/db2tutorial.com\/wp-content\/uploads\/2019\/05\/Db2-Subquery-Example.png\",\"contentUrl\":\"https:\/\/db2tutorial.com\/wp-content\/uploads\/2019\/05\/Db2-Subquery-Example.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.db2tutorial.com\/db2-basics\/db2-subquery\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.db2tutorial.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Db2 Basics\",\"item\":\"https:\/\/www.db2tutorial.com\/db2-basics\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Db2 Subquery\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.db2tutorial.com\/#website\",\"url\":\"https:\/\/www.db2tutorial.com\/\",\"name\":\"DB2 Tutorial\",\"description\":\"A Comprehensive DB2 Tutorial\",\"publisher\":{\"@id\":\"https:\/\/www.db2tutorial.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.db2tutorial.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.db2tutorial.com\/#organization\",\"name\":\"Db2 Tutorial\",\"url\":\"https:\/\/www.db2tutorial.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.db2tutorial.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/db2tutorial.com\/wp-content\/uploads\/2019\/03\/favicon.png\",\"contentUrl\":\"https:\/\/db2tutorial.com\/wp-content\/uploads\/2019\/03\/favicon.png\",\"width\":500,\"height\":500,\"caption\":\"Db2 Tutorial\"},\"image\":{\"@id\":\"https:\/\/www.db2tutorial.com\/#\/schema\/logo\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Db2 Subquery","description":"In this tutorial, you will learn about Db2 subquery or subselect which is a select statement nested inside another statement such as SELECT, INSERT, UPDATE, and DELETE.","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.db2tutorial.com\/db2-basics\/db2-subquery\/","og_locale":"en_US","og_type":"article","og_title":"Db2 Subquery","og_description":"In this tutorial, you will learn about Db2 subquery or subselect which is a select statement nested inside another statement such as SELECT, INSERT, UPDATE, and DELETE.","og_url":"https:\/\/www.db2tutorial.com\/db2-basics\/db2-subquery\/","og_site_name":"DB2 Tutorial","article_modified_time":"2020-04-11T14:59:24+00:00","og_image":[{"url":"https:\/\/db2tutorial.com\/wp-content\/uploads\/2019\/05\/Db2-Subquery-Example.png","type":"","width":"","height":""}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.db2tutorial.com\/db2-basics\/db2-subquery\/","url":"https:\/\/www.db2tutorial.com\/db2-basics\/db2-subquery\/","name":"Db2 Subquery","isPartOf":{"@id":"https:\/\/www.db2tutorial.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.db2tutorial.com\/db2-basics\/db2-subquery\/#primaryimage"},"image":{"@id":"https:\/\/www.db2tutorial.com\/db2-basics\/db2-subquery\/#primaryimage"},"thumbnailUrl":"https:\/\/db2tutorial.com\/wp-content\/uploads\/2019\/05\/Db2-Subquery-Example.png","datePublished":"2019-05-30T09:11:52+00:00","dateModified":"2020-04-11T14:59:24+00:00","description":"In this tutorial, you will learn about Db2 subquery or subselect which is a select statement nested inside another statement such as SELECT, INSERT, UPDATE, and DELETE.","breadcrumb":{"@id":"https:\/\/www.db2tutorial.com\/db2-basics\/db2-subquery\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.db2tutorial.com\/db2-basics\/db2-subquery\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.db2tutorial.com\/db2-basics\/db2-subquery\/#primaryimage","url":"https:\/\/db2tutorial.com\/wp-content\/uploads\/2019\/05\/Db2-Subquery-Example.png","contentUrl":"https:\/\/db2tutorial.com\/wp-content\/uploads\/2019\/05\/Db2-Subquery-Example.png"},{"@type":"BreadcrumbList","@id":"https:\/\/www.db2tutorial.com\/db2-basics\/db2-subquery\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.db2tutorial.com\/"},{"@type":"ListItem","position":2,"name":"Db2 Basics","item":"https:\/\/www.db2tutorial.com\/db2-basics\/"},{"@type":"ListItem","position":3,"name":"Db2 Subquery"}]},{"@type":"WebSite","@id":"https:\/\/www.db2tutorial.com\/#website","url":"https:\/\/www.db2tutorial.com\/","name":"DB2 Tutorial","description":"A Comprehensive DB2 Tutorial","publisher":{"@id":"https:\/\/www.db2tutorial.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.db2tutorial.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.db2tutorial.com\/#organization","name":"Db2 Tutorial","url":"https:\/\/www.db2tutorial.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.db2tutorial.com\/#\/schema\/logo\/image\/","url":"https:\/\/db2tutorial.com\/wp-content\/uploads\/2019\/03\/favicon.png","contentUrl":"https:\/\/db2tutorial.com\/wp-content\/uploads\/2019\/03\/favicon.png","width":500,"height":500,"caption":"Db2 Tutorial"},"image":{"@id":"https:\/\/www.db2tutorial.com\/#\/schema\/logo\/image\/"}}]}},"_links":{"self":[{"href":"https:\/\/www.db2tutorial.com\/wp-json\/wp\/v2\/pages\/384","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.db2tutorial.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.db2tutorial.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.db2tutorial.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.db2tutorial.com\/wp-json\/wp\/v2\/comments?post=384"}],"version-history":[{"count":1,"href":"https:\/\/www.db2tutorial.com\/wp-json\/wp\/v2\/pages\/384\/revisions"}],"predecessor-version":[{"id":1081,"href":"https:\/\/www.db2tutorial.com\/wp-json\/wp\/v2\/pages\/384\/revisions\/1081"}],"up":[{"embeddable":true,"href":"https:\/\/www.db2tutorial.com\/wp-json\/wp\/v2\/pages\/141"}],"wp:attachment":[{"href":"https:\/\/www.db2tutorial.com\/wp-json\/wp\/v2\/media?parent=384"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}