<?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: Ian Macharia Mwangi</title>
    <description>The latest articles on DEV Community by Ian Macharia Mwangi (@ian_mwangi).</description>
    <link>https://dev.to/ian_mwangi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3409980%2F2226dcbf-98d7-4f82-aeed-437ebef4a105.png</url>
      <title>DEV Community: Ian Macharia Mwangi</title>
      <link>https://dev.to/ian_mwangi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ian_mwangi"/>
    <language>en</language>
    <item>
      <title>Understanding Degrees of Freedom and Their Importance in Statistics</title>
      <dc:creator>Ian Macharia Mwangi</dc:creator>
      <pubDate>Fri, 07 Nov 2025 19:33:31 +0000</pubDate>
      <link>https://dev.to/ian_mwangi/understanding-degrees-of-freedom-and-their-importance-in-statistics-29af</link>
      <guid>https://dev.to/ian_mwangi/understanding-degrees-of-freedom-and-their-importance-in-statistics-29af</guid>
      <description>&lt;p&gt;When I first started learning statistics, the term “degrees of freedom” (df) felt mysterious — it popped up in formulas for t-tests, chi-square tests, ANOVA, and even in regression analysis, yet no one seemed to clearly explain what it actually meant.&lt;br&gt;
After diving deeper, I realized that degrees of freedom are not just a mathematical artifact — they reflect how much information we truly have available to estimate something.&lt;/p&gt;

&lt;p&gt;Let’s unpack what that means and why it matters.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;What Are Degrees of Freedom?
In simple terms, degrees of freedom represent the number of independent values in a dataset that are free to vary when estimating a statistical parameter.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Think of it as:&lt;/p&gt;

&lt;p&gt;“How many data points can change without breaking the rules imposed by the estimation process?”&lt;/p&gt;

&lt;p&gt;Formula:&lt;br&gt;
df = n - k&lt;br&gt;
(where n = number of observations, k = number of estimated parameters or constraints)&lt;/p&gt;

&lt;p&gt;Example: Understanding Through Intuition&lt;/p&gt;

&lt;p&gt;Suppose you have three numbers:&lt;br&gt;
Let’s call them x1, x2, x3.&lt;/p&gt;

&lt;p&gt;You know their mean is 10.&lt;/p&gt;

&lt;p&gt;This constraint means that:&lt;/p&gt;

&lt;p&gt;Equation:&lt;br&gt;
(x1 + x2 + x3) / 3 = 10&lt;br&gt;
→ x1 + x2 + x3 = 30&lt;/p&gt;

&lt;p&gt;Now, if you pick any two of these numbers freely (say x1 = 8 and x2 = 11), then x3 is no longer free — it must be 11 to make the total 30.&lt;/p&gt;

&lt;p&gt;So even though there are three values, only two can vary freely.&lt;/p&gt;

&lt;p&gt;Hence, the degrees of freedom = 3 − 1 = 2.&lt;/p&gt;

&lt;p&gt;This is why, when calculating sample variance, we divide by (n − 1) instead of n — one degree of freedom is lost because we already used the sample mean to estimate the center of the data.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Degrees of Freedom in Common Statistical Tests&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Degrees of freedom appear in almost every statistical test because they determine the shape of the underlying probability distribution used for inference.&lt;/p&gt;

&lt;p&gt;Let’s look at a few examples.&lt;/p&gt;

&lt;p&gt;a. t-Test&lt;/p&gt;

&lt;p&gt;The formula for the t-statistic is:&lt;/p&gt;

&lt;p&gt;Equation:&lt;br&gt;
t = (x̄ − μ₀) / (s / √n)&lt;/p&gt;

&lt;p&gt;Where:&lt;/p&gt;

&lt;p&gt;x̄ = sample mean&lt;/p&gt;

&lt;p&gt;μ₀ = hypothesized population mean&lt;/p&gt;

&lt;p&gt;s = sample standard deviation&lt;/p&gt;

&lt;p&gt;n = sample size&lt;/p&gt;

