<?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: Keith Kinuthia</title>
    <description>The latest articles on DEV Community by Keith Kinuthia (@keeeeithhhhh).</description>
    <link>https://dev.to/keeeeithhhhh</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2141759%2F95f8c51d-2119-4be5-b72e-30457ec0a4f2.png</url>
      <title>DEV Community: Keith Kinuthia</title>
      <link>https://dev.to/keeeeithhhhh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/keeeeithhhhh"/>
    <language>en</language>
    <item>
      <title>SQL 101: Introduction to SQL</title>
      <dc:creator>Keith Kinuthia</dc:creator>
      <pubDate>Sun, 29 Sep 2024 13:25:46 +0000</pubDate>
      <link>https://dev.to/keeeeithhhhh/sql-101-introduction-to-sql-1b71</link>
      <guid>https://dev.to/keeeeithhhhh/sql-101-introduction-to-sql-1b71</guid>
      <description>&lt;p&gt;Before understanding what &lt;strong&gt;SQL&lt;/strong&gt; is, we must first understand the concept of &lt;strong&gt;data&lt;/strong&gt; and &lt;strong&gt;data storage&lt;/strong&gt;.&lt;br&gt;&lt;br&gt;
In today's world, data is everywhere—from the emails we send to the transactions we make, everything generates data. &lt;strong&gt;Data&lt;/strong&gt; is simply a collection of facts or information, and it can take many forms: text, numbers, images, videos, or even signals.&lt;br&gt;&lt;br&gt;
To make sense of this vast amount of information, data needs to be organized and stored in a structured way. This is where &lt;strong&gt;databases&lt;/strong&gt; come in.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Understanding Databases&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;database&lt;/strong&gt; is a collection of data that is stored in a form that allows for easy storage, retrieval, and management.&lt;br&gt;&lt;br&gt;
There exist two forms of databases: &lt;strong&gt;Relational databases&lt;/strong&gt; and &lt;strong&gt;Non-Relational Databases&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Relational Databases&lt;/strong&gt; are also called &lt;strong&gt;SQL databases&lt;/strong&gt;. These are databases that store data in structured, predefined tables with rows and columns.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Non-Relational Databases&lt;/strong&gt; are also called &lt;strong&gt;No-SQL databases&lt;/strong&gt;. These are databases that store data without a predefined schema. These databases are designed to handle unstructured, semi-structured, or rapidly changing data.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To manage data in a database, we use &lt;strong&gt;database management software&lt;/strong&gt;.&lt;br&gt;&lt;br&gt;
This is where we can begin understanding what SQL is.&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;SQL&lt;/strong&gt; stands for &lt;strong&gt;Structured Query Language&lt;/strong&gt;. It is the standard language used to communicate with relational databases. It allows users to perform operations such as querying, updating, and managing data stored in tables contained in databases.&lt;/p&gt;

