SelectFromData
AnalyticsLesson 1 of 9 · 20 min

Your First Query: SELECT, FROM, WHERE

SQL is how you ask a database a question. Almost every question starts with the same three words: SELECT (which columns you want), FROM (which table they live in), and WHERE (which rows to keep).

In one line

SELECT picks the columns, FROM names the table, WHERE keeps only the rows that match a condition. Master these three and you can already answer most everyday questions.

SELECT and FROM

The simplest query asks for some columns from a table. Here's a tiny employees table. Run it and see every row come back:

Pick three columns from the employees table
Editable · runs in your browser

SELECT * is shorthand for "every column". Handy when exploring, but in real queries you usually name the columns you actually need.

WHERE keeps only the rows you want

Before the syntax, the picture. WHERE tests its condition against every row and keeps only the ones where it is true. 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: return 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

Add a WHERE clause to filter. The condition is tested against every row; only the rows where it's true come back. Run it, then change Engineering to Sales:

Only the engineers
Editable · runs in your browser

A few things to notice:

  • Text values go in single quotes: 'Engineering'. Numbers don't: salary > 50000.
  • = tests equality. You also have <, >, <=, >=, and <> (not equal).
Numeric comparison — no quotes around the number
Editable · runs in your browser

Watch out

The single biggest beginner trip-up: using = to compare text but forgetting the quotes, or quoting a number. WHERE department = Engineering (no quotes) makes the database look for a column named Engineering and errors. Text → quotes; numbers → no quotes.

Putting it together

Read a query in the order the database cares about it: FROM a table, WHERE some rows match, then SELECT these columns. Even though you write SELECT first, it helps to think "which table, which rows, which columns."

From practice

On real systems you almost never want SELECT * in production code; naming columns makes the query faster, clearer, and safe against someone adding a giant column to the table later. Get the habit early.

Your turn

Write the query yourself. 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

Next: once you can pull the right rows, you'll want to sort them, take just the top few, and drop duplicates.