{"id":2010,"date":"2019-08-12T07:11:34","date_gmt":"2019-08-12T07:11:34","guid":{"rendered":"https:\/\/www.testpreptraining.com\/tutorial\/?page_id=2010"},"modified":"2023-04-17T09:28:53","modified_gmt":"2023-04-17T09:28:53","slug":"query-data-by-using-subqueries-and-apply","status":"publish","type":"page","link":"https:\/\/www.testpreptraining.ai\/tutorial\/query-data-by-using-subqueries-and-apply\/","title":{"rendered":"Query data using subqueries and APPLY"},"content":{"rendered":"\n<p>Subqueries and APPLY are powerful tools in SQL that can be used to retrieve data from multiple tables and join them together. Here&#8217;s an explanation of how to use subqueries and APPLY in SQL:<\/p>\n\n\n\n<p>Subqueries: A subquery is a query that is nested inside another query. It is used to retrieve data from a single table or multiple tables that meet a specific condition. A subquery can be used in various clauses, such as SELECT, WHERE, and FROM. The result of the subquery is used by the outer query to retrieve data.<\/p>\n\n\n\n<p>For example, consider the following query:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SELECT name\nFROM customers\nWHERE customer_id IN (SELECT customer_id FROM orders WHERE order_date >= '2022-01-01');\n<\/code><\/pre>\n\n\n\n<p>In this query, the subquery retrieves customer_id values from the orders table where the order_date is greater than or equal to &#8216;2022-01-01&#8217;. The outer query then uses these customer_id values to retrieve names from the customers table.<\/p>\n\n\n\n<p>APPLY: APPLY is an operator that allows you to join a table with a table-valued function. A table-valued function returns a table, which can be used to join with the other table. There are two types of APPLY operators: CROSS APPLY and OUTER APPLY.<\/p>\n\n\n\n<p>CROSS APPLY returns only the rows from the left table that have a matching row in the right table. OUTER APPLY returns all the rows from the left table, even if there is no matching row in the right table.<\/p>\n\n\n\n<p>For example, consider the following query:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SELECT *\nFROM orders\nOUTER APPLY (SELECT TOP 1 * FROM customers WHERE customer_id = orders.customer_id ORDER BY customer_name) AS customer;\n<\/code><\/pre>\n\n\n\n<p>In this query, the APPLY operator is used to join the orders table with the customers table using the customer_id column. The TOP 1 clause ensures that only one row is returned from the customers table. The ORDER BY clause sorts the customers table by customer_name. The result of the query is a table that contains all the columns from the orders table and one additional column from the customers table.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example of how to use subqueries and APPLY in Microsoft SQL Server:<\/strong><\/h3>\n\n\n\n<p>Subqueries: Suppose we have two tables, orders and customers. The orders table contains information about orders placed by customers, and the customers table contains information about the customers. We can use a <a href=\"https:\/\/learn.microsoft.com\/en-us\/sql\/relational-databases\/performance\/subqueries?view=sql-server-ver16\" target=\"_blank\" rel=\"noreferrer noopener\">subquery<\/a> to retrieve the names of customers who have placed orders in the last month.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SELECT name\nFROM customers\nWHERE customer_id IN (SELECT customer_id FROM orders WHERE order_date >= DATEADD(MONTH, -1, GETDATE()));\n<\/code><\/pre>\n\n\n\n<p>In this query, the subquery retrieves customer_id values from the orders table where the order_date is greater than or equal to one month ago from the current date using the DATEADD and GETDATE functions. The outer query then uses these customer_id values to retrieve names from the customers table.<\/p>\n\n\n\n<p>APPLY: Suppose we have the same two tables, orders and customers. We want to retrieve all orders and the corresponding customer information for each order. We can use OUTER APPLY to achieve this.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SELECT *\nFROM orders\nOUTER APPLY (SELECT TOP 1 * FROM customers WHERE customer_id = orders.customer_id ORDER BY customer_name) AS customer;\n<\/code><\/pre>\n\n\n\n<p>In this query, the OUTER APPLY operator is used to join the orders table with the customers table using the customer_id column. The TOP 1 clause ensures that only one row is returned from the customers table. The ORDER BY clause sorts the customers table by customer_name. The result of the query is a table that contains all the columns from the orders table and one additional column from the customers table.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>70-761 Exam Practice Questions<\/strong><\/h2>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Q1. Which of the following queries retrieves all orders and the corresponding customer information for each order, including orders with no matching customer?<\/strong><\/h4>\n\n\n\n<p>A.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SELECT *\nFROM orders\nJOIN customers ON orders.customer_id = customers.customer_id;\n<\/code><\/pre>\n\n\n\n<p>B.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SELECT *\nFROM orders\nCROSS APPLY (SELECT TOP 1 * FROM customers WHERE customer_id = orders.customer_id ORDER BY customer_name) AS customer;\n<\/code><\/pre>\n\n\n\n<p>C.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SELECT *\nFROM orders\nOUTER APPLY (SELECT TOP 1 * FROM customers WHERE customer_id = orders.customer_id ORDER BY customer_name) AS customer;\n<\/code><\/pre>\n\n\n\n<p>D.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SELECT *\nFROM orders\nOUTER JOIN customers ON orders.customer_id = customers.customer_id;\n<\/code><\/pre>\n\n\n\n<p>Answer: c <\/p>\n\n\n\n<p>Explanation: The OUTER APPLY operator is used to join the orders table with the customers table using the customer_id column, and the OUTER keyword ensures that all orders are included in the result set, even if there is no matching customer. The TOP 1 and ORDER BY clauses ensure that only one row is returned from the customers table and that it is sorted by customer_name.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Q2. Which of the following queries retrieves the names of customers who have not placed any orders?<\/strong><\/h4>\n\n\n\n<p>A.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SELECT name\nFROM customers\nWHERE customer_id NOT IN (SELECT customer_id FROM orders);\n<\/code><\/pre>\n\n\n\n<p>B.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SELECT name\nFROM customers\nWHERE NOT EXISTS (SELECT customer_id FROM orders WHERE orders.customer_id = customers.customer_id);\n<\/code><\/pre>\n\n\n\n<p>C.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SELECT name\nFROM customers\nWHERE customer_id IN (SELECT customer_id FROM orders WHERE order_date >= DATEADD(MONTH, -1, GETDATE()));\n<\/code><\/pre>\n\n\n\n<p>D.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SELECT name\nFROM customers\nWHERE customer_id = ALL (SELECT customer_id FROM orders);\n<\/code><\/pre>\n\n\n\n<p>Answer: b <\/p>\n\n\n\n<p>Explanation: The NOT EXISTS operator is used to retrieve rows from the customers table where there is no matching row in the orders table. The subquery checks for customer_id values in the orders table that match the customer_id values in the customers table.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Q3. Which of the following queries retrieves the order IDs and corresponding customer names for orders placed in the last week?<\/strong><\/h4>\n\n\n\n<p>A.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SELECT order_id, customer_name\nFROM orders\nJOIN customers ON orders.customer_id = customers.customer_id\nWHERE order_date >= DATEADD(WEEK, -1, GETDATE());\n<\/code><\/pre>\n\n\n\n<p>B.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SELECT order_id, customer_name\nFROM orders\nOUTER APPLY (SELECT TOP 1 name AS customer_name FROM customers WHERE customer_id = orders.customer_id) AS customer\nWHERE order_date >= DATEADD(WEEK, -1, GETDATE());\n<\/code><\/pre>\n\n\n\n<p>C.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SELECT order_id, customer_name\nFROM orders\nOUTER APPLY (SELECT TOP 1 name AS customer_name FROM customers WHERE customer_id = orders.customer_id ORDER BY customer_name) AS customer\nWHERE order_date >= DATEADD(WEEK, -1, GETDATE());\n<\/code><\/pre>\n\n\n\n<p>D.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SELECT order_id, customer_name\nFROM orders\nCROSS APPLY (SELECT TOP 1 name AS customer_name FROM customers WHERE customer_id = orders.customer_id ORDER BY customer_name) AS customer\nWHERE order_date >= DATEADD(WEEK, -1, GETDATE());\n<\/code><\/pre>\n\n\n\n<p>Answer: c <\/p>\n\n\n\n<p>Explanation: The OUTER APPLY operator is used to join the orders table with the customers table using the customer_id column, and the TOP 1 and ORDER BY clauses ensure that only one row is returned from the customers table and that it is sorted by customer_name. The WHERE clause filters the result set to include only orders placed in the last week.<\/p>\n\n\n\n<p><strong><a href=\"https:\/\/www.testpreptraining.ai\/tutorial\/exam-70-761-querying-data-with-transact-sql\/\" target=\"_blank\" rel=\"noreferrer noopener\">Back to Tutorial 70-761<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Subqueries and APPLY are powerful tools in SQL that can be used to retrieve data from multiple tables and join them together. Here&#8217;s an explanation of how to use subqueries and APPLY in SQL: Subqueries: A subquery is a query that is nested inside another query. It is used to retrieve data from a single&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_acf_changed":false,"footnotes":""},"categories":[],"tags":[367,360,358,369,357,368],"class_list":["post-2010","page","type-page","status-publish","hentry","tag-apply-operator","tag-exam-70-761","tag-microsoft-70-761","tag-microsoft-70-761-exam-dumps","tag-microsoft-exam","tag-query-data"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Query data using subqueries and APPLY - Testprep Training Tutorials<\/title>\n<meta name=\"description\" content=\"Learn about Query Data by using subqueries and APPLY operator. Beccome Microsoft Certified with practice test on Microsoft 70-761 Now!\" \/>\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.testpreptraining.ai\/tutorial\/query-data-by-using-subqueries-and-apply\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Query data using subqueries and APPLY - Testprep Training Tutorials\" \/>\n<meta property=\"og:description\" content=\"Learn about Query Data by using subqueries and APPLY operator. Beccome Microsoft Certified with practice test on Microsoft 70-761 Now!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.testpreptraining.ai\/tutorial\/query-data-by-using-subqueries-and-apply\/\" \/>\n<meta property=\"og:site_name\" content=\"Testprep Training Tutorials\" \/>\n<meta property=\"article:modified_time\" content=\"2023-04-17T09:28:53+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=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.testpreptraining.ai\/tutorial\/query-data-by-using-subqueries-and-apply\/\",\"url\":\"https:\/\/www.testpreptraining.ai\/tutorial\/query-data-by-using-subqueries-and-apply\/\",\"name\":\"Query data using subqueries and APPLY - Testprep Training Tutorials\",\"isPartOf\":{\"@id\":\"https:\/\/www.testpreptraining.ai\/tutorial\/#website\"},\"datePublished\":\"2019-08-12T07:11:34+00:00\",\"dateModified\":\"2023-04-17T09:28:53+00:00\",\"description\":\"Learn about Query Data by using subqueries and APPLY operator. Beccome Microsoft Certified with practice test on Microsoft 70-761 Now!\",\"breadcrumb\":{\"@id\":\"https:\/\/www.testpreptraining.ai\/tutorial\/query-data-by-using-subqueries-and-apply\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.testpreptraining.ai\/tutorial\/query-data-by-using-subqueries-and-apply\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.testpreptraining.ai\/tutorial\/query-data-by-using-subqueries-and-apply\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.testpreptraining.ai\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Query data using subqueries and APPLY\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.testpreptraining.ai\/tutorial\/#website\",\"url\":\"https:\/\/www.testpreptraining.ai\/tutorial\/\",\"name\":\"Testprep Training Tutorials\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.testpreptraining.ai\/tutorial\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.testpreptraining.ai\/tutorial\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.testpreptraining.ai\/tutorial\/#organization\",\"name\":\"Testprep Training\",\"url\":\"https:\/\/www.testpreptraining.ai\/tutorial\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.testpreptraining.ai\/tutorial\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.testpreptraining.com\/tutorial\/wp-content\/uploads\/2020\/07\/tpt-logo-6.png\",\"contentUrl\":\"https:\/\/www.testpreptraining.com\/tutorial\/wp-content\/uploads\/2020\/07\/tpt-logo-6.png\",\"width\":583,\"height\":153,\"caption\":\"Testprep Training\"},\"image\":{\"@id\":\"https:\/\/www.testpreptraining.ai\/tutorial\/#\/schema\/logo\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Query data using subqueries and APPLY - Testprep Training Tutorials","description":"Learn about Query Data by using subqueries and APPLY operator. Beccome Microsoft Certified with practice test on Microsoft 70-761 Now!","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.testpreptraining.ai\/tutorial\/query-data-by-using-subqueries-and-apply\/","og_locale":"en_US","og_type":"article","og_title":"Query data using subqueries and APPLY - Testprep Training Tutorials","og_description":"Learn about Query Data by using subqueries and APPLY operator. Beccome Microsoft Certified with practice test on Microsoft 70-761 Now!","og_url":"https:\/\/www.testpreptraining.ai\/tutorial\/query-data-by-using-subqueries-and-apply\/","og_site_name":"Testprep Training Tutorials","article_modified_time":"2023-04-17T09:28:53+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.testpreptraining.ai\/tutorial\/query-data-by-using-subqueries-and-apply\/","url":"https:\/\/www.testpreptraining.ai\/tutorial\/query-data-by-using-subqueries-and-apply\/","name":"Query data using subqueries and APPLY - Testprep Training Tutorials","isPartOf":{"@id":"https:\/\/www.testpreptraining.ai\/tutorial\/#website"},"datePublished":"2019-08-12T07:11:34+00:00","dateModified":"2023-04-17T09:28:53+00:00","description":"Learn about Query Data by using subqueries and APPLY operator. Beccome Microsoft Certified with practice test on Microsoft 70-761 Now!","breadcrumb":{"@id":"https:\/\/www.testpreptraining.ai\/tutorial\/query-data-by-using-subqueries-and-apply\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.testpreptraining.ai\/tutorial\/query-data-by-using-subqueries-and-apply\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.testpreptraining.ai\/tutorial\/query-data-by-using-subqueries-and-apply\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.testpreptraining.ai\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Query data using subqueries and APPLY"}]},{"@type":"WebSite","@id":"https:\/\/www.testpreptraining.ai\/tutorial\/#website","url":"https:\/\/www.testpreptraining.ai\/tutorial\/","name":"Testprep Training Tutorials","description":"","publisher":{"@id":"https:\/\/www.testpreptraining.ai\/tutorial\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.testpreptraining.ai\/tutorial\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.testpreptraining.ai\/tutorial\/#organization","name":"Testprep Training","url":"https:\/\/www.testpreptraining.ai\/tutorial\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.testpreptraining.ai\/tutorial\/#\/schema\/logo\/image\/","url":"https:\/\/www.testpreptraining.com\/tutorial\/wp-content\/uploads\/2020\/07\/tpt-logo-6.png","contentUrl":"https:\/\/www.testpreptraining.com\/tutorial\/wp-content\/uploads\/2020\/07\/tpt-logo-6.png","width":583,"height":153,"caption":"Testprep Training"},"image":{"@id":"https:\/\/www.testpreptraining.ai\/tutorial\/#\/schema\/logo\/image\/"}}]}},"_links":{"self":[{"href":"https:\/\/www.testpreptraining.ai\/tutorial\/wp-json\/wp\/v2\/pages\/2010","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testpreptraining.ai\/tutorial\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.testpreptraining.ai\/tutorial\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.testpreptraining.ai\/tutorial\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testpreptraining.ai\/tutorial\/wp-json\/wp\/v2\/comments?post=2010"}],"version-history":[{"count":11,"href":"https:\/\/www.testpreptraining.ai\/tutorial\/wp-json\/wp\/v2\/pages\/2010\/revisions"}],"predecessor-version":[{"id":61368,"href":"https:\/\/www.testpreptraining.ai\/tutorial\/wp-json\/wp\/v2\/pages\/2010\/revisions\/61368"}],"wp:attachment":[{"href":"https:\/\/www.testpreptraining.ai\/tutorial\/wp-json\/wp\/v2\/media?parent=2010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testpreptraining.ai\/tutorial\/wp-json\/wp\/v2\/categories?post=2010"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testpreptraining.ai\/tutorial\/wp-json\/wp\/v2\/tags?post=2010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}