SelectFromData
AnalyticsLesson 2 of 12 · 12 min

WHERE: Keep Only the Rows You Mean

SELECT … FROM gives you columns — but every row comes along. Real questions are about some rows: the engineers, the orders above €50, the customers in Ghent. WHERE is how you say which.

In one line

WHERE tests a condition against every row and keeps only the rows where it's true. One clause, and your query starts answering real questions.

See it before you write it

Filter the five employees to just the engineers, and three rows fall away:

namedepartmentsalary
AdaEngineering62000
BoSales48000
CyEngineering71000
DiSales52000
EliMarketing45000
WHERE department = 'Engineering'
namedepartmentsalary
AdaEngineering62000
CyEngineering71000
Greyed, struck-through rows fail the condition and are dropped; only matching rows pass through.

Try it now — write the query the visual just showed: the name and salary of every employee in Engineering.

Your turn · Exercise

Write the query the visual showed: name and salary of every employee in the Engineering department.

The data

employees

5 rows returned

namedepartmentsalary
AdaEngineering62,000
BoSales48,000
CyEngineering71,000
DiSales52,000
EliMarketing45,000
Runs in your browser

Numbers compare without quotes

Conditions aren't just = on text. You have <, >, <=, >=, and <> (not equal). Run it, then change 50000 to 60000:

Numeric comparison — no quotes around the number
Editable · runs in your browser

Watch out

The single biggest beginner trip-up: text goes in single quotes, numbers don't. WHERE department = Engineering (no quotes) makes the database look for a column named Engineering and errors. WHERE salary > '50000' quotes a number and invites subtle bugs. Text → quotes; numbers → bare.

Read it the way the database does

Even though you write SELECT first, the database thinks: FROM this table → WHERE these rows match → SELECT these columns. "Which table, which rows, which columns" is the order that makes queries easy to reason about — and it's exactly what the workspace's X-ray button shows you, stage by stage.

Your turn

Return the name and salary of every employee in the Sales department.

Your turn · Exercise

Return the name and salary of every employee in the Sales department.

The data

employees

5 rows returned

namedepartmentsalarycity
AdaEngineering62,000Antwerp
BoSales48,000Ghent
CyEngineering71,000Antwerp
DiSales52,000Bruges
EliMarketing45,000Ghent
Runs in your browser
Check yourself

Which condition is written correctly?


Next: the rows are right — now put them in order.