<?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: Felix Otieno</title>
    <description>The latest articles on DEV Community by Felix Otieno (@felix_otieno_536ee8413e58).</description>
    <link>https://dev.to/felix_otieno_536ee8413e58</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%2F2975883%2F13decc2e-a1cf-4151-a75f-733c76726a09.png</url>
      <title>DEV Community: Felix Otieno</title>
      <link>https://dev.to/felix_otieno_536ee8413e58</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/felix_otieno_536ee8413e58"/>
    <language>en</language>
    <item>
      <title>Filtering Data in SQL: A Beginner’s Guide to WHERE, AND, LIKE, and Other Operators</title>
      <dc:creator>Felix Otieno</dc:creator>
      <pubDate>Sun, 25 Oct 2026 17:10:30 +0000</pubDate>
      <link>https://dev.to/felix_otieno_536ee8413e58/filtering-data-in-sql-a-beginners-guide-to-where-and-like-and-other-operators-3ma8</link>
      <guid>https://dev.to/felix_otieno_536ee8413e58/filtering-data-in-sql-a-beginners-guide-to-where-and-like-and-other-operators-3ma8</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;I learned how to use SQL operators to filter and retrieve specific records from a database. I practised these concepts using PostgreSQL and a hospital database containing patient information such as names, ages, counties, insurance providers, departments, bills, and admission dates.&lt;/p&gt;

&lt;p&gt;SQL operators are important because they help us retrieve meaningful information without having to examine every record manually. For example, a hospital might want to find female patients admitted to the maternity department, patients with bills above KSh 50,000, or patients whose email addresses are missing.&lt;/p&gt;

&lt;p&gt;In this article, I will share what I learned about comparison operators, logical operators, range and list operators, NULL handling, and pattern matching in SQL.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Comparison Operators&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Comparison operators allow us to compare values in a database. They include:&lt;/p&gt;

&lt;p&gt;= — equal to&lt;/p&gt;

&lt;p&gt;&amp;lt;&amp;gt; — not equal to&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;— greater than&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&amp;lt; — less than&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;= — greater than or equal to&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&amp;lt;= — less than or equal to&lt;/p&gt;

&lt;p&gt;For example, I can retrieve patients whose bills exceed KSh 150,000.&lt;/p&gt;

&lt;p&gt;SELECT *&lt;br&gt;
FROM patients&lt;br&gt;
WHERE bill_amount &amp;gt; 150000;&lt;/p&gt;

&lt;p&gt;This query returns patients whose bills are greater than 150,000. A bill of exactly 150,000 would not be included because the operator is &amp;gt; rather than &amp;gt;=.&lt;/p&gt;

&lt;p&gt;I also learned that comparison operators work with different data types. Numeric comparisons work with numbers, while date comparisons can identify records within specific periods.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;SELECT *&lt;br&gt;
FROM patients&lt;br&gt;
WHERE admission_date &amp;gt;= '2026-01-01'&lt;br&gt;
  AND admission_date &amp;lt;= '2026-03-31';&lt;/p&gt;

&lt;p&gt;This retrieves patients admitted during the first quarter of 2026.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Logical Operators: AND, OR, and NOT&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Logical operators combine or reverse conditions.&lt;/p&gt;

&lt;p&gt;AND requires all specified conditions to be true. For example, to find female patients in the maternity department:&lt;/p&gt;

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

&lt;p&gt;Both conditions must be satisfied for a patient to appear in the results.&lt;/p&gt;

&lt;p&gt;OR returns records when at least one condition is true. For example:&lt;/p&gt;

&lt;p&gt;SELECT *&lt;br&gt;
FROM patients&lt;br&gt;
WHERE county = 'Kisumu'&lt;br&gt;
   OR county = 'Nairobi';&lt;/p&gt;

&lt;p&gt;This retrieves patients from either Kisumu or Nairobi.&lt;/p&gt;

&lt;p&gt;NOT reverses a condition. For example:&lt;/p&gt;

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

&lt;p&gt;This excludes patients in the listed departments.&lt;/p&gt;

&lt;p&gt;These operators are useful when business questions require multiple conditions to be evaluated together.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Range and List Operators: BETWEEN and IN&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I learned that BETWEEN checks whether a value falls within a specified range. Its boundaries are inclusive, meaning both the minimum and maximum values are included.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;SELECT *&lt;br&gt;
FROM patients&lt;br&gt;
WHERE bill_amount BETWEEN 10000 AND 50000;&lt;/p&gt;

