<?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: Sharon-nyabuto</title>
    <description>The latest articles on DEV Community by Sharon-nyabuto (@sharonnyabuto).</description>
    <link>https://dev.to/sharonnyabuto</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%2F3849348%2F063abdb2-e306-4721-a3a7-73ed0f20c060.png</url>
      <title>DEV Community: Sharon-nyabuto</title>
      <link>https://dev.to/sharonnyabuto</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sharonnyabuto"/>
    <language>en</language>
    <item>
      <title>Making Decisions Within Decisions in Python</title>
      <dc:creator>Sharon-nyabuto</dc:creator>
      <pubDate>Wed, 15 Jul 2026 06:11:48 +0000</pubDate>
      <link>https://dev.to/sharonnyabuto/taking-conditional-logic-further-with-nested-if-statements-94d</link>
      <guid>https://dev.to/sharonnyabuto/taking-conditional-logic-further-with-nested-if-statements-94d</guid>
      <description>&lt;h2&gt;
  
  
  Taking Conditional Logic Further with Nested if Statements
&lt;/h2&gt;

&lt;p&gt;Previously, we learnt how to use &lt;code&gt;if&lt;/code&gt;, &lt;code&gt;elif&lt;/code&gt;, and &lt;code&gt;else&lt;/code&gt; to make decisions in Python. Those allowed our programs to choose between different paths depending on whether a condition evaluated to &lt;code&gt;True&lt;/code&gt; or &lt;code&gt;False&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Most of the real world decisions depend on more than one condition.&lt;/p&gt;

&lt;p&gt;Think about logging into your bank account to withdraw money. Entering the correct PIN is only the first step. Once you're in, the system still needs to check whether you have sufficient funds before approving the withdrawal. &lt;br&gt;
How do we build that kind of layered decision-making in Python?&lt;br&gt;
That's exactly what we'll explore in this article as we learn about nested &lt;code&gt;if&lt;/code&gt; statements.&lt;/p&gt;
&lt;h3&gt;
  
  
  Nested &lt;code&gt;if&lt;/code&gt; Statements
&lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;nested &lt;code&gt;if&lt;/code&gt; statement&lt;/strong&gt; is simply an &lt;code&gt;if&lt;/code&gt; statement placed inside another &lt;code&gt;if&lt;/code&gt; statement. It allows a program to evaluate another condition only after the first condition has been met.&lt;/p&gt;
&lt;h3&gt;
  
  
  Syntax
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;condition_1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;condition_2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="c1"&gt;# Code to execute
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The inner &lt;code&gt;if&lt;/code&gt; statement is only evaluated if the outer &lt;code&gt;if&lt;/code&gt; statement is &lt;code&gt;True&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Example:&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Suppose you're analysing survey responses for a scholarship programme. Applicants must first be at least 18 years old. If they meet that requirement, then you can check whether their household income qualifies them for financial assistance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;21&lt;/span&gt;
&lt;span class="n"&gt;monthly_income&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;18000&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;monthly_income&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;30000&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Eligible for the scholarship.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Age requirement met, but income is above the eligibility threshold.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Applicant does not meet the minimum age requirement.&lt;/span&gt;&lt;span class="sh"&gt;"&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 what happens step by step:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Python stores the value &lt;code&gt;21&lt;/code&gt; in the variable &lt;code&gt;age&lt;/code&gt; and &lt;code&gt;18000&lt;/code&gt; in the variable &lt;code&gt;monthly_income&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It evaluates the first condition: &lt;code&gt;age &amp;gt;= 18&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Since &lt;code&gt;21&lt;/code&gt; is greater than or equal to &lt;code&gt;18&lt;/code&gt;, the first condition evaluates to &lt;code&gt;True&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Because the first condition is &lt;code&gt;True&lt;/code&gt;, Python moves to the nested &lt;code&gt;if&lt;/code&gt; statement and evaluates the second condition: &lt;code&gt;monthly_income &amp;lt; 30000&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Since &lt;code&gt;18000&lt;/code&gt; is less than &lt;code&gt;30000&lt;/code&gt;, the second condition also evaluates to &lt;code&gt;True&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Python executes the nested &lt;code&gt;if&lt;/code&gt; block and prints:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  Eligible for the scholarship.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because both conditions were satisfied, the two &lt;code&gt;else&lt;/code&gt; blocks are skipped and the program ends.&lt;/p&gt;

&lt;p&gt;Notice the order of the checks. Python only evaluates the applicant's income &lt;strong&gt;after&lt;/strong&gt; confirming they meet the minimum age requirement. &lt;br&gt;
If the applicant were under 18, the income would never be checked because the program would immediately execute the outer &lt;code&gt;else&lt;/code&gt; block. This is what makes nested &lt;code&gt;if&lt;/code&gt;statements useful; they allow one decision to depend on the outcome of another.&lt;/p&gt;

&lt;p&gt;Here are a few other examples, that demonstrate how multiple conditions can be evaluated one after another.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Scenario 1: Job Application Screener
&lt;/span&gt;
&lt;span class="c1"&gt;# Check whether the applicant has tertiary education. If they do, ask for their years of experience. 
# If they have at least 3 years of experience, ask for their expected salary. If the salary expectation is Ksh 80,000 or less, shortlist them for an interview; otherwise, indicate that their salary expectation is too high. 
# If they have fewer than 3 years of experience, print "Minimum 3 years of experience required." If they do not have tertiary education, print "Higher education is required for this position."
&lt;/span&gt;
&lt;span class="n"&gt;tertiary_education&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Do you have a diploma, degree or higher?&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;degree&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;yes&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;experience&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;How many years of experince&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;experience&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Enter your expected salary&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;80000&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Shortlisted for interview&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Salary expectation too high&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Minimum 3 years experience required&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Higher education is required for this position&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
`&lt;/p&gt;

&lt;p&gt;`&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Scenario 2: Mobile Money Transfer Validator
&lt;/span&gt;
&lt;span class="c1"&gt;# Write a program to validate a mobile money transfer. 
# If the user has an account, check that they have sufficient funds and that the transfer amount does not exceed the daily limit of Ksh 150,000. 
# If the transfer is successful, display the amount sent and the remaining balance. If the user does not have an account, print "Please register first."
&lt;/span&gt;
&lt;span class="n"&gt;mobile_checker&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Do they have an active account? (Yes/No)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;mobile_checker&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;yes&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;acc_balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;What is your account balance?&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;acc_balance&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;amount_to_send&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;How much do you want to send??&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;amount_to_send&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;acc_balance&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;new_balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;acc_balance&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;amount_to_send&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;amount_to_send&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;150000&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Amount exceeded daily limit&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Successful transfer&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Amount sent: Ksh., &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;amount_to_send&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;, Your account balance is: Kshs., &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;new_balance&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Insufficient funds: Kshs., &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;acc_balance&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;You have a low balance&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Please register first.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
&lt;code&gt;&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Scenario 3: School Report Card Generator
&lt;/span&gt;
&lt;span class="c1"&gt;# Write a program that generates a student's report card. Ask for the student's name and marks (out of 100) in Mathematics, English, Kiswahili, Science, and Social Studies. For each subject, assign a grade and remark using the grading scale provided. Then calculate the total marks (out of 500), the average score, and the overall grade based on the average using the same grading scale.
# Next, determine the student's promotion status: students with an average of 75 or above are promoted, those with an average between 50 and 74 are promoted but advised to work harder, while those below 50 should repeat the class.
# Finally, check whether the student received a grade E in any subject. If so, display a warning indicating that remedial lessons are required for the relevant subject(s). 
&lt;/span&gt;
&lt;span class="c1"&gt;# Print a neatly formatted report showing the student's name, subject grades, total marks, average, overall grade, promotion status and any remedial warning.
&lt;/span&gt;
&lt;span class="n"&gt;subjects&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Maths&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;English&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Kiswahili&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Science&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Social Studies&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;student_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;What is your name?&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;student_name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; : Performance Summary&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;==================================&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;total_marks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="n"&gt;failed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;""&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;subject&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;subjects&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;marks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;What was your score in &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;subject&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="mi"&gt;75&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;marks&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;grade&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;  A  - Excellent&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;marks&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;74&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;grade&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;  B  - Very Good&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;marks&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;59&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;grade&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;  C  - Good&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;marks&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;49&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;grade&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;  D  - Pass&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;grade&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;  E  - Need Improvement&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="n"&gt;total_marks&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;marks&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;subject&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; | Score: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;marks&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; | Grade:&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;grade&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;grade&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;  E  - Need Improvement&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;failed&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;subject&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;  
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Total marks: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;total_marks&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; / 500&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;failed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; 
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;NOTE: Remedial lessons required in &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;failed&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;average&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;total_marks&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;subjects&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Average Score: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;average&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;average&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;75&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;PROMOTED to next class&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;average&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;74&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;PROMOTED -  work harder next term&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; 
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;REPEAT - see your class teacher&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Mary Nancy : Performance Summary
==================================

Maths          | Score: 43 | Grade:  D  - Pass
English        | Score: 23 | Grade:  E  - Need Improvement
Kiswahili      | Score: 56 | Grade:  C  - Good
Science        | Score: 89 | Grade:  A  - Excellent
Social Studies | Score: 67 | Grade:  B  - Very Good
Total marks: 278 / 500

NOTE: Remedial lessons required in English 

Average Score: 55.6
PROMOTED - Work harder next term 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Don't stop with the examples in this article. Modify them, build your own, and experiment with different scenarios, because the best way to master programming is by writing and testing your own code. &lt;br&gt;
As you continue practicing, you may find yourself using nested conditionals whenever a problem requires multiple levels of decision-making. &lt;/p&gt;
&lt;h3&gt;
  
  
  Bonus Tip: f-Strings
&lt;/h3&gt;

&lt;p&gt;Before we wrap up, there's one small feature you've probably noticed throughout the examples in this article. Many of the &lt;code&gt;print()&lt;/code&gt; statements begin with the letter &lt;code&gt;f&lt;/code&gt;, like this;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;subject&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; | Score: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;marks&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; | Grade:&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;grade&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;f&lt;/code&gt; stands for &lt;strong&gt;formatted string&lt;/strong&gt; (or &lt;strong&gt;f-string&lt;/strong&gt;). It allows you to insert the value of a variable directly into a string by placing it inside curly braces &lt;code&gt;{}&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Without an f-string, you would have to write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;subject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; | Score: &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;marks&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; | Grade: &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;grade&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While both approaches work, f-strings are easier to read and become especially useful as your programs grow longer and your output includes multiple variables.&lt;/p&gt;

&lt;p&gt;As we continue through this series, we'll use f-strings regularly to produce cleaner and more readable output. &lt;/p&gt;

</description>
      <category>python</category>
      <category>learning</category>
    </item>
    <item>
      <title>Teaching Python to Make Decisions</title>
      <dc:creator>Sharon-nyabuto</dc:creator>
      <pubDate>Mon, 13 Jul 2026 21:11:20 +0000</pubDate>
      <link>https://dev.to/sharonnyabuto/teaching-python-to-make-decisions-2k0k</link>
      <guid>https://dev.to/sharonnyabuto/teaching-python-to-make-decisions-2k0k</guid>
      <description>&lt;h2&gt;
  
  
  Understanding if, elif, and else
&lt;/h2&gt;

&lt;p&gt;In the previous article, we explored what Python is and why it's such a valuable tool for data analysts. We also wrote a few simple programs that followed instructions exactly as we wrote them.&lt;/p&gt;

&lt;p&gt;In this article, we will explore how to teach Python to make decisions using conditional statements. By the end, you'll understand how to use &lt;code&gt;if&lt;/code&gt;, &lt;code&gt;elif&lt;/code&gt;, and &lt;code&gt;else&lt;/code&gt; to control the flow of your programs and hopefully solve simple real-world problems.&lt;/p&gt;




&lt;h3&gt;
  
  
  So what is a conditional statement?
&lt;/h3&gt;

&lt;p&gt;We make decisions all the time in our lives. If it rains, we carry an umbrella. If we're hungry, we eat. If an assignment is due tomorrow, we probably shouldn't leave it until midnight. Programs need to make decisions too, that is what conditional statements are for.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conditional Statements&lt;/strong&gt; allow a program to evaluate conditions and choose what to do based on whether those conditions are &lt;code&gt;True&lt;/code&gt; or &lt;code&gt;False.&lt;/code&gt; &lt;br&gt;
The three keywords you'll most commonly use when writing conditional statements are &lt;code&gt;if&lt;/code&gt;, &lt;code&gt;elif&lt;/code&gt;, and &lt;code&gt;else&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Comparison Operators
&lt;/h3&gt;

&lt;p&gt;Before we can make decisions in Python, we need a way to compare values. &lt;br&gt;
Comparison operators evaluate two values and return either &lt;code&gt;True&lt;/code&gt; or &lt;code&gt;False&lt;/code&gt;, which are the outcomes that drive &lt;code&gt;if&lt;/code&gt;, &lt;code&gt;elif&lt;/code&gt;, and &lt;code&gt;else&lt;/code&gt; statements. Common comparison operators are: &lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Operator&lt;/th&gt;
&lt;th&gt;Meaning&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;==&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Equal to&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;!=&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Not equal to&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Greater than&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;&amp;lt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Less than&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;&amp;gt;=&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Greater than or equal to&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;&amp;lt;=&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Less than or equal to&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Example:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;
&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# equal to        -&amp;gt; True
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# not equal to    -&amp;gt; True
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;# greater than    -&amp;gt; True
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&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;18&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;# less than       -&amp;gt; False
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# greater or equal -&amp;gt; True
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&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="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# less or equal    -&amp;gt; False
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Note: The &lt;code&gt;=&lt;/code&gt; operator assigns a value to a variable, (like age = 25), while &lt;code&gt;==&lt;/code&gt; checks whether two values are equal. This is one of the most common mix-ups I had when I started out.&lt;/em&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  The IF statement
&lt;/h3&gt;

&lt;p&gt;The if statement is what lets your code actually make a decision : It runs a block of code only if a condition is &lt;strong&gt;True&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Example:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;You&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;re an adult.&lt;/span&gt;&lt;span class="sh"&gt;"&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 what happens step by step:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;age = 25&lt;/code&gt; - Python stores the value &lt;code&gt;25&lt;/code&gt; in the variable &lt;code&gt;age&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;age &amp;gt;= 18&lt;/code&gt; - It evaluates the condition. &lt;/li&gt;
&lt;li&gt;Since &lt;code&gt;25&lt;/code&gt; is &lt;em&gt;greater than&lt;/em&gt; &lt;code&gt;18&lt;/code&gt; the condition  is &lt;code&gt;True&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The indented line is executed, and the output prints:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;You're an adult.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The colon &lt;code&gt;:&lt;/code&gt; shows the end of the condition, and indicates everything after is what needs to be done if the condition is met.&lt;/p&gt;

&lt;p&gt;The indentation &lt;em&gt;(usually 4 spaces)&lt;/em&gt; is how Python knows which lines of code belong inside the &lt;code&gt;if&lt;/code&gt; statement.&lt;/p&gt;

