<?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: Winny Muusi</title>
    <description>The latest articles on DEV Community by Winny Muusi (@muusidata).</description>
    <link>https://dev.to/muusidata</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%2F4185903%2Fc1ef2496-f011-4d29-8c68-78ea4a2134cc.png</url>
      <title>DEV Community: Winny Muusi</title>
      <link>https://dev.to/muusidata</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/muusidata"/>
    <language>en</language>
    <item>
      <title>Filtering In SQL Is Just Asking Your Data Politely.</title>
      <dc:creator>Winny Muusi</dc:creator>
      <pubDate>Thu, 29 Oct 2026 15:19:48 +0000</pubDate>
      <link>https://dev.to/muusidata/filtering-in-sql-is-just-asking-your-data-politely-4ajn</link>
      <guid>https://dev.to/muusidata/filtering-in-sql-is-just-asking-your-data-politely-4ajn</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;A Database Is like a Messy Wardrobe. SQL Is How You Find the Right Cloth.&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;You could be new to SQL(postgres) or probably transitioning careers, and maybe you don't have a guide. For querying this the easy way,think of it like a cluttered wardbobe and with one or a few query(ies) you declutter and find the right cloth. That's how you get to know your dataset.&lt;/p&gt;

&lt;p&gt;Dataset I'll use has this info: &lt;br&gt;
&lt;strong&gt;Table_name:&lt;/strong&gt; sales_data&lt;br&gt;
&lt;strong&gt;Columns:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;order_id&lt;/li&gt;
&lt;li&gt;order_date&lt;/li&gt;
&lt;li&gt;customer_id&lt;/li&gt;
&lt;li&gt;customer_name&lt;/li&gt;
&lt;li&gt;city&lt;/li&gt;
&lt;li&gt;region&lt;/li&gt;
&lt;li&gt;product_id&lt;/li&gt;
&lt;li&gt;product_name&lt;/li&gt;
&lt;li&gt;category&lt;/li&gt;
&lt;li&gt;unit_price&lt;/li&gt;
&lt;li&gt;quantity&lt;/li&gt;
&lt;li&gt;discount_pct&lt;/li&gt;
&lt;li&gt;total_amount&lt;/li&gt;
&lt;li&gt;payment_method&lt;/li&gt;
&lt;li&gt;sales_rep&lt;/li&gt;
&lt;li&gt;order_status&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;NOTE://&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;where:&lt;/code&gt; is a clause used to filters values that meets the condition.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;*&lt;/code&gt; is a symbol for all, basically means you're querying all columns from the table.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Filtering our data :
&lt;/h2&gt;
&lt;h3&gt;
  
  
  1. Comparison (=, &amp;lt;&amp;gt;/!=, &amp;gt;, &amp;lt;, &amp;gt;=, &amp;lt;=)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Equal to&lt;/code&gt; = &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Not Equal&lt;/code&gt; &amp;lt;&amp;gt;/!= &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Greater than&lt;/code&gt; &amp;gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Less than&lt;/code&gt; &amp;lt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Greater than or equal to&lt;/code&gt; &amp;gt;=&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Less than or equal to&lt;/code&gt; &amp;lt;=&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's use any of the comparison operators, the syntax is the same only operator changes.&lt;br&gt;
