liveBook · Manning

Logical OR

As explained in Learn SQL in a Month of Lunches

The OR operator is a logical SQL operator that evaluates to true when at least one connected condition is true.

Logical OR filters rows when at least one connected condition is true. In a WHERE clause, it is useful when a request allows alternatives: find titles with an Advance of 5,000 or a Royalty of 15 percent. section 5.2.2

Finding titles that meet either condition

SELECT TitleName, Advance, Royalty FROM title WHERE Advance = 5000 OR Royalty = 15; A row needs to match only one condition, so this returns more rows than requiring both conditions with AND. Adding another OR condition, such as Price = 9.95, can include still more rows. section 5.2.2

When OR is combined with AND, use parentheses to make the intended grouping explicit. SQL evaluates AND before OR, so Price = 9.95 AND (PublicationDate = '2016-02-06' OR Advance = 5000) keeps the price requirement in force for either alternative. section 5.2.3

Practical checks:

  • Use OR when any one of several conditions should include a row. section 5.2.2
  • Use parentheses whenever AND and OR appear together; they prevent unintended results and make the logic easier to read. section 5.2.3
  • Match filtered values exactly, and avoid quoting numeric values because implicit conversion can affect query duration. section 5.4
SELECT
    TitleName,
    Price,
    PublicationDate,
    Advance
FROM title
WHERE Price = 9.95
    AND (PublicationDate = '2016-02-06'
    OR Advance = 5000);

This query demonstrates keeping Price fixed while allowing either a publication date or an advance to satisfy the grouped alternative. section 5.2.3

How Learn SQL in a Month of Lunches covers it

How deep it goes
Explains alternative conditions, result-set growth, mixed-operator precedence, and corrective parentheses through worked queries.
Examples
Uses title advances, royalties, prices, publication dates, and author payment or name filters.
What it assumes
Assumes basic WHERE-clause filtering and relates OR to the previously used AND operator.
What you can do after
Apply it via SQL to retrieve rows matching one or more alternative filter conditions.
How it connects
Connects OR with AND, WHERE clauses, filter conditions, parentheses, and result sets.

Also explained in

Compare how 6 books explain logical OR →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn SQL in a Month of Lunches. The section links point to the parts of the book it draws on.