<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ricardo Tovar</title>
    <description>The latest articles on DEV Community by Ricardo Tovar (@ricardomtt).</description>
    <link>https://dev.to/ricardomtt</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F883150%2F11a05cbe-4797-480d-802d-2b273833f618.jpeg</url>
      <title>DEV Community: Ricardo Tovar</title>
      <link>https://dev.to/ricardomtt</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ricardomtt"/>
    <language>en</language>
    <item>
      <title>Subqueries</title>
      <dc:creator>Ricardo Tovar</dc:creator>
      <pubDate>Fri, 03 Jan 2025 01:29:10 +0000</pubDate>
      <link>https://dev.to/ricardomtt/subqueries-kcm</link>
      <guid>https://dev.to/ricardomtt/subqueries-kcm</guid>
      <description>&lt;p&gt;Subqueries are queries that come within another larger query. They are useful to obtain specific information that will later be used in the main query.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Type&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Single row subquery:&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Return a single value.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Multirow subquery&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Return multiple rows&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Correlated subquery&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It depends on a column in the main query, meaning it is executed once for each row in the outer query.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Nested subquery&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A subquery that contains another subquery within it.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Scalar subquery&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Returns a single value(similar to single row subquery) but can be used as a column in the SELECT.&lt;/p&gt;

&lt;p&gt;A subquery can appear in 3 places of your query:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;SELECT&lt;/li&gt;
&lt;li&gt;FROM OR INNER&lt;/li&gt;
&lt;li&gt;WHERE&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And each one of these parts have a set of rules .&lt;br&gt;
For example :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A subquery in SELECT should return a single value like:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT 
    nombre,
    (SELECT MAX(salary) FROM Employees) AS max_salary
FROM Employees;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A subquery in FROM OR INNER should return a set of rows&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A subquery in WHERE should return a column with many rows like:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT NAME FROM EMPLOYEES WHERE DEPARTMENT_ID = (SELECT ID FROM DEPARTMENTS WHERE DEPARTMENT = 'IT');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>sql</category>
      <category>database</category>
      <category>mysql</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