&lt;strong&gt;For example:&lt;/strong&gt; Filter orders where quantity is 3 or more.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sales_data&lt;/span&gt;
&lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;quantity&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Logical (AND, OR, NOT, and parentheses)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;AND:&lt;/code&gt; returns results when both conditions are met.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;OR:&lt;/code&gt; returns results when one condition is met.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;NOT:&lt;/code&gt; negates the condition or queries what does not match the criteria or viceversa of (true and false).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's use each operator and see how it works.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For example:&lt;/strong&gt; Filter orders made by clients in Nairobi and Mombasa.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sales_data&lt;/span&gt;
&lt;span class="k"&gt;where&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;and&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;'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;strong&gt;For Example:&lt;/strong&gt; Filter cancelled or returned 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="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sales_data&lt;/span&gt;
&lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;order_status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Cancelled'&lt;/span&gt;
&lt;span class="k"&gt;or&lt;/span&gt; &lt;span class="n"&gt;order_status&lt;/span&gt; &lt;span class="o"&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;strong&gt;For example:&lt;/strong&gt; Filter orders in the Eastern region that are not Completed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sales_data&lt;/span&gt;
&lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;region&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Eastern'&lt;/span&gt;
&lt;span class="k"&gt;and&lt;/span&gt; &lt;span class="k"&gt;not&lt;/span&gt; &lt;span class="n"&gt;order_status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="s1"&gt;'Completed'&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;Explanation :&lt;/strong&gt; The AND is to combine the conditions but the NOT has to query orders where the order status was not completed.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Range/List (BETWEEN, IN, NOT IN)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Between:&lt;/code&gt; includes the beginning and end values of the range.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;IN:&lt;/code&gt; checks if the conditions matches the values in the list.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;NOT IN:&lt;/code&gt; excludes the values that match the list condition.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;For example:&lt;/strong&gt; Filter orders placed between mid-march and  mid-may 2025.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sales_data&lt;/span&gt;
&lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;order_date&lt;/span&gt; &lt;span class="k"&gt;between&lt;/span&gt; &lt;span class="s1"&gt;'2025-03-15'&lt;/span&gt; &lt;span class="k"&gt;and&lt;/span&gt; &lt;span class="s1"&gt;'2025-05-15'&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;For example:&lt;/strong&gt; Filter orders paid by Card or Bank Transfer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sales_data&lt;/span&gt;
&lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;payment_method&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;'Card'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Bank Transfer'&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;For example:&lt;/strong&gt; Filter orders made by clients out of Nairobi, kisumu, mombasa.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sales_data&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;not&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;'Kisumu'&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;h3&gt;
  
  
  4. NULL (IS NULL, IS NOT NULL)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;IS NULL:&lt;/code&gt; checks for empty values.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;IS NOT NULL:&lt;/code&gt; checks for non empty values.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;For example:&lt;/strong&gt; Filter orders without discount.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sales_data&lt;/span&gt; 
&lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;discount_pct&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;For example:&lt;/strong&gt; Filter orders that have a discount.&lt;br&gt;
This will include zeros.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sales_data&lt;/span&gt;
&lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;discount_pct&lt;/span&gt; &lt;span class="k"&gt;is&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;A query for you:&lt;/strong&gt; Filter orders with discount but not where values were zeros since it still mean the discount is zero only that is not empty.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Check the clause below&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sales_data&lt;/span&gt;
&lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;discount_pct&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;not&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;
&lt;span class="k"&gt;and&lt;/span&gt; &lt;span class="n"&gt;discount_pct&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt;&lt;span class="mi"&gt;0&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;Explanation:&lt;/strong&gt; Let me explain a bit. We've IS NOT NULL, AND and !=&lt;br&gt;
Is not null retrieves non empty values, the AND makes sure both conditions have to be true, != means not equal to or it negates the equal to so it retrieves orders where discount is not null and not zero basically meaning there must be a discount.&lt;/p&gt;
&lt;h3&gt;
  
  
  5. LIKE wildcards(%, _)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;%:&lt;/code&gt; shows zero, one or multiple characters.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;_:&lt;/code&gt; shows a single character.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;For example:&lt;/strong&gt; Filter products with 'inch' in the name.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sales_data&lt;/span&gt;
&lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;product_name&lt;/span&gt; &lt;span class="k"&gt;like&lt;/span&gt; &lt;span class="s1"&gt;'%inch%'&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;For example:&lt;/strong&gt; Filter customer Id whose name starts with C1.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sales_data&lt;/span&gt;
&lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="k"&gt;like&lt;/span&gt; &lt;span class="s1"&gt;'C1__'&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;Explanation:&lt;/strong&gt; I used two underscores(__) to specify the ID should end with 2 characters.&lt;/p&gt;

&lt;h3&gt;
  
  
  Wrap Up.
&lt;/h3&gt;

&lt;p&gt;For a beginner, dataset can feel like a messy wardrobe. All you need to do first is go through it and check the columns and data types. With a few filters you can understand it.&lt;br&gt;
Then, ensure for every query you analyze which columns and which clauses you need to get the expected output before writing the code.&lt;br&gt;
&lt;strong&gt;At last, One common mistake to avoid :&lt;/strong&gt; don't forget to close with &lt;code&gt;;&lt;/code&gt;.&lt;/p&gt;

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