&lt;p&gt;Because one parameter (the mean) is estimated, the degrees of freedom = n − 1.&lt;/p&gt;

&lt;p&gt;The t-distribution’s shape changes with df:&lt;/p&gt;

&lt;p&gt;With small df (e.g., 5 or 10), it has heavier tails (more uncertainty).&lt;/p&gt;

&lt;p&gt;As df increases, it approaches the normal distribution.&lt;/p&gt;

&lt;p&gt;b. ANOVA (Analysis of Variance)&lt;/p&gt;

&lt;p&gt;In ANOVA, degrees of freedom partition total variability into between-group and within-group components.&lt;/p&gt;

&lt;p&gt;Equations:&lt;br&gt;
Total df = N − 1&lt;br&gt;
Between-groups df = k − 1&lt;br&gt;
Within-groups df = N − k&lt;/p&gt;

&lt;p&gt;Where:&lt;/p&gt;

&lt;p&gt;N = total number of observations&lt;/p&gt;

&lt;p&gt;k = number of groups&lt;/p&gt;

&lt;p&gt;These df values are used to compute F-statistics, which test whether group means differ significantly.&lt;/p&gt;

&lt;p&gt;c. Chi-Square Tests&lt;/p&gt;

&lt;p&gt;In a chi-square goodness-of-fit test, the degrees of freedom equal:&lt;/p&gt;

&lt;p&gt;Equation:&lt;br&gt;
df = k − 1&lt;/p&gt;

&lt;p&gt;where k = number of categories (one degree is lost because probabilities must sum to 1).&lt;/p&gt;

&lt;p&gt;In a chi-square test of independence,&lt;br&gt;
Equation:&lt;br&gt;
df = (rows − 1) × (columns − 1)&lt;/p&gt;

&lt;p&gt;d. Regression Analysis&lt;/p&gt;

&lt;p&gt;In regression, degrees of freedom are divided between model parameters and residuals.&lt;/p&gt;

&lt;p&gt;Equations:&lt;br&gt;
Regression df (Model df) = number of predictors&lt;br&gt;
Residual df = n − k − 1&lt;/p&gt;

&lt;p&gt;Where:&lt;/p&gt;

&lt;p&gt;n = number of data points&lt;/p&gt;

&lt;p&gt;k = number of predictors (excluding the intercept)&lt;/p&gt;

&lt;p&gt;Residual df measures how much information is left to estimate the variance of errors.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Why Degrees of Freedom Matter&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Degrees of freedom are not just a technical detail — they directly affect statistical accuracy and inference.&lt;/p&gt;

&lt;p&gt;Here’s why they’re important:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;They Control the Shape of Sampling Distributions&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Every inferential test — t, F, or chi-square — depends on a specific distribution that changes shape with df.&lt;br&gt;
Fewer degrees of freedom → wider tails → more uncertainty → harder to achieve statistical significance.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;They Reflect How Much Information You Actually Have&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Even if you have 100 data points, if your model estimates 10 parameters, you only have 90 degrees of freedom left to estimate variability.&lt;br&gt;
That’s why overfitting models (too many predictors) reduce statistical power.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;They Determine Confidence and Reliability&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Lower df means less reliable estimates — confidence intervals widen, p-values become larger, and results are less stable.&lt;br&gt;
In essence, df quantifies the balance between data richness and model complexity.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A Real-Life Analogy&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Imagine you’re organizing a team photo.&lt;br&gt;
You have 10 people (data points) to arrange, but 1 spot is fixed (constraint).&lt;br&gt;
You can freely move only 9 people around.&lt;/p&gt;

&lt;p&gt;That’s 9 degrees of freedom — the number of ways you can vary things before constraints lock the system.&lt;/p&gt;

&lt;p&gt;The same concept applies to statistics — every estimated parameter reduces flexibility, just like every fixed position limits how freely the group can move.&lt;/p&gt;

&lt;h4&gt;
  
  
  Conclusion
&lt;/h4&gt;

