Quizz

The full text of this chapter, to read or revisit. To practise, do the exercises.

β–Ά Do the exercises

Exercise 1 β€” Pick the columns

Which query returns only the name and level columns of the codemon table?

Options
β€’ 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;
Options
β€’ The level column first, then the name column
β€’ The name column first, then the level column
β€’ All the columns of the table
β€’ An error: columns must follow the table order

Hint : Result columns always appear in the order you write them in the SELECT.

Exercise 4 β€” SQL vocabulary

Match each keyword with its role.

Items to match
β€’ SELECT
β€’ FROM
β€’ *
β€’ AS
Definitions
β€’ Chooses the columns (or expressions) to display
β€’ Chooses the table to read
β€’ Means "all the columns"
β€’ Gives a temporary name to a column or table

Exercise 5 β€” Spot the error

Which of these queries contains a syntax error?

Options
β€’ 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?

Options
β€’ 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;
Options
β€’ An alias for the codemon table
β€’ A column named c
β€’ A new table created by the query
β€’ The first letter of every name

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.

Items to match
β€’ SELECT name AS n
β€’ SELECT name n
β€’ FROM codemon AS c
β€’ c.power
Definitions
β€’ Column alias with the AS keyword
β€’ Column alias without AS (allowed, less readable)
β€’ Table alias
β€’ Column accessed through a table alias

Exercise 11 β€” Double trouble

What is the value of double for Firochu?

SELECT name, power * 2 AS double FROM codemon;
Options
β€’ 170
β€’ 85
β€’ 26
β€’ 98

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;
Options
β€’ 3
β€’ 3.5
β€’ 4
β€’ An error

Hint : Both numbers are integers, so SQLite drops the decimal part.

Exercise 14 β€” Decimal division

Which query returns 3.5?

Options
β€’ 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.

Items to match
β€’ +
β€’ *
β€’ /
β€’ %
β€’ ||
Definitions
β€’ Addition
β€’ Multiplication
β€’ Division
β€’ Remainder of a division
β€’ Text concatenation

Exercise 17 β€” Glue the text

Which query returns Aquachu-water for Aquachu?

Options
β€’ 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;
Options
β€’ fire β€” the literal is the same on every row
β€’ water β€” its real type
β€’ An error: fire is not a column
β€’ Nothing, the column is empty

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?

Options
β€’ '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.

Items to match
β€’ SELECT 2 + 3;
β€’ SELECT 10 / 4;
β€’ SELECT 10 / 4.0;
β€’ SELECT 10 % 3;
β€’ SELECT 'a' || 'b';
Definitions
β€’ 5
β€’ 2
β€’ 2.5
β€’ 1
β€’ ab

Exercise 22 β€” Priority

What does this query return?

SELECT 2 + 3 * 4;
Options
β€’ 14
β€’ 20
β€’ 24
β€’ 9

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?

Options
β€’ SELECT 6 * 7; works and returns 42
β€’ A SELECT always needs a FROM clause
β€’ SELECT 6 * 7; returns an error
β€’ You need a table named 42

Hint : 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.

Back to the course