&lt;h3&gt;
  
  
  The ELSE statement
&lt;/h3&gt;

&lt;p&gt;In an &lt;code&gt;if&lt;/code&gt; statement, you specify what needs to be done if the condition is True. &lt;br&gt;
You can also tell Python what to do if the condition is &lt;em&gt;not&lt;/em&gt; met, using &lt;code&gt;else&lt;/code&gt;. The statement then becomes;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Example:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;score&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;68&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;score&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;70&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;You have passed&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Please retake the test&lt;/span&gt;&lt;span class="sh"&gt;"&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 what happens step by step:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Python stores the value &lt;code&gt;68&lt;/code&gt; in the variable &lt;code&gt;score&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;It evaluates the condition &lt;code&gt;score &amp;gt;= 70&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Since &lt;code&gt;68&lt;/code&gt; is less than &lt;code&gt;70&lt;/code&gt;, the condition evaluates to &lt;code&gt;False&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Because the &lt;code&gt;if&lt;/code&gt; condition is &lt;code&gt;False&lt;/code&gt;, Python skips the &lt;code&gt;if&lt;/code&gt; block.&lt;/li&gt;
&lt;li&gt;Python then executes the &lt;code&gt;else&lt;/code&gt; block and prints:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Please retake the test
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;else&lt;/code&gt; block acts as a fallback, and  is only executed when the condition in the &lt;code&gt;if&lt;/code&gt; statement evaluates to &lt;code&gt;False&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;if-else&lt;/code&gt; statement is always used when you have &lt;strong&gt;two&lt;/strong&gt; possible outcomes.&lt;/p&gt;

&lt;h3&gt;
  
  
  The ELIF statement
&lt;/h3&gt;

&lt;p&gt;Suppose there are more than two possible outcomes? &lt;br&gt;
The if-else statements will not sufficiently represent all of them. This is where the &lt;code&gt;elif&lt;/code&gt; statement becomes useful&lt;/p&gt;

&lt;p&gt;&lt;code&gt;elif&lt;/code&gt; ("else if") lets you check additional conditions in order.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Example:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;34&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Older Adults&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Youth&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Teens&lt;/span&gt;&lt;span class="sh"&gt;"&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 what happens step by step:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Python stores the value &lt;code&gt;34&lt;/code&gt; in the variable &lt;code&gt;age&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;It evaluates the first condition: &lt;code&gt;age &amp;gt;= 40&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Since &lt;code&gt;34&lt;/code&gt; is less than &lt;code&gt;40&lt;/code&gt;, the first condition evaluates to &lt;code&gt;False&lt;/code&gt;, so Python moves to the next condition.&lt;/li&gt;
&lt;li&gt;Python evaluates the &lt;code&gt;elif&lt;/code&gt; condition: &lt;code&gt;age &amp;gt;= 20&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Since &lt;code&gt;34&lt;/code&gt; is greater than &lt;code&gt;20&lt;/code&gt;, the &lt;code&gt;elif&lt;/code&gt; condition evaluates to &lt;code&gt;True&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Python executes the &lt;code&gt;elif&lt;/code&gt; block and prints:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Youth
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Python then skips the &lt;code&gt;else&lt;/code&gt; block and the conditional statement ends.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Think of &lt;code&gt;elif&lt;/code&gt; as Python saying, "If the first condition wasn't true, check this one instead." &lt;br&gt;
The conditions are evaluated from top to bottom and only the first condition that evaluates to &lt;code&gt;True&lt;/code&gt; is executed.&lt;/p&gt;


&lt;h3&gt;
  
  
  Putting Conditionals into Practice
&lt;/h3&gt;

&lt;p&gt;Now that we've covered the basics of &lt;code&gt;if&lt;/code&gt;, &lt;code&gt;elif&lt;/code&gt;, and &lt;code&gt;else&lt;/code&gt;, it's time to see how they work together. &lt;br&gt;
The following examples are a little more practical, and demonstrate how Python can make decisions based on multiple conditions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Examples&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Q1. A cinema allows entry if: age &amp;gt;= 18 AND has_ticket == True. Write the condition and test it with different values.
&lt;/span&gt;
&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;How old are you?&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;# Ask for age
&lt;/span&gt;&lt;span class="n"&gt;has_ticket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;has_ticket&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Welcome to the Movies!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; 
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Restricted&lt;/span&gt;&lt;span class="sh"&gt;"&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 python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Q2. Build an electricity bill calculator. Units used: 0–50 → Ksh 12/unit, 51–200 → Ksh 15/unit, 201+ → Ksh 18/unit. Ask for units used and print the total bill.
&lt;/span&gt;
&lt;span class="n"&gt;electricity_units&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;How many units have you used?&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;electricity_units&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;electricity_units&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Your bill is &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;electricity_units&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;electricity_units&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Your bill is &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;electricity_units&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
     &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Your bill is &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;electricity_units&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;18&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 python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Q3. Ask for a student's marks (0–100). Print their KCSE grade: A (75+), B (60–74), C (50–59), D (40–49), E (below 40).
&lt;/span&gt;&lt;span class="n"&gt;student_marks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;What were your marks?&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;student_marks&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;75&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;A&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;student_marks&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;B&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;student_marks&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;C&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;student_marks&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;D&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;E&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Don't worry about counting your &lt;code&gt;elif&lt;/code&gt; statements! You can have as many as your program needs, as you can see in the last example. Python simply checks each condition in order and stops as soon as it finds one that's &lt;code&gt;True.&lt;/code&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Your turn, try it yourself!
&lt;/h3&gt;

&lt;p&gt;The examples in this article are just a starting point. Experiment with different values, create your own conditions, and see how changing the logic affects the output. Programming is one of those skills that's learned best by doing.&lt;/p&gt;

&lt;h3&gt;
  
  
  What's Next?
&lt;/h3&gt;

&lt;p&gt;We've now learned how to make Python choose between different paths. Next, we'll build on that foundation with &lt;code&gt;nested if&lt;/code&gt; statements, where one condition is evaluated inside another. It's a small step in syntax, but a big step towards writing more realistic programs. &lt;/p&gt;

&lt;p&gt;Thanks for reading, and I hope you'll join me for the next part of the series. Until then, happy coding!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Python for Data Analysts: Getting Started</title>
      <dc:creator>Sharon-nyabuto</dc:creator>
      <pubDate>Sun, 12 Jul 2026 14:44:43 +0000</pubDate>
      <link>https://dev.to/sharonnyabuto/python-for-data-analysts-getting-started-29o6</link>
      <guid>https://dev.to/sharonnyabuto/python-for-data-analysts-getting-started-29o6</guid>
      <description>&lt;h2&gt;
  
  
  Understanding what Python is, why it matters, and the concepts every beginner will encounter.
&lt;/h2&gt;

&lt;p&gt;A month ago, if you'd asked whether I could write Python, the answer would have been no, simply because Python just looked intimidating.&lt;/p&gt;

&lt;p&gt;As a research and data analyst however, I do love a challenge,  and I asked myself the practical question: &lt;em&gt;How could I actually use Python in my day-to-day work?&lt;/em&gt;&lt;br&gt;
A dive into Python revealed one certainty: Python is not &lt;em&gt;easy&lt;/em&gt;, but it's nowhere near as intimidating as I'd built it up to be. This is a look back at my first week learning Python: the assumptions I walked in with, and how quickly most of them fell apart.&lt;/p&gt;
&lt;h2&gt;
  
  
  So what is Python and why does it matter in Data analysis?
&lt;/h2&gt;

&lt;p&gt;Python is a programming language: a way of giving a computer clear, step-by-step instructions to perform tasks. The tasks can be as simple as basic calculations or as involved as cleaning data, analysing thousands of data entries, automating repetitive tasks, or building applications. &lt;/p&gt;

&lt;p&gt;One of the reasons Python is so popular among both beginners and data analysts is its readable syntax. It often resembles plain English, making it easier to learn while remaining powerful enough for real-world data analysis.&lt;/p&gt;

&lt;p&gt;For example, one of the first programs most people write looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hello, World!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It simply tells Python to display &lt;code&gt;Hello, World!&lt;/code&gt; on the screen. It's a tiny program, but it marks the beginning of almost every programmer's journey, including mine.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why does Python matter?
&lt;/h3&gt;

&lt;p&gt;If you've spent enough time working with spreadsheets, you've probably thought, &lt;em&gt;"There has to be an easier way to do this."&lt;/em&gt; Whether it's cleaning data, repeating the same steps every week, or working with increasingly large datasets, there comes a point where spreadsheets begin to show their limits. That's where Python comes in.&lt;/p&gt;

&lt;p&gt;In data analytics, Python is particularly useful for:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Automating repetitive tasks&lt;/strong&gt; 

&lt;ul&gt;
&lt;li&gt;Tasks you'd normally repeat manually (cleaning files, renaming columns, merging spreadsheets) can be scripted once and reused.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scale&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;As datasets grow, spreadsheets can become slow, cumbersome, or even unresponsive. Python is designed to handle much larger amounts of data efficiently. We'll look at the tools that make this possible including Python libraries, in a later article.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reproducibility&lt;/strong&gt; 

&lt;ul&gt;
&lt;li&gt;Every step of your analysis is written into a script. That means you can run the same code tomorrow, next month, or even next year and reproduce the same results, without having to memorize or manually repeat every step.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Collaboration&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Because each step of the analysis is written into the script, colleagues can review, understand, and build on your work without having to guess what was done. This makes teams work seamlessly on the same project.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Flexibility in connectivity&lt;/strong&gt; 

&lt;ul&gt;
&lt;li&gt;Python can pull data directly from a database, an API, or a folder of raw files, instead of relying on someone exporting a CSV for you.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Specialized Tools&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;One of Python's greatest strengths is its ecosystem of libraries. Libraries are tools created by the Python community that extend what Python can do, without you having to build everything from scratch. &lt;em&gt;(We'll introduce some of them as we progress through the series)&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Learning why Python matters gave me the motivation to begin. Learning how to use it, however, came with a few surprises. Looking back, many of the things that intimidated me at first weren't really difficult, but simply unfamiliar.&lt;/p&gt;




&lt;h3&gt;
  
  
  Common Terms You'll Encounter in Python - Getting familiar with the Python Environment
&lt;/h3&gt;

&lt;p&gt;Far from jumping into data analysis, my first few days were spent learning the basic concepts and getting familiar with common terms, most of which had names that were new to me.&lt;/p&gt;

&lt;p&gt;Here are five that stood out to me when I was getting started;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Terminal:&lt;/strong&gt; I assumed one wrong command could break my computer, only to realize it's really just another way of talking to your machine, and it stops feeling like a 'black box' after a handful of basic commands.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Integrated Development Environment/IDE:&lt;/strong&gt; This is where you actually write your code. Think of it as a text editor built specifically for writing and running programs, with extra features like error highlighting, autocomplete, and a built-in terminal. &lt;strong&gt;VS Code&lt;/strong&gt; is one of the most popular choices.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Script:&lt;/strong&gt; This is the file containing Python code, usually with a .py extension. Instead of typing commands one at a time into the terminal, you write them all into a script and run the whole thing at once.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Library:&lt;/strong&gt; Pre-written code that someone else has already built, so you don't have to write everything from scratch. Need to work with dates, analyse data, or make charts? There's very likely a library for that — you just import it into your script and use it. &lt;strong&gt;Pandas&lt;/strong&gt; is one of the most common ones for data analysis.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Indentation:&lt;/strong&gt; Indentation isn't cosmetic in Python, it's part of the syntax, defining which lines belong together. It took me a couple of errors and examples before fully comprehending, but once that clicked, it stopped feeling confusing.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Early on, I felt like I had to fully understand every new term,  variables, functions, loops, modules, packages, libraries, before I could write code. Turns out you only need to understand the concept you're using &lt;em&gt;today&lt;/em&gt;. The rest will fall into place as you keep writing code.&lt;/p&gt;

&lt;h2&gt;
  
  
  My first few lines of Python
&lt;/h2&gt;

&lt;p&gt;A variable is just a name that stores information:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Sharon&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&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 python"&gt;&lt;code&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nationality&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;city&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Sharon&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Kenyan&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Kisumu&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; 
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;My name is &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;. I am a &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;nationality&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; and I live in &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;city&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Python can do calculations for you:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;income&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;25000&lt;/span&gt;
&lt;span class="n"&gt;expenses&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;18000&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;income&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;expenses&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or take input from the user:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;What is your name? &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Welcome, &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While these may not be impressive on their own, together they show programming isn't about writing something complex from day one, but about stacking small blocks to solve a bigger problem.&lt;/p&gt;

&lt;h2&gt;
  
  
  Biggest lessons so far
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Progress comes from consistent practice.&lt;/li&gt;
&lt;li&gt;Errors are part of the workflow, not a verdict on your ability.&lt;/li&gt;
&lt;li&gt;Typing code yourself teaches more than copy-pasting it.&lt;/li&gt;
&lt;li&gt;Small projects teach more than another hour of tutorials.&lt;/li&gt;
&lt;li&gt;Looking things up is normal, sometimes even the best analysts do it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Programming isn't about knowing every answer. It's about knowing how to find one. This is the first article in my Python learning series, documenting the process as I go.&lt;/p&gt;

&lt;p&gt;In the next article, we'll look at how Python uses &lt;code&gt;if&lt;/code&gt; statements &lt;code&gt;nested if&lt;/code&gt; statements to make choices based on different conditions. This i think is one of the biggest steps towards writing programs that actually think before they act.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final thoughts
&lt;/h2&gt;

&lt;p&gt;If you're on the fence, don't wait until you feel "ready." Write one line of code and trust that understanding builds with reps.&lt;/p&gt;

&lt;p&gt;I'm still learning too, and that's exactly what this series is for.&lt;/p&gt;

</description>
      <category>analytics</category>
      <category>beginners</category>
      <category>datascience</category>
      <category>python</category>
    </item>
    <item>
      <title>Cleaning Messy Phone Numbers in PostgreSQL Using REGEXP_REPLACE</title>
      <dc:creator>Sharon-nyabuto</dc:creator>
      <pubDate>Mon, 18 May 2026 22:34:00 +0000</pubDate>
      <link>https://dev.to/sharonnyabuto/cleaning-messy-phone-numbers-in-postgresql-using-regexp-replace-3n83</link>
      <guid>https://dev.to/sharonnyabuto/cleaning-messy-phone-numbers-in-postgresql-using-regexp-replace-3n83</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;The first thing anyone that has worked with real world data will tell you is that rarely does data come in the clean, uniform format you expect. Phone numbers are one of the biggest culprits. &lt;/p&gt;

&lt;p&gt;One person enters &lt;code&gt;+254712345678&lt;/code&gt;, another writes &lt;code&gt;0712345678&lt;/code&gt;, someone else throws in &lt;code&gt;254-712-345-678&lt;/code&gt;, another &lt;code&gt;(254)-712345678&lt;/code&gt; and another &lt;code&gt;254 712 345 678&lt;/code&gt;.  Before you know it, your database has a different version of phone numbers in every row.&lt;/p&gt;

