<?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: Goran Kortjie</title>
    <description>The latest articles on DEV Community by Goran Kortjie (@ifierygod).</description>
    <link>https://dev.to/ifierygod</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%2F444037%2F92f14527-35d0-4c0e-9079-d9fae6ffa61b.jpeg</url>
      <title>DEV Community: Goran Kortjie</title>
      <link>https://dev.to/ifierygod</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ifierygod"/>
    <language>en</language>
    <item>
      <title>Learn SQL: Microsoft SQL Server - Episode 16: Outer Join | Left and Right</title>
      <dc:creator>Goran Kortjie</dc:creator>
      <pubDate>Sat, 08 Jan 2022 22:33:25 +0000</pubDate>
      <link>https://dev.to/ifierygod/learn-sql-microsoft-sql-server-episode-16-outer-join-left-and-right-4ina</link>
      <guid>https://dev.to/ifierygod/learn-sql-microsoft-sql-server-episode-16-outer-join-left-and-right-4ina</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F69dr51pdk5j5k1v9leav.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F69dr51pdk5j5k1v9leav.gif" alt="hi-everyone"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this last entry, I want to revisit how &lt;strong&gt;Join&lt;/strong&gt; operations work, we know so far they will match the data from one table to another table and pull the joining records.&lt;/p&gt;

&lt;p&gt;However sometimes that might not be enough. In some cases you might want to pull the records from one table and then joining records from the other table with empty values, whenever there is no matching records.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Thats sounds confusing so lets dive into SSMS to make it more clear.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Lets write a SQL query that will pull all customers who have placed an order. Any number of orders. We will be using &lt;strong&gt;Sales.SalesOrder.Header&lt;/strong&gt; and &lt;strong&gt;Sales.Customer&lt;/strong&gt; tables.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sales.Customer&lt;/strong&gt; is the list of customers and &lt;strong&gt;Sales.SalesOrderHeader&lt;/strong&gt; is the list of all the orders.&lt;/p&gt;

&lt;p&gt;We will construct this query as follows.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Select * from Sales.Customer a
join Sales.SalesOrderHeader b
on a.CustomerID = b.CustomerID
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;I made use of aliases to make it a little easier to construct this query statement, by giving &lt;strong&gt;Sales.Customer&lt;/strong&gt; an Alias of &lt;strong&gt;a&lt;/strong&gt; and &lt;strong&gt;Sales.SalesOrderHeader&lt;/strong&gt; the Alias of &lt;strong&gt;b&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We basically want to join the two tables based on the matching &lt;strong&gt;CustomerID&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmbt8o1de9u36f7byftcy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmbt8o1de9u36f7byftcy.gif" alt="Join-tables"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice how many results was returned from this statement. 31465 total rows.&lt;/p&gt;

&lt;p&gt;We will add a comment inside our query to indicate this result. A comment is not evaluated by SQL. It is typically used by us as programmers and developers to explain our intent to other programmers and our future selfs when we return to our code.&lt;/p&gt;

&lt;p&gt;The way we write comments in SQL is by prefixing our comment with two dashes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-- total (comment)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;for example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-- total number of records
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we want to get a list of all the customers, whether they have placed orders or not for this result set, if they haven't placed an order then all the sales records will be &lt;strong&gt;Null&lt;/strong&gt; or empty.&lt;/p&gt;

&lt;h4&gt;
  
  
  Left
&lt;/h4&gt;

&lt;p&gt;To accomplish this we need to write an &lt;strong&gt;Outer Join&lt;/strong&gt;. To write an &lt;strong&gt;Outer Join&lt;/strong&gt; we need to decide which table do we want to get all the data from. We specify the table we want to get all the data from by indicating it as either &lt;strong&gt;Left&lt;/strong&gt; or &lt;strong&gt;Right&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Select * from Sales.Customer a
left join Sales.SalesOrderHeader b
on a.CustomerID = b.CustomerID
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the statement above, &lt;strong&gt;Left&lt;/strong&gt; refers to the &lt;strong&gt;Sales.Customer&lt;/strong&gt; table. Which means we want to get all the data from &lt;strong&gt;Sales.Customer&lt;/strong&gt; and if there are any matching records in the &lt;strong&gt;Sales.SalesOrderHeader&lt;/strong&gt; table return those records, otherwise leave those records as &lt;strong&gt;Null&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq679idn76r7vqdra9d2o.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq679idn76r7vqdra9d2o.gif" alt="left-join"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Right
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;Right&lt;/strong&gt; method works exactly like the &lt;strong&gt;Left&lt;/strong&gt;, except it refers to the table on end of the query statement. In our example above it would refer to &lt;strong&gt;Sales.SalesOrderHeader&lt;/strong&gt; table.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxqdkt9oqmyfos5odosuj.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxqdkt9oqmyfos5odosuj.gif" alt="right-method"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That concludes this series on Learn SQL. I must say I was intimidated by SQL when I first heard of it back in 2020. &lt;/p&gt;

&lt;p&gt;After going on this journey I have realised my error in judgement and found myself understanding the concepts quite easily, since they resemble javascript in many ways, thankfully not all ways.&lt;/p&gt;

&lt;p&gt;Once again I hope you enjoyed this series and have a great 2022. Namaste.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa3719v1wx1c7oa9izye1.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa3719v1wx1c7oa9izye1.gif" alt="Yoga"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>sql</category>
      <category>learning</category>
      <category>database</category>
      <category>programming</category>
    </item>
    <item>
      <title>Learn SQL: Microsoft SQL Server - Episode 15: Deleting records from a table</title>
      <dc:creator>Goran Kortjie</dc:creator>
      <pubDate>Sat, 08 Jan 2022 21:49:50 +0000</pubDate>
      <link>https://dev.to/ifierygod/learn-sql-microsoft-sql-server-episode-15-deleting-records-from-a-table-3phm</link>
      <guid>https://dev.to/ifierygod/learn-sql-microsoft-sql-server-episode-15-deleting-records-from-a-table-3phm</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgxdmfu39ewsvamwxyc9g.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgxdmfu39ewsvamwxyc9g.gif" alt="Hello-Goran"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Time for the scariest segment of SQL query statements. As in the previous entry, we need to make sure that we are working with the correct data.&lt;/p&gt;

&lt;p&gt;Continuing with our previous episode, we will be working in the &lt;strong&gt;HumanResourcesDepartment&lt;/strong&gt; table. This is where we will be performing the delete operation, we will delete the row with the &lt;strong&gt;DepartmentID&lt;/strong&gt; = 17. &lt;/p&gt;

&lt;p&gt;To construct a delete statement we need to use the following syntax.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;delete from [table-name]
where [column-name] = value
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Notice how this allows us to wipe out the entire &lt;strong&gt;HumanResourcesDepartment&lt;/strong&gt; table if we were to omit the &lt;strong&gt;Where&lt;/strong&gt; clause.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk2a2sc8y1u5dmd2qm5zz.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk2a2sc8y1u5dmd2qm5zz.gif" alt="Delete-a-single-record"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can use any Column-Name in the table to specify which row we want to delete. Generally using a &lt;em&gt;&lt;strong&gt;key&lt;/strong&gt;&lt;/em&gt; would be safest for deleting a single record in a table.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;As a side-note...&lt;/em&gt;We can also delete multiple rows simultaneously by specifying a column that has a value present in more than one record or row. For example in the &lt;code&gt;GroupName&lt;/code&gt; with the value &lt;u&gt;'Executive General and Administration'&lt;/u&gt;. If we pass in this value in our &lt;strong&gt;Where&lt;/strong&gt; clause it will remove each row containing &lt;u&gt;'Executive General and Administration'&lt;/u&gt; in it's &lt;code&gt;GroupName&lt;/code&gt; column.&lt;/p&gt;

&lt;h4&gt;
  
  
  Be safe
&lt;/h4&gt;

&lt;p&gt;To make this perfectly clear, we need to ensure the row or rows we want to delete are the values specified in the &lt;em&gt;Where clause&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;I personally would double-check the row or rows I need to delete by using a Select statement.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Select * from [Table-Name]
Where [column-name] = [value]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By using this statement before we perform our delete operation we can confirm that we are working with the correct data.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpw6xygk8h6ask1hfbj8f.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpw6xygk8h6ask1hfbj8f.gif" alt="select-statement-check-before-delete"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Always make sure you are working with the correct data and never perform a &lt;strong&gt;Delete&lt;/strong&gt; operation without a &lt;em&gt;Where clause&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Thats all from me, I hope you enjoyed this episode and look forward to the next and final entry on &lt;strong&gt;Learn SQL&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvdeaxbsfv994vby89e9n.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvdeaxbsfv994vby89e9n.gif" alt="relax-man"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>sql</category>
      <category>learning</category>
      <category>database</category>
      <category>programming</category>
    </item>
    <item>
      <title>Learn SQL: Microsoft SQL Server - Episode 14: Update records in a table</title>
      <dc:creator>Goran Kortjie</dc:creator>
      <pubDate>Sat, 08 Jan 2022 21:42:48 +0000</pubDate>
      <link>https://dev.to/ifierygod/learn-sql-microsoft-sql-server-episode-14-update-records-in-a-table-2g95</link>
      <guid>https://dev.to/ifierygod/learn-sql-microsoft-sql-server-episode-14-update-records-in-a-table-2g95</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxl2u9n2bv203omf85hqt.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxl2u9n2bv203omf85hqt.gif" alt="goran-greeting"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Have you ever wondered how to update your data in a table by using SQL Statements?&lt;/strong&gt; In this entry we are going to explore and demonstrate how we can accomplish this exciting task.&lt;/p&gt;

&lt;p&gt;The task of updating data comes in handy, when we find the data in the table to be incorrect, or in a scenario where we need to make some changes based on some new information we received.&lt;/p&gt;

