Querying Data with SQL#
š¦ Data Preparation š¢ Spreadsheets, SQL & Organization Lesson 023
ā Previous Ā· Next ā¶ Ā· ā Section Ā· ā Hub
Important
⨠AI-generated content. This page was written with the assistance of an AI language model and is provided as a learning aid. Despite careful review, it may still contain mistakes, omissions, or out-of-date information. Whether you are new to the topic, a team lead, or a senior practitioner, treat it as a starting point rather than an authoritative reference: read it critically and independently verify anything you act on (code, commands, figures, and factual claims) against official documentation and primary sources before relying on it.
Asking a database a question#
With a database environment in hand, the core skill is the query ā an SQL statement that retrieves exactly the data you want. The foundations introduced SQLās shape; this lesson is the hands-on core, the three clauses that answer the majority of real questions and the foundation everything later in the SQL thread builds on.
The essential query#
Every basic retrieval combines three clauses:
SELECT product, region, amount
FROM orders
WHERE amount > 100;
SELECTnames the columns to return.SELECT *returns all columns; naming specific columns is tidier and, on large cloud warehouses, cheaper (you are billed for the columns scanned).FROMnames the table the data comes from.WHEREsets the condition rows must meet to be included ā the filter, in query form.
Read top to bottom, it is a sentence: select these columns from this table where this condition holds.
Building up the WHERE clause#
Most of a queryās power lives in WHERE, which supports:
Comparisons ā
=,<>(not equal),>,<,>=,<=:WHERE region = 'North',WHERE amount >= 50.Combinations ā
AND(both must hold),OR(either),NOT:WHERE region = 'North' AND amount > 100.Ranges and sets ā
BETWEENfor ranges,INfor lists:WHERE amount BETWEEN 50 AND 200,WHERE region IN ('North', 'South').Pattern matching ā
LIKEwith wildcards for text:WHERE product LIKE 'Pro%'(products starting with āProā).
Ordering the results#
A query can also sort its output with ORDER BY ā the SQL counterpart of the
spreadsheet sort:
SELECT product, amount
FROM orders
WHERE region = 'North'
ORDER BY amount DESC;
ORDER BY amount DESC returns the rows largest-first; ASC (the default) is
smallest-first. Filtering with WHERE and ordering with ORDER BY together
are the SQL version of the spreadsheetās filter-then-sort ā the same analytical
move, now at database scale.
Why queries beat exporting#
A query goes to the data where it lives, returns only what you asked for, and is text ā repeatable, shareable, reviewable. Instead of exporting a million rows to a spreadsheet and filtering there, you ask the database for the thousand that matter. This is why SQL scales where spreadsheets strain, and why it is the most consistently demanded analyst skill.
The caveat#
A query returns exactly what you specify ā which is not always what you meant.
WHERE amount > 100 silently excludes rows where amount is null (missing),
and no error warns you; a LIKE pattern that is slightly off quietly returns
the wrong rows. SQLās precision demands precision from you, and the habit of
checking results ā does the row count look right, do sampled rows match
expectations ā applies to every query, exactly as the sanity-check habit applied
to spreadsheet formulas. The deeper query techniques come in the analysis
section; this is the foundation they extend.
Hint
See also
Source article Adapted (context, re-expressed) in our own words from: https://insightful-data-lab.com/2023/09/04/15229/ (insightful-data-lab.com).