&lt;p&gt;This returns patients whose bills range from 10,000 to 50,000, including patients with bills of exactly 10,000 or 50,000.&lt;/p&gt;

&lt;p&gt;The IN operator checks whether a value matches any item in a specified list. It can make queries shorter and easier to read.&lt;/p&gt;

&lt;p&gt;SELECT *&lt;br&gt;
FROM patients&lt;br&gt;
WHERE insurance IN ('SHA', 'AAR', 'Britam');&lt;/p&gt;

&lt;p&gt;This retrieves patients insured by SHA, AAR, or Britam.&lt;/p&gt;

&lt;p&gt;Instead of writing several conditions joined by OR, I can use IN when checking one column against multiple possible values.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Working with NULL Values&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Another important lesson was understanding NULL. In SQL, NULL represents a missing or unknown value. It is different from zero, an empty string, or the text 'NULL'.&lt;/p&gt;

&lt;p&gt;To find patients whose email addresses are missing, I use:&lt;/p&gt;

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

&lt;p&gt;To find patients who have an email address, I use:&lt;/p&gt;

&lt;p&gt;SELECT first_name, last_name, insurance&lt;br&gt;
FROM patients&lt;br&gt;
WHERE email IS NOT NULL;&lt;/p&gt;

&lt;p&gt;A common mistake is writing email = NULL. This does not correctly test for missing values because comparisons with NULL generally produce an unknown result.&lt;/p&gt;

&lt;p&gt;The correct operators are IS NULL and IS NOT NULL.&lt;/p&gt;

&lt;p&gt;This knowledge is particularly useful when cleaning datasets and identifying incomplete records before analysis.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Pattern Matching with LIKE and ILIKE&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Pattern matching helps retrieve records based on text patterns.&lt;/p&gt;

&lt;p&gt;The LIKE operator is case-sensitive in PostgreSQL. Two useful wildcards are:&lt;/p&gt;

&lt;p&gt;% represents zero or more characters.&lt;/p&gt;

&lt;p&gt;_ represents exactly one character.&lt;/p&gt;

&lt;p&gt;For example, to find patients whose first names begin with K:&lt;/p&gt;

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

&lt;p&gt;To find patients whose email addresses end in gmail.com:&lt;/p&gt;

&lt;p&gt;SELECT *&lt;br&gt;
FROM patients&lt;br&gt;
WHERE email LIKE '%gmail.com';&lt;/p&gt;

&lt;p&gt;I also learned that ILIKE performs case-insensitive pattern matching in PostgreSQL.&lt;/p&gt;

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

&lt;p&gt;Unlike LIKE 'k%', this query can match names beginning with either lowercase or uppercase K.&lt;/p&gt;

&lt;p&gt;These operators are helpful when searching customer names, email addresses, product descriptions, and other text fields.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. Common Mistakes to Avoid&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;During my practice, I identified several mistakes beginners should watch out for.&lt;/p&gt;

&lt;p&gt;Using = NULL: Use &lt;strong&gt;IS NULL&lt;/strong&gt; instead.&lt;/p&gt;

&lt;p&gt;Forgetting that LIKE is case-sensitive: In PostgreSQL, LIKE 'k%' will not match Kevin. Use ILIKE 'k%' when case-insensitive matching is required.&lt;/p&gt;

&lt;p&gt;Misunderstanding BETWEEN: Both boundaries are included. Use &amp;gt; and &amp;lt; when the boundaries should be excluded.&lt;/p&gt;

&lt;p&gt;Misusing AND and OR: Logical operators can produce unexpected results when conditions are not grouped correctly. Parentheses make the intended logic explicit.&lt;/p&gt;

&lt;p&gt;For example, if I want patients from Kisumu or Nairobi who have bills above 50,000, I should write:&lt;/p&gt;

&lt;p&gt;SELECT *&lt;br&gt;
FROM patients&lt;br&gt;
WHERE (county = 'Kisumu' OR county = 'Nairobi')&lt;br&gt;
  AND bill_amount &amp;gt; 50000;&lt;/p&gt;

&lt;p&gt;The parentheses ensure that the county condition is evaluated together before applying the bill requirement.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;SQL practice helped me understand how operators make database queries more precise and useful. I learned how to compare values, combine conditions, filter ranges and lists, identify missing data, and search for text patterns using PostgreSQL.&lt;/p&gt;

&lt;p&gt;These skills are useful in data analytics, reporting, database administration, and application development. I plan to keep practicing with larger datasets so that I can write more efficient queries and answer real-world questions with confidence.&lt;/p&gt;

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