&lt;p&gt;Degrees of freedom are a way of measuring flexibility in your data.&lt;br&gt;
They tell you how many independent pieces of information remain once your model has made certain assumptions or used certain estimates.&lt;br&gt;
In short:&lt;/p&gt;

&lt;p&gt;The more parameters you estimate, the fewer degrees of freedom you have left — and the more cautious you should be when trusting your results.&lt;/p&gt;

&lt;p&gt;Understanding degrees of freedom helps you interpret why statistical tests behave the way they do, and it’s one of the keys to moving from just running analyses to truly understanding them.&lt;/p&gt;

&lt;p&gt;Would you like me to extend this article with a Python example showing how degrees of freedom appear when using scipy.stats.ttest_ind() or pandas.DataFrame.var() (which uses n−1 by default)?&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>datascience</category>
      <category>learning</category>
    </item>
    <item>
      <title>Similarities Between a Stored Procedure in SQL and a Function in Python</title>
      <dc:creator>Ian Macharia Mwangi</dc:creator>
      <pubDate>Wed, 05 Nov 2025 09:37:11 +0000</pubDate>
      <link>https://dev.to/ian_mwangi/similarities-between-a-stored-procedure-in-sql-and-a-function-in-python-36hc</link>
      <guid>https://dev.to/ian_mwangi/similarities-between-a-stored-procedure-in-sql-and-a-function-in-python-36hc</guid>
      <description>&lt;p&gt;Although SQL and Python belong to very different ecosystems — one for database management and the other for general-purpose programming — their core design principles overlap when it comes to modularizing and reusing logic.&lt;/p&gt;

&lt;p&gt;Both stored procedures in SQL and functions in Python serve as reusable, encapsulated units of code designed to perform a task efficiently.&lt;/p&gt;

&lt;p&gt;Let’s break down the main similarities:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Encapsulation of Logic&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Both structures allow you to group multiple steps or statements into a single callable unit.&lt;br&gt;
This helps you avoid repeating logic and keeps your codebase organized.&lt;/p&gt;

&lt;p&gt;SQL Stored Procedure:&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;PROCEDURE&lt;/span&gt; &lt;span class="n"&gt;GetEmployeesByDept&lt;/span&gt;
    &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;DeptID&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt;
&lt;span class="k"&gt;AS&lt;/span&gt;
&lt;span class="k"&gt;BEGIN&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;EmployeeName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Salary&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;DepartmentID&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;DeptID&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;END&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="n"&gt;def&lt;/span&gt; &lt;span class="n"&gt;get_employees_by_dept&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dept_id&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="nv"&gt;"SELECT EmployeeName, Salary FROM Employees WHERE DepartmentID = {dept_id}"&lt;/span&gt;
    &lt;span class="o"&gt;#&lt;/span&gt; &lt;span class="n"&gt;Imagine&lt;/span&gt; &lt;span class="n"&gt;this&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="n"&gt;sent&lt;/span&gt; &lt;span class="k"&gt;to&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="k"&gt;database&lt;/span&gt; &lt;span class="k"&gt;and&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="k"&gt;are&lt;/span&gt; &lt;span class="n"&gt;returned&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;execute_query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In both cases, you’ve encapsulated a reusable block that performs a defined action.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Parameters and Arguments&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Both can accept parameters (inputs) that alter their behavior without changing the internal logic.&lt;/p&gt;

&lt;p&gt;In SQL: parameters are declared with @ and passed during execution (EXEC GetEmployeesByDept @DeptID = 2;)&lt;/p&gt;

&lt;p&gt;In Python: parameters are passed as function arguments (get_employees_by_dept(2))&lt;/p&gt;

&lt;p&gt;This design makes both highly flexible and reusable across different contexts.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Reusable and Maintainable&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Once defined, both can be reused multiple times in different scripts, queries, or applications.&lt;/p&gt;

&lt;p&gt;Stored procedures can be called from different SQL scripts or even external applications.&lt;/p&gt;

&lt;p&gt;Python functions can be imported and reused across multiple modules.&lt;/p&gt;

