<?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: NijjohUnno</title>
    <description>The latest articles on DEV Community by NijjohUnno (@nijjohunno).</description>
    <link>https://dev.to/nijjohunno</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%2F1072021%2F18b71a25-1ea7-4242-ae74-86e1130c4be7.jpeg</url>
      <title>DEV Community: NijjohUnno</title>
      <link>https://dev.to/nijjohunno</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nijjohunno"/>
    <language>en</language>
    <item>
      <title>Demystifying SQL: An Introduction to SQL Using SQL Server</title>
      <dc:creator>NijjohUnno</dc:creator>
      <pubDate>Thu, 13 Jul 2023 09:50:45 +0000</pubDate>
      <link>https://dev.to/nijjohunno/demystifying-sql-an-introduction-to-sql-using-sql-server-5ak6</link>
      <guid>https://dev.to/nijjohunno/demystifying-sql-an-introduction-to-sql-using-sql-server-5ak6</guid>
      <description>&lt;h2&gt;
  
  
  Introduction to SQL using Microsoft SQL Server
&lt;/h2&gt;

&lt;p&gt;SQL stands for Structured Query Language. This is a native programming language used for managing and manipulating relational database. MS SQL server is a relational database management system developed by &lt;a href="https://www.microsoft.com/en-us/"&gt;Microsoft&lt;/a&gt;. You can download it from their &lt;a href="https://www.microsoft.com/en-us/sql-server/sql-server-downloads"&gt;website&lt;/a&gt;. We shall use The BikeStore Database downloaded from &lt;a href="http://www.sqlservertutorial.net/load-sample-database/"&gt;sqlservertutorial&lt;/a&gt;.&lt;br&gt;
We use a query, a set of command(s), to interact with the database to retrieve or manipulate the data in the database.&lt;/p&gt;

&lt;h3&gt;
  
  
  SQL Queries clauses
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1. SELECT
&lt;/h4&gt;

&lt;p&gt;We use this keyword before we define the column(s) we would like to retrieve from a table(s). To limit the query results we use the keyword &lt;strong&gt;TOP()&lt;/strong&gt; after the &lt;strong&gt;SELECT&lt;/strong&gt; keyword. &lt;strong&gt;FROM&lt;/strong&gt; keyword is used to define the table that we want to get our results from. &lt;strong&gt;DISTINCT&lt;/strong&gt; is used to retrieve only unique values. Retrieved column(s) can be aliased using the &lt;strong&gt;AS&lt;/strong&gt; keyword. Asterisks &lt;strong&gt;(*)&lt;/strong&gt; symbol is used to retrieve all columns from a table.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5iHkgpKw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ketf3bj2ydincyk5jue.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5iHkgpKw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ketf3bj2ydincyk5jue.png" alt="SQL Query" width="684" height="415"&gt;&lt;/a&gt;&lt;br&gt;
The above image shows an example of how the clauses can be used. The &lt;strong&gt;USE&lt;/strong&gt; keyword is used to define the database that we would like to work on. The query retrieves 5 brands from the brands table and aliases the results as brands.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zHrq0vMl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b2hgr7s16rvjoe8wybck.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zHrq0vMl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b2hgr7s16rvjoe8wybck.png" alt="SQL Query" width="800" height="208"&gt;&lt;/a&gt;&lt;br&gt;
The query above retrieves all records from the customers table with all the columns.&lt;/p&gt;

&lt;h4&gt;
  
  
  2. WHERE
&lt;/h4&gt;

&lt;p&gt;Filtering is retrieving results that meet specified condition(s). We use the &lt;strong&gt;WHERE&lt;/strong&gt; clause to achieve this in SQL. Once we have defined the column(s) and the table that we want to retrieve data from, we define the condition(s) that a column(s) should meet. The operators used in conjunction with WHERE clause are &lt;strong&gt;=, &amp;lt;&amp;gt;, &amp;gt; ,&amp;lt;, BETWEEN,LIKE, IS NULL, IS NOT NULL.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YttzsbOT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vmbbv2ubypsbfvnheo2l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YttzsbOT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vmbbv2ubypsbfvnheo2l.png" alt="SQL Query" width="800" height="200"&gt;&lt;/a&gt;&lt;br&gt;
The query above returns the record of the customer whose customer_id is 10. We use quotation marks(' ') when the data type of column that the criteria need to be met is a &lt;strong&gt;string&lt;/strong&gt; or &lt;strong&gt;date.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--V2wjE7ol--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oaml5wf1ow57xnqlschb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--V2wjE7ol--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oaml5wf1ow57xnqlschb.png" alt="SQL Query" width="800" height="240"&gt;&lt;/a&gt;&lt;br&gt;
The query returns all the customers from NY state.&lt;br&gt;
We use the key words &lt;strong&gt;AND, OR&lt;/strong&gt; when we have multiple conditions to be met.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KoIfagbd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ro3xr5fr7we8idj4bz7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KoIfagbd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ro3xr5fr7we8idj4bz7.png" alt="SQL Query" width="605" height="328"&gt;&lt;/a&gt;&lt;br&gt;
The query retrieves the email of staff that fall under &lt;strong&gt;store id 3&lt;/strong&gt; and the ID of their manager is &lt;strong&gt;7&lt;/strong&gt;. When &lt;strong&gt;AND&lt;/strong&gt; is used, the results must meet all the defined conditions in the &lt;strong&gt;WHERE&lt;/strong&gt; clause while at least one condition must be met when &lt;strong&gt;OR&lt;/strong&gt; is used. &lt;/p&gt;

