Conditions Basics
Part of the Fundamentals section of Coddy's SQL journey — lesson 5 of 72.
Sometimes we would like to fetch records that meet a certain condition.
For example
- fetch all of the records that have the family name "Aothly"
- fetch all of the records that the amount is bigger than 5
- fetch all of the records with the country "Mexico"
To add conditions we can use the WHERE keyword
For example here is a sales table:
| coin | amount |
| AGK | 13 |
| GOL | 21 |
| KLA | 15 |
| AGK | 18 |
To fetch all of the records with the coin "AGK" we will write:
SELECT * FROM sales
WHERE coin = 'AGK'To fetch all of the records with amount smaller or equal to 20 we will write:
SELECT * FROM sales
WHERE amount <= 20Challenge
EasyAvailable tables and columns:
<strong>events</strong>:<strong>event_id</strong>,<strong>people</strong>
Fetch all of the event_ids where people is less than 14 people
Cheat sheet
Use the WHERE keyword to add conditions to your SQL queries:
SELECT * FROM table_name
WHERE column_name = 'value'For numeric comparisons:
SELECT * FROM table_name
WHERE column_name <= 20Common comparison operators: =, <, >, <=, >=
Try it yourself
SELECT event_id FROM events
-- Write your code below
WHEREThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4More Keywords
The IN keywordThe BETWEEN keywordThe LIKE keywordThe AS keywordRecap - Cellphone Models2Conditions
Conditions BasicsThe AND keywordThe OR keywordThe NOT keywordMultiple Conditions CombinedParenthesisBooleans5Arithmetic Operations
Mathematical OperatorsMathematical ColumnsThe Modulo OperationThe ROUND() Function3Specific Return Format
Null valuesSort Results Part 1Sort Results Part 2Recap - Cyber Security FirmLimit number of recordsRecap - Vehicle Factory6Intro Challenges
Recap - Parliamentary ElectionRecap - Police Criminal ArrestRecap - Bar Beverage ContainerRecap - Engineer new columns