Quiz: SQL Basics
A review of basic syntax for SELECT statements, from FROM to some aggregations with GROUP BY
-
In your
padjo-2016
folder, create a new spreadsheet namedquiz-sql-basics
-
The
quiz-sql-basics
spreadsheets should be a simple 2-column spreadsheet that looks like this (no header necessary):1 A 2 B 3 C 4 Short answer blah blah blah
Instructions
All of the SQL questions assume you have a database that includes a table named people
.
The people
table looks exactly like this:
name | sex | age |
---|---|---|
Austin | M | 33 |
Blair | M | 90 |
Carolina | F | 28 |
Dani | F | 41 |
Donald | M | 70 |
Eliza | F | 37 |
Farida | F | 23 |
Georgina | F | 19 |
Hillary | F | 68 |
Leland | M | 16 |
Liam | M | 22 |
Michael | M | 48 |
Phoebe | F | 52 |
Sherry | F | 39 |
Zed | M | 42 |
Questions
Question 1
What is the result of this query:
SELECT 'Hello, world';
‘Hello’ |
---|
world |
‘Hello, world’ |
---|
Hello, world |
Hello | world |
---|---|
Hello | world |
‘Hello, world’ |
---|
HELLO, WORLD |
‘Hello, world’ |
---|
Hello world |
Question 2
Which of these queries contains a syntax error? (Remember that SQL keywords and names of tables and columns are not case-sensitive)
SELECT * FROM people;
Select * from PEOPLE;
SELECT 'HELLO' FROM people;
FROM people SELECT *;
select 'hello', 'world' from people;
Question 3
To get only the first five rows from people
, with the columns in the default order, which one of the only following queries will work?
SELECT 5 FROM people;
FROM people SELECT *, LIMIT 5;
SELECT FROM people LIMIT 5;
SELECT * FROM people LIMIT 5;
SELECT * LIMIT 5 FROM people;
Question 4
The following query will result in an error:
SELECT name, gender, name
FROM PEOPLE
LIMIT 2;
What error message will result?
No such table: PEOPLE
Called name column too many times
No such column: gender
Failed to include column: sex
Cannot call LIMIT without calling ORDER first
Question 5
We want to query the people
table and get all of its rows and columns, except with the rows sorted in order of oldest person first. Which of the following queries will do the job?
SELECT * FROM people ORDER BY name DESC;
FROM SELECT ORDER BY age DESC;
SELECT * FROM people ORDER BY age DESC;
SELECT age FROM people ORDER BY age DESC;
SELECT * FROM people ORDER BY age DESC LIMIT 9;
Question 6
Which query would result in exactly this:
name |
---|
Austin |
SELECT * FROM people;
SELECT * FROM people LIMIT 1;
SELECT name, sex FROM people LIMIT 1;
SELECT name FROM people;
SELECT name FROM people LIMIT 1;
Question 7
Which query would result in exactly this result:
name | first_letter |
---|---|
Austin | A |
SELECT * FROM people LIMIT 1;
SELECT name, first_letter FROM people LIMIT 1;
SELECT name, SUBSTR(name, 1, 1) AS first_letter FROM people LIMIT 1;
SELECT name, SUBSTR(name, 1, 1) FROM people LIMIT 1;
SELECT SUBSTR(name, 1, 1), name FROM people LIMIT 1;
Question 8
Given the following query with incomplete WHERE
clause:
SELECT name, age
FROM people
WHERE __________
LIMIT 2
And the desired result of:
name | age |
---|---|
Blair | 90 |
Dani | 41 |
Which of the following conditional statements should be used?
age > 40
age < 40
name > 40
age > 50
age < 100
Question 9
Given the following query with incomplete WHERE
clause:
SELECT
name, age, sex
FROM
people
WHERE __________
LIMIT 3;
And the desired result of:
name | age | sex |
---|---|---|
Hillary | 68 | F |
Phoebe | 52 | F |
Which of the following conditional statements should be used?
age > 50
sex = 'F'
age > 50 AND sex='F'
age > 50 OR sex='F'
age > 52 AND age = 68
Question 10
Given this incomplete query:
SELECT *
FROM people
WHERE sex = 'M'
______
And the desired result of:
name | sex | age |
---|---|---|
Zed | M | 42 |
Michael | M | 48 |
Which of the following SQL lines will complete the query and get the desired result?
LIMIT 2;
ORDER BY age DESC LIMIT 2;
ORDER BY name;
ORDER BY name DESC LIMIT 2;
LIMIT 2 ORDER BY name DESC;
Question 11
We want to do a simple count of the people table. This is what the desired result will look like:
COUNT(*) |
---|
15 |
Which of the following queries will get the desired result?
SELECT COUNT FROM people;
SELECT COUNT(*) FROM people;
SELECT ROWCOUNT FROM people;
COUNT people;
SELECT *, COUNT FROM people;
Question 12
Let’s get a count of the people table by sex
, listed in order of highest row count per sex.
We want the exact results, headers, and column arrangement here:
sex | sexcount |
---|---|
F | 8 |
M | 7 |
Which of the following queries will return the exact desired result?
SELECT
sex, COUNT(*) AS sexcount
FROM people;
SELECT
sex, sexcount
FROM people
GROUP BY sex
ORDER BY sexcount DESC;
SELECT sex,
COUNT(*) AS sexcount
FROM people
GROUP BY sex
ORDER BY sexcount DESC;
SELECT sex,
COUNT(*) AS sexcount
FROM people
ORDER BY sexcount DESC;
SELECT sex,
COUNT(*) AS sexcount
FROM people
GROUP BY sex
ORDER BY sex DESC;
Question 13
We want to create a simple list showing just the sex
of the 5 oldest people in the people table.
Write the query out as a short answer.
Hints:
- No aggregation, or even
WHERE
clause required. - Do not include any unnecessary columns or rows.
- These are the keywords you’ll need:
FROM
,LIMIT
,ORDER
,SELECT
,BY
,DESC
- …Not in that order.
When your query is executed, this what the exact result should be:
sex |
---|
M |
M |
F |
F |
M |
Answers
- B
- D
- D
- C
- C
- E
- C
- A
- C
- D
- B
- C
-
The query:
SELECT sex FROM people ORDER BY age DESC LIMIT 5;