Name the result
aliases, expressions and text labels
A recap editor wants readable headings, a word budget and a label such as Rick in Pilot, all without touching the stored lines.
Can you
- Give an output column a heading with AS and say what did not change
- Calculate a new column from a stored one and check it against one row
- Tell a column name from a quoted piece of text, and spot the query that confuses them
The reading
Read the whole chapterName the result
The recap editor does not want a heading called characterName. Write characterName AS speaker and the output column is called speaker. That is all an alias does: it names a column of this result, for this query. Open the schema panel and the table still says characterName. Nothing was renamed.
An expression calculates a column that was never stored. wordCount + 5 AS five_more_words gives 16 for id 1, whose wordCount is 11. Run SELECT wordCount FROM scripts WHERE id = 1 afterwards and you get 11 again. SELECT reads; it never writes. Check an expression against one row you can do in your head, 11 plus 5, before you trust it on 1,905.
SELECT id, characterName AS speaker, wordCount + 5 AS five_more_words
FROM scripts
ORDER BY id
LIMIT 3;Text joins with ||, not with +. characterName || ' in ' || episodeName AS scene_label gives Rick in Pilot. The single quotes around ' in ' make it a piece of text, spaces included. Leave the quotes off a column name; put them on anything that is literally text.
SELECT characterName || ' in ' || episodeName AS scene_label
FROM scripts
ORDER BY id
LIMIT 5;Now put quotes where they do not belong: SELECT 'characterName' AS speaker. The engine does not complain. It prints the word characterName on every row, because quoted text is a value, not a column. It ran. That is not the same as being right. When a column repeats one word all the way down, look for quotes that should not be there.
Rule of thumb. An alias names the output and an expression calculates it; neither changes what is stored.
Going deeper (optional, not on the exam)
The browser engine divides two whole numbers as whole numbers: 5 / 2 gives 2, not 2.5. Write 5 / 2.0 when you want the fraction, and runtime / 60.0 when you turn minutes into hours. Chapter 4 comes back to this.
+ adds numbers. Write characterName + episodeName and the engine tries to add two pieces of text and answers 0 rather than complaining. The text operator is ||.
An alias with a space in it needs double quotes: phone AS "Front Desk". Single quotes are for text values. You can sort by an alias, ORDER BY five_more_words, but inside WHERE repeat the expression itself.
The ladder
Start at the first rung even if the idea looks familiar: it is answerable from the reading alone, and the climb is where the difficulty lives. A wrong answer sends you back to the paragraph that teaches it, then to a hint, then to the worked answer.
Learn
- Practise this rung
Level 1: Watch it answered
- Practise this rung
Level 2: Fill the blank
Practise
- Practise this rung
Level 3: Predict the result
- Practise this rung
Level 4: Write it yourself
Master
- Practise this rung
Level 5: Repair it
- Practise this rung
Level 6: Transfer it
Keep practising
The whole bank for this concept, in any order, with the same review, hint and worked answer.
Practice here is evidence for you and your instructor, never a grade. Grades live in Canvas.