&lt;p&gt;A key thing to note is that &lt;strong&gt;SQL is a language&lt;/strong&gt;, and like any language, it has various &lt;strong&gt;dialects&lt;/strong&gt;. There exists a base language (&lt;strong&gt;ANSI standard&lt;/strong&gt;) and then different flavors of SQL are determined by the &lt;strong&gt;Relational Database Management Software&lt;/strong&gt; you use (&lt;strong&gt;RDBMS&lt;/strong&gt;).&lt;br&gt;&lt;br&gt;
The most common RDBMS today are: &lt;strong&gt;Oracle&lt;/strong&gt;, &lt;strong&gt;MySQL&lt;/strong&gt;, &lt;strong&gt;Microsoft SQL Server&lt;/strong&gt;, and &lt;strong&gt;PostgreSQL&lt;/strong&gt; (&lt;a href="https://www.statista.com/statistics/809750/worldwide-popularity-ranking-database-management-systems/" rel="noopener noreferrer"&gt;in order of popularity as of June 2024&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The summary&lt;/strong&gt; is that there exist &lt;strong&gt;tables&lt;/strong&gt; that store data, and those tables are housed within &lt;strong&gt;databases&lt;/strong&gt;, and those databases are managed using a &lt;strong&gt;relational database management software&lt;/strong&gt;.&lt;/p&gt;


&lt;h3&gt;
  
  
  &lt;strong&gt;SQL Syntax&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;One of the best things about SQL is that it is a &lt;strong&gt;procedural language&lt;/strong&gt;, meaning that it feels like writing human instructions. An example would be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;column_a&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;table_a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even if one didn't have technical computer knowledge, they would be able to understand what this snippet of code was written to do, which is to return &lt;code&gt;column_a&lt;/code&gt; that exists within &lt;code&gt;table_a&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Something to note is that &lt;strong&gt;key SQL statements are not case-sensitive&lt;/strong&gt;, meaning the code we used above could still be correct if written as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="n"&gt;column_a&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;table_a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, it is standard practice to capitalize key SQL statements like &lt;code&gt;SELECT&lt;/code&gt; and &lt;code&gt;FROM&lt;/code&gt;, not only to enhance &lt;strong&gt;neatness and readability&lt;/strong&gt;, but also to ensure you can easily spot errors in your code.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Let's Get Down to Writing SQL Code!&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The first statement you will encounter is &lt;strong&gt;SELECT&lt;/strong&gt;. It is used to select what you specify it to select from a table.&lt;br&gt;&lt;br&gt;
While you can select one column like what we did above, you can also select multiple columns:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
    &lt;span class="n"&gt;column_a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;column_b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;column_c&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;table_a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two things to note here:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;In one's head, one might be wondering why we &lt;strong&gt;indented&lt;/strong&gt; the code. It is just to enhance neatness. The indent is not part of SQL syntax.&lt;/li&gt;
&lt;li&gt;Also, you might have noticed that the code always ends with a &lt;strong&gt;semicolon&lt;/strong&gt;. This is because this is how the &lt;strong&gt;RDBMS knows where to terminate&lt;/strong&gt; the 'procedure' or SQL code.&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;&lt;strong&gt;FROM&lt;/strong&gt; is used to specify which table the data should come from.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Aliases&lt;/strong&gt; - Let's say you want some columns to have a specific name when the results are returned after the query is run. This is where you would use &lt;strong&gt;aliases&lt;/strong&gt;. The key word for alias is &lt;strong&gt;AS&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
    &lt;span class="n"&gt;column_a&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;column_b&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;column_c&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="k"&gt;c&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;table_a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;strong&gt;Filtering Results&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Imagine you want to filter your results. This is where you would use the &lt;strong&gt;magic SQL keywords&lt;/strong&gt; responsible for filtering: &lt;strong&gt;WHERE&lt;/strong&gt;, &lt;strong&gt;LIKE&lt;/strong&gt;, &lt;strong&gt;HAVING&lt;/strong&gt;, and &lt;strong&gt;REGEXP&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We shall only cover &lt;strong&gt;WHERE&lt;/strong&gt;, though I will give you some insights on the rest.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;LIKE&lt;/strong&gt; and &lt;strong&gt;REGEXP&lt;/strong&gt; are related. &lt;strong&gt;REGEXP&lt;/strong&gt; is a more advanced version of &lt;strong&gt;LIKE&lt;/strong&gt; used to filter data using specific criteria, for example, giving records where values in a certain column end with 'th'.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;HAVING&lt;/strong&gt;, on the other hand, is another version of &lt;strong&gt;WHERE&lt;/strong&gt;, but with more specific criteria.&lt;strong&gt;HAVING&lt;/strong&gt; for example can take &lt;strong&gt;GROUP BY&lt;/strong&gt; queries and filter them while &lt;strong&gt;WHERE&lt;/strong&gt; cannot
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
    &lt;span class="n"&gt;column_a&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;column_b&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;column_c&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="k"&gt;c&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;table_a&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;column_a&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
    &lt;span class="n"&gt;CustomerID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;COUNT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SaleID&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;SalesCount&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Sales&lt;/span&gt;
&lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;CustomerID&lt;/span&gt;
&lt;span class="k"&gt;HAVING&lt;/span&gt; &lt;span class="n"&gt;SalesCount&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you read the SQL code like human instructions, you get an idea of what it is trying to do:&lt;br&gt;&lt;br&gt;
We want to select &lt;code&gt;column_a&lt;/code&gt;, &lt;code&gt;column_b&lt;/code&gt;, and &lt;code&gt;column_c&lt;/code&gt;, each with their own alias, and we are selecting from &lt;code&gt;table_a&lt;/code&gt;. From those records we have selected, we want only those where the values in &lt;code&gt;column_a&lt;/code&gt; are less than 10.&lt;/p&gt;


&lt;h3&gt;
  
  
  &lt;strong&gt;Creating Tables and Databases&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;To &lt;strong&gt;create&lt;/strong&gt; anything, we use the keyword &lt;strong&gt;CREATE&lt;/strong&gt;.&lt;br&gt;&lt;br&gt;
We can create &lt;strong&gt;tables&lt;/strong&gt; and &lt;strong&gt;databases&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The syntax for this is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="n"&gt;database_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;table_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Whenever we create a table, we have to think about what data is going into the table and what &lt;strong&gt;data types&lt;/strong&gt; each field will take on.&lt;br&gt;&lt;br&gt;
There are various data types, but we shall first divide them into &lt;strong&gt;number data types&lt;/strong&gt;, &lt;strong&gt;character data types&lt;/strong&gt;, and &lt;strong&gt;datetime data types&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Number data types&lt;/strong&gt; include &lt;code&gt;INT&lt;/code&gt;, &lt;code&gt;AUTO_INCREMENT&lt;/code&gt;, &lt;code&gt;FLOAT&lt;/code&gt;. The key thing to note is that &lt;code&gt;INT&lt;/code&gt; represents integers. While we have subdivisions of &lt;code&gt;INT&lt;/code&gt;, which include &lt;code&gt;BIGINT&lt;/code&gt; and &lt;code&gt;SMALLINT&lt;/code&gt;, they only represent the range of values that can be taken but not the integer aspect. &lt;code&gt;AUTO_INCREMENT&lt;/code&gt; is an integer data type that increases every time a record is added to a table. It is at times used as a surrogate &lt;strong&gt;primary key&lt;/strong&gt; in tables.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Character data types&lt;/strong&gt; include &lt;code&gt;CHAR&lt;/code&gt;, &lt;code&gt;VARCHAR&lt;/code&gt;, and &lt;code&gt;TEXT&lt;/code&gt;. &lt;code&gt;CHAR&lt;/code&gt; and &lt;code&gt;VARCHAR&lt;/code&gt; have the same characteristics, but there is one big difference. &lt;code&gt;CHAR&lt;/code&gt; takes on the full specification of characters given, but &lt;code&gt;VARCHAR&lt;/code&gt; does not.&lt;br&gt;&lt;br&gt;
For example, if I specify a field to take on &lt;code&gt;VARCHAR(10)&lt;/code&gt; and another to take &lt;code&gt;CHAR(10)&lt;/code&gt;, meaning each to take 10 characters, and then I input the string 'hello' in both fields, when I do a count of the characters in the &lt;code&gt;VARCHAR&lt;/code&gt;, it returns 5, but for the &lt;code&gt;CHAR&lt;/code&gt;, it returns 10. Basically, &lt;code&gt;CHAR&lt;/code&gt; will add white space to the remaining spaces to fill the limit given, which is 10 characters. That is also why &lt;code&gt;VARCHAR&lt;/code&gt; is named as it is, which stands for &lt;strong&gt;Variable Character&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Datetime data types&lt;/strong&gt; include &lt;code&gt;DATE&lt;/code&gt;, &lt;code&gt;TIME&lt;/code&gt;, and &lt;code&gt;DATETIME&lt;/code&gt;. You may ask why I haven't added the rest like &lt;code&gt;INTERVAL&lt;/code&gt; and &lt;code&gt;YEAR&lt;/code&gt;. This is because they are not part of the &lt;strong&gt;ANSI standard&lt;/strong&gt; of SQL and vary across &lt;strong&gt;RDBMS&lt;/strong&gt;. &lt;code&gt;DATE&lt;/code&gt; represents dates, &lt;code&gt;TIME&lt;/code&gt; represents time, and &lt;code&gt;DATETIME&lt;/code&gt; is a combination of &lt;code&gt;DATE&lt;/code&gt; and &lt;code&gt;TIME&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is a snippet of code putting all these things together:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;teachers&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="nb"&gt;BIGINT&lt;/span&gt; &lt;span class="n"&gt;AUTO_INCREMENT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;-- This is autoincremental data type&lt;/span&gt;
    &lt;span class="n"&gt;first_name&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;last_name&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;school&lt;/span&gt; &lt;span class="nb"&gt;CHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;hire_date&lt;/span&gt; &lt;span class="nb"&gt;DATETIME&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="nb"&gt;FLOAT&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;teachers&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;first_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;last_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;school&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hire_date&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Ken'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Hubert'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;"Murang'a"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'2020-01-01 15:49:20'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;65000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;strong&gt;The LIMIT Clause&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;This is one of the most important clauses when querying any table. Imagine this: You work at Instagram and have access to their SQL databases (assuming they have those). Imagine querying a table of all Instagram users and you want to filter all the records where the age of the user is below 20. Logically, that could be in the tens of millions, if not hundreds. Imagine hitting run and just crashing the system because you've requested more than the system can handle. This is how the &lt;strong&gt;LIMIT&lt;/strong&gt; clause could help you. It helps retrieve a subset of the records from that query, hence avoiding disaster!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;age&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;ig_users&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;
&lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Here's your article with the requested formatting in markdown:&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Joins in Relational Databases&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;One of the main aims of &lt;strong&gt;SQL&lt;/strong&gt; databases is to reduce redundancy when storing data in tables. This will involve storing data in multiple tables that have relationships with each other. When querying the tables to get data, we might need to &lt;strong&gt;join&lt;/strong&gt; tables to get the results we want.&lt;/p&gt;

&lt;p&gt;There are a few types of &lt;strong&gt;JOINS&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;JOIN&lt;/strong&gt; or &lt;strong&gt;INNER JOIN&lt;/strong&gt; brings columns that are matching in both tables.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;LEFT JOIN&lt;/strong&gt; brings every row in the &lt;strong&gt;LEFT Table&lt;/strong&gt; and it finds matching rows in the &lt;strong&gt;RIGHT&lt;/strong&gt; table.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;RIGHT JOIN&lt;/strong&gt; brings every row in the &lt;strong&gt;RIGHT Table&lt;/strong&gt; and it finds matching rows in the &lt;strong&gt;LEFT&lt;/strong&gt; table. Remember, a &lt;strong&gt;right join&lt;/strong&gt; is just a &lt;strong&gt;left join&lt;/strong&gt; with the tables inverted.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;FULL OUTER JOIN&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CROSS JOIN&lt;/strong&gt; - Returns every possible combination of rows from both tables.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;An example that will show the differences between these joins can be learnt by implementing the code snippet below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;schools_left&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;left_school&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;schools_right&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;right_school&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;schools_left&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;left_school&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="k"&gt;VALUES&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Oak Street School'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Roosevelt High School'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Washington Middle School'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Jefferson High School'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;schools_right&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;right_school&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;VALUES&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Oak Street School'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Roosevelt High School'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Morrison Elementary'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Chase Magnet Academy'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Jefferson High School'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;INNER JOIN&lt;/strong&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;schools_left&lt;/span&gt; &lt;span class="n"&gt;sl&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;schools_right&lt;/span&gt; &lt;span class="n"&gt;sr&lt;/span&gt; 
    &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;sl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;LEFT AND RIGHT JOIN&lt;/strong&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;schools_left&lt;/span&gt; &lt;span class="n"&gt;sl&lt;/span&gt;
&lt;span class="k"&gt;LEFT&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;schools_right&lt;/span&gt; &lt;span class="n"&gt;sr&lt;/span&gt;
    &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;sl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;schools_left&lt;/span&gt; &lt;span class="n"&gt;sl&lt;/span&gt;
&lt;span class="k"&gt;RIGHT&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;schools_right&lt;/span&gt; &lt;span class="n"&gt;sr&lt;/span&gt;
    &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;sl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You’d use either of these join types in a few circumstances:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You want your query results to contain all the rows from one of the tables.&lt;/li&gt;
&lt;li&gt;You want to look for missing values in one of the tables; for example, when you’re comparing data about an entity representing two different time periods.&lt;/li&gt;
&lt;li&gt;When you know some rows in a joined table won’t have matching values.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;FULL OUTER JOIN&lt;/strong&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;schools_left&lt;/span&gt; &lt;span class="n"&gt;sl&lt;/span&gt;
&lt;span class="k"&gt;FULL&lt;/span&gt; &lt;span class="k"&gt;OUTER&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;schools_right&lt;/span&gt; &lt;span class="n"&gt;sr&lt;/span&gt;
    &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;sl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;-- FULL OUTER JOINS AREN'T PART OF MySQL But rather found in PostgreSQL&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;CROSS JOIN&lt;/strong&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;schools_left&lt;/span&gt; &lt;span class="n"&gt;sl&lt;/span&gt;
&lt;span class="k"&gt;CROSS&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;schools_right&lt;/span&gt; &lt;span class="n"&gt;sr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A good way to find missing records is using the &lt;strong&gt;NULL&lt;/strong&gt; operator combining the &lt;strong&gt;JOINS&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;An example is finding columns that didn't have a match after a &lt;strong&gt;LEFT JOIN&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;schools_left&lt;/span&gt; &lt;span class="n"&gt;sl&lt;/span&gt; 
&lt;span class="k"&gt;LEFT&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;schools_right&lt;/span&gt; &lt;span class="n"&gt;sr&lt;/span&gt; 
    &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;sl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;sr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;right_school&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this introduction, one should be able to play around with creating databases, tables, inputting data into them, and querying the data as a whole and also filtering the queried results. You should also be able to experiment with various joins and practice how to retrieve data stored in different tables.&lt;br&gt;&lt;br&gt;
Also, while playing around, you might encounter runtime errors which are very useful in guiding you to understand what not to do while writing SQL queries and also give you a better understanding of the language itself.&lt;br&gt;&lt;br&gt;
Thank you for reading!&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