&lt;p&gt;In this article, we will explore how to fix that, using a powerful PostgreSQL function called &lt;code&gt;regexp_replace&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;By the end, you will understand what it does, how to build the pattern, and how to apply it to actually clean your data.&lt;/p&gt;




&lt;h2&gt;
  
  
  First, What Even Is &lt;code&gt;regexp_replace&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;First things first, we need to understand what this function is and what it does.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;regexp_replace&lt;/code&gt; is a PostgreSQL function that finds a pattern inside a string and replaces whatever matches that pattern with something else you specify. The word &lt;strong&gt;regexp&lt;/strong&gt; is short for &lt;strong&gt;regular expression&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The syntax looks like this:&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="n"&gt;regexp_replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;source&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;replacement_string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;flag&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let us break each part down simply:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Part&lt;/th&gt;
&lt;th&gt;What It Means&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;source&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The string you are searching inside. In our case, this is the column &lt;code&gt;passenger_phone&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;pattern&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;What you are looking for : What do you want to find and remove or replace?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;replacement_string&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;What do you want to put in place of what you found? If you just want to delete it, you pass an empty string &lt;code&gt;''&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;flag&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Controls how the matching behaves. The two you will use most are &lt;code&gt;i&lt;/code&gt; for case-insensitive matching and &lt;code&gt;g&lt;/code&gt; for global, meaning it applies the replacement to everything it finds in the string, not just the first match&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Now that we have the hang of it, let us move on to building the pattern.&lt;/p&gt;




&lt;h2&gt;
  
  
  Understanding the Special Characters in Patterns
&lt;/h2&gt;

&lt;p&gt;A &lt;code&gt;pattern&lt;/code&gt; is basically a set of rules that tells PostgreSQL what to look for. It is like saying "find any character that is not a digit" or "find this pattern, but only at the beginning" etc. &lt;/p&gt;

&lt;p&gt;To write these rules/instructions, there are special characters that are used.&lt;/p&gt;

&lt;p&gt;Here are the ones we will use today:&lt;/p&gt;

&lt;h3&gt;
  
  
  The &lt;code&gt;^&lt;/code&gt; character
&lt;/h3&gt;

&lt;p&gt;Depending on where it is placed, this will do either of 2 things;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When &lt;code&gt;^&lt;/code&gt; is at the beginning of a pattern&lt;/strong&gt; it means: &lt;em&gt;start matching from the very beginning of the string.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;^'hello'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This means: only match &lt;code&gt;hello&lt;/code&gt; if it appears at the start of the string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;^[0-9]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This means: Only the &lt;strong&gt;first character&lt;/strong&gt; of a string is checked and matched if it is a digit. Anything after that is ignored.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When &lt;code&gt;^&lt;/code&gt; is inside square brackets &lt;code&gt;[ ]&lt;/code&gt;&lt;/strong&gt;, it means: &lt;em&gt;NOT these characters.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[^0-9]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This means: &lt;strong&gt;Every character&lt;/strong&gt; in the string is checked, any one that is not a digit gets matched.&lt;/p&gt;

&lt;p&gt;Same symbol, two different purposes depending on its position.&lt;/p&gt;




&lt;h3&gt;
  
  
  The &lt;code&gt;\&lt;/code&gt; character (backslash)
&lt;/h3&gt;