&lt;h4&gt;
  
  
  Operators
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;   Equal to: "&lt;strong&gt;=&lt;/strong&gt;"&lt;/li&gt;
&lt;li&gt;   Not equal to: "&lt;strong&gt;&amp;lt;&amp;gt;&lt;/strong&gt;", "&lt;strong&gt;!=&lt;/strong&gt;"&lt;/li&gt;
&lt;li&gt;   Greater than: "&lt;strong&gt;&amp;gt;&lt;/strong&gt;"&lt;/li&gt;
&lt;li&gt;   Less than: "&lt;strong&gt;&amp;lt;&lt;/strong&gt;"&lt;/li&gt;
&lt;li&gt;   Greater than or equal to: "&lt;strong&gt;&amp;gt;=&lt;/strong&gt;"&lt;/li&gt;
&lt;li&gt;   Less than or equal to: "&lt;strong&gt;&amp;lt;=&lt;/strong&gt;"&lt;/li&gt;
&lt;li&gt;   &lt;strong&gt;NOT&lt;/strong&gt;: Negates a condition&lt;/li&gt;
&lt;li&gt;   &lt;strong&gt;IN&lt;/strong&gt;: Checks if a value matches any value in a list.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XO-itzrb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rgk5m2qijs4z435a9x8j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XO-itzrb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rgk5m2qijs4z435a9x8j.png" alt="SQL Query" width="800" height="282"&gt;&lt;/a&gt;&lt;br&gt;
The above query retrieves customers from the following states: NY, CA, TX.&lt;br&gt;
&lt;strong&gt;LIKE&lt;/strong&gt;: Performs pattern matching against character data using wildcard characters:&lt;br&gt;
   "&lt;strong&gt;%&lt;/strong&gt;" represents zero or more characters.&lt;br&gt;
   "&lt;strong&gt;_&lt;/strong&gt;" represents a single character.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KwoZy6Xu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7fwz3tl3n7glmyox6stb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KwoZy6Xu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7fwz3tl3n7glmyox6stb.png" alt="SQL Query" width="800" height="267"&gt;&lt;/a&gt;&lt;br&gt;
The query above retrieves customers with first_name starting with "&lt;strong&gt;De&lt;/strong&gt;".&lt;br&gt;
&lt;strong&gt;BETWEEN&lt;/strong&gt;: Checks if a value is within a specified range (inclusive).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZW0IXQTz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xprrabmru1wh457ilhfm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZW0IXQTz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xprrabmru1wh457ilhfm.png" alt="SQL Query" width="800" height="470"&gt;&lt;/a&gt;&lt;br&gt;
The query above retrieves orders with order_date between &lt;strong&gt;2016-01-01&lt;/strong&gt; and &lt;strong&gt;2016-01-07&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;IS NULL / IS NOT NULL&lt;/strong&gt;: Checks for the presence or absence of NULL values in a column.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3ubc4EC2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ten3vao9zvmu3khpaw8t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3ubc4EC2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ten3vao9zvmu3khpaw8t.png" alt="SQL Query" width="800" height="298"&gt;&lt;/a&gt;&lt;br&gt;
The query above retrieves customers who do not have a phone number.&lt;/p&gt;

&lt;h4&gt;
  
  
  3. GROUP BY
&lt;/h4&gt;

&lt;p&gt;This clause is used to group rows based on a column(s). You can summarize data using this clause. It is commonly used alongside aggregate functions (SUM, COUNT, MIN, MAX, AVERAGE).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--B2nIFkD6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ez7zz6bwfkk6ok38ivcz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--B2nIFkD6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ez7zz6bwfkk6ok38ivcz.png" alt="SQL Query" width="690" height="395"&gt;&lt;/a&gt;&lt;br&gt;
The query above retrieves the total number of customers in each state.&lt;/p&gt;

&lt;h4&gt;
  
  
  4. HAVING
&lt;/h4&gt;

&lt;p&gt;This clause is used in conjunction with the &lt;strong&gt;GROUP BY&lt;/strong&gt; clause to filter grouped rows based on specific conditions. You can use comparison operators (&lt;strong&gt;=, &amp;lt;, &amp;gt;&lt;/strong&gt;), logical operators (&lt;strong&gt;AND, OR&lt;/strong&gt;), and aggregate functions within the &lt;strong&gt;HAVING&lt;/strong&gt; clause.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--goPn-j2_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xzfk1iks5da0jf1yue4k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--goPn-j2_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xzfk1iks5da0jf1yue4k.png" alt="SQL Query" width="691" height="334"&gt;&lt;/a&gt;&lt;br&gt;
The query above retrieves any state that has more than 500 customers.&lt;/p&gt;

&lt;h4&gt;
  
  
  5. ORDER BY
&lt;/h4&gt;

&lt;p&gt;Sorting is arranging the retrieved results in either ascending or descending order. SQL uses &lt;strong&gt;ORDER BY&lt;/strong&gt; to sort results. The column(s) are defined after the &lt;strong&gt;ORDER BY&lt;/strong&gt;. If you are sorting your column in descending order, you add the keyword &lt;strong&gt;DESC&lt;/strong&gt; after the column(s) name(s). Otherwise you leave it that way for ascending order.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--B1FgbrHk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mjq6wm3lj8g1mwccu0b6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--B1FgbrHk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mjq6wm3lj8g1mwccu0b6.png" alt="SQL Query" width="800" height="454"&gt;&lt;/a&gt;&lt;br&gt;
The query above retrieves all staff ordered by their first_name in descending order.&lt;/p&gt;

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