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:
- Use
>,>=,<,<=,=and<>on numbers. - Predict which rows an
ORkeeps, and add parentheses when you mix it withAND. - Explain
ORvsUNIONin an interview. - Solve LeetCode 595 alone.
Four words you need
>=, AND.× before + in math.3000000 or 'Y'.1. Comparing numbers
= equal <> not equal (also written !=)
> greater than >= greater than OR EQUAL
< less than <= less than OR EQUAL
- "At least" means
>=. "More than" means>. Interview questions choose these words on purpose. Read them slowly. - Numbers have no quotes:
area >= 3000000. Only text gets quotes. - MySQL doesn't allow
3_000_000or3,000,000. Count the zeros: 3 million has six.
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.
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;
- Same result as
OR, becauseUNIONremoves duplicates. A city that is both big and hot (like dot R) appears once. UNION ALLkeeps duplicates, so R would appear twice. That would be a wrong answer here.- Why anyone writes it this way: if each column has its own index, each half of the
UNIONcan use its own index. Modern MySQL can often do this forORon its own, through a method called Index Merge (MySQL manual). So say "ORis the simple answer.UNIONis an option ifEXPLAINshows a full table scan." That shows you know the trade-off.
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:
- Your query, and the LeetCode result.
- Two full sentences, as you would say them to an interviewer: (1) why
ORand notANDhere; (2) what would change if you wrote it withUNION ALLinstead ofUNION.
To pass (check these yourself first):
- LeetCode says Accepted.
- You used
>=for "at least", and the numbers have no quotes. - The columns come out in the order name, population, area.
- Your sentences are full sentences, and sentence 2 mentions duplicate rows.
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.
- Operator Precedence (MySQL 8.0 manual) 📄 Docs · 2 min read · The official order. Comparisons, then NOT, then AND, then OR.
- Index Merge Optimization (MySQL 8.0 manual) 📄 Docs · 10 min read · Optional. Read only the first example and the "union" section. It backs up the OR vs UNION talking point.
- The WHERE clause, Use The Index, Luke 📄 Article · book chapter · Markus Winand. The best free explanation of how WHERE uses indexes. Save it for Sprint 5.
💬 Is something still unclear? Ask me in the chat. I'm your tutor for this course.