<?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: Elizabeth Kimani</title>
    <description>The latest articles on DEV Community by Elizabeth Kimani (@elizabeth_kimani_bca65d6e).</description>
    <link>https://dev.to/elizabeth_kimani_bca65d6e</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%2F4212162%2F1b5790e6-4fc0-4b60-beb5-d88d092c3fa8.png</url>
      <title>DEV Community: Elizabeth Kimani</title>
      <link>https://dev.to/elizabeth_kimani_bca65d6e</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/elizabeth_kimani_bca65d6e"/>
    <language>en</language>
    <item>
      <title>Filtering Data in SQL: A Beginner's Guide to WHERE, IN, BETWEEN, NULL, and LIKE</title>
      <dc:creator>Elizabeth Kimani</dc:creator>
      <pubDate>Wed, 28 Oct 2026 20:44:12 +0000</pubDate>
      <link>https://dev.to/elizabeth_kimani_bca65d6e/filtering-data-in-sql-a-beginners-guide-to-where-in-between-null-and-like-3iod</link>
      <guid>https://dev.to/elizabeth_kimani_bca65d6e/filtering-data-in-sql-a-beginners-guide-to-where-in-between-null-and-like-3iod</guid>
      <description>&lt;p&gt;Hi everyone! I am learning SQL, and this week we studied operators. Operators are the words and symbols we write after &lt;code&gt;WHERE&lt;/code&gt; to tell SQL which rows we want. I practised on a practice hospital table called &lt;code&gt;patients&lt;/code&gt; in PostgreSQL. It has columns like &lt;code&gt;county&lt;/code&gt;, &lt;code&gt;department&lt;/code&gt;, &lt;code&gt;age&lt;/code&gt;, &lt;code&gt;bill_amount&lt;/code&gt;, &lt;code&gt;insurance&lt;/code&gt; and `email. In this post, I explain what I learned, using queries I wrote myself.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Comparison operators
&lt;/h2&gt;

&lt;p&gt;Comparison operators compare a column with a value. They are &lt;code&gt;=&lt;/code&gt;, &lt;code&gt;!=&lt;/code&gt; (or &lt;code&gt;&amp;lt;&amp;gt;&lt;/code&gt;), &lt;code&gt;&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;&lt;/code&gt;, &lt;code&gt;&amp;gt;=&lt;/code&gt; and &lt;code&gt;&amp;lt;=&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;sql&lt;br&gt;
SELECT first_name, last_name, department, bill_amount&lt;br&gt;
FROM patients&lt;br&gt;
WHERE bill_amount &amp;gt; 100000;&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This shows only the patients whose bill is more than 100,000. Two small things I noticed. Text must be in single quotes, like &lt;code&gt;county = 'Kisumu'&lt;/code&gt;, but numbers must not be. For dates, the &lt;code&gt;YYYY-MM-DD&lt;/code&gt; format, like &lt;code&gt;'2026-04-01'&lt;/code&gt;, is the safest.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Logical operators: AND, OR and NOT
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;AND&lt;/code&gt; means both conditions must be true. &lt;code&gt;OR&lt;/code&gt; means at least one must be true.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;sql&lt;br&gt;
SELECT *&lt;br&gt;
FROM patients&lt;br&gt;
WHERE gender = 'Female'&lt;br&gt;
  AND department = 'Maternity';&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This gives me only female patients in the Maternity department. A row must pass both tests to appear.&lt;/p&gt;

&lt;p&gt;For &lt;code&gt;OR&lt;/code&gt;, I first wrote &lt;code&gt;county = 'Nairobi' OR county = 'Mombasa'&lt;/code&gt;. It works, but I prefer &lt;code&gt;IN&lt;/code&gt; because it groups the values clearly. I explain &lt;code&gt;IN&lt;/code&gt; in the next section.&lt;/p&gt;

&lt;p&gt;One more thing. SQL does &lt;code&gt;AND&lt;/code&gt; before &lt;code&gt;OR&lt;/code&gt;. When I use both in one query, I put brackets around the part that belongs together, so the meaning is clear to anyone reading it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;sql&lt;br&gt;
SELECT *&lt;br&gt;
FROM patients&lt;br&gt;
WHERE (gender = 'Male' AND age &amp;gt; 40) OR bill_amount &amp;gt; 150000;&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Range and list operators: BETWEEN and IN
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;BETWEEN&lt;/code&gt; selects values inside a range, and it includes both ends. I wanted to be sure, so I tested it. One patient has a bill of exactly 45,000. When I made 45,000 the upper limit, that patient was still in the result. This proved that the last number is included.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;sql&lt;br&gt;
SELECT first_name, bill_amount&lt;br&gt;
FROM patients&lt;br&gt;
WHERE bill_amount BETWEEN 10000 AND 45000;&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;IN&lt;/code&gt; checks a column against a list of values. It is shorter than writing many &lt;code&gt;OR&lt;/code&gt; conditions. Adding &lt;code&gt;NOT&lt;/code&gt; gives the opposite.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;sql&lt;br&gt;
SELECT *&lt;br&gt;
FROM patients&lt;br&gt;
WHERE department NOT IN ('Cardiology', 'Orthopedics', 'Emergency');&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This gives me every patient who is not in those three departments.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. NULL: a missing value
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;NULL&lt;/code&gt; does not mean zero, and it does not mean an empty space. It means the value is missing or unknown. To find these rows, we use &lt;code&gt;IS NULL&lt;/code&gt;. To find rows that do have a value, we use &lt;code&gt;IS NOT NULL&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;sql&lt;br&gt;
SELECT *&lt;br&gt;
FROM patients&lt;br&gt;
WHERE email IS NULL;&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This lists the patients who have no email on file. In a real hospital, a list like this would show the staff which records are incomplete.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. LIKE: searching for patterns
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;LIKE&lt;/code&gt; searches for text that follows a pattern. It has two special symbols:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;%&lt;/code&gt; means any number of characters, even none&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;_&lt;/code&gt; means exactly one character&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;sql&lt;br&gt;
SELECT *&lt;br&gt;
FROM patients&lt;br&gt;
WHERE first_name LIKE 'K%';&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This finds every first name that starts with K. I also tried &lt;code&gt;'%gmail%'&lt;/code&gt; on the email column to find Gmail users, and &lt;code&gt;'_i%'&lt;/code&gt; to find names with &lt;code&gt;i&lt;/code&gt; as the second letter.&lt;/p&gt;

&lt;h2&gt;
  
  
  Common mistakes
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Mistake 1: using &lt;code&gt;= NULL&lt;/code&gt;.&lt;/strong&gt; I wrote this on purpose to see what happens.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;sql&lt;br&gt;
SELECT *&lt;br&gt;
FROM patients&lt;br&gt;
WHERE email = NULL;&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It returns nothing, even though some emails are missing. &lt;code&gt;NULL&lt;/code&gt; means unknown, so nothing can be equal to it. The correct way is &lt;code&gt;IS NULL&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;**Mistake 2: forgetting that &lt;code&gt;LIKE&lt;/code&gt; is case-sensitive.&lt;br&gt;
**In PostgreSQL, &lt;code&gt;LIKE&lt;/code&gt; sees &lt;code&gt;K&lt;/code&gt; and &lt;code&gt;k&lt;/code&gt; as two different letters. When I searched for &lt;code&gt;'k%'&lt;/code&gt; in lowercase, I got no rows because the names start with a capital K. The fix is &lt;code&gt;ILIKE&lt;/code&gt;, which "ignores k and K”&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;sql&lt;br&gt;
SELECT *&lt;br&gt;
FROM patients&lt;br&gt;
WHERE first_name ILIKE 'k%';&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Mistake 3: mixing &lt;code&gt;AND&lt;/code&gt; with &lt;code&gt;OR&lt;/code&gt; without brackets.&lt;/strong&gt; Because &lt;code&gt;AND&lt;/code&gt; runs first, a query can return rows I did not mean to include. Brackets make my meaning clear.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I learned
&lt;/h2&gt;

&lt;p&gt;Filtering is easy once you know the operators, but small things change the answer: quotes, &lt;code&gt;NULL&lt;/code&gt;, capital letters, and brackets. From now on, I will check my results every time, not only check that the query ran without an error.&lt;/p&gt;

&lt;p&gt;I am still learning, so if you have tips for a beginner, I would love to hear them. Thank you for reading!&lt;/p&gt;

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