&lt;p&gt;To demonstrate this, let’s use a &lt;strong&gt;SELECT&lt;/strong&gt; statement on the &lt;strong&gt;HumanResourcesDepartment&lt;/strong&gt; table in SSMS.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fklcg8xzc1lnttpjphoyf.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fklcg8xzc1lnttpjphoyf.gif" alt="select-human-resource-department-table"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;result set&lt;/em&gt; of running this SQL statement gives us the data currently inside the &lt;strong&gt;HumanResourcesDepartment&lt;/strong&gt; table. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I have inserted the data from the previous episode, back into the table for consistency.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Imagine we made a mistake with the last entry when we inserted the data into the table and instead of &lt;u&gt;'Growth and Education'&lt;/u&gt;, the &lt;code&gt;GroupName&lt;/code&gt; column should contain &lt;u&gt;'Growth and Development'&lt;/u&gt;. To accomplish this, we need to construct an &lt;em&gt;Update Query Statement&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The syntax for the update statement is as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;update [Table-Name] 
set [Column-Name] = value
Where [Unique-Identifier] = [Identifier]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Ensure the unique identifier is in fact unique in the table, usually the key for the table would be unique and therefore makes an excellent value to use.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;By following this syntax, if we want to change the value in &lt;code&gt;GroupName&lt;/code&gt;, this is how it would look.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;update [HumanResources].[Department]
set GroupName = 'Growth and Development'
Where DepartmentID = 17
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we execute this &lt;em&gt;Update Statement&lt;/em&gt; and then check the data in the &lt;strong&gt;HumanResourcesDepartment&lt;/strong&gt; table, we should see the data has updated.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyyv2cow61l8lmfz0hawl.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyyv2cow61l8lmfz0hawl.gif" alt="update-table"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Just to make it absolutely clear, the &lt;strong&gt;Where&lt;/strong&gt; clause inside of your Update Statement is very scary, if we provide it with incorrect information it could end up updating data where we did not intend or worse update an entire column of data.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;I personally would double-check the column I need to update by using a &lt;strong&gt;Select&lt;/strong&gt; statement.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Select * from [Table-Name]
Where [Unique-Identifier] = [Identifier]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By using this &lt;em&gt;Select Statement&lt;/em&gt; before we perform our &lt;em&gt;Update Statement&lt;/em&gt; we can confirm that we are working with the correct data.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkgrcxfq2x4nodq3zpdwf.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkgrcxfq2x4nodq3zpdwf.gif" alt="check-before-update"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Update Multiple Columns
&lt;/h4&gt;

&lt;p&gt;We can even update multiple columns in one SQL statement. For instance, we want to change the value of the &lt;code&gt;Name&lt;/code&gt; column from &lt;u&gt;'Learning'&lt;/u&gt; to &lt;u&gt;'Upskill'&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;All we need to do is separate the Column-Name in the &lt;em&gt;SET Clause&lt;/em&gt; with dashes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;update [Table-Name] 
set [Column-Name] = value, [Column-Name] = value, ...
Where [Unique-Identifier] = [Identifier]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;We can update as many columns as we want for the given table. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let’s update the &lt;code&gt;Name&lt;/code&gt; and &lt;code&gt;ModifiedDate&lt;/code&gt; for the row with the &lt;strong&gt;DepartmentID&lt;/strong&gt; = 17.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;update [HumanResources].[Department]
set GroupName = 'Growth and Development', Name = 'Upskill', ModifiedDate = getDate()
Where DepartmentID = 17
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flej2b94161wni6cuwjae.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flej2b94161wni6cuwjae.gif" alt="update-multiple-columns"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's all, this is how we can update records in a table. Easy right!&lt;/p&gt;

&lt;p&gt;I really hope this entry was informative and helps you along your journey to mastering SQL.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnndvmaegu9xyk0jbr6n5.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnndvmaegu9xyk0jbr6n5.gif" alt="baby-yoda"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>sql</category>
      <category>beginners</category>
      <category>database</category>
      <category>programming</category>
    </item>
    <item>
      <title>Learn SQL: Microsoft SQL Server - Episode 13: Insert new records in a Table</title>
      <dc:creator>Goran Kortjie</dc:creator>
      <pubDate>Thu, 23 Dec 2021 17:01:49 +0000</pubDate>
      <link>https://dev.to/ifierygod/learn-sql-microsoft-sql-server-episode-13-insert-new-records-in-a-table-535b</link>
      <guid>https://dev.to/ifierygod/learn-sql-microsoft-sql-server-episode-13-insert-new-records-in-a-table-535b</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MGYJ7Odf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rre6ht67x43tvsgs6cku.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MGYJ7Odf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rre6ht67x43tvsgs6cku.gif" alt="greetings" width="480" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this episode we will be discussing a key concept of SQL and database administration, we will be inserting a new record into a table.&lt;/p&gt;

&lt;p&gt;We will be using the AdventureWorks database as usual, here we will be inserting a record into the &lt;strong&gt;HumanResourcesDepartment&lt;/strong&gt; table.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WPxCKZLT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ozjyarh41n3jbksgk8f9.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WPxCKZLT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ozjyarh41n3jbksgk8f9.gif" alt="12" width="800" height="100"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We first need to see which columns are available in this table. Inside the &lt;strong&gt;HumanResourcesDepartment&lt;/strong&gt; table there are four columns, namely the &lt;code&gt;DepartmentID&lt;/code&gt;, &lt;code&gt;Name&lt;/code&gt;, &lt;code&gt;GroupName&lt;/code&gt; and &lt;code&gt;ModifiedDate&lt;/code&gt; columns respectively.&lt;/p&gt;

&lt;p&gt;These are the columns we will be inserting records, we will be performing these tasks using a SQL statement.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Rbnf0Tnn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dx6qbh6ihrl0t9302dxp.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Rbnf0Tnn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dx6qbh6ihrl0t9302dxp.gif" alt="see-columns" width="600" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In most cases, the &lt;u&gt;primary key&lt;/u&gt; &lt;strong&gt;(PK)&lt;/strong&gt; column does not require an explicit value to be entered. This value is &lt;u&gt;automatically generated&lt;/u&gt; from the system.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--byoItYcP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2q4x2de1htz2gh0ijkaf.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--byoItYcP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2q4x2de1htz2gh0ijkaf.gif" alt="11" width="800" height="100"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To determine whether we need to add a value to a specific column or not, follow these steps in SMSS.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Right-click&lt;/strong&gt; on the table-name and click on &lt;code&gt;design&lt;/code&gt;. When we click on &lt;code&gt;design&lt;/code&gt; we will see all the column names, their data types and whether they allow &lt;em&gt;NULL&lt;/em&gt; values or not.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wDqN1af2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2wfl1ta95cun33z1ofdp.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wDqN1af2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2wfl1ta95cun33z1ofdp.gif" alt="4" width="800" height="100"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Below we will see a &lt;code&gt;Column Properties&lt;/code&gt; window. When we click on our different columns above, we see the column properties below change.&lt;/p&gt;

&lt;p&gt;We click on the &lt;strong&gt;DepartmentID&lt;/strong&gt; column; next in the &lt;code&gt;Column Properties&lt;/code&gt; window scroll down until we see &lt;strong&gt;Identity Specification&lt;/strong&gt;. We expand this by clicking on the arrow on the &lt;strong&gt;left-side&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;These values indicate that this column is in fact an &lt;strong&gt;identity&lt;/strong&gt; column. &lt;code&gt;Identity Increment&lt;/code&gt; means the identity column increases by 1 every-time a new value is entered and the &lt;code&gt;Identity Seed&lt;/code&gt; means the starting value was 1.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4DWxzKM1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mjwkpuu9yynxdco8tkxx.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4DWxzKM1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mjwkpuu9yynxdco8tkxx.gif" alt="6" width="800" height="100"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In short, every-time we add a new record; we will automatically generate a new value, which will be one higher than the previous highest value.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2EOtuFlp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/17f2qvvtbc3f6t36eigk.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2EOtuFlp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/17f2qvvtbc3f6t36eigk.gif" alt="column-properties" width="600" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We could check the other columns to determine if they are &lt;strong&gt;Identity&lt;/strong&gt; columns, however they are not number values and additionally they do not increment, therefore it is safe to assume that &lt;strong&gt;DepartmentID&lt;/strong&gt; column is the only column that is &lt;strong&gt;identity&lt;/strong&gt; in this table.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;However we should double-check them to make absolutely certain.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8YaNFjp_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/de04e724pcf9q0j9r30e.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8YaNFjp_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/de04e724pcf9q0j9r30e.gif" alt="1" width="800" height="100"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Insert a new record
&lt;/h4&gt;

&lt;p&gt;The structure of an &lt;u&gt;insert statement&lt;/u&gt; is a bit different from what we have seen thus far.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;insert into [Table-Name] (column-name/s)
values (values to insert into sql)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For example, to insert a new record in the &lt;strong&gt;HumanResourcesDepartment&lt;/strong&gt; table we can write it as follows.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;insert into [HumanResources].[Department] (Name, GroupName, ModifiedDate)
values ('Learning', 'Growth and Education', getdate())
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Notice how the values in parenthesis, &lt;strong&gt;(column-names/s)&lt;/strong&gt; and &lt;strong&gt;(values to insert into sql)&lt;/strong&gt; need to correspond.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This means &lt;code&gt;Name&lt;/code&gt; corresponds to &lt;strong&gt;'Learning'&lt;/strong&gt;; &lt;code&gt;GroupName&lt;/code&gt; corresponds to &lt;strong&gt;'Growth and Education'&lt;/strong&gt; and &lt;code&gt;ModifiedDate&lt;/code&gt; corresponds to &lt;strong&gt;getdate()&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Getdate&lt;/strong&gt; is a special &lt;code&gt;built-in&lt;/code&gt; function provided by SMSS and allows us to get the current date.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--byoItYcP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2q4x2de1htz2gh0ijkaf.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--byoItYcP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2q4x2de1htz2gh0ijkaf.gif" alt="11" width="800" height="100"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s see this in action within SMSS.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oTrAd14S--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rhegcuayp5ejxr7k4yqu.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oTrAd14S--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rhegcuayp5ejxr7k4yqu.gif" alt="inserting-records" width="600" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once we execute this SQL query we get the message &lt;code&gt;1 row affected&lt;/code&gt; followed by the time that the query executed.&lt;/p&gt;

