Overview

5 Filtering on specific values

This chapter moves from returning whole tables to retrieving only the rows you need by applying conditions with the WHERE clause. It introduces filtering as the core of purposeful SQL and shows how it narrows broad result sets to meaningful subsets. You learn the foundational mechanics for filtering across data types, then progress to composing multiple conditions, controlling their evaluation, and integrating sorting so results are both correct and readable.

For single-condition filtering, the chapter covers numeric, string, and date values. With numbers, use = for equality and avoid commas or currency symbols; precision (like 10000 vs 10000.00) doesn’t affect equality. Strings must be wrapped in single quotes and must match exactly—watch for subtle issues like smart quotes or hidden spaces. Dates are quoted too, typically in YYYY-MM-DD (optionally with time); because they’re effectively numeric under the hood, precision matters—omitting time works only when stored values have 00:00:00.

For multiple conditions, AND requires every condition to be true, generally shrinking results, while OR accepts any matching condition, often expanding them. Because SQL evaluates AND before OR, parentheses are essential to enforce intended logic when combining both. The chapter also clarifies placement: ORDER BY always follows WHERE. Throughout, tips and exercises reinforce correct quoting, exact matching, operator precedence, and building clear, predictable filters that return exactly the data you intend.

shows that only one row meets the filter criteria for an Advance value of 10000.00.
The result of a query for the Advance from Title where the Title Name is Anne of Fact Tables.
Incorrect single quotes are used, which have been copied from a Microsoft Word document. Workbench is already letting you know this with the red square with an X to the left of the line.
The TitleName and PublicationDate for a title with a publication from March 14, 2020. The time values are the equivalent of midnight at the start of the day.
Although multiple rows in the Title table have a Price value of $5000, adding a second filter condition for a Royalty of 15 percent reduces the result set to one row.
The results for any rows in the Title table that have either an Advance of $5000 or a Royalty of 15 percent.
The seven rows that can match any one of the three conditions of an Advance of $5000, a Royalty of 15 percent, or a Price of $9.95.
The rows returned from the query do not match our intended filter conditions for Title Names with a Price of $9.95 and either a Publication Date of February 6, 2016, or an Advance of $5000.00.
With the parenthetical notation, the query now returns data that has a Price of $9.95 and either a Publication Date of February 6th, 2016, or an Advance of $5000.00.
The four rows that match any one of the three conditions of a Price of $9.95, a Publication Date of April 30, 2015, or an Advance of $5000.00, and the results are sorted alphabetically by TitleName.

Lab answers

  1. The query executes as expected, although behind the scenes the RDBMS has made the data match. This means it had to convert either the value in quotes to a number, or all the values in the Price column to a string, which could negatively impact the duration of the query, if we were using a larger set of data. For this reason, you should avoid putting single quotes around numeric values.
  2. The query returns no values because there is an extra space after the word “Tables” in the WHERE clause. For a string used in a filter to be correct, it needs to be exactly as the values are in the table, including unseen characters, such as spaces, tabs, and carriage returns.
  3. The query will result in a syntax error, as the ORDER BY cannot come before the WHERE clause.
  4. This is similar to the query used in section 5.2.3. Using AND and OR and parentheses, your query should return two rows and should look something like this:

FAQ

What is filtering in SQL, and which clause is used for it?Filtering restricts rows returned from a broader dataset to only those that meet specific conditions. You apply these conditions with the WHERE clause, which appears after the FROM clause.
How do I filter on numeric values correctly?Use comparison operators like =, >, <, >=, <= on numbers without commas or currency symbols. For example: WHERE Advance = 10000.00 is the same as WHERE Advance = 10000 due to numeric precision rules.
Can I put commas or currency symbols in numeric literals?No. Literals like 1,000 or $1000 cause syntax errors. Write numbers as plain numerals, e.g., 1000 or 1000.00.
Do string values in filters need quotes? Which quotes?Yes—enclose string literals in single quotes ('...'). Use the straight single quote on the same key as the double quote; smart quotes or the grave accent (`) will cause syntax errors.
Why didn’t my string filter match when the text looks the same?String comparisons must match exactly, including spaces and invisible characters. A trailing space like 'Anne of Fact Tables ' will not match 'Anne of Fact Tables'.
How do I filter on dates and times?Use single-quoted date/time literals in the default format 'YYYY-MM-DD' or 'YYYY-MM-DD HH:MM:SS'. If stored times are midnight, '2020-03-14' equals '2020-03-14 00:00:00'; otherwise, include the time for precise matching.
How do I require multiple conditions to all be true?Use the AND operator between conditions. Each added AND typically narrows the result set because every condition must be satisfied.
How do I return rows that meet any of several conditions?Use the OR operator. Each added OR generally widens the result set because a row can match any one of the listed conditions.
What happens when I mix AND and OR in the same WHERE clause?AND has higher precedence than OR, so the database evaluates AND parts first. Use parentheses to enforce your intended logic, e.g., WHERE A AND (B OR C).
Where should ORDER BY appear when used with WHERE?ORDER BY must come after the WHERE clause. For example: SELECT ... FROM ... WHERE ... ORDER BY ColumnName.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Learn SQL in a Month of Lunches ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Learn SQL in a Month of Lunches ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Learn SQL in a Month of Lunches ebook for free