&lt;p&gt;Both help enforce the DRY principle — Don’t Repeat Yourself.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Structured Flow Control&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Both support conditional logic and loops, allowing more complex, programmatic behavior.&lt;/p&gt;

&lt;p&gt;SQL Example:&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;PROCEDURE&lt;/span&gt; &lt;span class="n"&gt;UpdateSalary&lt;/span&gt;
    &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;EmpID&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;IncreasePercent&lt;/span&gt; &lt;span class="nb"&gt;DECIMAL&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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;AS&lt;/span&gt;
&lt;span class="k"&gt;BEGIN&lt;/span&gt;
    &lt;span class="n"&gt;IF&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;IncreasePercent&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="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;Employees&lt;/span&gt;
        &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;Salary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Salary&lt;/span&gt; &lt;span class="o"&gt;+&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="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;IncreasePercent&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;100&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;EmployeeID&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;EmpID&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;END&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The syntax differs, but both follow the same logical pattern.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Return Values (or Results)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A stored procedure can return a result set or output parameters, while a Python function returns a value or object.&lt;/p&gt;

&lt;p&gt;SQL: SELECT statements inside a stored procedure return results to the caller.&lt;/p&gt;

&lt;p&gt;Python: return sends back data to the calling code.&lt;/p&gt;

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

&lt;p&gt;SQL&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;EXEC&lt;/span&gt; &lt;span class="n"&gt;GetHighEarners&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;MinSalary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;80000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;get_high_earners&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;80000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both act as black boxes — you provide input and get output.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Security and Access Control&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In SQL, stored procedures help control access to underlying tables by granting users permission to run procedures instead of querying tables directly.&lt;br&gt;
In Python, while not about access control in the same sense, functions can hide implementation details, exposing only necessary interfaces.&lt;/p&gt;

&lt;p&gt;In both cases, this encapsulation helps with abstraction and safety.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Performance Benefits&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Stored procedures are precompiled and cached by the database engine, leading to faster execution for repeated tasks.&lt;/p&gt;

&lt;p&gt;Python functions can also improve performance indirectly — by reducing redundant code and centralizing logic that might otherwise run inefficiently in multiple places.&lt;/p&gt;

&lt;p&gt;While the underlying performance mechanics differ, the intent — optimization through reuse — is shared.&lt;br&gt;
Both act as black boxes — you provide input and get output.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Security and Access Control&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In SQL, stored procedures help control access to underlying tables by granting users permission to run procedures instead of querying tables directly.&lt;br&gt;
In Python, while not about access control in the same sense, functions can hide implementation details, exposing only necessary interfaces.&lt;/p&gt;

&lt;p&gt;In both cases, this encapsulation helps with abstraction and safety.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Performance Benefits&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Stored procedures are precompiled and cached by the database engine, leading to faster execution for repeated tasks.&lt;/p&gt;

&lt;p&gt;Python functions can also improve performance indirectly — by reducing redundant code and centralizing logic that might otherwise run inefficiently in multiple places.&lt;/p&gt;

&lt;p&gt;While the underlying performance mechanics differ, the intent — optimization through reuse — is shared.&lt;/p&gt;

&lt;p&gt;In conclusion, at a conceptual level, a stored procedure in SQL is to a database what a function is to a Python program — both encapsulate logic, promote reusability, and enhance maintainability.&lt;/p&gt;

&lt;p&gt;When I first started bridging SQL with Python (for analytics and ETL work), recognizing these parallels made it much easier to design systems where each layer handled what it’s best at:&lt;/p&gt;

&lt;p&gt;SQL for data manipulation and set-based logic,&lt;/p&gt;

&lt;p&gt;Python for orchestration, computation, and automation.&lt;/p&gt;