&lt;p&gt;We can perform a &lt;strong&gt;SELECT&lt;/strong&gt; statement to confirm our record has been inserted into the &lt;strong&gt;HumanResourcesDepartment&lt;/strong&gt; table.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MsF1NihZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/876w1ie02nc55jdzaxlz.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MsF1NihZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/876w1ie02nc55jdzaxlz.gif" alt="added-record" width="600" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There you have it, this is how we insert a new record into a table.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DRyIgkzQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/54n8oyp2u5oohem46ysu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DRyIgkzQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/54n8oyp2u5oohem46ysu.png" alt="cover" width="800" height="100"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Learn continually - there's always "one more thing" to learn.&lt;br&gt;
-Steve Jobs&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EUtDkPtm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p803ofktm85thr19t2ui.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EUtDkPtm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p803ofktm85thr19t2ui.gif" alt="Yoga-monkey" width="640" height="640"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>sql</category>
      <category>database</category>
      <category>programming</category>
      <category>analytics</category>
    </item>
    <item>
      <title>Learn SQL: Microsoft SQL Server - Episode 12: Finding the length of a String</title>
      <dc:creator>Goran Kortjie</dc:creator>
      <pubDate>Sat, 18 Dec 2021 07:35:20 +0000</pubDate>
      <link>https://dev.to/ifierygod/learn-sql-microsoft-sql-server-episode-12-finding-the-length-of-a-string-2h8l</link>
      <guid>https://dev.to/ifierygod/learn-sql-microsoft-sql-server-episode-12-finding-the-length-of-a-string-2h8l</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F06tfwqtkcxfxd0kuqmee.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F06tfwqtkcxfxd0kuqmee.gif" alt="red-puppy"&gt;&lt;/a&gt;&lt;br&gt;
Many times we have to look at how many characters there are in a string. Sometimes we even need to find the position of a character or a set of characters in a particular string in a column.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fisa66rjq0sym1ekmirfz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fisa66rjq0sym1ekmirfz.png" alt="spread-1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this episode, we will look at how we can accomplish this very exercise.&lt;/p&gt;

&lt;p&gt;In SMSS, for this example we will be using the &lt;strong&gt;Person.EmailAddress&lt;/strong&gt; table. We will start by writing a SQL statement to &lt;strong&gt;SELECT&lt;/strong&gt; everything from this table.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbdpva4fnapab6xc9ig4c.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbdpva4fnapab6xc9ig4c.gif" alt="skid-1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We do this to get familiar with the columns and what data exist inside of them.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpuz7nb8i1t75hrqwfco5.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpuz7nb8i1t75hrqwfco5.gif" alt="select-person-emailaddress-table"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will be using the &lt;strong&gt;EmailAddress&lt;/strong&gt; column in our SQL statement. The first thing we want to do is &lt;em&gt;count&lt;/em&gt; the number of characters in the &lt;strong&gt;EmailAddress&lt;/strong&gt; column for each row. To accomplish this we need to modify our SQL statement.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feokp2orfw4ldwp3mvj8j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feokp2orfw4ldwp3mvj8j.png" alt="spread-2"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  LEN
&lt;/h4&gt;

&lt;p&gt;We will first be replacing the * with &lt;strong&gt;EmailAddress&lt;/strong&gt;, this is to see which email address we are working with. We calculate the &lt;strong&gt;length&lt;/strong&gt; or the number of characters by using the &lt;strong&gt;LEN&lt;/strong&gt; function. &lt;strong&gt;LEN&lt;/strong&gt; measures the length of a column.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F36r8g0zzsbdp6vd8i94r.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F36r8g0zzsbdp6vd8i94r.gif" alt="skid-2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lets see this in practice in SMSS.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftrlz6ufv288vtuk8ro0f.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftrlz6ufv288vtuk8ro0f.gif" alt="select-len-statement"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This sort of function comes in handy, when we need to determine if there are any empty columns, or to extract a certain number of characters from a column. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feokp2orfw4ldwp3mvj8j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feokp2orfw4ldwp3mvj8j.png" alt="spread-2"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  CHARINDEX
&lt;/h4&gt;

&lt;p&gt;Next we are going to use a another function called &lt;strong&gt;CharIndex&lt;/strong&gt;. The &lt;strong&gt;CharIndex&lt;/strong&gt; function allows you to find the &lt;em&gt;position/index&lt;/em&gt; of a given character or a set of characters.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqwqk9kcdgh2bbukbxqlm.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqwqk9kcdgh2bbukbxqlm.gif" alt="skid-3"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;CharIndex&lt;/strong&gt; function takes two parameters or arguments, the &lt;u&gt;first argument&lt;/u&gt; is the character or set of characters you hope to find; this is written inbetween quotation marks, since it will most likely be a string. The &lt;u&gt;second argument&lt;/u&gt; is the &lt;strong&gt;column-name&lt;/strong&gt; where you want to look for this value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CharIndex(firstArg, secondArg)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbdpva4fnapab6xc9ig4c.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbdpva4fnapab6xc9ig4c.gif" alt="skid-1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Remember, to enclose the value of the first argument in single quotation marks.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To make it easier to identify we give the &lt;strong&gt;CharIndex&lt;/strong&gt; query an &lt;strong&gt;Alias&lt;/strong&gt;. In this example we will call it &lt;code&gt;@ Position&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frnrqjguumy88kngqnkhj.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frnrqjguumy88kngqnkhj.gif" alt="find the position of a character"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There you have it, two new functions you can use to find the &lt;strong&gt;LEN&lt;/strong&gt;gth of a set of characters in a column and &lt;strong&gt;CharIndex&lt;/strong&gt; to find the &lt;u&gt;position&lt;/u&gt; or &lt;u&gt;index&lt;/u&gt; of a particular character or set of characters in a particular column.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fisa66rjq0sym1ekmirfz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fisa66rjq0sym1ekmirfz.png" alt="spread-1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;😯 Now you have more tools added to your toolbelt 😯, don't forget to play around with them and think of some scenario's where they might be useful.&lt;/p&gt;

&lt;p&gt;I hope you enjoyed this episode and get excited about learning SQL.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1n8ino79s1tme4x16l9h.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1n8ino79s1tme4x16l9h.gif" alt="yoga"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>sql</category>
      <category>database</category>
      <category>programming</category>
    </item>
    <item>
      <title>Learn SQL: Microsoft SQL Server - Episode 11: Extracting data from Strings</title>
      <dc:creator>Goran Kortjie</dc:creator>
      <pubDate>Fri, 12 Nov 2021 06:33:10 +0000</pubDate>
      <link>https://dev.to/ifierygod/learn-sql-microsoft-sql-server-episode-11-extracting-data-from-strings-3d89</link>
      <guid>https://dev.to/ifierygod/learn-sql-microsoft-sql-server-episode-11-extracting-data-from-strings-3d89</guid>
      <description>&lt;p&gt;&lt;strong&gt;In this episode we are going to discuss how to extract characters from a string value.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's say for instance, we wanted to pull data from the &lt;strong&gt;Person.Person&lt;/strong&gt; table. As we should know by now, this table contains all the information about all people in the database, &lt;u&gt;customers and employees&lt;/u&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lD2XTV8I--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gyl2xdfk6a3hn5irtu73.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lD2XTV8I--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gyl2xdfk6a3hn5irtu73.gif" alt="skid-1" width="600" height="75"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example, we will be selecting only the &lt;strong&gt;firstName&lt;/strong&gt; and the &lt;strong&gt;lastName&lt;/strong&gt; columns from the &lt;strong&gt;Person.Person&lt;/strong&gt; table. However, we do not want to pull the entire &lt;strong&gt;lastName&lt;/strong&gt; of person in the database, instead we just want to pull the &lt;em&gt;&lt;strong&gt;first character&lt;/strong&gt;&lt;/em&gt; from the &lt;strong&gt;lastName&lt;/strong&gt; value.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YGG132bp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yzjnsvq6m255dyodynlc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YGG132bp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yzjnsvq6m255dyodynlc.png" alt="skid-cover-1" width="800" height="100"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  LEFT
&lt;/h3&gt;

&lt;p&gt;SSMS provides a &lt;code&gt;built-in&lt;/code&gt; function called &lt;strong&gt;Left&lt;/strong&gt; and it takes two &lt;u&gt;arguments/parameters&lt;/u&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  First Value
&lt;/h4&gt;

&lt;p&gt;We need to provide it with the &lt;em&gt;string&lt;/em&gt; value we want to &lt;em&gt;extract&lt;/em&gt; the character from, in this case &lt;strong&gt;lastName&lt;/strong&gt;. &lt;/p&gt;

&lt;h4&gt;
  
  
  Second Value
&lt;/h4&gt;

