Lesson 2 · Milestone: Filter

Comparisons, OR and parentheses

Lesson 1 tested text: = 'Y'. Real questions test numbers: "bigger than", "at least". And they often say "this or that". This lesson covers both, plus the one rule that silently breaks queries: AND runs before OR.

What you will be able to do:

Four words you need

operatorA symbol that compares or combines. Examples: >=, AND.
precedenceWhich operator runs first when there are no parentheses. Like × before + in math.
literalA fixed value written in the query: 3000000 or 'Y'.
UNIONGlues the rows of two queries together and removes duplicate rows.

1. Comparing numbers

=    equal                 <>   not equal (also written !=)
>    greater than          >=   greater than OR EQUAL
<    less than             <=   less than OR EQUAL

2. OR keeps more rows than AND

Picture each row as a dot. Here the x-axis is area and the y-axis is population. The dashed lines are the two limits. The shaded part is what the query keeps.

WHERE area ≥ X AND pop ≥ Y RPQS X Y area → pop ↑ keeps R only: BOTH tests pass WHERE area ≥ X OR pop ≥ Y RPQS X Y area → pop ↑ removes S only: BOTH tests fail

The dots are made-up rows, not real data. R passes both tests. P passes only area. Q passes only population. S passes neither. Notice that R is kept once, even though it passes both tests.

3. The trap: AND runs before OR

In MySQL, AND has higher precedence than OR, the same way × comes before + in math (MySQL manual). So the database adds hidden parentheses for you:

-- Cities(name, country, population, avg_temp)
-- Goal: Brazilian cities that are big OR hot.

WHERE country = 'BR' AND population >= 1000000 OR avg_temp > 30
-- MySQL reads it as:
WHERE (country = 'BR' AND population >= 1000000) OR avg_temp > 30
-- Bug: every hot city in the WORLD comes back.

WHERE country = 'BR' AND (population >= 1000000 OR avg_temp > 30)
-- Correct. The parentheses say what you mean.

Rule for interviews: whenever AND and OR appear together, add parentheses. It costs nothing, and the reader never has to guess.

4. Interview talking point: OR vs UNION

You can write "A or B" in a second way: run two queries and glue their rows together.

SELECT name FROM Cities WHERE population >= 1000000
UNION                                   -- removes duplicate rows
SELECT name FROM Cities WHERE avg_temp > 30;

Practice from memory

Try not to scroll up.

1. With no parentheses, how does MySQL read a OR b AND c?

2. A row passes both tests in WHERE A OR B. How many times is it in the result?

3. The same row, but now with query A UNION ALL query B. How many times?

4. A country has population exactly 25000000. Does population >= 25000000 keep it?

5. What decides the order of the columns in the result?

Fill in the blanks: a different table

Cities(name, country, population, avg_temp). Return name and avg_temp of cities in 'BR' that have at least 1 million people or an average temperature above 30.

SELECT name, 
FROM Cities
WHERE country = 'BR'
  AND population  1000000
        avg_temp  30;

🏆 Challenge: LeetCode 595 (Big Countries)

Your task: open 595 and choose MySQL. The table is World(name, continent, area, population, gdp). A country is big if its area is at least 3,000,000 km² or its population is at least 25,000,000. Return name, population and area of the big countries. Time box: 7 minutes.

Send me in the chat:

To pass (check these yourself first):

Read next

Main source: the MySQL 8.0 operator precedence list. It is one screen long. Find AND and OR in it and see which one is higher.

💬 Is something still unclear? Ask me in the chat. I'm your tutor for this course.