</description>
      <category>python</category>
      <category>sql</category>
      <category>database</category>
      <category>programming</category>
    </item>
    <item>
      <title>Understanding the Difference Between Subquery, CTE, and Stored Procedure</title>
      <dc:creator>Ian Macharia Mwangi</dc:creator>
      <pubDate>Wed, 05 Nov 2025 09:31:26 +0000</pubDate>
      <link>https://dev.to/ian_mwangi/understanding-the-difference-between-subquery-cte-and-stored-procedure-1k37</link>
      <guid>https://dev.to/ian_mwangi/understanding-the-difference-between-subquery-cte-and-stored-procedure-1k37</guid>
      <description>&lt;p&gt;When working with SQL, one of the key skills that separates intermediate developers from advanced database professionals is knowing how and when to use different query structures.&lt;/p&gt;

&lt;p&gt;Three of the most commonly misunderstood SQL components are subqueries, Common Table Expressions (CTEs), and stored procedures. While they might seem similar at first glance — all allow you to organize or modularize your logic — they serve different purposes and have different performance implications.&lt;/p&gt;

&lt;p&gt;In this article, I’ll break down each concept, highlight where it shines, and show some real-world examples from my own experience.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Subquery — A Query Within a Query&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A subquery is simply a query nested inside another SQL statement. It’s often used to filter, calculate, or compare values dynamically.&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;Example&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; 
    &lt;span class="n"&gt;EmployeeName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;Salary&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;Salary&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;Salary&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;Employees&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the inner query calculates the average salary, and the outer query selects only employees earning more than that.&lt;/p&gt;

&lt;p&gt;This pattern is great for short, inline calculations or when you don’t want to repeat the same logic elsewhere.&lt;/p&gt;

&lt;p&gt;When to Use a Subquery&lt;/p&gt;

&lt;p&gt;When you need a quick, one-off result from another table or calculation.&lt;/p&gt;

&lt;p&gt;When joining tables would make the main query unnecessarily complex.&lt;/p&gt;

&lt;p&gt;When readability and simplicity are more important than reusability.&lt;/p&gt;

&lt;p&gt;Limitations&lt;/p&gt;

&lt;p&gt;Subqueries can be less efficient than joins in some databases because they execute repeatedly for each row (depending on the optimizer).&lt;/p&gt;

&lt;p&gt;They can’t always be reused elsewhere in the same query.&lt;/p&gt;

&lt;p&gt;Deeply nested subqueries can make debugging painful.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;CTE (Common Table Expression) — A Temporary Result Set&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A CTE, introduced in SQL Server 2005 and supported by most modern databases, allows you to define a temporary, named result set that can be referenced within the same statement.&lt;/p&gt;

&lt;p&gt;Example&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;DepartmentAverage&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;DepartmentID&lt;/span&gt;&lt;span class="p"&gt;,&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;Salary&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;AvgSalary&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;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;DepartmentID&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;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;EmployeeName&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;DepartmentID&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;Salary&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AvgSalary&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;DepartmentAverage&lt;/span&gt; &lt;span class="n"&gt;d&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;DepartmentID&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DepartmentID&lt;/span&gt;
&lt;span class="k"&gt;WHERE&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;Salary&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AvgSalary&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the CTE DepartmentAverage calculates each department’s average salary once, and the outer query uses it to find above-average earners.&lt;/p&gt;

&lt;p&gt;This approach is more readable and maintainable than embedding multiple subqueries.&lt;/p&gt;

&lt;p&gt;When to Use a CTE&lt;/p&gt;

&lt;p&gt;When you want to organize complex queries into readable logical blocks.&lt;/p&gt;

&lt;p&gt;When you need to reference the same derived result multiple times within one query.&lt;/p&gt;

&lt;p&gt;When working with recursive queries (e.g., hierarchical data like org charts).&lt;/p&gt;

&lt;p&gt;Limitations&lt;/p&gt;

&lt;p&gt;CTEs exist only during the execution of the query; they are not stored permanently.&lt;/p&gt;

&lt;p&gt;They don’t automatically improve performance (they’re mainly about readability and structure).&lt;/p&gt;

&lt;p&gt;For very large datasets, a temporary table might perform better.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Stored Procedure — Precompiled Logic on the Server&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A stored procedure is a precompiled, reusable block of SQL logic stored in the database.&lt;br&gt;
Unlike subqueries or CTEs, which are part of a single query, a stored procedure is a stand-alone database object.&lt;/p&gt;

