{"id":471,"date":"2014-10-07T10:56:00","date_gmt":"2014-10-07T17:56:00","guid":{"rendered":"https:\/\/blogs.ubc.ca\/coetoolbox\/?page_id=471"},"modified":"2016-10-21T13:33:23","modified_gmt":"2016-10-21T20:33:23","slug":"nested-queries","status":"publish","type":"page","link":"https:\/\/blogs.ubc.ca\/coetoolbox\/databases\/nested-queries\/","title":{"rendered":"Nested Queries"},"content":{"rendered":"<p>A nested query or subquery is a query within a query. Subqueries are usually added in the WHERE clause of the SQL statement when you know the conditions for a field&#8217;s value but do not know the exact value in the database. Conditions can be created using comparison operators such as =, IN, EXIST or UNIQUE.<\/p>\n<h2><strong>Comparing single values from subqueries<br \/>\n<\/strong><\/h2>\n<p>When nested queries return a single value, the returned value can be compared using\u00a0 =, &lt;, &gt;, etc. Example: finding student(s) who got the best grade registered.<\/p>\n<p style=\"padding-left: 30px;\">SELECT StudentID, FinalGrade<br \/>\nFROM Registrations<br \/>\nWHERE FinalGrade <span style=\"color: #0000ff;\">=<\/span> <span style=\"color: #993300;\"><em>(SELECT MAX (FinalGrade)<\/em><\/span> <span style=\"color: #993300;\"><em>FROM Registrations<\/em><\/span><span style=\"color: #993300;\"><em>)<\/em><\/span><\/p>\n<p>The nested query returns the maximum grade recorded, which is used in the parent query to find the corresponding student.<\/p>\n<h2>IN operator<\/h2>\n<p>Compares a field to each value in a list returned by a nested query. Example: names of students who have registered in BAMS 580D.<\/p>\n<p style=\"padding-left: 30px;\">SELECT StudentName<br \/>\nFROM Students<br \/>\nWHERE StudentID <span style=\"color: #0000ff;\">IN<\/span> <span style=\"color: #993300;\"><em>(SELECT StudentID\u000b<\/em> <em>FROM Registrations<\/em> <em>WHERE CourseID= &#8216;BAMS 580D&#8217;)<\/em><\/span><\/p>\n<h2>EXISTS operator<\/h2>\n<p>Returns TRUE if the result set of the subquery is not empty. Example: names of students who have registered in BAMS 580:<\/p>\n<p style=\"padding-left: 30px;\">SELECT S.StudentName<br \/>\nFROM Students S<br \/>\nWHERE <span style=\"color: #0000ff;\">EXISTS<\/span> <span style=\"color: #993300;\"><em>(SELECT *\u000b FROM Registrations R\u000b<\/em><\/span><br \/>\n<span style=\"color: #993300;\"> <em> WHERE R.CourseID= \u2018BAMS 580\u2019 AND S.StudentID = R.StudentID)<\/em><\/span><\/p>\n<p>The subquery is recomputed for each Student tuple of the first query so the fields from the parent query can be used in the nested query.<\/p>\n<h2>Other subquery operators<\/h2>\n<p>Other operators to compare results from nested queries are:<\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>UNIQUE<\/td>\n<td>Checks for duplicate tuples in a subquery and returns true if there are no duplicates.<\/td>\n<\/tr>\n<tr>\n<td>ALL<\/td>\n<td>Compares a value with every value in a list or returned by a query.<br \/>\nMust be preceded by =, &gt;, &lt;, etc. Evaluates to TRUE if the query returns no rows.<\/td>\n<\/tr>\n<tr>\n<td>ANY<\/td>\n<td>Compares a value to each value in a list or returned by a query.<br \/>\nMust be preceded by =, &gt;, &lt;, etc. Evaluates to FALSE if the query returns no rows.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Subqueries in FROM clause<\/h2>\n<p>Used in the FROM clause, nested queries are an alternate way of pre-processing data from tables, for instance to filter students from 2014:<\/p>\n<p style=\"padding-left: 30px;\">SELECT Students2014.*<br \/>\nFROM (SELECT StudentID FROM Students WHERE Year = 2014) as Students2014<\/p>\n<h2>WITH clause<\/h2>\n<p>Another way of creating nested queries is using the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms175972%28v=sql.110%29.aspx\" target=\"_blank\">WITH clause<\/a> (not available in MS Access), which creates a temporary table that can be queried.<\/p>\n<p>See <a href=\"http:\/\/beginner-sql-tutorial.com\/sql-subquery.htm\" target=\"_blank\">SQL subquery<\/a> to read more about subqueries. See <a href=\"https:\/\/docs.oracle.com\/html\/A95915_01\/sqopr.htm\" target=\"_blank\">Oracle SQL Operators<\/a> to read more about operators.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A nested query or subquery is a query within a query. Subqueries are usually added in the WHERE clause of the SQL statement when you know the conditions for a field&#8217;s value but do not know the exact value in the database. Conditions can be created using comparison operators such as =, IN, EXIST or [&hellip;]<\/p>\n","protected":false},"author":22982,"featured_media":0,"parent":74,"menu_order":5,"comment_status":"open","ping_status":"closed","template":"page-templates\/full-width.php","meta":{"footnotes":""},"class_list":["post-471","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/pages\/471","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/users\/22982"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/comments?post=471"}],"version-history":[{"count":13,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/pages\/471\/revisions"}],"predecessor-version":[{"id":1541,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/pages\/471\/revisions\/1541"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/pages\/74"}],"wp:attachment":[{"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/media?parent=471"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}