SELECT: reading data
Choose columns, compute expressions, rename with aliases, remove duplicates and limit results.
SELECT is the statement you'll write more than any other. It asks the database a question and gets back a result set, a temporary table of rows and columns. In this lesson you'll choose columns, compute new ones, rename them, remove duplicates and limit how many rows come back.
Sample data#
Run this once to follow along:
The basic shape#
For example:
Selecting everything#
* means "every column". It's handy while exploring, but in application code you should list the columns you need. That's clearer, sends less data over the network, and your code won't break when someone adds a column.
Expressions and computed columns#
A SELECT list can hold any expression, not just column names: arithmetic, function calls, text and more.
Computed columns don't change the table. They exist only in the result.
Aliases#
AS renames a column (or a table) in the result. The AS keyword is optional, but including it reads better. Quote aliases that contain spaces with backticks or double quotes:
Table aliases (books AS b) become essential once you join tables, because they let you write b.title instead of books.title.
DISTINCT: removing duplicates#
With several columns, DISTINCT removes duplicate combinations:
Andy Weir wrote two sci-fi books, but the pair (Andy Weir, sci-fi) appears only once.
LIMIT: taking only some rows#
LIMIT 3, 2 is an older shorthand for LIMIT 2 OFFSET 3 (offset first!). The explicit OFFSET form is easier to read.
Without
ORDER BY, SQL doesn't promise any particular order. "The first 3 rows" can change between runs. PairLIMITwithORDER BYwhenever the order matters. You'll learn sorting properly in the next lesson.
Selecting without a table#
SELECT can evaluate expressions on their own, which is perfect for trying out functions:
Note that / always gives a decimal result in MySQL, while DIV gives an integer.
How MySQL reads your query#
You write clauses in this order:
But the database evaluates them roughly in the numbered order. This explains a classic surprise: you can't use a SELECT alias inside WHERE (step 2 happens before step 5), but you can use it in ORDER BY. Don't worry about GROUP BY and HAVING yet; they have their own lesson.
Formatting tips#
- Put each major clause on its own line and indent column lists. Long queries become much easier to read and review.
- End every statement with
;. - Comments start with
--(dash, dash, space) or go between/* ... */.
Common mistakes#
- Commas. A missing comma between columns, as in
SELECT title author FROM books, isn't an error: it silently makesauthoran alias fortitle! A trailing comma beforeFROMis an error. - Using a
SELECTalias inWHERE.WHERE sale_price < 10fails withUnknown column. Repeat the expression, or use a subquery or CTE (covered later). - Assuming row order. Without
ORDER BY, it's undefined.
What's next#
Next you'll zoom in on exactly the rows you need with WHERE and its operators, and put them in order with ORDER BY and LIMIT.
Check your understanding
Quick quiz
1.Which query returns each distinct genre only once?
2.What does
SELECT * FROM books LIMIT 5 OFFSET 10;return?3.Why should application code usually avoid
SELECT *?
Finished reading?
Mark this lesson complete to track your progress.