&lt;p&gt;Example&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;PROCEDURE&lt;/span&gt; &lt;span class="n"&gt;GetHighEarners&lt;/span&gt;
    &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;MinSalary&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt;
&lt;span class="k"&gt;AS&lt;/span&gt;
&lt;span class="k"&gt;BEGIN&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;EmployeeName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Salary&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;DepartmentID&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;Salary&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;MinSalary&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;END&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXEC&lt;/span&gt; &lt;span class="n"&gt;GetHighEarners&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;MinSalary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;80000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This approach is efficient and scalable, especially for complex business logic that runs frequently.&lt;/p&gt;

&lt;p&gt;When to Use a Stored Procedure&lt;/p&gt;

&lt;p&gt;When you need to encapsulate logic and reuse it across multiple applications.&lt;/p&gt;

&lt;p&gt;When you want to improve performance through precompilation.&lt;/p&gt;

&lt;p&gt;When enforcing security or access control — users can execute procedures without seeing the underlying tables.&lt;/p&gt;

&lt;p&gt;When building ETL processes, batch jobs, or automated reporting.&lt;/p&gt;

&lt;p&gt;Limitations&lt;/p&gt;

&lt;p&gt;Stored procedures require database-level maintenance — not ideal for ad-hoc queries.&lt;/p&gt;

&lt;p&gt;They can become hard to version-control if business logic changes often.&lt;/p&gt;

&lt;p&gt;They’re specific to a database engine (e.g., T-SQL for SQL Server, PL/pgSQL for PostgreSQL).&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;My Take — Choosing the Right One&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In practice, I use all three, depending on the situation:&lt;/p&gt;

&lt;p&gt;Subqueries for quick filters or checks.&lt;/p&gt;

&lt;p&gt;CTEs when readability matters and I want to logically break a big query into parts.&lt;/p&gt;

&lt;p&gt;Stored procedures when I’m implementing repeatable or parameterized business logic.&lt;/p&gt;

&lt;p&gt;There’s no universal “best” — it depends on whether you’re optimizing for performance, maintainability, or reuse.&lt;/p&gt;

&lt;p&gt;The key is to understand how each behaves under the hood so you can choose the right tool for the job.&lt;/p&gt;

&lt;p&gt;Final Thoughts&lt;/p&gt;

&lt;p&gt;Think of it like this:&lt;/p&gt;

&lt;p&gt;A subquery is a snippet of logic.&lt;/p&gt;

&lt;p&gt;A CTE is a named snippet.&lt;/p&gt;

&lt;p&gt;A stored procedure is a function you can call again and again.&lt;/p&gt;

&lt;p&gt;Mastering when to use each will make your SQL code not only faster but also more elegant and maintainable.&lt;/p&gt;

</description>
      <category>performance</category>
      <category>sql</category>
      <category>tutorial</category>
      <category>database</category>
    </item>
    <item>
      <title>Excel’s Strengths and Weaknesses in Predictive Analysis and Its Role in Data-Driven Business Decisions</title>
      <dc:creator>Ian Macharia Mwangi</dc:creator>
      <pubDate>Sun, 10 Aug 2025 17:53:54 +0000</pubDate>
      <link>https://dev.to/ian_mwangi/excels-strengths-and-weaknesses-in-predictive-analysis-and-its-role-in-data-driven-business-1f9e</link>
      <guid>https://dev.to/ian_mwangi/excels-strengths-and-weaknesses-in-predictive-analysis-and-its-role-in-data-driven-business-1f9e</guid>
      <description>&lt;h1&gt;
  
  
  Excel’s Strengths and Weaknesses in Predictive Analysis and Its Role in Data-Driven Business Decisions
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;When it comes to business tools, Microsoft Excel is almost legendary. From finance to marketing, small startups to multinational corporations, it’s hard to find a professional who hasn’t used it in some form. Over the years, Excel has evolved from a simple spreadsheet program into a versatile analytical tool—capable of handling everything from basic data organization to complex forecasting models.&lt;/p&gt;

