Quizz
The full text of this chapter, to read or revisit. To practise, do the exercises.
βΆ Do the exercisesExercise 1 β Pick the columns
Which query returns only the name and level columns of the codemon table?
SELECT name, level FROM codemon;SELECT name AND level FROM codemon;SELECT * FROM codemon (name, level);SELECT codemon FROM name, level;Hint : List the columns you want after SELECT, separated by commas, then name the table after FROM.
Exercise 2 β All the columns
Complete the query to retrieve every column of the codemon table.
Hint : The asterisk means "all columns", and FROM is followed by the table name.
Exercise 3 β Column order
What does the result of this query look like?
SELECT level, name FROM codemon;Hint : Result columns always appear in the order you write them in the SELECT.
Exercise 4 β SQL vocabulary
Match each keyword with its role.
SELECTFROM*ASExercise 5 β Spot the error
Which of these queries contains a syntax error?
SELECT name, FROM codemon;SELECT name FROM codemon;SELECT * FROM codemon;SELECT name, type FROM codemon;Hint : Look closely at the commas: a comma must separate two columns, never sit right before FROM.
Exercise 6 β Rename a column
Complete the query so that the power column is displayed as strength.
Hint : The keyword that introduces an alias is AS.
Exercise 7 β Alias with a space
Which query correctly displays the power column under the header Battle Power?
SELECT power AS "Battle Power" FROM codemon;SELECT power AS Battle Power FROM codemon;SELECT power Battle Power FROM codemon;SELECT "Battle Power" AS power FROM codemon;Hint : An alias containing a space must be wrapped in double quotes, and the alias comes after AS.
Exercise 8 β What is c?
In this query, what does c represent?
SELECT c.name, c.power
FROM codemon AS c;Hint : A table alias is defined in the FROM clause and reused in front of column names.
Exercise 9 β Use the table alias
Complete the query so that the codemon table is aliased as c and both columns are referenced through that alias.
Hint : Once the table has an alias, prefix the columns with it: alias.column.
Exercise 10 β Alias forms
Match each snippet with what it does.
SELECT name AS nSELECT name nFROM codemon AS cc.powerExercise 11 β Double trouble
What is the value of double for Firochu?
SELECT name, power * 2 AS double
FROM codemon;Hint : Firochu has a power of 85. The expression multiplies it by 2.
Exercise 12 β Add them up
Complete the query so it shows each name and the sum of power and level in a column named total.
Hint : Addition uses +, and the alias is introduced with AS.
Exercise 13 β Integer division
What does this query return?
SELECT 7 / 2;Hint : Both numbers are integers, so SQLite drops the decimal part.
Exercise 14 β Decimal division
Which query returns 3.5?
SELECT 7 / 2.0;SELECT 7 / 2;SELECT 7 DIV 2;SELECT 7 // 2;Hint : As soon as one operand is a decimal number, the division keeps the decimals.
Exercise 15 β Even or odd?
Complete the query to show the remainder of each level divided by 2.
Hint : The remainder operator is the percent sign.
Exercise 16 β Operators
Match each operator with its meaning.
+*/%||Exercise 17 β Glue the text
Which query returns Aquachu-water for Aquachu?
SELECT name || '-' || type FROM codemon;SELECT name + '-' + type FROM codemon;SELECT name, '-', type FROM codemon;SELECT name & '-' & type FROM codemon;Hint : In SQL, text is concatenated with the || operator.
Exercise 18 β Build a sentence
Complete the query so it produces sentences like Aquachu is level 15.
Hint : Each piece of text is glued to the next one with ||.
Exercise 19 β A fixed value
What does the kind column contain for Aquachu?
SELECT name, 'fire' AS kind
FROM codemon;Hint : A text between single quotes is a literal: it doesn't read the table, it repeats the same value.
Exercise 20 β Which quotes?
Which of these is the correct way to write the text literal *water* in SQL?
'water'"water"water`water`Hint : Single quotes are for text values. Double quotes are for column and table names.
Exercise 21 β Query and result
Match each query with its result.
SELECT 2 + 3;SELECT 10 / 4;SELECT 10 / 4.0;SELECT 10 % 3;SELECT 'a' || 'b';Exercise 22 β Priority
What does this query return?
SELECT 2 + 3 * 4;Hint : Multiplication is applied before addition, exactly like in math class.
Exercise 23 β Fix the priority
Complete the query so that power and level are added before multiplying by 2.
Hint : Parentheses force an operation to happen first.
Exercise 24 β No table needed
Which statement is true?
SELECT 6 * 7; works and returns 42SELECT 6 * 7; returns an errorHint : A SELECT can compute values on its own, without reading any table.
Exercise 25 β The full picture
Complete the query: the name and the doubled power (named double_power) of every CodΓ©mon, with the table aliased as c.
Hint : Multiply with *, name the result with AS, read the table with FROM and alias it with AS.