&lt;p&gt;The &lt;em&gt;number&lt;/em&gt; of characters that we want to return starting from the &lt;strong&gt;Left&lt;/strong&gt;, in this case it is &lt;code&gt;1&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Left(lastName, 1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lD2XTV8I--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gyl2xdfk6a3hn5irtu73.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lD2XTV8I--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gyl2xdfk6a3hn5irtu73.gif" alt="skid-1" width="600" height="75"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can test this in SSMS, and to make it easier to understand we can give the &lt;strong&gt;Left&lt;/strong&gt; function an &lt;strong&gt;Alias&lt;/strong&gt; of &lt;code&gt;Last Initial&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PF0PWm-q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/03cv0bmmbi50kkw8gq8b.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PF0PWm-q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/03cv0bmmbi50kkw8gq8b.gif" alt="discuss-1" width="600" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We can change the number of characters that get returned by modifying the second argument.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Another way to extract those characters is by using a &lt;strong&gt;Substring&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SubString&lt;/strong&gt; is another &lt;em&gt;built-in&lt;/em&gt; function that takes three &lt;u&gt;arguments/values&lt;/u&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YGG132bp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yzjnsvq6m255dyodynlc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YGG132bp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yzjnsvq6m255dyodynlc.png" alt="skid-cover-1" width="800" height="100"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  SUBSTRING
&lt;/h3&gt;

&lt;h4&gt;
  
  
  First Value
&lt;/h4&gt;

&lt;p&gt;The &lt;em&gt;column&lt;/em&gt; that we want to use to extract the value from.&lt;/p&gt;

&lt;h3&gt;
  
  
  Second Value
&lt;/h3&gt;

&lt;p&gt;Here we need to provide the &lt;code&gt;position&lt;/code&gt; we want to start extracting the value at, eg choosing 2 would mean we want to start extracting from the second character, starting from the &lt;strong&gt;Left&lt;/strong&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Third Value
&lt;/h4&gt;

&lt;p&gt;The &lt;em&gt;number&lt;/em&gt; of characters we want to extract.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SUBSTRING(lastName, 1, 1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we can see, it works similar to &lt;strong&gt;LEFT&lt;/strong&gt;, we could also assign an &lt;strong&gt;Alias&lt;/strong&gt; to the &lt;strong&gt;SUBSTRING&lt;/strong&gt; function to make things easier to understand.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---7bmpNDk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/smq2myox6r9onotpcv6l.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---7bmpNDk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/smq2myox6r9onotpcv6l.gif" alt="discuss-3" width="600" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YGG132bp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yzjnsvq6m255dyodynlc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YGG132bp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yzjnsvq6m255dyodynlc.png" alt="skid-cover-1" width="800" height="100"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  RIGHT
&lt;/h3&gt;

&lt;p&gt;Similar to &lt;strong&gt;LEFT&lt;/strong&gt; their is another function called &lt;strong&gt;RIGHT&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Right&lt;/strong&gt; works like &lt;strong&gt;Left&lt;/strong&gt; in that it takes two arguments.&lt;/p&gt;

&lt;h4&gt;
  
  
  First Value
&lt;/h4&gt;

&lt;p&gt;The &lt;em&gt;string&lt;/em&gt; value we want to use to extract characters from.&lt;/p&gt;

&lt;h4&gt;
  
  
  Second Value
&lt;/h4&gt;

&lt;p&gt;The &lt;em&gt;number&lt;/em&gt; of characters we want to extract starting from the &lt;strong&gt;Right&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;RIGHT(lastname, 3)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will return the last three characters from the &lt;strong&gt;lastName&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OIGydULG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ed03zd3v9uq5f6qh9hjs.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OIGydULG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ed03zd3v9uq5f6qh9hjs.gif" alt="discuss-4" width="600" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This is how we extract values from a string column or just any string value.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GazFdmYU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s7u6h3c7516254hcdpg4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GazFdmYU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s7u6h3c7516254hcdpg4.png" alt="skid-cover-2" width="800" height="100"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Extracting values has really made me hungry...I hope you enjoyed this episode and put it into practice. Signing off...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--v94mCKK5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lvm2ew4gmx2gwtxzvxo5.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--v94mCKK5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lvm2ew4gmx2gwtxzvxo5.gif" alt="friday-sleepday" width="600" height="338"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>sql</category>
      <category>database</category>
      <category>programming</category>
    </item>
    <item>
      <title>Learn SQL: Microsoft SQL Server - Episode 10: Joining Multiple Columns and Strings</title>
      <dc:creator>Goran Kortjie</dc:creator>
      <pubDate>Thu, 11 Nov 2021 06:02:59 +0000</pubDate>
      <link>https://dev.to/ifierygod/learn-sql-microsoft-sql-server-episode-10-joining-multiple-columns-and-strings-216l</link>
      <guid>https://dev.to/ifierygod/learn-sql-microsoft-sql-server-episode-10-joining-multiple-columns-and-strings-216l</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xiLUfnWl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bhgyhg6y7rvsud0zs9bs.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xiLUfnWl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bhgyhg6y7rvsud0zs9bs.gif" alt="demon-bunny-greets" width="880" height="495"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sometimes we need to join the data from multiple columns or string values to get the desired result&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Without wasting anytime lets dive into SSMS and see it in practice.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--h9Wc8JhH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aeel8wwp1s6tlqzqqyov.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--h9Wc8JhH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aeel8wwp1s6tlqzqqyov.gif" alt="skid-3" width="879" height="73"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will be using the &lt;strong&gt;Person.Person&lt;/strong&gt; table. This time however we will be joining the &lt;strong&gt;firstName&lt;/strong&gt; and &lt;strong&gt;lastName&lt;/strong&gt; columns to make a new &lt;strong&gt;Full Name&lt;/strong&gt; column. &lt;/p&gt;

&lt;p&gt;To accomplish this we first need to write a &lt;strong&gt;SELECT&lt;/strong&gt; statement to select the &lt;strong&gt;firstName&lt;/strong&gt; and &lt;strong&gt;lastName&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--x2XWLb2T--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6uhpr2m3vpf0mz2q8t5s.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--x2XWLb2T--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6uhpr2m3vpf0mz2q8t5s.gif" alt="discuss-1" width="600" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here in our result set we have the &lt;strong&gt;firstName&lt;/strong&gt; and &lt;strong&gt;lastName&lt;/strong&gt; of all the people in our &lt;strong&gt;Person.Person&lt;/strong&gt; table. If we need to join the &lt;strong&gt;firstName&lt;/strong&gt; and &lt;strong&gt;lastName&lt;/strong&gt; to form a &lt;strong&gt;Full Name&lt;/strong&gt; we can do this via two different syntaxes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eVyyDUmv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qytlqle665p7oovu0y6f.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eVyyDUmv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qytlqle665p7oovu0y6f.gif" alt="skid-1" width="879" height="73"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Using the Plus sign +
&lt;/h3&gt;

&lt;p&gt;Quite literally we add a plus sign between the &lt;strong&gt;firstName&lt;/strong&gt; and &lt;strong&gt;lastName&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;firstName + lastName
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's see what result we get in SSMS.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--S6Ii9LnV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vm3aikcwjffov7d71nsd.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--S6Ii9LnV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vm3aikcwjffov7d71nsd.gif" alt="discuss-2" width="600" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Did you see?&lt;/code&gt; When we added the plus sign it joined the two string values, however it did not add a space in-between. Therefore we needed to add an empty string &lt;code&gt;' '&lt;/code&gt; by ourselves between the &lt;strong&gt;firstName&lt;/strong&gt; and &lt;strong&gt;lastName&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;firstName + ' ' + lastName
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--h9Wc8JhH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aeel8wwp1s6tlqzqqyov.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--h9Wc8JhH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aeel8wwp1s6tlqzqqyov.gif" alt="skid-3" width="879" height="73"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally we added an &lt;strong&gt;Alias&lt;/strong&gt; to make things more simple to understand.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You have to use the plus sign syntax if you are using older versions of SSMS. For newer versions after SSMS 2008, use the &lt;strong&gt;Concat&lt;/strong&gt; syntax. The plus syntax works in all versions thus far.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eVyyDUmv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qytlqle665p7oovu0y6f.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eVyyDUmv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qytlqle665p7oovu0y6f.gif" alt="skid-1" width="879" height="73"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Using the Concat function
&lt;/h3&gt;

&lt;p&gt;We can also use what is called the &lt;strong&gt;Concat&lt;/strong&gt; function, similar to other functions, we wrap our arguments inside the function. In this case we wrap the &lt;strong&gt;firstName&lt;/strong&gt;, the &lt;strong&gt;empty string&lt;/strong&gt;, and the &lt;strong&gt;lastName&lt;/strong&gt; inside our &lt;strong&gt;Concat&lt;/strong&gt; function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Concat(firstName, ' ', lastName)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--h9Wc8JhH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aeel8wwp1s6tlqzqqyov.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--h9Wc8JhH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aeel8wwp1s6tlqzqqyov.gif" alt="skid-3" width="879" height="73"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can add a many arguments or columns/strings inside the &lt;strong&gt;Concat&lt;/strong&gt; function as we please. However notice that all of them have to be separated by a &lt;code&gt;comma&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ubYiTM12--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gqg1p3z4cib8mzfjaiaa.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ubYiTM12--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gqg1p3z4cib8mzfjaiaa.gif" alt="discuss-3" width="600" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we run these two syntaxes side by side, we can see they produce the exact same result. Now we have two ways of combining data from columns and string values.&lt;/p&gt;

&lt;p&gt;Pretty cool stuff! I really enjoyed this one.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--b6xEKzeh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3oebvj1dym3j8lh0628s.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--b6xEKzeh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3oebvj1dym3j8lh0628s.gif" alt="skelly" width="600" height="338"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>sql</category>
      <category>database</category>
      <category>datascience</category>
    </item>
    <item>
      <title>Learn SQL: Microsoft SQL Server - Episode 9: Aggregation in Where Clause</title>
      <dc:creator>Goran Kortjie</dc:creator>
      <pubDate>Wed, 10 Nov 2021 18:48:00 +0000</pubDate>
      <link>https://dev.to/ifierygod/learn-sql-microsoft-sql-server-episode-9-aggregation-in-where-clause-25bb</link>
      <guid>https://dev.to/ifierygod/learn-sql-microsoft-sql-server-episode-9-aggregation-in-where-clause-25bb</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wr8_7tr4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9tele45r212abw91j336.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wr8_7tr4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9tele45r212abw91j336.png" alt="goran-says-hi" width="398" height="398"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Many times we might need to use aggregated values in our &lt;strong&gt;Where Clause&lt;/strong&gt;, as a way of limiting the result set of aggregated values. You would think it would be as easy as adding a &lt;strong&gt;Where Clause&lt;/strong&gt; in our query statement, however we will see in this episode, this is not the case.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gr3Bf7q_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ix1pr53eupqnfaio6gsb.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gr3Bf7q_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ix1pr53eupqnfaio6gsb.gif" alt="skid-12" width="879" height="73"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;By using the previous episode as an example, you can check that out on the link below to get up to speed. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It's a quick read so once you're done head on back here! &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IgeY80f2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0me8zjay4n4i68r464ie.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IgeY80f2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0me8zjay4n4i68r464ie.png" alt="skid-cover-4" width="880" height="73"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="/ifierygod" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--piBIAdwB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/practicaldev/image/fetch/s--_sKyIPjI--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/444037/92f14527-35d0-4c0e-9079-d9fae6ffa61b.jpeg" alt="ifierygod"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/ifierygod/learn-sql-microsoft-sql-server-episode-8-null-values-in-aggregation-22d2" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Learn SQL: Microsoft SQL Server - Episode 8: NULL values in Aggregation&lt;/h2&gt;
      &lt;h3&gt;Goran Kortjie ・ Nov 6 '21 ・ 3 min read&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#sql&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#database&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#beginners&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#challenge&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;
. 

&lt;p&gt;We might want to select all those customers whose order count is less than two times or more than two, three, four, five etc...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zJVC8ca6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r8281hpafx5y20o5ahz4.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zJVC8ca6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r8281hpafx5y20o5ahz4.gif" alt="skid-11" width="880" height="61"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;Count&lt;/strong&gt; is in this example the aggregated value. However a &lt;strong&gt;Where Clause&lt;/strong&gt; does not take in an aggregated value. In this episode we are going to discuss how to use aggregated values to select our records.&lt;/p&gt;

&lt;p&gt;Lets move into SMSS and see this in practice.&lt;/p&gt;

&lt;p&gt;In this example we will be using the &lt;strong&gt;Sales.SalesOrderHeader&lt;/strong&gt; table. As we know this table stores the information for every order the customer has placed. There is essentially one record for each sales order, then there are &lt;strong&gt;CustomerID's&lt;/strong&gt; that stores information about the customer who placed this order.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zJVC8ca6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r8281hpafx5y20o5ahz4.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zJVC8ca6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r8281hpafx5y20o5ahz4.gif" alt="skid-11" width="880" height="61"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What we are trying to do here is to find the &lt;strong&gt;CustomerID&lt;/strong&gt; &lt;em&gt;Where&lt;/em&gt; the sales count is more &lt;code&gt;than X / less than X&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;First we will start by using a &lt;strong&gt;SELECT&lt;/strong&gt; statement and select the &lt;strong&gt;CustomerID&lt;/strong&gt; and the &lt;strong&gt;SalesOrderID&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7UaFgAZO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4vcyz11kumb2zrzk4mkj.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7UaFgAZO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4vcyz11kumb2zrzk4mkj.gif" alt="discussion-1" width="600" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What we need now is the &lt;strong&gt;Count&lt;/strong&gt; of &lt;strong&gt;Sales Orders&lt;/strong&gt;. Therefore we wrap the &lt;strong&gt;salesOrderId&lt;/strong&gt; in the &lt;strong&gt;Count&lt;/strong&gt; function. Notice we are grouping by the &lt;strong&gt;Customer level&lt;/strong&gt;, meaning for each customer give us the count of their &lt;strong&gt;salesOrderId's&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--h1NI9FBZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kuug4oqwb2fnr1gwn4xm.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--h1NI9FBZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kuug4oqwb2fnr1gwn4xm.gif" alt="skid-7" width="879" height="73"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally we need to tell SQL where to group it by and in this case we group it by the &lt;strong&gt;CustomerID&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--k0KqS_C6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fxv72b9ai3791f4dulsi.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--k0KqS_C6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fxv72b9ai3791f4dulsi.gif" alt="discussion-2" width="600" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To make it easier to understand we could add an &lt;strong&gt;Alias&lt;/strong&gt; to the &lt;strong&gt;Count&lt;/strong&gt; function that wraps around &lt;strong&gt;salesOrderId&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--h1NI9FBZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kuug4oqwb2fnr1gwn4xm.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--h1NI9FBZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kuug4oqwb2fnr1gwn4xm.gif" alt="skid-7" width="879" height="73"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In order to only select the &lt;em&gt;customers&lt;/em&gt; whose &lt;em&gt;sales order&lt;/em&gt; &lt;em&gt;Count&lt;/em&gt; is less than two, in this scenario, normally we would use the &lt;strong&gt;Where clause&lt;/strong&gt;. However if we try this we will see SSMS throws an error.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sJvz4Qpo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8oid9kl767fuu10601z4.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sJvz4Qpo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8oid9kl767fuu10601z4.gif" alt="discussion-3" width="600" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Instead of &lt;strong&gt;Where&lt;/strong&gt;, we replace it with &lt;strong&gt;Having&lt;/strong&gt;. Also notice we cannot use an &lt;strong&gt;Alias&lt;/strong&gt; when we use &lt;strong&gt;Having&lt;/strong&gt;, we must use the complete &lt;strong&gt;Count&lt;/strong&gt; function query string containing the &lt;strong&gt;salesOrderId&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kd1QhKW0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tfx32rkh0ly1zixlmb9y.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kd1QhKW0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tfx32rkh0ly1zixlmb9y.gif" alt="discussion-4" width="600" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Voila! &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gr3Bf7q_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ix1pr53eupqnfaio6gsb.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gr3Bf7q_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ix1pr53eupqnfaio6gsb.gif" alt="skid-12" width="879" height="73"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we can see all the &lt;em&gt;customers&lt;/em&gt; whose &lt;em&gt;sales order&lt;/em&gt; &lt;em&gt;Count&lt;/em&gt; is less than two. We could even change the operator to less than equal to two, which in this case will return customers whose sale orders are two and less than two in the result set.&lt;/p&gt;

&lt;p&gt;Awesome right!🥳&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UIBWAxqG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jpchmuphmj1xueik4m90.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UIBWAxqG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jpchmuphmj1xueik4m90.png" alt="sql" width="880" height="495"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Keep in mind, for any of the &lt;em&gt;aggregated functions&lt;/em&gt;, such as &lt;strong&gt;MIN&lt;/strong&gt;, &lt;strong&gt;MAX&lt;/strong&gt;, &lt;strong&gt;AVG&lt;/strong&gt; or &lt;strong&gt;SUM&lt;/strong&gt;, you will have to use &lt;strong&gt;HAVING&lt;/strong&gt; if you want to restrict your result set, by those aggregated values.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IgeY80f2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0me8zjay4n4i68r464ie.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IgeY80f2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0me8zjay4n4i68r464ie.png" alt="skid-cover-4" width="880" height="73"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope you enjoyed this episode, happy learning...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---0aaf1vl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s61js9hqsv5datguo9hu.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---0aaf1vl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s61js9hqsv5datguo9hu.gif" alt="meditation" width="640" height="640"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>sql</category>
      <category>database</category>
      <category>datascience</category>
    </item>
    <item>
      <title>Learn SQL: Microsoft SQL Server - Episode 8: NULL values in Aggregation</title>
      <dc:creator>Goran Kortjie</dc:creator>
      <pubDate>Sat, 06 Nov 2021 13:40:19 +0000</pubDate>
      <link>https://dev.to/ifierygod/learn-sql-microsoft-sql-server-episode-8-null-values-in-aggregation-22d2</link>
      <guid>https://dev.to/ifierygod/learn-sql-microsoft-sql-server-episode-8-null-values-in-aggregation-22d2</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpe5novza5mehmopymkle.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpe5novza5mehmopymkle.gif" alt="welcome"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this episode we will be discussing the impact of &lt;strong&gt;NULL&lt;/strong&gt; values in &lt;em&gt;Aggregation&lt;/em&gt;. &lt;strong&gt;&lt;em&gt;Null&lt;/em&gt;&lt;/strong&gt; value means that a column does not contain any value. It is not an empty string or 0, it is the absence of any value.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwn4vsqbl4qgfghvvt8tz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwn4vsqbl4qgfghvvt8tz.png" alt="skid-1-cover"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  NULL
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Null&lt;/em&gt;&lt;/strong&gt;* values are excluded from calculations. Whenever we does aggregations such as &lt;strong&gt;SUM&lt;/strong&gt;, the &lt;strong&gt;NULL&lt;/strong&gt; values are not participants in these calculations.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1owno79rqpckmqyq96dr.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1owno79rqpckmqyq96dr.gif" alt="skid-4"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is &lt;strong&gt;not&lt;/strong&gt; an issue when calculations are being performed on &lt;strong&gt;MIN&lt;/strong&gt;, &lt;strong&gt;MAX&lt;/strong&gt; and &lt;strong&gt;SUM&lt;/strong&gt;, because when &lt;strong&gt;NULL&lt;/strong&gt; is ignored is does not alter the calculation in anyway since it is the equivalent of 0 in these specific calculations.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fydqd8odqgwdg1d0sf0y6.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fydqd8odqgwdg1d0sf0y6.gif" alt="diagram-1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is an issue when calculations are being performed on &lt;strong&gt;Count&lt;/strong&gt; and &lt;strong&gt;AVG&lt;/strong&gt;, &lt;u&gt;this is the reason for the episode&lt;/u&gt;; to demonstrate the impact &lt;strong&gt;NULL&lt;/strong&gt; values have in aggregations such as &lt;strong&gt;Count&lt;/strong&gt; and &lt;strong&gt;AVG&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let’s see this in practice in SSMS.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo8nvgz4n5o9nvoelyib7.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo8nvgz4n5o9nvoelyib7.gif" alt="skid-11"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I have created a simple table to better explain the impact of NULL values, since the size of most tables in AdventureWorks are large which in turn makes it difficult to immediately see the impact they have.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgzjhpkxdaijk8lfiof3h.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgzjhpkxdaijk8lfiof3h.gif" alt="practical-1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we check to see the count of the &lt;strong&gt;OrderSales&lt;/strong&gt; table, we will see it picks up all 4 of the values. But if we check to see the count of only the &lt;em&gt;order value&lt;/em&gt; column, then we are receive only 3 in total. This means that the &lt;strong&gt;Count&lt;/strong&gt; function only factored in &lt;strong&gt;NON-NULL&lt;/strong&gt; values.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo8nvgz4n5o9nvoelyib7.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo8nvgz4n5o9nvoelyib7.gif" alt="skid-11"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;MIN&lt;/strong&gt; will calculate the minimum value.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvfjlu50z3i781cdo42br.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvfjlu50z3i781cdo42br.gif" alt="practical-2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;MAX&lt;/strong&gt; will calculate the maximum value.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F34kppw6ik59mxq2rqr9e.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F34kppw6ik59mxq2rqr9e.gif" alt="practical-3"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SUM&lt;/strong&gt; will calculate the sum of all values.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fneqxy4pbxawmxuz50bof.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fneqxy4pbxawmxuz50bof.gif" alt="practical-4"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;However, &lt;strong&gt;AVG&lt;/strong&gt; will not work as presumed, instead of dividing by four rows it will not accept &lt;strong&gt;NULL&lt;/strong&gt; as valid and therefore not include it in its calculation. Unfortunately because of this we end up with invalid data.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F80hzag3tzytpdumpoukr.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F80hzag3tzytpdumpoukr.gif" alt="practical-5"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Average&lt;/em&gt;&lt;/strong&gt; is suppose to take the &lt;strong&gt;SUM&lt;/strong&gt; of all the values which is 60 and divide them by 4, which is the total amount of rows. We should receive 15. &lt;/p&gt;

&lt;p&gt;However, we receive back 20. Since the row with a &lt;strong&gt;NULL&lt;/strong&gt; value was not included in the calculation, therefore we only divided the total of 60 by 3.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1owno79rqpckmqyq96dr.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1owno79rqpckmqyq96dr.gif" alt="skid-4"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Keep this in mind when you are querying your database, especially when working with &lt;strong&gt;AVG&lt;/strong&gt;, since this could lead to inaccurate data. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This can be especially troublesome in a large dataset such as &lt;em&gt;AdventureWorks&lt;/em&gt;, this behaviour is not immediately apparent and can be easily missed or forgotten.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwn4vsqbl4qgfghvvt8tz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwn4vsqbl4qgfghvvt8tz.png" alt="skid-1-cover"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope this episode was helpful, I will try to keep this little bit of knowledge at the back of my mind whenever I am querying databases and I hope you do to. Till next time...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn73b5kzvhre8e0zfsaoe.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn73b5kzvhre8e0zfsaoe.gif" alt="meditation"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>sql</category>
      <category>database</category>
      <category>beginners</category>
      <category>challenge</category>
    </item>
    <item>
      <title>Learn SQL: Microsoft SQL Server - Episode 7: Aggregate data with SUM and AVG</title>
      <dc:creator>Goran Kortjie</dc:creator>
      <pubDate>Thu, 04 Nov 2021 20:54:11 +0000</pubDate>
      <link>https://dev.to/ifierygod/learn-sql-microsoft-sql-server-episode-7-aggregate-data-with-sum-and-avg-3md0</link>
      <guid>https://dev.to/ifierygod/learn-sql-microsoft-sql-server-episode-7-aggregate-data-with-sum-and-avg-3md0</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg0e3oadwtdm45cs2gfd6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg0e3oadwtdm45cs2gfd6.png" alt="wear-your-mask-people"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this episode we are going to explore two additional functions we can utilise to aggregate our data, namely &lt;strong&gt;SUM&lt;/strong&gt; and &lt;strong&gt;AVERAGE&lt;/strong&gt; &lt;em&gt;(AVG)&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkbhyvlec17czxyb3ytu7.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkbhyvlec17czxyb3ytu7.gif" alt="skid-8"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As the name suggests &lt;strong&gt;SUM&lt;/strong&gt; allows us to calculate the &lt;u&gt;sum&lt;/u&gt; of the value in a given column. &lt;strong&gt;AVERAGE&lt;/strong&gt; allows us to calculate the &lt;u&gt;average&lt;/u&gt; value of all the values in a specified column.&lt;/p&gt;

&lt;p&gt;Let's get practical and see how this works in SSMS.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi9qnc79j09mzjbdmzax1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi9qnc79j09mzjbdmzax1.png" alt="skid-2-cover"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Calculate SUM
&lt;/h3&gt;

&lt;p&gt;First off, let's open a new query window in the &lt;em&gt;AdventureWorks&lt;/em&gt; database. We will be using the &lt;strong&gt;Sales.SalesOrderHeader&lt;/strong&gt; table. we will be using &lt;strong&gt;SUM&lt;/strong&gt; to calculate the &lt;u&gt;sum&lt;/u&gt; of the &lt;strong&gt;Subtotal&lt;/strong&gt; column, which is the total order values.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F670t2n6cw91iz29ysvdn.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F670t2n6cw91iz29ysvdn.gif" alt="practical-1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here we have the &lt;strong&gt;SUM&lt;/strong&gt; of all the orders in the &lt;strong&gt;Sales.SalesOrderHeader&lt;/strong&gt; table.&lt;/p&gt;

&lt;p&gt;If for instance we need to calculate the &lt;strong&gt;SUM&lt;/strong&gt; of the totals by the &lt;em&gt;CustomerID&lt;/em&gt; or &lt;em&gt;SalesPersonID&lt;/em&gt;, then we just need to specify this in our query statement. In our example we will use the &lt;strong&gt;CustomerID&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1na8lzhuc9nxb6ysehfa.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1na8lzhuc9nxb6ysehfa.gif" alt="skid-5"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We use the &lt;strong&gt;Group By&lt;/strong&gt; function to tell SQL by which column we want to calculate the total by, let’s provide an &lt;strong&gt;Alias&lt;/strong&gt; for the &lt;strong&gt;SUM&lt;/strong&gt; function and call it &lt;u&gt;Total Order Value&lt;/u&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj5exkt065agew1qfz0bp.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj5exkt065agew1qfz0bp.gif" alt="practical-2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Voila! we now see for each customer their total order value.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi9qnc79j09mzjbdmzax1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi9qnc79j09mzjbdmzax1.png" alt="skid-2-cover"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Calculate AVG
&lt;/h3&gt;

&lt;p&gt;Next we will look at the &lt;strong&gt;AVERAGE&lt;/strong&gt; function, which works similar to &lt;strong&gt;SUM&lt;/strong&gt;. There we will be copying the &lt;strong&gt;SUM&lt;/strong&gt; query statement and pasting it below itself. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkbhyvlec17czxyb3ytu7.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkbhyvlec17czxyb3ytu7.gif" alt="skid-8"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The only changes we will make is instead of &lt;strong&gt;SUM&lt;/strong&gt; we replace it with &lt;strong&gt;AVG&lt;/strong&gt;, this is the Average. We will also change the &lt;strong&gt;Alias&lt;/strong&gt; to &lt;u&gt;Average Order Value&lt;/u&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9axmxckz2rqdnv3akfne.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9axmxckz2rqdnv3akfne.gif" alt="practical-3"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Awesome, we can read the Average order value for each customer. We can even combine these two statements into one.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F84vesy6yhswsgznl9z27.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F84vesy6yhswsgznl9z27.gif" alt="practical-4"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here we have the &lt;em&gt;CustomerID&lt;/em&gt;, the &lt;em&gt;Total Order Value&lt;/em&gt; of each customer as well as their &lt;em&gt;Average Order Value&lt;/em&gt;. We can confirm these numbers by supplying a &lt;strong&gt;Count&lt;/strong&gt; function in our query statement.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1na8lzhuc9nxb6ysehfa.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1na8lzhuc9nxb6ysehfa.gif" alt="skid-5"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s see an example of this in SSMS.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm947eg6lt09lluhzn46j.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm947eg6lt09lluhzn46j.gif" alt="practical-5"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;As we can see the numbers add up correctly!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi9qnc79j09mzjbdmzax1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi9qnc79j09mzjbdmzax1.png" alt="skid-2-cover"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thats all she wrote, this is how we can calculate the &lt;strong&gt;SUM&lt;/strong&gt; and &lt;strong&gt;Average&lt;/strong&gt;. Remember to keep practicing and to eat your vegetables. Signing off for now, see you next time...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmujtb8rb496e3uu1nqsc.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmujtb8rb496e3uu1nqsc.gif" alt="medidation"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>sql</category>
      <category>database</category>
      <category>beginners</category>
      <category>challenge</category>
    </item>
    <item>
      <title>Learn SQL: Microsoft SQL Server - Episode 6: Alias and Aggregate Data</title>
      <dc:creator>Goran Kortjie</dc:creator>
      <pubDate>Sat, 30 Oct 2021 21:57:27 +0000</pubDate>
      <link>https://dev.to/ifierygod/learn-sql-microsoft-sql-server-episode-6-alias-and-aggregate-data-30af</link>
      <guid>https://dev.to/ifierygod/learn-sql-microsoft-sql-server-episode-6-alias-and-aggregate-data-30af</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XvDlHToe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c54n9myxbfuwixwwln1f.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XvDlHToe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c54n9myxbfuwixwwln1f.gif" alt="goran-greets-everyone" width="600" height="333"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this episode we will be discussing how to use &lt;strong&gt;Aliases&lt;/strong&gt; in SQL queries and how to &lt;u&gt;aggregate&lt;/u&gt; our data.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Use Alias
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Alias&lt;/th&gt;
&lt;th&gt;Alias&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;less code&lt;/td&gt;
&lt;td&gt;readability&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;When we use &lt;strong&gt;Aliases&lt;/strong&gt; our SQL becomes more readable and easy to understand, plus we will also have better column names.&lt;/p&gt;

&lt;p&gt;We can see what &lt;strong&gt;Aliases&lt;/strong&gt; are in SSMS.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--T_kL54le--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/drn6l5gq6jk1mzhhyquq.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--T_kL54le--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/drn6l5gq6jk1mzhhyquq.gif" alt="Skid-17" width="879" height="73"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this instance, we want to pull all the data with a &lt;strong&gt;BusinessEntityID&lt;/strong&gt; greater than 2000, we want to &lt;strong&gt;Order BY&lt;/strong&gt;, &lt;strong&gt;BusinessEntityID&lt;/strong&gt; in &lt;em&gt;ascending&lt;/em&gt; order and we want to see the &lt;strong&gt;BusinessEntityID&lt;/strong&gt; in our result set, meaning we should include it in our &lt;strong&gt;SELECT&lt;/strong&gt; query.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KkeC3UxK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kbc09owljgv2roj7h0ya.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KkeC3UxK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kbc09owljgv2roj7h0ya.gif" alt="cumbersome-query-statement" width="600" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice how cumbersome it is, we need to specify the table we are referring to in each query statement. This is where &lt;em&gt;aliases&lt;/em&gt; can help. Also if we want to give more meaningful names to any of the columns then &lt;em&gt;aliases&lt;/em&gt; can also be useful.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DfS-v4g2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vb9swx9ingpxwfyt5z4a.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DfS-v4g2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vb9swx9ingpxwfyt5z4a.gif" alt="skid-14" width="879" height="73"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can give &lt;em&gt;aliases&lt;/em&gt; to our columns and tables. We will start by giving &lt;em&gt;aliases&lt;/em&gt; to all the tables we have used so far. For example instead of referring to &lt;strong&gt;Person.Person&lt;/strong&gt; we can create an &lt;em&gt;&lt;strong&gt;alias&lt;/strong&gt;&lt;/em&gt; for it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--I1X0SVhW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fp6txox24trcw1xeju1x.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--I1X0SVhW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fp6txox24trcw1xeju1x.gif" alt="alias-in-query" width="600" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We use the &lt;strong&gt;AS&lt;/strong&gt; keyword to create an &lt;em&gt;alias&lt;/em&gt;, we can name it whatever we want but in most cases we would simplify it to make it easier to read and understand.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TPL21WzK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l4mg247m95qh40c565tz.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TPL21WzK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l4mg247m95qh40c565tz.gif" alt="Skid-11" width="879" height="73"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice how when we created an &lt;em&gt;alias&lt;/em&gt; for our tables, SSMS no longer accepted the original table name and it threw an error in the form of the red underline. We had to change them to the &lt;em&gt;alias&lt;/em&gt; name we gave our tables.&lt;/p&gt;

&lt;p&gt;We can also create an &lt;em&gt;alias&lt;/em&gt; for our columns in the same way we created an &lt;em&gt;alias&lt;/em&gt; for our tables.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7oOnwpqc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vawumeshivdg7xykls4y.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7oOnwpqc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vawumeshivdg7xykls4y.gif" alt="alias-columns" width="600" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice in our result set, when we gave the &lt;strong&gt;firstName&lt;/strong&gt; the &lt;em&gt;alias&lt;/em&gt; of &lt;strong&gt;Fname&lt;/strong&gt; it showed up in the result set. We can give our columns more meaningful names. Lets say for instance, we want to give our &lt;strong&gt;firstName&lt;/strong&gt; column the &lt;em&gt;alias&lt;/em&gt; of &lt;em&gt;&lt;strong&gt;first name&lt;/strong&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DfS-v4g2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vb9swx9ingpxwfyt5z4a.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DfS-v4g2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vb9swx9ingpxwfyt5z4a.gif" alt="skid-14" width="879" height="73"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To do this we need to enclose the &lt;em&gt;alias&lt;/em&gt; name in brackets or we need to enclose it inside double quotes, since it has a space in-between and SQL does not like that.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tD8KTQQy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/alfxuakogbu1uemyar5j.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tD8KTQQy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/alfxuakogbu1uemyar5j.gif" alt="column-as-alias" width="600" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FHCDEkQL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/15pa2aop1q7kquktxjqq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FHCDEkQL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/15pa2aop1q7kquktxjqq.png" alt="skid-4" width="880" height="73"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Aggregate Data
&lt;/h3&gt;

&lt;p&gt;We have several functions available that we can use to &lt;em&gt;aggregate&lt;/em&gt; data from our database.&lt;/p&gt;

&lt;h4&gt;
  
  
  COUNT
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Count&lt;/strong&gt; allows us to &lt;em&gt;count&lt;/em&gt; the number of records that will be returned based on our query statement.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ylB872HZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a0nh3vva0vwzqt0ancq2.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ylB872HZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a0nh3vva0vwzqt0ancq2.gif" alt="count-function" width="600" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can even use this function with columns to determine how many &lt;u&gt;non NULL&lt;/u&gt; values exist in the database. Meaning &lt;em&gt;that&lt;/em&gt; column we query should have a value.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6Jqgofu4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jib5fc5tg6am2odv84a4.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6Jqgofu4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jib5fc5tg6am2odv84a4.gif" alt="count-function-1" width="600" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ulPq-Vie--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aydtgvkgoxecv3pfmsib.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ulPq-Vie--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aydtgvkgoxecv3pfmsib.png" alt="skid-3" width="880" height="73"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  MIN
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Min&lt;/strong&gt; allows us to return the minimum value for the specified column for that table.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Min&lt;/strong&gt; can be used in any type of column, but most of the time it is used for &lt;u&gt;numbers and dates&lt;/u&gt;.&lt;/p&gt;

&lt;p&gt;To demonstrate &lt;strong&gt;Min&lt;/strong&gt; we will use the &lt;strong&gt;Sales.SalesOrderHeader&lt;/strong&gt; table and pull data from the &lt;strong&gt;subTotal&lt;/strong&gt; column.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--T_kL54le--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/drn6l5gq6jk1mzhhyquq.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--T_kL54le--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/drn6l5gq6jk1mzhhyquq.gif" alt="Skid-17" width="879" height="73"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--k269oVBm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/670jjkkc63vj4ekxod2y.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--k269oVBm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/670jjkkc63vj4ekxod2y.gif" alt="min-query" width="600" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FHCDEkQL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/15pa2aop1q7kquktxjqq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FHCDEkQL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/15pa2aop1q7kquktxjqq.png" alt="skid-4" width="880" height="73"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Max
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Max&lt;/strong&gt; allows us to return the maximum value for the specified column for that table.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Max&lt;/strong&gt; can be used in any type of column, but most of the time it is used for &lt;u&gt;numbers and dates&lt;/u&gt;.&lt;/p&gt;

&lt;p&gt;We can use the &lt;strong&gt;Max&lt;/strong&gt; function in the same way as the &lt;strong&gt;Min&lt;/strong&gt; function, We can also use a &lt;strong&gt;Where&lt;/strong&gt; clause to limit the return data by a particular date, ID or customer or anything we want to use. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--T_kL54le--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/drn6l5gq6jk1mzhhyquq.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--T_kL54le--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/drn6l5gq6jk1mzhhyquq.gif" alt="Skid-17" width="879" height="73"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Q-MHEqyK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uc1qnbtzmmi10fk3wzyi.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Q-MHEqyK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uc1qnbtzmmi10fk3wzyi.gif" alt="max-where-clause" width="600" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ulPq-Vie--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aydtgvkgoxecv3pfmsib.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ulPq-Vie--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aydtgvkgoxecv3pfmsib.png" alt="skid-3" width="880" height="73"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Group BY
&lt;/h4&gt;

&lt;p&gt;What if we had to look at &lt;strong&gt;Count&lt;/strong&gt; of records for a particular column, lets say we want to see how many orders have each customer placed, or the &lt;strong&gt;Min&lt;/strong&gt; or &lt;strong&gt;Max&lt;/strong&gt; value of orders placed by each customer.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Group By&lt;/strong&gt; can help us to accomplish this task.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--T_kL54le--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/drn6l5gq6jk1mzhhyquq.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--T_kL54le--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/drn6l5gq6jk1mzhhyquq.gif" alt="Skid-17" width="879" height="73"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We start off with a &lt;strong&gt;SELECT&lt;/strong&gt; statement and use the &lt;u&gt;COUNT(&lt;em&gt;)&lt;/em&gt;&lt;/u&gt; function on the **Sales.SalesOrderHeader*. This will tell us the amount of orders in this table.&lt;/p&gt;

&lt;p&gt;We now want to see the &lt;u&gt;order count&lt;/u&gt; by each customer, naturally we would add the &lt;strong&gt;CustomerID&lt;/strong&gt; either before or after the &lt;strong&gt;Count(*)&lt;/strong&gt; function in the &lt;strong&gt;Select&lt;/strong&gt; statement. If we hit execute we get an error, the reason is we need to specify at what level are we counting the records at.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gQNnwvYQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wdcir1a4xpr2klpm27e3.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gQNnwvYQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wdcir1a4xpr2klpm27e3.gif" alt="group-by-query-statement" width="600" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since we have &lt;strong&gt;CustomerID&lt;/strong&gt; in our statement, we are trying to do the &lt;em&gt;count&lt;/em&gt; at the &lt;strong&gt;CustomerID&lt;/strong&gt; level. However we need to explicitly tell SSMS this. We do this using &lt;strong&gt;Group BY&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DfS-v4g2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vb9swx9ingpxwfyt5z4a.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DfS-v4g2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vb9swx9ingpxwfyt5z4a.gif" alt="skid-14" width="879" height="73"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Aggregated&lt;/strong&gt; columns will show no column names in all our SQL statements. This is where an &lt;em&gt;&lt;strong&gt;Alias&lt;/strong&gt;&lt;/em&gt; is useful, by allowing us to give our &lt;em&gt;aggregated&lt;/em&gt; data meaningful names.&lt;/p&gt;

&lt;p&gt;We can even sort this list. Let's order the list by the &lt;em&gt;alias&lt;/em&gt; in &lt;em&gt;ascending&lt;/em&gt; and then in &lt;em&gt;descending&lt;/em&gt; order.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yQFqb2zr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k2ba3yut67vnt79h2v3e.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yQFqb2zr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k2ba3yut67vnt79h2v3e.gif" alt="order-by-group-by" width="600" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can now see which customers have how many orders, we can do the same query with &lt;strong&gt;MIN&lt;/strong&gt; and &lt;strong&gt;MAX&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TPL21WzK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l4mg247m95qh40c565tz.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TPL21WzK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l4mg247m95qh40c565tz.gif" alt="Skid-11" width="879" height="73"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will start with &lt;strong&gt;MIN&lt;/strong&gt; function to calculate the minimum &lt;strong&gt;SubTotal&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EqIgI6n2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ke4fwvqwdxjhsqbl1e64.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EqIgI6n2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ke4fwvqwdxjhsqbl1e64.gif" alt="order-by-group-by-min" width="600" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can use the &lt;strong&gt;MAX&lt;/strong&gt; function in the same statement. We use the &lt;strong&gt;Order By&lt;/strong&gt; function to decide how we want to sort the result set, either by &lt;u&gt;Min Order Value&lt;/u&gt; or &lt;u&gt;Max Order Value&lt;/u&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ba3kVex4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9i0gdwoe2vc3bpafyi23.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ba3kVex4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9i0gdwoe2vc3bpafyi23.gif" alt="order-by-group-by-max-and-min" width="600" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can even chain this further by adding other &lt;em&gt;aggregated&lt;/em&gt; values such as &lt;strong&gt;Count&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--h3gG5lmS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3svy508jvru3b47nel6c.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--h3gG5lmS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3svy508jvru3b47nel6c.gif" alt="group-by-order-by-min-max-count" width="600" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thus far we have looked at &lt;em&gt;aggregating&lt;/em&gt; by one column, However in any real case scenarios, we might need one or more columns to &lt;em&gt;aggregate&lt;/em&gt; the data at. Let's say we want to find out the &lt;u&gt;total order count&lt;/u&gt; by each sales person in each territory.&lt;/p&gt;

&lt;p&gt;This means we are &lt;em&gt;aggregating&lt;/em&gt; at the &lt;u&gt;sales person&lt;/u&gt; level and at the &lt;u&gt;territory&lt;/u&gt; level. To do this we need to create a new query.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TPL21WzK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l4mg247m95qh40c565tz.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TPL21WzK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l4mg247m95qh40c565tz.gif" alt="Skid-11" width="879" height="73"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will still be working in the &lt;strong&gt;Sales.SalesOrderHeader&lt;/strong&gt; table, if we have a peek at the columns within we will notice the &lt;strong&gt;SalesPersonID&lt;/strong&gt; and &lt;strong&gt;TerritoryID&lt;/strong&gt; columns. We will be using these columns in our query statement.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bvMjhsOJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lncs6v5k9tlvpc6lm0qk.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bvMjhsOJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lncs6v5k9tlvpc6lm0qk.gif" alt="total-orders-by-each-salesperson" width="600" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can also order the result set of this query statement by the &lt;strong&gt;SalesPersonID&lt;/strong&gt;. From the result set we can see the breakdown of each sales persons sales in each territory.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lCbDWCQD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xbwg40nyxysh4dk25s02.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lCbDWCQD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xbwg40nyxysh4dk25s02.gif" alt="skid-1" width="879" height="73"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;How cool was that, we were able to do some awesome things with just the basics of SQL to pull data from our database that is insightful and easy to understand.&lt;/p&gt;

&lt;p&gt;I really hope you enjoyed it, stay zen folks!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jDjEp6kw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5cbf38324eligaprddys.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jDjEp6kw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5cbf38324eligaprddys.gif" alt="Image description" width="640" height="640"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>sql</category>
      <category>database</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Learn SQL: Microsoft SQL Server - Episode 5: Joining Tables</title>
      <dc:creator>Goran Kortjie</dc:creator>
      <pubDate>Fri, 29 Oct 2021 03:46:37 +0000</pubDate>
      <link>https://dev.to/ifierygod/learn-sql-microsoft-sql-server-episode-5-joining-tables-32fd</link>
      <guid>https://dev.to/ifierygod/learn-sql-microsoft-sql-server-episode-5-joining-tables-32fd</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2Jf4ou6x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rqs1t876uy30hbty6nbt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2Jf4ou6x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rqs1t876uy30hbty6nbt.png" alt="goran-and-cat-greets" width="880" height="489"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Joining Multiple Tables
&lt;/h3&gt;

&lt;p&gt;Thus far we have discussed how to pull data from a single table. However in most cases, we will need to pull the data from multiple tables in order to get the bigger picture for our analysis or reporting.&lt;/p&gt;

&lt;p&gt;Before we can join tables in a SQL query, we need to be familiar with our database structure, which tables there are, what kind of columns are there, and how the tables connect to each other.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0xJBUtqc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zuhccgyk7ddmkgjcsib9.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0xJBUtqc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zuhccgyk7ddmkgjcsib9.gif" alt="skid-14" width="879" height="73"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the AdventureWorks, the &lt;strong&gt;Person&lt;/strong&gt; schema contains a-lot of information about a person, the people who are in the database, the person table contains columns that hold different pieces of information of a person. &lt;/p&gt;

&lt;p&gt;It holds the &lt;strong&gt;Primary Key&lt;/strong&gt; (PK), which is &lt;strong&gt;BusinessEntityID&lt;/strong&gt;. This means that &lt;strong&gt;BusinessEntityID&lt;/strong&gt; is a &lt;u&gt;unique record&lt;/u&gt; that identifies each record. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;BusinessEntityID&lt;/strong&gt; is also the &lt;strong&gt;Foreign Key&lt;/strong&gt; (FK) for this table. This means it is used to connect to other related tables.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--niXrbAnF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ju6060muhkka8iya3j9g.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--niXrbAnF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ju6060muhkka8iya3j9g.gif" alt="sql-select-query" width="600" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There is also a table called &lt;strong&gt;Person.EmailAddress&lt;/strong&gt;, which contains information on each persons &lt;em&gt;email address&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FJKPXAKM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0pocki1vzwvoyfb1g4wj.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FJKPXAKM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0pocki1vzwvoyfb1g4wj.gif" alt="skid-15" width="879" height="73"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The table has a the &lt;strong&gt;Primary Key&lt;/strong&gt; of &lt;strong&gt;BusinessEntityID&lt;/strong&gt; and each &lt;em&gt;email address&lt;/em&gt; has a &lt;strong&gt;Primary Key&lt;/strong&gt; of &lt;strong&gt;EmailAddressID&lt;/strong&gt;, the &lt;strong&gt;EmailAddressID&lt;/strong&gt; allows for a person to have multiple email addresses.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WvS_ejQF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pq9yo667pdjfam6e29pq.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WvS_ejQF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pq9yo667pdjfam6e29pq.gif" alt="sql-select-email-address" width="600" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In order for us to pull the &lt;strong&gt;firstName&lt;/strong&gt;, &lt;strong&gt;lastName&lt;/strong&gt; and &lt;strong&gt;emailAddress&lt;/strong&gt; for a person, we need to join these two tables. A way to join them is to take a key that is common between both of them, which in this case is &lt;strong&gt;BusinessEntityID&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0xJBUtqc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zuhccgyk7ddmkgjcsib9.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0xJBUtqc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zuhccgyk7ddmkgjcsib9.gif" alt="skid-14" width="879" height="73"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The syntax for joining tables is as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Select [Column Names | * ]
From [Table Name]
On [Table.primaryKey] = [Table.primaryKey]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--D5Tanx67--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gafhtkwz7iiq1chdjbed.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--D5Tanx67--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gafhtkwz7iiq1chdjbed.gif" alt="Joining-tables" width="600" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can limit the amount of columns return to us by specifying the columns we want to pull in the select statement.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--doYalvrx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dku8l2im8y4rrgxbkin3.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--doYalvrx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dku8l2im8y4rrgxbkin3.gif" alt="limit-query-and-error-checking" width="600" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice how when we try to pull the data from &lt;strong&gt;BusinessEntityID&lt;/strong&gt;, SQL throws an error, it will also indicate an error by underlining with a red line. The reason why is we an not being specific from which table we want to pull the &lt;strong&gt;BusinessEntityID&lt;/strong&gt;, since it exist in both tables.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FJKPXAKM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0pocki1vzwvoyfb1g4wj.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FJKPXAKM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0pocki1vzwvoyfb1g4wj.gif" alt="skid-15" width="879" height="73"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we specify we want to pull from the &lt;strong&gt;person.BusinessEntityID&lt;/strong&gt; is works perfectly.&lt;/p&gt;

&lt;p&gt;Using this same syntax we can join multiple tables. Lets say for instance we need to join another table and it is the &lt;strong&gt;Person.PersonPhone&lt;/strong&gt; table.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4g6R117C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nxop7go1vcneu08kc5yn.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4g6R117C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nxop7go1vcneu08kc5yn.gif" alt="join-multiple-tables" width="600" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can even limit this further by specifying a &lt;strong&gt;Where&lt;/strong&gt; clause, lets say for instance we need to limit this query to where the &lt;em&gt;firstName&lt;/em&gt; is Kim, we can accomplish this as follows:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--y3WR_cCU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mlzfd5zxuseps5cjmfts.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--y3WR_cCU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mlzfd5zxuseps5cjmfts.gif" alt="adding-a-where-clause" width="600" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We are now able to find each person with the &lt;em&gt;first name&lt;/em&gt; as Kim, we can see their &lt;em&gt;last name&lt;/em&gt;, &lt;em&gt;email address&lt;/em&gt; and &lt;em&gt;phone number&lt;/em&gt; by joining multiple tables, that is pretty amazing!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--whlqc0KN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/avgigriowxz6i6241uvl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--whlqc0KN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/avgigriowxz6i6241uvl.png" alt="skid-1" width="880" height="73"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have come to the end of this discussion about joining tables, I really hope you enjoyed reading it, join me in the next episode...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kNN4pcPb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bkpp1xl6761axpfzu7y0.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kNN4pcPb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bkpp1xl6761axpfzu7y0.gif" alt="calming-mind" width="640" height="640"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>sql</category>
      <category>database</category>
      <category>beginners</category>
      <category>datascience</category>
    </item>
  </channel>
</rss>