&lt;p&gt;In my research, I found that while Excel can absolutely support predictive analysis and guide data-driven business decisions, it’s not without its pitfalls. Let’s explore both sides of the coin.&lt;/p&gt;




&lt;h2&gt;
  
  
  Strengths of Excel in Predictive Analysis
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Accessibility and Familiarity
&lt;/h3&gt;

&lt;p&gt;One of Excel’s biggest strengths is its universal familiarity. Most professionals already have some level of comfort using it, which means predictive models can be implemented without extensive training. Plus, it’s included in most Office 365 subscriptions, so the barrier to entry is low.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Built-in Analytical Tools
&lt;/h3&gt;

&lt;p&gt;Excel offers functions like &lt;code&gt;FORECAST.LINEAR&lt;/code&gt;, &lt;code&gt;TREND&lt;/code&gt;, and even the Data Analysis Toolpak for regression analysis. Combined with pivot tables, charts, and conditional formatting, users can quickly turn raw data into meaningful trends and forecasts.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Flexibility
&lt;/h3&gt;

&lt;p&gt;Unlike specialized analytics software, Excel isn’t locked into a specific type of analysis. You can customize formulas, link datasets, and even use VBA (Visual Basic for Applications) to automate repetitive predictive tasks.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Integration with Other Tools
&lt;/h3&gt;

&lt;p&gt;Excel plays nicely with other platforms—importing from databases, APIs, and CSV files is straightforward. This makes it easier to feed historical data into predictive models.&lt;/p&gt;




&lt;h2&gt;
  
  
  Weaknesses of Excel in Predictive Analysis
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Scalability Issues
&lt;/h3&gt;

&lt;p&gt;Excel works well for small to medium-sized datasets, but once you start dealing with millions of rows or real-time data streams, it becomes sluggish or prone to crashing.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Limited Advanced Modeling
&lt;/h3&gt;

&lt;p&gt;While Excel handles basic forecasting well, it’s not designed for advanced machine learning models or AI-driven predictions. Specialized tools like Python (with scikit-learn) or R are better suited for that.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Error Sensitivity
&lt;/h3&gt;

&lt;p&gt;Human error in formula entry, data input, or referencing can lead to inaccurate predictions. In predictive analysis, even small mistakes can cause significant misdirection in decision-making.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Collaboration Limitations
&lt;/h3&gt;

&lt;p&gt;Although Excel Online and cloud storage have improved collaboration, version control issues can still arise, especially in predictive models that require constant updates.&lt;/p&gt;




&lt;h2&gt;
  
  
  Role of Excel in Data-Driven Business Decisions
&lt;/h2&gt;

&lt;p&gt;Despite its limitations, Excel remains a cornerstone in business decision-making. Here’s why:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Quick Prototyping&lt;/strong&gt;: Before committing resources to complex analytics platforms, businesses can use Excel to create quick, cost-effective predictive models.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Decision Support&lt;/strong&gt;: Forecasting sales, estimating demand, and budgeting can be done efficiently in Excel, helping leaders make informed choices.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Data Visualization&lt;/strong&gt;: Through charts, dashboards, and conditional formatting, decision-makers can quickly grasp trends and patterns.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Accessibility Across Departments&lt;/strong&gt;: From finance teams tracking cash flow to marketing teams analyzing campaign performance, Excel’s familiarity ensures that insights are understandable company-wide.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Excel is not a magic wand for predictive analysis—it’s a versatile but limited tool. It shines in its accessibility, flexibility, and ability to bridge the gap between raw data and actionable insights. However, for large-scale, high-complexity predictive modeling, businesses may need to integrate Excel with more specialized tools.&lt;/p&gt;

&lt;p&gt;The real power lies in knowing when Excel is “good enough” and when it’s time to scale up. Used wisely, it can still be a trusted ally in making smart, data-driven business decisions.&lt;/p&gt;

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