&lt;p&gt;The backslash is used to tell PostgreSQL: &lt;em&gt;treat the next character as a character, not as a special operator.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;For example, the &lt;code&gt;+&lt;/code&gt; sign in regular expressions has a special meaning (we will cover it in a moment). So if you want to find a plus sign &lt;code&gt;+&lt;/code&gt; in your data, like &lt;code&gt;+254&lt;/code&gt;, you write it as &lt;code&gt;\+&lt;/code&gt; to tell PostgreSQL &lt;em&gt;"I mean the actual plus sign, not the operator."&lt;/em&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Square brackets &lt;code&gt;[ ]&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Square brackets let you define a group of characters to match. For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;[0-9]&lt;/code&gt; means match any digit from 0 to 9&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[a-z]&lt;/code&gt; means match any lowercase letter&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[^0-9]&lt;/code&gt; means match anything that is NOT a digit (the &lt;code&gt;^&lt;/code&gt; inside means NOT, as we've mentioned above)&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  The &lt;code&gt;+&lt;/code&gt; operator
&lt;/h3&gt;

&lt;p&gt;When used outside brackets, &lt;code&gt;+&lt;/code&gt; means: &lt;em&gt;keep matching one or more of the preceding character or group.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;So &lt;code&gt;[0-9]+&lt;/code&gt; means: Match the digits as long as they are all next to each other with no other characters in between. (Without the + , it stops after the first match)&lt;/p&gt;




&lt;h3&gt;
  
  
  The &lt;code&gt;$&lt;/code&gt; character
&lt;/h3&gt;

&lt;p&gt;Just like &lt;code&gt;^&lt;/code&gt; marks the start of a string, &lt;code&gt;$&lt;/code&gt; marks the end. When you write &lt;code&gt;$&lt;/code&gt; at the end of your pattern, you are saying: &lt;em&gt;the string must end here, nothing else allowed after this.&lt;/em&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Putting them together: &lt;code&gt;^[0-9]+$&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Now that you know each piece, read this pattern again:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;^[0-9]+$
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;^&lt;/code&gt; → start from the beginning of the string&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[0-9]&lt;/code&gt; → the characters must be digits&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;+&lt;/code&gt; → keep going, there can be one or more of them&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;$&lt;/code&gt; → and this must be the end of the string, nothing else after&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All together: &lt;strong&gt;the entire string, from start to finish, must contain only digits.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is the pattern we will use to identify which phone numbers are already clean and which ones need fixing.&lt;/p&gt;




&lt;h2&gt;
  
  
  Our Problem: What Does the Messy Data Look Like?
&lt;/h2&gt;

&lt;p&gt;Let us say when we run this:&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;passenger_phone&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;dirty_safari_data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We get back a mix of things like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;+254712345678
254712345678
0712345678
0712-345-678
+254 712 345 678
(0) 712345678

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The goal is to standardize all of these into the local format starting with &lt;code&gt;0&lt;/code&gt;, containing only digits, like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0712345678
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two problems to solve:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Some numbers start with &lt;code&gt;+254&lt;/code&gt; or &lt;code&gt;254&lt;/code&gt; instead of &lt;code&gt;0&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Some numbers have extra characters like brackets, spaces, dashes, or plus signs&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Step 1: Replace &lt;code&gt;+254&lt;/code&gt; or &lt;code&gt;254&lt;/code&gt; at the Start With &lt;code&gt;0&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The first thing we want to do is find any phone number that starts with &lt;code&gt;+254&lt;/code&gt; or &lt;code&gt;254&lt;/code&gt; and swap that prefix out for &lt;code&gt;0&lt;/code&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;regexp_replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;passenger_phone&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'^(&lt;/span&gt;&lt;span class="se"&gt;\+&lt;/span&gt;&lt;span class="s1"&gt;254|254)'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'0'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'g'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;contacts&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let us read the pattern &lt;code&gt;^(\+254|254)&lt;/code&gt; carefully:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;^&lt;/code&gt; → look at the start of the string only&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;(&lt;/code&gt; &lt;code&gt;)&lt;/code&gt; → group what is inside together&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\+254&lt;/code&gt; → literally the characters &lt;code&gt;+254&lt;/code&gt; (the backslash makes &lt;code&gt;+&lt;/code&gt; a literal character)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;|&lt;/code&gt; → OR&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;254&lt;/code&gt; → literally the characters &lt;code&gt;254&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So the full pattern says: &lt;em&gt;at the start of the string, find either &lt;code&gt;+254&lt;/code&gt; or &lt;code&gt;254&lt;/code&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The replacement is &lt;code&gt;'0'&lt;/code&gt;, so wherever that prefix is found, replace it with &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The flag is &lt;code&gt;'g'&lt;/code&gt; for global.&lt;/p&gt;

&lt;p&gt;After this step, &lt;code&gt;+254712345678&lt;/code&gt; becomes &lt;code&gt;0712345678&lt;/code&gt; and &lt;code&gt;254712345678&lt;/code&gt; also becomes &lt;code&gt;0712345678&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 2: Remove Any Character That Is Not a Digit
&lt;/h2&gt;

&lt;p&gt;Some numbers still have spaces, dashes, or other characters sitting in them. The second &lt;code&gt;regexp_replace&lt;/code&gt; cleans all of that up.&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;regexp_replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;passenger_phone&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'[^0-9]'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'g'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;contacts&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The pattern &lt;code&gt;[^0-9]&lt;/code&gt; means: &lt;em&gt;any character that is NOT a digit.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The replacement is &lt;code&gt;''&lt;/code&gt; (an empty string), so we are just deleting those characters.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;'g'&lt;/code&gt; flag means do this for every non-digit character found in the string, not just the first one.&lt;/p&gt;

&lt;p&gt;So &lt;code&gt;0712-345-678&lt;/code&gt; becomes &lt;code&gt;0712345678&lt;/code&gt; and &lt;code&gt;0712 345 678&lt;/code&gt; also becomes &lt;code&gt;0712345678&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 3: The &lt;code&gt;WHERE&lt;/code&gt; Clause — Only Update What Needs Fixing
&lt;/h2&gt;

&lt;p&gt;We do not want to touch phone numbers that are already clean. That is where our &lt;code&gt;^[0-9]+$&lt;/code&gt; pattern comes in handy. We use it in the &lt;code&gt;WHERE&lt;/code&gt; clause to filter for records where the phone number is &lt;strong&gt;not&lt;/strong&gt; already a clean string of digits.&lt;/p&gt;

&lt;p&gt;You might be tempted to write it like this:&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;WHERE&lt;/span&gt; &lt;span class="n"&gt;passenger_phone&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s1"&gt;'^[0-9]+$'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But this is not correct. The &lt;code&gt;!=&lt;/code&gt; operator just compares strings literally, so this would check if the phone number is literally equal to the text &lt;code&gt;^[0-9]+$&lt;/code&gt;, which is not what we want.&lt;/p&gt;

&lt;p&gt;In PostgreSQL, to check whether a string does NOT match a regular expression, you use the &lt;code&gt;!~&lt;/code&gt; operator. The corrected version should 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;WHERE&lt;/span&gt; &lt;span class="n"&gt;passenger_phone&lt;/span&gt; &lt;span class="o"&gt;!~&lt;/span&gt; &lt;span class="s1"&gt;'^[0-9]+$'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This now correctly says: &lt;em&gt;only return (or update) rows where the phone number does not match the pattern of being purely digits from start to finish.&lt;/em&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Putting It All Together
&lt;/h2&gt;

&lt;h3&gt;
  
  
  The SELECT query (to preview your cleaned data before changing anything)
&lt;/h3&gt;

&lt;p&gt;Always preview before you update. This is a good habit.&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;passenger_phone&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;original&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;regexp_replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;regexp_replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;passenger_phone&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'^(&lt;/span&gt;&lt;span class="se"&gt;\+&lt;/span&gt;&lt;span class="s1"&gt;254|254)'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'0'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'g'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="s1"&gt;'[^0-9]'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'g'&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;cleaned_phone&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;contacts&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;passenger_phone&lt;/span&gt; &lt;span class="o"&gt;!~&lt;/span&gt; &lt;span class="s1"&gt;'^[0-9]+$'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice how we nested one &lt;code&gt;regexp_replace&lt;/code&gt; inside another. The inner one runs first (fixing the prefix), and then the outer one runs on the result (removing non-digits). It is like running two cleaning steps in one go.&lt;/p&gt;




&lt;h3&gt;
  
  
  The UPDATE query (to actually apply the changes)
&lt;/h3&gt;

&lt;p&gt;Once you are happy with the preview, run the update:&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;UPDATE&lt;/span&gt; &lt;span class="n"&gt;contacts&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;passenger_phone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;regexp_replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;regexp_replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;passenger_phone&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'^(&lt;/span&gt;&lt;span class="se"&gt;\+&lt;/span&gt;&lt;span class="s1"&gt;254|254)'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'0'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'g'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="s1"&gt;'[^0-9]'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'g'&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;passenger_phone&lt;/span&gt; &lt;span class="o"&gt;!~&lt;/span&gt; &lt;span class="s1"&gt;'^[0-9]+$'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This updates only the rows that have messy phone numbers, leaving the already clean ones untouched.&lt;/p&gt;




&lt;h2&gt;
  
  
  Quick Recap
&lt;/h2&gt;

&lt;p&gt;Here is a summary of everything covered:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Pattern&lt;/th&gt;
&lt;th&gt;What It Does&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;^&lt;/code&gt; at the start of a pattern&lt;/td&gt;
&lt;td&gt;Anchors the match to the start of the string&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;^&lt;/code&gt; inside &lt;code&gt;[ ]&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Means NOT — match anything except what follows&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;\+&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Treat &lt;code&gt;+&lt;/code&gt; as a literal plus sign, not an operator&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;[0-9]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Match any digit from 0 to 9&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;[^0-9]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Match any character that is NOT a digit&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;+&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;One or more of the preceding character/group&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;$&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Anchors the match to the end of the string&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;^[0-9]+$&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The entire string must be digits only, nothing else&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;g&lt;/code&gt; flag&lt;/td&gt;
&lt;td&gt;Apply the replacement globally (every match, not just the first)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Regular expressions can look intimidating and confusing at first, but once you understand what each character means and why it is there, they start making a lot of sense. The key takeaway here is to build your pattern step by step, rather than trying to write the whole thing at once.&lt;/p&gt;

&lt;p&gt;Also, always test with a &lt;code&gt;SELECT&lt;/code&gt; before running an &lt;code&gt;UPDATE&lt;/code&gt;. You can never be too careful when modifying data directly in a table.&lt;/p&gt;

</description>
      <category>sql</category>
      <category>postgres</category>
      <category>beginners</category>
    </item>
    <item>
      <title>SQL Subqueries vs. CTEs: A Guide to Writing Better Queries</title>
      <dc:creator>Sharon-nyabuto</dc:creator>
      <pubDate>Tue, 21 Apr 2026 19:54:31 +0000</pubDate>
      <link>https://dev.to/sharonnyabuto/sql-subqueries-vs-ctes-a-guide-to-writing-better-queries-1f44</link>
      <guid>https://dev.to/sharonnyabuto/sql-subqueries-vs-ctes-a-guide-to-writing-better-queries-1f44</guid>
      <description>&lt;p&gt;When you first learn SQL, queries are usually straightforward: you &lt;code&gt;SELECT&lt;/code&gt; some columns from a table and apply a &lt;code&gt;WHERE&lt;/code&gt; filter to narrow down the results.&lt;/p&gt;

&lt;p&gt;As your data questions become more complex, however, you realize sometimes you need the answer to one question before you can answer another. &lt;br&gt;
For example: &lt;em&gt;"Which employees earn above the company average?"&lt;/em&gt;, requires calculating the average salary first, then use the result to filter employees.&lt;/p&gt;

&lt;p&gt;To solve this, SQL provides two incredibly powerful tools: &lt;strong&gt;Subqueries&lt;/strong&gt; and &lt;strong&gt;Common Table Expressions (CTEs)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This guide will break down these tools and cover:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Part 1: Subqueries&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What subqueries are and how they work&lt;/li&gt;
&lt;li&gt;The different types of subqueries&lt;/li&gt;
&lt;li&gt;When to use subqueries&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Part 2: CTEs (Common Table Expressions)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What CTEs are and why they matter&lt;/li&gt;
&lt;li&gt;The different types of CTEs&lt;/li&gt;
&lt;li&gt;When to use CTEs&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Part 3: A clear comparison of Subqueries vs. CTEs&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;h3&gt; PART 1: SUBQUERIES&lt;a&gt;&lt;/a&gt;  &lt;/h3&gt;

&lt;p&gt;Also known as an inner query or a nested query, a subquery is a query embedded within another SQL query.&lt;/p&gt;

&lt;p&gt;Subqueries are typically found nested inside standard SQL clauses such as &lt;code&gt;SELECT&lt;/code&gt;, &lt;code&gt;FROM&lt;/code&gt;, &lt;code&gt;WHERE&lt;/code&gt;, or &lt;code&gt;HAVING&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;SQL follows a strict order of execution:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The innermost subquery is always executed first. Its result is then passed on to be used by the outer query (the containing query).&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Subqueries allow you to use a single query rather than multiple queries, making your code more flexible and dynamic. They are categorized either by their output or by execution.&lt;/p&gt;

&lt;h4&gt; Subqueries Categorized by Output &lt;/h4&gt;

&lt;p&gt;One way to understand subqueries is by looking at the result they return.&lt;br&gt;
A subquery can return a single value, a list of values, or an entire table, depending on the query.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;/em&gt;&lt;/p&gt;&lt;h5&gt;&lt;em&gt; Scalar Subquery&lt;/em&gt;&lt;/h5&gt;&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Returns exactly one value (one row, one column).&lt;/p&gt;

&lt;p&gt;Scalar subqueries are used with single value comparison operators; &lt;strong&gt;=, &amp;lt;, &amp;gt;, &amp;lt;= &amp;amp; &amp;gt;=&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For example: &lt;em&gt;"Which students scored above the class average on the math test?"&lt;/em&gt; requires a &lt;em&gt;scalar subquery&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;(Assuming the class average is 80.)&lt;/em&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;student_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;score&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;score&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;AVG&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;score&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
               &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;student_name&lt;/th&gt;
&lt;th&gt;score&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Awino Judy&lt;/td&gt;
&lt;td&gt;95&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Wanjiku   Stacy&lt;/td&gt;
&lt;td&gt;88&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Kiprop  Bryan&lt;/td&gt;
&lt;td&gt;82&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Note: The scalar subquery &lt;code&gt;SELECT AVG(score) FROM students&lt;/code&gt; &lt;strong&gt;returns a single value&lt;/strong&gt; (80), then the outer query uses this value to filter students who scored above the class average&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;&lt;/em&gt;&lt;/p&gt;&lt;h5&gt;&lt;em&gt;Multi-row Subquery (List Subquery)&lt;/em&gt;&lt;/h5&gt;&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Returns a column of values, i.e. multiple rows but one column.&lt;br&gt;
It is often used with &lt;code&gt;IN&lt;/code&gt; and &lt;code&gt;NOT IN&lt;/code&gt; operators to filter results against a list of values.&lt;/p&gt;

&lt;p&gt;For example: "Which students registered for the math exam?" requires a &lt;em&gt;multi-row subquery&lt;/em&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;student_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;student_name&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;student_id&lt;/span&gt; 
&lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;student_id&lt;/span&gt; 
  &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;exam_registrations&lt;/span&gt; 
  &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;exam_subject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Math'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;student_id&lt;/th&gt;
&lt;th&gt;student_name&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Wanjiku Stacy&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Kiprop  Bryan&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Awino  Judy&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;Mutua  Henry&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;The list subquery first generates a list of student IDs from the exam_registrations table, then the outer query uses &lt;code&gt;IN&lt;/code&gt; to filter the students table against that list.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;&lt;/em&gt;&lt;/p&gt;&lt;h5&gt;&lt;em&gt;Table Subquery (Derived Table)&lt;/em&gt;&lt;/h5&gt;&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Returns multiple rows and columns, acting like a temporary table.&lt;br&gt;
This subquery is used in the &lt;code&gt;FROM&lt;/code&gt; clause, and must always have an alias.&lt;/p&gt;

&lt;p&gt;For example: &lt;em&gt;"Which students scored more than 160 total points in all 3 math tests?"&lt;/em&gt;  requires a  &lt;em&gt;table subquery&lt;/em&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;student_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;total_score&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; 
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;student_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
&lt;span class="k"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;score&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;total_score&lt;/span&gt;
  &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;exam_results&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;student_name&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;student_totals&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;total_score&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;160&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;student_name&lt;/th&gt;
&lt;th&gt;total_score&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Awino   Nancy&lt;/td&gt;
&lt;td&gt;185&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Wanjiku Stacy&lt;/td&gt;
&lt;td&gt;170&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Kiprop  Bryan&lt;/td&gt;
&lt;td&gt;165&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;The table subquery ran first to calculate everyone's total score, creating a temporary &lt;strong&gt;virtual table&lt;/strong&gt; which we aliased &lt;strong&gt;(student_totals)&lt;/strong&gt;. The outer query used this table to filter the results to get the students with more than 160 marks.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h4&gt; Subqueries Categorized by Execution &lt;/h4&gt;

&lt;p&gt;Subqueries can also be categorized based on how they work with the rest of the query. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;/em&gt;&lt;/p&gt;&lt;h4&gt;&lt;em&gt;Correlated Subquery&lt;/em&gt;&lt;/h4&gt;&lt;p&gt;&lt;/p&gt;

&lt;p&gt;A correlated subquery is &lt;strong&gt;dependent&lt;/strong&gt; on data from the outer query. &lt;br&gt;
It runs for every row in the outer query, using values from the outer query to filter its result.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;/em&gt;&lt;/p&gt;&lt;h4&gt;&lt;em&gt;Uncorrelated (Independent) Subquery&lt;/em&gt;&lt;/h4&gt;&lt;p&gt;&lt;/p&gt;

&lt;p&gt;An uncorrelated subquery runs once, and is fully &lt;strong&gt;independent&lt;/strong&gt; of the outer query. &lt;br&gt;
This means that it evaluates its logic exactly &lt;strong&gt;once&lt;/strong&gt;, gets its result, and simply hands that result over to the main query to complete the task.&lt;/p&gt;

&lt;p&gt;If you're ever uncertain as to whether a subquery is correlated (dependent) or uncorrelated (independent), you can use the &lt;strong&gt;&lt;em&gt;highlight test&lt;/em&gt;&lt;/strong&gt; on your SQL editor.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Highlight &lt;strong&gt;just&lt;/strong&gt; the subquery and run it:&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If it returns a result: It is an uncorrelated subquery. It can stand on its own.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If it throws an error: It is a correlated subquery. It depends on information from the outer query to make sense and run correctly.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;When to use Subqueries&lt;/h3&gt;

&lt;p&gt;Subqueries are useful when; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Calculating metrics like averages or totals on the fly, without hardcoding numbers.&lt;/li&gt;
&lt;li&gt;Filtering results using a dynamically generated list of values i.e. (List subqueries).&lt;/li&gt;
&lt;li&gt;Using row-specific logic&lt;/li&gt;
&lt;li&gt;There's need to aggregate data before filtering.&lt;/li&gt;
&lt;li&gt;Working with older legacy databases that don't support more modern features.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Subqueries are excellent for simple logic, but when they are deeply nested, they become difficult to read and more difficult to maintain. This is where Common Table Expressions (CTEs) come in.&lt;/p&gt;




&lt;h3&gt; PART 2: The CTE (COMMON TABLE EXPRESSION)&lt;a&gt;&lt;/a&gt; &lt;/h3&gt;

&lt;p&gt;A CTE is a temporary, named result set defined at the top of a query using the &lt;code&gt;WITH&lt;/code&gt; clause, that exists only for the duration of your query.&lt;/p&gt;

&lt;p&gt;Once it is defined, you can query and reference the CTE as you would a normal table in your database.&lt;/p&gt;

&lt;p&gt;Because a CTE is defined at the top of your script, it allows you to read the code logically from &lt;strong&gt;top-to-bottom&lt;/strong&gt; &lt;/p&gt;

&lt;h5&gt;The Basic Syntax of a CTE &lt;/h5&gt; 

&lt;p&gt;No matter how complex a query gets, almost every CTE follows this simple, two-part structure:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Define&lt;/strong&gt; the CTE using the &lt;code&gt;WITH&lt;/code&gt; clause at the top.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use&lt;/strong&gt; the CTE in your main query. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Syntax;&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;WITH&lt;/span&gt; &lt;span class="n"&gt;cte_name&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="c1"&gt;-- Step 1: Write your query here. &lt;/span&gt;
    &lt;span class="c1"&gt;-- This creates your temporary, virtual table.&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;column1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;column2&lt;/span&gt;
    &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;table1&lt;/span&gt;
    &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;condition&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;-- Step 2: Write your main query here, reading from the CTE!&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;column1&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;cte_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we know the basic syntax, we can dive into the two types of CTEs commonly encountered: Recursive and Non-Recursive &lt;em&gt;(based on whether or not they reference themselves).&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Non-Recursive CTEs&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
They are temporary result sets that &lt;em&gt;&lt;strong&gt;run once and don’t reference themselves&lt;/strong&gt;&lt;/em&gt;. &lt;br&gt;
They’re commonly used for breaking complex queries into modular blocks.&lt;/p&gt;

&lt;p&gt;The CTE syntax 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;WITH&lt;/span&gt; &lt;span class="n"&gt;cte_name&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="c1"&gt;-- Your SQL logic here (SELECT, JOIN, GROUP BY, etc.)&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;cte_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Use Case:&lt;/strong&gt; Replacing nested subqueries for better readability and reusability.&lt;/p&gt;

&lt;p&gt;Earlier when we wanted to find &lt;em&gt;students who scored a total of more than 160 points across all their maths tests&lt;/em&gt;, We solved it using a &lt;em&gt;Table Subquery&lt;/em&gt; in the &lt;code&gt;FROM&lt;/code&gt; clause.&lt;/p&gt;

&lt;p&gt;Let's try the exact same question, using a CTE.&lt;br&gt;
&lt;em&gt;"Which students scored more than 160 total points in all 3 math tests?"&lt;/em&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;WITH&lt;/span&gt; &lt;span class="n"&gt;student_totals&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;student_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;score&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;total_score&lt;/span&gt;
  &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;exam_scores&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;student_name&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;student_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;total_score&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;student_totals&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;total_score&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;160&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;student_name&lt;/th&gt;
&lt;th&gt;total_score&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Awino  Nancy&lt;/td&gt;
&lt;td&gt;185&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Wanjiku Stacy&lt;/td&gt;
&lt;td&gt;170&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Kiprop  Bryan&lt;/td&gt;
&lt;td&gt;165&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;This CTE achieves the same result as our Table Subquery Example, in a cleaner, more readable way.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Recursive CTEs&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Recursive CTEs are temporary result sets that reference themselves to explore and map out hierarchical (tree-like) data (e.g., organizational charts, category trees).&lt;br&gt;
 Syntax;&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;WITH&lt;/span&gt; &lt;span class="k"&gt;RECURSIVE&lt;/span&gt; &lt;span class="n"&gt;cte_name&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="c1"&gt;-- Base case: Start with the root of the hierarchy&lt;/span&gt;
  &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;column1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;column2&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="k"&gt;table&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;parent_id&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;
  &lt;span class="k"&gt;UNION&lt;/span&gt; &lt;span class="k"&gt;ALL&lt;/span&gt;
  &lt;span class="c1"&gt;-- Recursive case: Reference the CTE itself to traverse child rows&lt;/span&gt;
  &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;column1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;column2&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="k"&gt;table&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;
  &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;cte_name&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;parent_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cte_name&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;cte_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Use Case:&lt;/strong&gt; Exploring hierarchical or tree structured data (e.g., reporting structures or category hierarchies).&lt;/p&gt;

&lt;p&gt;For example: &lt;em&gt;"Alice is a manager at a Nairobi tech startup. Who are the employees in her team hierarchy?"&lt;/em&gt; requires a recursive CTE.&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;WITH&lt;/span&gt; &lt;span class="k"&gt;RECURSIVE&lt;/span&gt; &lt;span class="n"&gt;team_hierarchy&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;employee_id&lt;/span&gt;&lt;span class="p"&gt;,&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;manager_id&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Alice'&lt;/span&gt;
  &lt;span class="k"&gt;UNION&lt;/span&gt; &lt;span class="k"&gt;ALL&lt;/span&gt;
  &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;employee_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&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;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;manager_id&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;
  &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;team_hierarchy&lt;/span&gt; &lt;span class="n"&gt;th&lt;/span&gt; 
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;manager_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;th&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;employee_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;team_hierarchy&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;employee_id&lt;/th&gt;
&lt;th&gt;name&lt;/th&gt;
&lt;th&gt;manager_id&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Alice&lt;/td&gt;
&lt;td&gt;NULL&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Bob&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Charlie&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;Diana&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;Eve&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;This result shows Alice, her direct reports (Bob, Charlie), and Bob’s direct reports (Diana, Eve)—the full team hierarchy.&lt;/em&gt;&lt;br&gt;
&lt;em&gt;The line &lt;code&gt;JOIN team_hierarchy th ON e.manager_id = th.employee_id&lt;/code&gt; is where the recursion happens.&lt;/em&gt; &lt;br&gt;
&lt;em&gt;&lt;code&gt;team_hierarchy&lt;/code&gt; is the CTE itself, we are joining the query to its own result. It takes every employee already in the CTE (e.g., Alice) and finds all employees who report to them. This repeats in a loop until no more reports are found&lt;/em&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;When to use CTEs&lt;/h3&gt;

&lt;p&gt;Subqueries are useful when; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A query has multiple logical steps&lt;/li&gt;
&lt;li&gt;You need to use the same data multiple times&lt;/li&gt;
&lt;li&gt;Querying hierarchical data&lt;/li&gt;
&lt;li&gt;You temporarily want to replace a view.&lt;/li&gt;
&lt;li&gt;When replacing nested subqueries.&lt;/li&gt;
&lt;/ul&gt;





&lt;h3&gt;PART 3: SUBQUERIES VS CTEs&lt;a&gt;&lt;/a&gt; &lt;/h3&gt;

&lt;p&gt;Here is a table comparison of subqueries and CTEs, based on architecture, readability and performance.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Subqueries&lt;/th&gt;
&lt;th&gt;CTEs&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Readability &amp;amp; Flow&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Reads &lt;strong&gt;inside-out&lt;/strong&gt;. Prone to creating "spaghetti code" if nested multiple times.&lt;/td&gt;
&lt;td&gt;Reads &lt;strong&gt;top-to-bottom&lt;/strong&gt;. Keeps code clean and highly modular.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Reusability&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;Single-use only.&lt;/strong&gt; If you need the same data twice, you have to type the whole subquery again.&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;Highly reusable.&lt;/strong&gt; Can be referenced multiple times in the main query&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Recursion&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Cannot handle self-referencing or hierarchical data.&lt;/td&gt;
&lt;td&gt;Perfect for hierarchical/tree-structured data using &lt;strong&gt;Recursive CTEs&lt;/strong&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Performance&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Fast for basic filters. &lt;strong&gt;Correlated Subqueries&lt;/strong&gt; can be slow on large datasets because they loop row-by-row.&lt;/td&gt;
&lt;td&gt;Generally the &lt;strong&gt;same performance&lt;/strong&gt; (optimizers treat standard CTEs and subqueries equally). However, CTEs can be temporarily cached in memory in databases like PostgreSQL for speed boosts.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Best Use Case&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Quick, single-step math (Scalar), checking lists (&lt;code&gt;IN&lt;/code&gt; / &lt;code&gt;EXISTS&lt;/code&gt;), or creating simple derived tables.&lt;/td&gt;
&lt;td&gt;Multi-step data transformations, chaining queries, building org charts, or acting as temporary views.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;





&lt;h3&gt;&lt;/h3&gt;
&lt;br&gt;
SQL is not just about writing queries, it’s about writing queries that others can easily understand.

&lt;p&gt;When in doubt, choose the option that is easiest for the next developer to understand. &lt;/p&gt;

&lt;p&gt;If you only remember one thing from this guide:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;strong&gt;Subqueries&lt;/strong&gt; for quick, single-step tasks and simple filters.&lt;/li&gt;
&lt;li&gt;Use &lt;strong&gt;CTEs&lt;/strong&gt; for multi-step queries, reusability, and keeping your code readable from top to bottom.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;SQL is all about practice. Don’t get discouraged if subqueries and CTEs take a few tries to master, with consistent practice, they get and feel more intuitive.&lt;/p&gt;

&lt;p&gt;If you found this guide helpful, don't forget to &lt;strong&gt;save it for later&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Do you prefer using subqueries or CTEs in your work?&lt;/em&gt;&lt;br&gt;
Let me know in the comments below. Happy querying! &lt;/p&gt;

</description>
      <category>sql</category>
      <category>learning</category>
    </item>
    <item>
      <title>📘SQL JOINs Explained Simply - A Beginner's Guide</title>
      <dc:creator>Sharon-nyabuto</dc:creator>
      <pubDate>Sun, 19 Apr 2026 03:47:31 +0000</pubDate>
      <link>https://dev.to/sharonnyabuto/sql-joins-explained-simply-a-beginners-guide-5edg</link>
      <guid>https://dev.to/sharonnyabuto/sql-joins-explained-simply-a-beginners-guide-5edg</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;SQL JOINs are used to &lt;strong&gt;combine data from two or more tables into a single result based on a shared column&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;They allow you to retrieve related data that is stored separately. &lt;br&gt;
For example, a database might have &lt;em&gt;customer names&lt;/em&gt; in one table and &lt;em&gt;purchase details&lt;/em&gt; in another. &lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;Join&lt;/strong&gt; bridges the tables, giving insights that one table alone may not provide.&lt;/p&gt;

&lt;p&gt;In this article, we’ll break down the different types of SQL JOINs and when to use them. Specifically, we’ll cover:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;INNER JOIN&lt;/li&gt;
&lt;li&gt;LEFT JOIN&lt;/li&gt;
&lt;li&gt;RIGHT JOIN&lt;/li&gt;
&lt;li&gt;FULL OUTER JOIN&lt;/li&gt;
&lt;li&gt;CROSS JOIN&lt;/li&gt;
&lt;li&gt;SELF JOIN&lt;/li&gt;
&lt;/ol&gt;


&lt;h2&gt;
  
  
  🔗 INNER JOIN
&lt;/h2&gt;

&lt;p&gt;An &lt;code&gt;INNER JOIN&lt;/code&gt; combines records based on a related column and returns &lt;strong&gt;only matching rows&lt;/strong&gt; from both tables. &lt;/p&gt;

&lt;p&gt;Syntax&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;SELECT&lt;/strong&gt; columns&lt;br&gt;
&lt;strong&gt;FROM&lt;/strong&gt; table1&lt;br&gt;
&lt;strong&gt;INNER JOIN&lt;/strong&gt; table2&lt;br&gt;
&lt;strong&gt;ON&lt;/strong&gt; table1.column = table2.column;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Example; If the &lt;em&gt;customers table&lt;/em&gt; and the &lt;em&gt;orders table&lt;/em&gt; share a column - the &lt;em&gt;customer_id,&lt;/em&gt; an &lt;code&gt;INNER JOIN&lt;/code&gt; is used to identify the customers who made orders;&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;customers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&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;order_id&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt;
&lt;span class="k"&gt;INNER&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;customer_id&lt;/th&gt;
&lt;th&gt;first_name&lt;/th&gt;
&lt;th&gt;last_name&lt;/th&gt;
&lt;th&gt;order_id&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;john&lt;/td&gt;
&lt;td&gt;doe&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;mary&lt;/td&gt;
&lt;td&gt;wanjiku&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;peter&lt;/td&gt;
&lt;td&gt;otieno&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;john&lt;/td&gt;
&lt;td&gt;doe&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;lucy&lt;/td&gt;
&lt;td&gt;njeri&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;An &lt;code&gt;INNER JOIN&lt;/code&gt; can be used to combine more than two tables. &lt;/p&gt;

&lt;p&gt;For example, to find out the customers, books they purchased and the corresponding order IDs;&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;orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;order_id&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;title&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt;
&lt;span class="k"&gt;INNER&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; 
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt;
&lt;span class="k"&gt;INNER&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;books&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; 
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;books&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;book_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;book_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;order_id&lt;/th&gt;
&lt;th&gt;first_name&lt;/th&gt;
&lt;th&gt;last_name&lt;/th&gt;
&lt;th&gt;title&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;john&lt;/td&gt;
&lt;td&gt;doe&lt;/td&gt;
&lt;td&gt;Learning SQL&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;mary&lt;/td&gt;
&lt;td&gt;wanjiku&lt;/td&gt;
&lt;td&gt;Data Analytics Basics&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;peter&lt;/td&gt;
&lt;td&gt;otieno&lt;/td&gt;
&lt;td&gt;Python for Data Science&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;john&lt;/td&gt;
&lt;td&gt;doe&lt;/td&gt;
&lt;td&gt;Data Analytics Basics&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;lucy&lt;/td&gt;
&lt;td&gt;njeri&lt;/td&gt;
&lt;td&gt;Advanced SQL Queries&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  ⬅️ LEFT JOIN/LEFT OUTER JOIN
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;LEFT JOIN&lt;/code&gt; returns &lt;strong&gt;all records from the left&lt;/strong&gt; table, and the &lt;strong&gt;matching records from the right&lt;/strong&gt; table. Where no match exists, a &lt;strong&gt;NULL&lt;/strong&gt; value is returned.&lt;/p&gt;

&lt;p&gt;In a &lt;code&gt;LEFT JOIN&lt;/code&gt;, the table specified in the &lt;code&gt;FROM&lt;/code&gt; clause is treated as the left table.&lt;/p&gt;

&lt;p&gt;Syntax&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;SELECT&lt;/strong&gt; columns&lt;br&gt;
&lt;strong&gt;FROM&lt;/strong&gt; table1&lt;br&gt;
&lt;strong&gt;LEFT JOIN&lt;/strong&gt; table2&lt;br&gt;
&lt;strong&gt;ON&lt;/strong&gt; table1.column = table2.column;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To show all the books and the customers who ordered them, a &lt;code&gt;LEFT JOIN&lt;/code&gt; is used;&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;title&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;order_id&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;books&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;orders&lt;/span&gt; 
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;books&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;book_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;book_id&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;customers&lt;/span&gt; 
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;title&lt;/th&gt;
&lt;th&gt;first_name&lt;/th&gt;
&lt;th&gt;order_id&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Learning SQL&lt;/td&gt;
&lt;td&gt;john&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Data Analytics Basics&lt;/td&gt;
&lt;td&gt;mary&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Python for Data Science&lt;/td&gt;
&lt;td&gt;peter&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Data Analytics Basics&lt;/td&gt;
&lt;td&gt;john&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Advanced SQL Queries&lt;/td&gt;
&lt;td&gt;lucy&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Machine Learning Intro&lt;/td&gt;
&lt;td&gt;NULL&lt;/td&gt;
&lt;td&gt;NULL&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Notice that the row with &lt;strong&gt;Machine Learning Intro&lt;/strong&gt; has &lt;code&gt;NULL&lt;/code&gt; values because it was not ordered.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  ➡️ RIGHT JOIN/RIGHT OUTER JOIN
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;RIGHT JOIN&lt;/code&gt; works like the &lt;code&gt;LEFT JOIN&lt;/code&gt;, but it keeps &lt;strong&gt;all the rows from the right&lt;/strong&gt; table and &lt;strong&gt;matching rows from the left&lt;/strong&gt; table.&lt;/p&gt;

&lt;p&gt;Syntax;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;SELECT&lt;/strong&gt; columns&lt;br&gt;
&lt;strong&gt;FROM&lt;/strong&gt; table1&lt;br&gt;
&lt;strong&gt;RIGHT JOIN&lt;/strong&gt; table2&lt;br&gt;
&lt;strong&gt;ON&lt;/strong&gt; table1.column = table2.column;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To show every book in the library, whether it was ordered or not;&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;books&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;book_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;order_id&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;orders&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;books&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;books&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;book_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;book_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;book_id&lt;/th&gt;
&lt;th&gt;title&lt;/th&gt;
&lt;th&gt;order_id&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Learning SQL&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Data Analytics Basics&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Python for Data Science&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Data Analytics Basics&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;Advanced SQL Queries&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;Machine Learning Intro&lt;/td&gt;
&lt;td&gt;NULL&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;⚠️ &lt;strong&gt;Note:&lt;/strong&gt; For both LEFT and RIGHT outer joins, the order in which you write your tables determines which one returns all of its values. You need to be keen about which table you put on the left and which goes on the right to get your intended result.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  🔄 FULL JOIN/FULL OUTER JOIN
&lt;/h2&gt;

&lt;p&gt;A &lt;code&gt;FULL JOIN&lt;/code&gt; returns all rows from both tables.&lt;br&gt;
As is the case in both the right and Left outer joins, for the rows that do not have a match, &lt;code&gt;NULL&lt;/code&gt; is returned.&lt;/p&gt;

&lt;p&gt;Syntax;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;SELECT&lt;/strong&gt; columns&lt;br&gt;
&lt;strong&gt;FROM&lt;/strong&gt; table1&lt;br&gt;
&lt;strong&gt;FULL OUTER JOIN&lt;/strong&gt; table2&lt;br&gt;
&lt;strong&gt;ON&lt;/strong&gt; table1.column = table2.column;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To retrieve all the customers and all the books, whether ordered or not, a &lt;code&gt;FULL JOIN&lt;/code&gt; will be used;&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;order_id&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;title&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; 
&lt;span class="k"&gt;FULL&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt;  
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt; 
&lt;span class="k"&gt;FULL&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;books&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;book_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;books&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;book_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;order_id&lt;/th&gt;
&lt;th&gt;first_name&lt;/th&gt;
&lt;th&gt;last_name&lt;/th&gt;
&lt;th&gt;title&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;john&lt;/td&gt;
&lt;td&gt;doe&lt;/td&gt;
&lt;td&gt;Learning SQL&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;peter&lt;/td&gt;
&lt;td&gt;otieno&lt;/td&gt;
&lt;td&gt;Python for Data Science&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;lucy&lt;/td&gt;
&lt;td&gt;njeri&lt;/td&gt;
&lt;td&gt;Advanced SQL Queries&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;mary&lt;/td&gt;
&lt;td&gt;wanjiku&lt;/td&gt;
&lt;td&gt;Data Analytics Basics&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;NULL&lt;/td&gt;
&lt;td&gt;david&lt;/td&gt;
&lt;td&gt;kimani&lt;/td&gt;
&lt;td&gt;NULL&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;NULL&lt;/td&gt;
&lt;td&gt;NULL&lt;/td&gt;
&lt;td&gt;NULL&lt;/td&gt;
&lt;td&gt;Machine Learning Intro&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;To retrieve only the matched or un-matched records, you can filter with the &lt;code&gt;WHERE&lt;/code&gt; clause.&lt;/p&gt;




&lt;h2&gt;
  
  
  ❌ CROSS JOIN
&lt;/h2&gt;

&lt;p&gt;A &lt;code&gt;CROSS JOIN&lt;/code&gt; returns the combination of each row from one table with each row from another table. &lt;/p&gt;

&lt;p&gt;Syntax;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;SELECT&lt;/strong&gt; *&lt;br&gt;
&lt;strong&gt;FROM&lt;/strong&gt; table1&lt;br&gt;
&lt;strong&gt;CROSS JOIN&lt;/strong&gt; table2;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A &lt;code&gt;CROSS JOIN&lt;/code&gt; does not have an &lt;code&gt;ON&lt;/code&gt; clause, because we are not looking for matching rows.&lt;/p&gt;

&lt;p&gt;If you have 2 tables; t-shirts color and t-shirt size tables, and wanted to find all possible combinations, a &lt;code&gt;CROSS JOIN&lt;/code&gt; is used.&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;tshirt_colors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;color_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
  &lt;span class="n"&gt;tshirt_sizes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size_name&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;tshirt_colors&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;tshirt_sizes&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;color_name&lt;/th&gt;
&lt;th&gt;size_name&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Black&lt;/td&gt;
&lt;td&gt;Small&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Black&lt;/td&gt;
&lt;td&gt;Medium&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Black&lt;/td&gt;
&lt;td&gt;Large&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Black&lt;/td&gt;
&lt;td&gt;Extra Large&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;White&lt;/td&gt;
&lt;td&gt;Small&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;White&lt;/td&gt;
&lt;td&gt;Medium&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;White&lt;/td&gt;
&lt;td&gt;Large&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;White&lt;/td&gt;
&lt;td&gt;Extra Large&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Navy&lt;/td&gt;
&lt;td&gt;Small&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Navy&lt;/td&gt;
&lt;td&gt;Medium&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Navy&lt;/td&gt;
&lt;td&gt;Large&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Navy&lt;/td&gt;
&lt;td&gt;Extra Large&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  🔁 SELF JOIN
&lt;/h2&gt;

&lt;p&gt;A &lt;code&gt;SELF JOIN&lt;/code&gt; is used when comparing rows within the same table. &lt;/p&gt;

&lt;p&gt;When using self joins, it is essential to use &lt;strong&gt;Aliases&lt;/strong&gt;, to distinguish the two "roles" of the same table for exact execution. &lt;br&gt;
&lt;em&gt;AS&lt;/em&gt; is used to introduce an alias&lt;/p&gt;

&lt;p&gt;Syntax;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;SELECT&lt;/strong&gt; column name (s) as &lt;em&gt;alias&lt;/em&gt;&lt;br&gt;
&lt;strong&gt;FROM&lt;/strong&gt; employees A&lt;br&gt;
&lt;strong&gt;JOIN&lt;/strong&gt; employees B &lt;br&gt;
&lt;strong&gt;ON&lt;/strong&gt; A.column = B.column;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To best illustrate self joins, we will use the &lt;em&gt;employees&lt;/em&gt; table below;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;employee_id&lt;/th&gt;
&lt;th&gt;name&lt;/th&gt;
&lt;th&gt;department_id&lt;/th&gt;
&lt;th&gt;manager_id&lt;/th&gt;
&lt;th&gt;salary&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Alice&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;NULL&lt;/td&gt;
&lt;td&gt;50000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Bob&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;45000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Charlie&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;47000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;Diana&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;NULL&lt;/td&gt;
&lt;td&gt;60000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;Eve&lt;/td&gt;
&lt;td&gt;NULL&lt;/td&gt;
&lt;td&gt;NULL&lt;/td&gt;
&lt;td&gt;40000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;Brian&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;50000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;Joy&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;35000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;Luke&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;45000&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;To show employees and their managers, we use a &lt;code&gt;SELF JOIN&lt;/code&gt; as follows;&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;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;employee&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;manager&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; 
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;manager_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;employee_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;employee&lt;/th&gt;
&lt;th&gt;manager&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Bob&lt;/td&gt;
&lt;td&gt;Alice&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Charlie&lt;/td&gt;
&lt;td&gt;Alice&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Brian&lt;/td&gt;
&lt;td&gt;Diana&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Joy&lt;/td&gt;
&lt;td&gt;Diana&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Luke&lt;/td&gt;
&lt;td&gt;Alice&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;p&gt;I hope this guide made joins less intimidating! &lt;br&gt;
If you found this beginner's guide helpful, &lt;strong&gt;save it for later&lt;/strong&gt; or &lt;strong&gt;share&lt;/strong&gt; it with a friend who's just starting their SQL journey!  Happy learning!!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Are there any joins I left out? Do you have any join tricks you want to share?&lt;/em&gt;&lt;br&gt;
Let me know in the comment section below, I’d love to hear from you!&lt;/p&gt;

</description>
      <category>sql</category>
      <category>learning</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Applying SQL Concepts: DDL, DML, Filtering, and Data Transformation</title>
      <dc:creator>Sharon-nyabuto</dc:creator>
      <pubDate>Sun, 12 Apr 2026 18:25:54 +0000</pubDate>
      <link>https://dev.to/sharonnyabuto/applying-sql-concepts-ddl-dml-filtering-and-data-transformation-30p7</link>
      <guid>https://dev.to/sharonnyabuto/applying-sql-concepts-ddl-dml-filtering-and-data-transformation-30p7</guid>
      <description>&lt;p&gt;&lt;strong&gt;Structured Query Language (SQL)&lt;/strong&gt; is the standard language used to interact with relational databases. &lt;br&gt;
SQL is a command type language. Whether you want to create, delete, update or read data, SQL provides commands to perform these operations. &lt;br&gt;
SQL commands are categorized based on their &lt;strong&gt;specific functionalities&lt;/strong&gt;, and allow for the creation, manipulation, retrieval and control of data and database structures.&lt;br&gt;
Among the command categories include; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Data Definition Language (DDL)&lt;/li&gt;
&lt;li&gt;Data Manipulation Language (DML)&lt;/li&gt;
&lt;li&gt;Data Query Language (DQL)&lt;/li&gt;
&lt;li&gt;Data Control Language (DCL)&lt;/li&gt;
&lt;li&gt;Transaction Control Language (TCL)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this article, we will look at Data Definition Language (DDL) and Data Manipulation Language (DML).&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;u&gt;DDL (DATA DEFINITION LANGUAGE)&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;Data Definition Language (DDL) is a set of commands used to &lt;strong&gt;define&lt;/strong&gt; and &lt;strong&gt;manage&lt;/strong&gt; the &lt;em&gt;structure&lt;/em&gt; of a database. &lt;br&gt;
It focuses on creating and modifying database objects such as tables, schemas, and indexes. &lt;br&gt;
DDL uses commands like &lt;strong&gt;&lt;em&gt;CREATE,&lt;/em&gt;&lt;/strong&gt; &lt;strong&gt;&lt;em&gt;ALTER,&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;DROP,&lt;/em&gt;&lt;/strong&gt; to determine how data is organized and stored within the database. &lt;br&gt;
These commands affect the &lt;strong&gt;structure&lt;/strong&gt; of the database and not the data itself. &lt;br&gt;
DDL operations are often automatically committed, therefore changes take effect immediately without needing a manual &lt;em&gt;COMMIT&lt;/em&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;u&gt;DML (DATA MANIPULATION LANGUAGE)&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;Data Manipulation Language (DDL) is a set of SQL commands used to &lt;strong&gt;manage&lt;/strong&gt; and &lt;strong&gt;manipulate&lt;/strong&gt; the &lt;em&gt;data _stored within database tables. &lt;br&gt;
Unlike DDL, which focuses on defining the structure of the database, DML is concerned with performing operations on the &lt;strong&gt;data&lt;/strong&gt; itself. &lt;br&gt;
Common DML commands include &lt;strong&gt;INSERT,&lt;/strong&gt;&lt;/em&gt; &lt;strong&gt;&lt;em&gt;UPDATE&lt;/em&gt;,&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;DELETE&lt;/em&gt;,&lt;/strong&gt; which are used to add new records, modify existing data, and remove records from a table respectively. &lt;br&gt;
In most database systems, DML changes are not automatically committed, meaning they can be rolled back if necessary before being finalized.&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;Below are some SQL commands and how they are used;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;CREATE&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
DDL command used to create an object in a database, e.g a table&lt;/p&gt;

&lt;p&gt;The syntax; &lt;br&gt;
CREATE TABLE table_name(&lt;br&gt;
column1 datatype constraints,&lt;br&gt;
column2 datatype constraints,&lt;br&gt;
column3 datatype constraints&lt;br&gt;
);&lt;br&gt;
For example, to create a schema in a database for a school, and add a table called &lt;em&gt;students&lt;/em&gt; in the Schema you use &lt;em&gt;CREATE&lt;/em&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;create&lt;/span&gt; &lt;span class="k"&gt;table&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;student_id&lt;/span&gt; &lt;span class="nb"&gt;INT&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;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;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;not&lt;/span&gt; &lt;span class="k"&gt;null&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="k"&gt;not&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="n"&gt;gender&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;1&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="n"&gt;date_of_birth&lt;/span&gt; &lt;span class="nb"&gt;DATE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
&lt;span class="k"&gt;class&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;10&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; 
&lt;span class="n"&gt;city&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="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2F2cnzby37arrc0hxvlm0l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F2cnzby37arrc0hxvlm0l.png" alt="Table _students_ Created" width="608" height="187"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;INSERT&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This command is used to add a new record in a database.&lt;/p&gt;

&lt;p&gt;The syntax;&lt;br&gt;
INSERT INTO table_name (column1, column2, column3)&lt;br&gt;
VALUES &lt;br&gt;
(value1, value2, value3),&lt;br&gt;
(value1, value2, value3);&lt;/p&gt;

&lt;p&gt;To add student details to the students table created above, you use &lt;em&gt;INSERT&lt;/em&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;insert&lt;/span&gt; &lt;span class="k"&gt;into&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;student_id&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;gender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;date_of_birth&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;city&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;'Amina'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'Wanjiku'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'F'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'2008-03-12'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'Form 3'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'Nairobi'&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;'Brian'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'Ochieng'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'M'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'2007-07-25'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'Form 4'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'Mombasa'&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;'Cynthia'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'Mutua'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'F'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'2008-11-05'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'Form 3'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'Kisumu'&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;'David'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'Kamau'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'M'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'2007-02-18'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'Form 4'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'Nairobi'&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;'Esther'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'Akinyi'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'F'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'2009-06-30'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'Form 2'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'Nakuru'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fl5oca738folscqftezfe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fl5oca738folscqftezfe.png" alt="_students_ table populated with student information" width="649" height="143"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;UPDATE&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
The update command is used to modify existing records in a database. &lt;/p&gt;

&lt;p&gt;The syntax;&lt;br&gt;
UPDATE table_name&lt;br&gt;
SET column1 = value1,&lt;br&gt;
WHERE condition;&lt;/p&gt;

&lt;p&gt;In the students table created, if student id number 2, moved to Nairobi from Mombasa, &lt;em&gt;UPDATE&lt;/em&gt; is used.&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;update&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt;
&lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="n"&gt;City&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Nairobi'&lt;/span&gt;
&lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;student_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fv7b6tqzq5dhlhtegy1cf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fv7b6tqzq5dhlhtegy1cf.png" alt="Result after city update" width="612" height="173"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;DELETE&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This deletes one or more records from a database object. &lt;br&gt;
It is used together with the _WHERE _clause to ensure that only specific rows are deleted.&lt;/p&gt;

&lt;p&gt;The syntax;&lt;/p&gt;

&lt;p&gt;DELETE FROM table_name&lt;br&gt;
WHERE condition;&lt;/p&gt;

&lt;p&gt;In the students table, if student_id 4 is no longer a student there, his records are removed using &lt;em&gt;DELETE&lt;/em&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;delete&lt;/span&gt; 
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt;
 &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;student_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Ftbq8aw39rovam6uyq494.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Ftbq8aw39rovam6uyq494.png" alt="_students_ table with one record removed" width="613" height="140"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;u&gt;FILTERING WITH WHERE&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;WHERE&lt;/strong&gt; clause filters rows based on one or more conditions, so your query only modifies the records that match. &lt;br&gt;
It is often used across &lt;strong&gt;SELECT,&lt;/strong&gt; &lt;strong&gt;UPDATE,&lt;/strong&gt; and &lt;strong&gt;DELETE&lt;/strong&gt; statements.&lt;/p&gt;

&lt;p&gt;The Where clause is used with logical and comparison operators such as equal to &lt;strong&gt;(=)&lt;/strong&gt;, greater than &lt;strong&gt;(&amp;gt;)&lt;/strong&gt; and less than &lt;strong&gt;(&amp;lt;)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It can also be used with other operators including;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;BETWEEN&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Where clause is used with the &lt;em&gt;BETWEEN&lt;/em&gt; operator, to filter records within a specified range.&lt;br&gt;
When using between, the result will include both the start and end values of the range.&lt;/p&gt;

&lt;p&gt;For example, to find students that scored between 55 and 95 marks from a results table, the values returned will include &lt;em&gt;55&lt;/em&gt; and &lt;em&gt;95&lt;/em&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;result_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;student_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;subject_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;marks&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;grade&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;exam_results&lt;/span&gt; 
&lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;marks&lt;/span&gt; &lt;span class="k"&gt;between&lt;/span&gt; &lt;span class="mi"&gt;55&lt;/span&gt; &lt;span class="k"&gt;and&lt;/span&gt; &lt;span class="mi"&gt;95&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2F3xk2s9wghusj0dpcyw3m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F3xk2s9wghusj0dpcyw3m.png" alt="Where clause used with Between " width="646" height="175"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IN&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;WHERE&lt;/strong&gt; clause can be used with the &lt;em&gt;IN&lt;/em&gt; operator to filter records based on multiple values. &lt;br&gt;
It makes queries shorter and easier to understand, instead of writing multiple conditions using the (=) operator combined with OR.&lt;/p&gt;

&lt;p&gt;Syntax;&lt;/p&gt;

&lt;p&gt;SELECT column1, column2&lt;br&gt;
FROM table_name&lt;br&gt;
WHERE column_name IN (value1, value2, value3);&lt;/p&gt;

&lt;p&gt;To find students from different cities in our students table, we use &lt;em&gt;IN&lt;/em&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;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;city&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt; 
&lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;city&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Nairobi'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'Nakuru'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'Mombasa'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fxfuhnzi5d2s6y36vrbe4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fxfuhnzi5d2s6y36vrbe4.png" alt="IN operator filtering our different cities" width="800" height="254"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;LIKE&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;WHERE&lt;/strong&gt; clause is used with the &lt;strong&gt;LIKE&lt;/strong&gt; operator when searching for a particular pattern. &lt;br&gt;
When using &lt;em&gt;LIKE&lt;/em&gt;, a wildcard is used; &lt;br&gt;
&lt;strong&gt;‘%’&lt;/strong&gt;- signifies any number of characters including 0.&lt;br&gt;
 &lt;strong&gt;‘_’&lt;/strong&gt; wildcard is used for a specific number of characters.&lt;/p&gt;

&lt;p&gt;Syntax is;&lt;br&gt;
SELECT column_name&lt;br&gt;
FROM table_name&lt;br&gt;
WHERE column_name LIKE 'pattern';&lt;/p&gt;

&lt;p&gt;For example, to find the list of subjects that contain the word 'studies' in a subjects table, &lt;em&gt;LIKE&lt;/em&gt; is used.&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;subject_name&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;subjects&lt;/span&gt;
&lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;subject_name&lt;/span&gt; &lt;span class="k"&gt;like&lt;/span&gt; &lt;span class="s1"&gt;'%Studies%'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2F5udywjq3i6e2u04uq0fc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F5udywjq3i6e2u04uq0fc.png" alt="Filtering using _Where_ clause and Like" width="800" height="153"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  CASE WHEN STATEMENT
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;CASE WHEN&lt;/strong&gt; statement is used to add conditional logic inside queries, similar to an &lt;em&gt;if-else&lt;/em&gt; structure.&lt;br&gt;
It checks conditions one by one and returns a value as soon as a matching condition is found.&lt;br&gt;
It helps categorize data dynamically and allows creation of new categories based on the specific conditions within a query.&lt;/p&gt;

&lt;p&gt;The syntax is;&lt;/p&gt;

&lt;p&gt;select column name, &lt;br&gt;
   case&lt;br&gt;
     when condition1 then 'result 1'&lt;br&gt;
     when condition2 then 'result 2'&lt;br&gt;
    else 'default result&lt;br&gt;
 end as new column_name &lt;/p&gt;

&lt;p&gt;For example, &lt;strong&gt;CASE WHEN&lt;/strong&gt; can be used to group marks into a new column called Performance, in the results table, as shown 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;select&lt;/span&gt; &lt;span class="n"&gt;result_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;student_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;subject_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;marks&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt;
        &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="n"&gt;marks&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt; &lt;span class="s1"&gt;'Distinction'&lt;/span&gt;
        &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="n"&gt;marks&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt; &lt;span class="s1"&gt;'Merit'&lt;/span&gt;
        &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="n"&gt;marks&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt; &lt;span class="s1"&gt;'Pass'&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="s1"&gt;'Fail'&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;Performance&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;exam_results&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fey0dt701fcwyrux0jkjq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fey0dt701fcwyrux0jkjq.png" alt="Case When statement" width="650" height="171"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  REFLECTION
&lt;/h2&gt;

&lt;p&gt;Learning Databases and SQL can seem difficult, but once you get into it, it becomes interesting especially if you are curious to see how you can create and modify your database along the way. &lt;/p&gt;

&lt;p&gt;The most interesting thing with SQL is that there are some rules for consistency and functionality across databases. Some of those rules are; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Always end your statements with a semicolon (;) &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;SQL commands and key words such as SELECT and INSERT are not case-sensitive.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Spaces and new lines are allowed for better readability.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Do not use SQL keywords as names, if you have to, write them in quotes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To include comments in your script, begin single line comments with --- and for multi-line comments you use /* at the beginning and */ at the end.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Commands like DROP and DELETE should be used with caution as they could result in permanent data loss without proper use of transactions or backups.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>sql</category>
      <category>beginners</category>
      <category>learning</category>
    </item>
    <item>
      <title>A Comprehensive Guide to Publishing and Embedding Power BI Reports on the Web with IFrames</title>
      <dc:creator>Sharon-nyabuto</dc:creator>
      <pubDate>Mon, 06 Apr 2026 23:25:01 +0000</pubDate>
      <link>https://dev.to/sharonnyabuto/a-comprehensive-guide-to-publishing-and-embedding-power-bi-reports-on-the-web-with-iframes-1004</link>
      <guid>https://dev.to/sharonnyabuto/a-comprehensive-guide-to-publishing-and-embedding-power-bi-reports-on-the-web-with-iframes-1004</guid>
      <description>&lt;p&gt;Microsoft PowerBI is one powerful tool when it comes to &lt;strong&gt;analyzing&lt;/strong&gt; and &lt;strong&gt;visualizing&lt;/strong&gt; data.&lt;/p&gt;

&lt;p&gt;Power BI allows users to connect to different data sources, transform data, build interactive dashboards and share insights with others.&lt;/p&gt;

&lt;p&gt;I have spent the last two weeks learning how to build queries, clean data on power query, use DAX functions, data modelling and relationships, as well as building dashboards and reports in Power BI.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reports and dashboards in Power BI&lt;/strong&gt;&lt;br&gt;
Reports and dashboards are important for visualizing data. &lt;/p&gt;

&lt;p&gt;While &lt;strong&gt;dashboards **are often a one-page summary of key insights, **reports&lt;/strong&gt; are multiple pages and with a detailed analysis.&lt;br&gt;
In the day to day operations, reports have to be shared with managers, teams and other stakeholders.&lt;br&gt;
Instead of sending &lt;em&gt;‘.pbix’&lt;/em&gt; files, which require Power BI desktop, &lt;strong&gt;embedding reports&lt;/strong&gt; enables viewing them in browsers, interaction with filters and accessing live and updated data. &lt;/p&gt;

&lt;p&gt;Embedding reports is a necessary move from static reports, to interactive web-based dashboards, that no one has a challenge accessing.&lt;/p&gt;

&lt;p&gt;In this article, we will learn step by step how to;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a &lt;strong&gt;Workspace&lt;/strong&gt; in Power BI Services&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Upload&lt;/strong&gt; and &lt;strong&gt;Publish&lt;/strong&gt; a Power BI report from Power BI desktop&lt;/li&gt;
&lt;li&gt;Generate an &lt;strong&gt;Embed&lt;/strong&gt; code&lt;/li&gt;
&lt;li&gt;Embed the report on a web page using an &lt;strong&gt;iframe&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Brief Overview of Power BI and the Publishing Process&lt;/strong&gt;&lt;br&gt;
Power BI consists of two components; &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;- &lt;strong&gt;Power BI Desktop&lt;/strong&gt;– Where the data is loaded, cleaned and transformed, relationships and visuals are built and reports designed.&lt;/li&gt;
&lt;li&gt;- &lt;strong&gt;Power BI Service&lt;/strong&gt;- Where the reports are published, workspaces and embedded links are created, dashboards are shared and work is done in collaboration with others.&lt;/li&gt;
&lt;/ol&gt;


&lt;h2&gt;
  
  
  &lt;u&gt;Creating a Workspace in Power BI Services&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;Workspace&lt;/strong&gt; is an &lt;em&gt;online folder&lt;/em&gt;, that is dedicated to &lt;em&gt;storing&lt;/em&gt; and &lt;em&gt;organizing&lt;/em&gt; your dashboards and reports.&lt;/p&gt;

&lt;p&gt;The default workspace is always ‘My Workspace’ which is often just for personal testing, and as a result, you cannot get a working public embed code if you generate your report here. Therefore, do &lt;strong&gt;NOT&lt;/strong&gt; use &lt;em&gt;My Workspace.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;How to create a Workspace step-by-step;&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open your browser and go to &lt;a href="https://app.powerbi.com" rel="noopener noreferrer"&gt;https://app.powerbi.com&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Log in using your Microsoft account&lt;/li&gt;
&lt;li&gt;On the left menu, click &lt;em&gt;Workspaces&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Click the &lt;em&gt;New Workspace&lt;/em&gt; button at the top right&lt;/li&gt;
&lt;li&gt;Enter a Workspace Name, &lt;em&gt;e.g. Electronics Sales&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Click &lt;em&gt;Apply&lt;/em&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fw5roapfy0tpefxa5r5e5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fw5roapfy0tpefxa5r5e5.png" alt="Creating a new Workspace" width="800" height="389"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once this is complete, your Workspace is ready. You can now publish your report.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;u&gt;Uploading and publishing a Power BI report from Power BI desktop.&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;Now we will upload the &lt;em&gt;.pbix&lt;/em&gt; file to the workspace we just created.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;How to publish a report to the Workspace step-by-step;&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open your report in Power BI desktop.&lt;/li&gt;
&lt;li&gt;On the top right corner of the home ribbon, click &lt;em&gt;‘Publish’&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Select the &lt;strong&gt;workspace&lt;/strong&gt; you just crated, &lt;em&gt;(e.g. Electronic Sales)&lt;/em&gt; from the list of Workspaces that Power BI will display.&lt;/li&gt;
&lt;li&gt;Click &lt;em&gt;Select&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Power BI will now upload your report. It may take a few seconds to 2 minutes, so give it some time. &lt;/li&gt;
&lt;li&gt;You will see a message that shows you have successfully published your report to Power BI Services.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fz5jb1cm9w6fzv0n21e5b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fz5jb1cm9w6fzv0n21e5b.png" alt="Notification for successful publishing" width="349" height="190"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Until you have confirmed that the report has been published, do not close your Power BI Desktop. &lt;br&gt;
If you close and there’s something wrong, you may have to start the process all over again.&lt;/p&gt;

&lt;p&gt;To confirm if the report has been uploaded to the Power BI Service;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Go back to &lt;em&gt;&lt;a href="https://app.powerbi.com" rel="noopener noreferrer"&gt;https://app.powerbi.com&lt;/a&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Open your &lt;em&gt;Workspace&lt;/em&gt; and refresh. You should see your report&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fudn4rtowa9swvlia9fd5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fudn4rtowa9swvlia9fd5.png" alt="Uploaded Report on Workspace" width="769" height="265"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;u&gt;Generating the Embed Code.&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;Embedding allows the report to be &lt;em&gt;displayed&lt;/em&gt; inside a website.&lt;br&gt;
Power BI provides an embed code, that can be copied and pasted in a website. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;How to generate a code, step-by-step;&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open your report in the Workspace.&lt;/li&gt;
&lt;li&gt;Click &lt;em&gt;‘File’&lt;/em&gt; at the top of the page.&lt;/li&gt;
&lt;li&gt;Select &lt;em&gt;‘Embed Report’&lt;/em&gt;, then click on Website or portal&lt;/li&gt;
&lt;li&gt;Click on create &lt;em&gt;Embed code.&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Then click &lt;strong&gt;Confirm.&lt;/strong&gt; &lt;/li&gt;
&lt;li&gt;Copy the embed code to be used later.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You will now see a pop-up window with the link to embed, and a box with the embed code needed, this is the only correct place to get your embed code from.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fiepxd7vflh9sgio115ze.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fiepxd7vflh9sgio115ze.png" alt="Popup with the Iframe" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The embed code,  is an &lt;strong&gt;Iframe&lt;/strong&gt; &lt;em&gt;(inline frame)&lt;/em&gt; which is a HTML tag that allows you to display another webpage inside your webpage. &lt;/p&gt;

&lt;p&gt;A typical iframe will look like this;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;iframe&lt;/span&gt; &lt;span class="na"&gt;title=&lt;/span&gt;&lt;span class="s"&gt;"Electronic_Data"&lt;/span&gt; &lt;span class="na"&gt;width=&lt;/span&gt;&lt;span class="s"&gt;"1140"&lt;/span&gt; &lt;span class="na"&gt;height=&lt;/span&gt;&lt;span class="s"&gt;"541.25"&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://app.powerbi.com/reportEmbed?reportId=1e766837-7d75-4a37-bc3d-3e0ea1f56073&amp;amp;autoAuth=true&amp;amp;ctid=c95984e8-6a70-4512-a440-1c79bca9cc37"&lt;/span&gt; &lt;span class="na"&gt;frameborder=&lt;/span&gt;&lt;span class="s"&gt;"0"&lt;/span&gt; &lt;span class="na"&gt;allowFullScreen=&lt;/span&gt;&lt;span class="s"&gt;"true"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/iframe&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The width and height parts of the code are flexible, and you can change those to whatever you like. All the other parts however, should remain exactly as they are.&lt;/p&gt;

&lt;p&gt;A common mistake often made is using the &lt;em&gt;Share&lt;/em&gt; button to embed a report. &lt;br&gt;
The share button generates links accessible only to those that have accounts in your organization. It will therefore not generate an appropriate embed code.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;u&gt;Embedding the report on a webpage&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;Embedding codes allow integration into websites.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;How to do it, step-by-step;&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open any plain text editor, such as VS code or Notepad.  Do not use Microsoft Word. &lt;/li&gt;
&lt;li&gt;Create an empty &lt;strong&gt;.html&lt;/strong&gt; file.&lt;/li&gt;
&lt;li&gt;Add basic HTML structure;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;html
&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Title of report e.g. Electronic Sales Data&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Heading e.g. Electronic Sales Dashboard&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;Power&lt;/span&gt; &lt;span class="na"&gt;BI&lt;/span&gt; &lt;span class="na"&gt;iframe&lt;/span&gt; &lt;span class="na"&gt;goes&lt;/span&gt; &lt;span class="na"&gt;here&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Click file &amp;gt; Save as, and name the file &lt;strong&gt;Index.html&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That is it, if you double click on the saved html file, it will open on any browser and you can load your report, and see all the interactive report running perfectly.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F0tsbpfi4xzhr44298jvf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F0tsbpfi4xzhr44298jvf.png" alt="Successfully embedded Report" width="800" height="360"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;u&gt;Troubleshooting common Issues&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;If something is not working, it could be one of the following; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Report requires users to log in&lt;br&gt;
&lt;strong&gt;&lt;em&gt;Fix:&lt;/em&gt;&lt;/strong&gt; New workspaces are sometimes set to private by default. Go back to your workspace, click &lt;em&gt;manage access&lt;/em&gt;, and add Everyone as &lt;strong&gt;Viewer&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Blank Screen after the publishing and embedding&lt;br&gt;
&lt;strong&gt;&lt;em&gt;Fix:&lt;/em&gt;&lt;/strong&gt; Give it time. This is a normal occurrence, as Power BI can take a few minutes for the embed code to work, after publishing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; The report works on your end but no one else can see it.&lt;br&gt;
&lt;strong&gt;&lt;em&gt;Fix:&lt;/em&gt;&lt;/strong&gt;It is possible you used ‘Share’ Button, when embedding, instead of generating the embed code. You might need to generate the embed code anew.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;u&gt;Key Insights and Conclusion&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;Now that you have learnt this process, you can go ahead and use it for your student projects, portfolios, and for work, absolutely free. &lt;/p&gt;

&lt;p&gt;You do not need a paid premium license to embed your Power BI reports. &lt;br&gt;
Every singe feature of your report will work exactly as it does on Power BI Desktop, the slicers, filters and interactions will work just fine inside the Iframe.&lt;/p&gt;

&lt;p&gt;In conclusion, you could build the best Power BI report, but if you cannot publish it and share it with other people, it's use may not be maximized, and a lot of insight could be lost.&lt;/p&gt;

</description>
      <category>analytics</category>
      <category>microsoft</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Understanding Data Modeling in Power BI: Joins, Relationships, and Schemas Explained</title>
      <dc:creator>Sharon-nyabuto</dc:creator>
      <pubDate>Sat, 04 Apr 2026 17:24:06 +0000</pubDate>
      <link>https://dev.to/sharonnyabuto/understanding-data-modeling-in-power-bi-joins-relationships-and-schemas-explained-4cop</link>
      <guid>https://dev.to/sharonnyabuto/understanding-data-modeling-in-power-bi-joins-relationships-and-schemas-explained-4cop</guid>
      <description>&lt;p&gt;&lt;strong&gt;&lt;em&gt;Ever tried to build a puzzle where half the pieces were in a different room?&lt;/em&gt;&lt;/strong&gt; &lt;br&gt;
That is how it feels working with raw data before understanding Data Modelling.&lt;/p&gt;




&lt;p&gt;In the real world, data is not stored in one sheet, it is scattered across dozens different files. A major chain for example, has separate files for their stores, customers, sales and products. If you tried to cram all that information into one massive table, managing it would be difficult to manage! &lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;So what is Data Modelling?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Data modelling is the process of organizing data into structured tables and defining how those tables relate to each other.  In professional environments, data is organized into 2 main types; &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Facts tables&lt;/strong&gt; - These store measurable events (the metrics). A sales table for instance, is a fact because it records every transaction, indicating how much was sold and when. Fact tables are usually long and full of numbers. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;2.&lt;strong&gt;Dimensions tables&lt;/strong&gt; - These provide descriptive contexts, such as customer names, color and product category. They are relatively smaller, and give meaning to the facts.&lt;/p&gt;

&lt;p&gt;Tables are connected using Joins &lt;em&gt;(in Power Query)&lt;/em&gt; or Relationships &lt;em&gt;(in the model view)&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;In this article, we will look at;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; SQL Joins in Power Query&lt;/li&gt;
&lt;li&gt; Power BI Relationships&lt;/li&gt;
&lt;li&gt; Schemas&lt;/li&gt;
&lt;li&gt; Role Playing Dimensions&lt;/li&gt;
&lt;li&gt; Common Modelling Issues to watch out for&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  SQL JOINS IN POWER QUERY
&lt;/h2&gt;

&lt;p&gt;Before data gets to the model, it has to be cleaned, this happens in Power Query using Joins.&lt;br&gt;&lt;br&gt;
A Join is a way of combining data from two tables based on a common column.  It is a rule, that tells the computer how to merge two tables into one physical table. &lt;/p&gt;

&lt;p&gt;Our scenario; An online retail store with two lists;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;•    Table A (Customers) – With Customer Name, Customer ID and Customer Location&lt;br&gt;
• Table B (Orders) - Order ID, Customer ID, Order Amount, Order Date&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;There are 6 main kinds of Joins;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Flk8er4wu80q4cgkmgmqh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Flk8er4wu80q4cgkmgmqh.png" alt="Joint Types" width="800" height="399"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;How to do it Step-By-Step &lt;em&gt;(The steps are common for ALL joins.)&lt;/em&gt;&lt;br&gt;
In Power Query:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Click your main table (e.g. Customer List)&lt;/li&gt;
&lt;li&gt;Click Merge Queries in the Home Ribbon&lt;/li&gt;
&lt;li&gt;Select your second table, (e.g. Orders List in our case)&lt;/li&gt;
&lt;li&gt;Click on the matching columns in both tables (e.g. Customer ID)&lt;/li&gt;
&lt;li&gt;Choose your Join Kind from the dropdown.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fgtg1ticsexcnlcekvv49.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fgtg1ticsexcnlcekvv49.png" alt="Selecting joints" width="538" height="326"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  POWER BI RELATIONSHIPS
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Relationships&lt;/strong&gt; define how tables are connected in a data model, so they can interact with each other in reports.  Relationships connect tables while keeping them separate, and are created in the Model View.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cardinality (The Match Ratio)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Cardinality&lt;/strong&gt;&lt;/em&gt; describes the nature of the relationship between two tables. It answers the question &lt;em&gt;How many&lt;/em&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;**_One-to-Many (1:M)_**
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;One record in one table relates to many records in another. For example; where one customer has multiple orders or where one bank account has multiple transactions.&lt;br&gt;
    &lt;strong&gt;&lt;em&gt;Many-to-Many (M:M)&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
Multiple records in both tables relate to each other. These can cause double-counting and incorrect numbers. Use bridge tables if necessary.&lt;br&gt;
    &lt;strong&gt;&lt;em&gt;One-to-One (1:1)&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
Where one record matches exactly one other in another table. This is pretty rare for relationships, and sometimes means that two tables should be merged instead.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Direction and Activity&lt;/strong&gt;&lt;br&gt;
Cross filter directions: Determines how filters flow between tables. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Single:&lt;/strong&gt;&lt;/em&gt; Filters flow one way, from dimension to fact. A good example is how filtering customers will affect Orders, but orders won’t filter customers. &lt;br&gt;
&lt;em&gt;&lt;strong&gt;Both:&lt;/strong&gt;&lt;/em&gt; Filters flow both ways. It is useful but can easily create confusion or incorrect results. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Active Vs Inactive Relationships&lt;/strong&gt;&lt;br&gt;
In Power BI, you can only have &lt;strong&gt;ONE&lt;/strong&gt;active relationship between two tables at a time. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Active filters&lt;/strong&gt; are the connections that are always on.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Inactive filters&lt;/strong&gt; are the connections that are always off, unless they are required for a certain calculation.&lt;/p&gt;

&lt;p&gt;PowerBI allows you to have one active relationship, and one or more inactive relationships at any given time. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;How to do it step-by-step&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Go to model view &lt;/li&gt;
&lt;li&gt;Click a column in your dimension table and drag it to the matching column in your fact table; A line appears&lt;/li&gt;
&lt;li&gt;Double click the line to check Cardinality and Direction&lt;/li&gt;
&lt;li&gt;You can make the relationship active or inactive&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;Differences Between Joins and Relationships&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F4s6uyfkedabzd2526qga.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F4s6uyfkedabzd2526qga.png" alt="JoinsVsRelationships" width="779" height="136"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  SCHEMAS
&lt;/h2&gt;

&lt;p&gt;Now that we know how to connect tables, it is important to also know how to organize them. In data modelling, tables follow a layout, i.e. a Schema. &lt;br&gt;
Every good model will separate data into either fact tables or dimensions tables.&lt;/p&gt;

&lt;p&gt;There are three main table designs;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A Flat Table (DLAT)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is where both your fact and dimension tables are in one place, as would appear in Excel. &lt;br&gt;
While this could be used for one off analysis or very small datasets, it is not advisable on big models, because as data grows, the table becomes very slow and very hard to manage. &lt;br&gt;
It also has a higher probability of duplicate data.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;How to get it;&lt;/em&gt;&lt;br&gt;
 Use the &lt;em&gt;&lt;strong&gt;Merge Queries&lt;/strong&gt;&lt;/em&gt; in Power Query to merge all your tables together into one single, massive table.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F4agvvs5rst2ygfm791pi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F4agvvs5rst2ygfm791pi.png" alt="Flat Table Example" width="630" height="175"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Star Schema&lt;/strong&gt;&lt;br&gt;
This is a model with one fact table at the center, and dimension tables surround it. &lt;br&gt;
It is the industry standard, and results in a clean, fast and simple layout. &lt;br&gt;
It is easy to understand and hard to break, and preferred in over 90% of reports. &lt;br&gt;
It is advisable to use this, unless you have a reason not to.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;How to get it;&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Load your Fact Table and Dimension Tables separately into Power Query.&lt;/li&gt;
&lt;li&gt;Enter Model View: Click the _Model View _icon on the far left sidebar..&lt;/li&gt;
&lt;li&gt;Click and drag a unique ID from your Dimension table (e.g., Product ID) onto the matching column in your Fact table.&lt;/li&gt;
&lt;li&gt;Drag the Fact table to the center and pan out your Dimension tables around it.&lt;/li&gt;
&lt;li&gt;Check for cardinality.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F1jjpt22fmajt66zqu4zf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F1jjpt22fmajt66zqu4zf.png" alt="A star Schema" width="624" height="333"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Snowflake Schema&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is a star schema, whose dimensions are further broken down to other dimensions. This is called &lt;strong&gt;&lt;em&gt;normalization.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It is used when there are complex relationships such as &lt;em&gt;Country&amp;gt;County&amp;gt;Sub-county.&lt;/em&gt;&lt;br&gt;
While it can save on space due to less repetition, it becomes more complicated and reports are slower. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;How to get it;&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;em&gt;Start Like a Star: Follow the steps above for your main Fact table.&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;Split a Dimension for example instead of loading Products directly into the Fact link, load Products and then a separate Categories table.&lt;/li&gt;
&lt;li&gt;Connect hierarchically, i.e. Fact → Products, then Products → Categories.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fnojq2x732gffg9gij23m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fnojq2x732gffg9gij23m.png" alt="Snowflake Schema" width="624" height="243"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  ROLE PLAYING DIMENSIONS
&lt;/h2&gt;

&lt;p&gt;Sometimes a single dimension table has to play more than one role. A great example is a date table, that you use to filter both Order dates, and Shipping Dates. In this case, the problem is handled using ‘inactive relationships. &lt;br&gt;
Managing relationships this way allows for a clean model, as it lets you slice data differently, and there's no need to clone the table twice.&lt;/p&gt;




&lt;h2&gt;
  
  
  COMMON MODELLING ISSUES TO WATCH OUT FOR.
&lt;/h2&gt;

&lt;p&gt;As you start building, here are a few issues to avoid;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;&lt;strong&gt;Many-to-Many relationships (M:M)&lt;/strong&gt;&lt;/em&gt; - This happens when you try to connect two tables that don't have a unique "one" side. It causes Power BI to get confused and often results in numbers that look correct but are actually completely wrong. &lt;/li&gt;
&lt;li&gt;
&lt;em&gt;&lt;strong&gt;Blanks in Keys&lt;/strong&gt;&lt;/em&gt;– If your ID column has empty cells, they could break the relationship silently. That is why you should clean your data in Power Query before loading.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;&lt;strong&gt;Flat tables&lt;/strong&gt;&lt;/em&gt;- Don’t try to keep everything in one table just because it feels like excel, you may have a hard time navigating or getting anything done.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>powerfuldevs</category>
      <category>learning</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How Excel is used in Real World Data Analysis</title>
      <dc:creator>Sharon-nyabuto</dc:creator>
      <pubDate>Mon, 30 Mar 2026 06:24:23 +0000</pubDate>
      <link>https://dev.to/sharonnyabuto/how-excel-is-used-in-real-world-data-analysis-4i79</link>
      <guid>https://dev.to/sharonnyabuto/how-excel-is-used-in-real-world-data-analysis-4i79</guid>
      <description>&lt;p&gt;A month ago, I challenged myself to learn data analysis, so I went shopping across the internet for a good place to start and the consensus was unanimous; check out &lt;em&gt;&lt;strong&gt;good old Excel.&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;So what exactly is Excel?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Far from just being a digital ledger, excel is a &lt;strong&gt;powerful data engine.&lt;/strong&gt; It is used in turning observations into trends in &lt;strong&gt;Field Research,&lt;/strong&gt; helps &lt;strong&gt;businesses&lt;/strong&gt; track sales and predict growth while in &lt;strong&gt;finance&lt;/strong&gt; it is used to ensure every cent is accounted for.&lt;/p&gt;

&lt;p&gt;Learning Excel felt very intimidating at first, the aggregate and lookup functions were all too new to me! Once I stopped being anxious however, I have been enjoying the journey of discovering this ability excel has! Here are a few things I have learned and use often;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;COUNTIF () FUNCTION&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;This simply counts how many times something appears in your list. In its simplest form, it asks:&lt;br&gt;
&lt;em&gt;&lt;strong&gt;=COUNTIF(Where do you want to look?, What do you want to look for?)&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fueyrjhk0z3zuuidnuo4m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fueyrjhk0z3zuuidnuo4m.png" alt="_Image1; COUNTIF()_"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;IF() FUNCTION&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;This operator allows excel to &lt;em&gt;&lt;strong&gt;‘think’&lt;/strong&gt;&lt;/em&gt;. It checks if a condition is true, and gives you one result if it is, and another if it is not.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Syntax =IF(logical_test, value_if_true, value_if_false) 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2F0784ga8wcxi19sevticc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F0784ga8wcxi19sevticc.png" alt="_Image2;IF()_"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can have as many conditions as you need, all you have to do is properly &lt;strong&gt;nest&lt;/strong&gt; them!&lt;/p&gt;

&lt;p&gt;&lt;code&gt;=IF(X3&amp;lt;2, "Poor Score",IF(X3&amp;lt;3,"Average Score",IF(X3&amp;lt;4,"Good Score","Excellent Score")))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fktety2yzgvxcg0d21aik.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fktety2yzgvxcg0d21aik.png" alt="_Image3;NESTED IF()_"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Lookup Functions&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In the real world, all the data will not necessarily be in the same place always. The sales and customer names may be in different sheets, and the lookup functions are the connectors!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;VLOOKUP&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It searches down the first column, to find a value and pulls information from a different column on the right.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; Syntax: =VLOOKUP(lookup_value, table_array, col_index_num,[range_lookup])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Translation:&lt;em&gt;=VLOOKUP(What are you looking for?, Where is the whole table?, Which column number has the answer?, FALSE)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;FALSE&lt;/strong&gt; at the end &lt;em&gt;‘tells’&lt;/em&gt; excel that you want a &lt;strong&gt;perfect match&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;=VLOOKUP(10010, A2:F13, 5, FALSE)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fxdxwzd5vh4jzkn07j03m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fxdxwzd5vh4jzkn07j03m.png" alt="Image4; VLOOKUP"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(This simlpy says; Find ID 10010 in this table, and give me the value from the 5th column. The result will be 82107)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Because it relies on a fixed column number, adding a new column to your table can break its logic. If you insert a new column before Column 5, it becomes Column 6, but &lt;strong&gt;VLOOKUP&lt;/strong&gt; will still look at Column 5! Since the 5th column is no longer where the salary &lt;em&gt;‘lives’&lt;/em&gt;, it might return the wrong data, without giving you any error message.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;XLOOKUP&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A relatively newer version that beats these limitations is the XLOOKUP, and it does not require one to count the columns! XLOOKUP works, regardless whether other columns are added or deleted.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Syntax: =XLOOKUP(Lookup_Value, Lookup_Array, Return_Array)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;=XLOOKUP("P-102", A:A, C:C)&lt;/code&gt;&lt;br&gt;
&lt;em&gt;(This simply says "Find P-102 in Column A, and give me the corresponding value from Column C."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;XLOOKUP only works on Office 365, Excel 2021, or later.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;(INDEX + MATCH)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;What if you’re working on an older model and still need to bypass VLOOKUP's limitations? You use &lt;strong&gt;INDEX and MATCH.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;MATCH finds the &lt;strong&gt;position&lt;/strong&gt; of a value, and INDEX grabs the &lt;strong&gt;value&lt;/strong&gt; from that position.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Syntax:=INDEX(Return_Column, MATCH(Lookup_Value, Lookup_Column, 0))

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;(Note: We use 0 for an exact match every time!)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;=INDEX(G2:G13, MATCH(10010, A1:A13, 0))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Translation: Find ID 10010 in Column A, then give me the value from the matching row in Column G.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This combo is powerful because unlike VLOOKUP, it can look to &lt;strong&gt;the left or right&lt;/strong&gt; without breaking! &lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;PIVOT TABLES&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Before embarking on excel, these used to look like &lt;em&gt;Martian tech&lt;/em&gt; to me! Now, I realize they are effective &lt;strong&gt;summary machines&lt;/strong&gt;! In a few clicks, you have a clean summary of your data. They take data and tell me the averages, totals and counts in less than a minute! &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You highlight all your data. &lt;/li&gt;
&lt;li&gt;Click Insert&lt;/li&gt;
&lt;li&gt;Select Pivot Table. &lt;/li&gt;
&lt;li&gt;Drag a field to Rows. &lt;/li&gt;
&lt;li&gt;Drag a different field to Values. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And just like that, you have every sum, average, count that you need.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fbag71whmc9cjuhxqc1d5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fbag71whmc9cjuhxqc1d5.png" alt="_Image4; Pivot Table_"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;REFLECTION&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;It’s only been two weeks, but my relationship with data has shifted. There is a massive sense of &lt;strong&gt;empowerment&lt;/strong&gt; in looking at a messy sheet and having a good idea how to &lt;strong&gt;fix it&lt;/strong&gt;, realizing i have the tools to make my data &lt;em&gt;talk&lt;/em&gt; to me! &lt;/p&gt;

&lt;p&gt;Since I’m still very much in the &lt;strong&gt;learning phase&lt;/strong&gt; and have only just scratched the surface, I’m curious: &lt;/p&gt;

&lt;p&gt;&lt;em&gt;What was that one surprisingly simple Excel trick that completely broke your brain when you first saw it?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If you're a seasoned pro, what is a golden tip you wish you knew when you were just starting out in excel?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Please drop your tips, shortcuts, or aha! moments, I’m all ears and ready to learn!&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>learning</category>
      <category>analytics</category>
    </item>
  </channel>
</rss>
