Field order in the output of a SELECT

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Field order in the output of a SELECT

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Query: SELECT lastName, firstName FROM students; Which column comes first in the output?
A lastName
B firstName
2. Which query gives output with the columns house, then points?
A SELECT points, house FROM students;
B SELECT house, points FROM students;
3. SELECT firstName FROM students; How many columns are in the output?
A 1 column
B 6 columns
4. The students table has 10 records. SELECT firstName, points FROM students; has no WHERE. How many rows of data does it output?
A 10 rows
B 1 row
5. Changing SELECT yearLevel, house to SELECT house, yearLevel changes the...
A number of rows
B order of the columns

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1SELECT lastName, firstName FROM students; AShows only the name and price columns; category and stock are left out of the output
2SELECT name, price FROM products; BSELECT house FROM students;
3Swapping the field order makes the columns appear in the opposite order, with the points column before the firstName column CSELECT points, firstName FROM students;
4With no WHERE clause, every record in the table appears in the output, and the heading row shows the field names DShows two columns, lastName first and then firstName, for every student, because there is no WHERE
5Query: SELECT house, firstName FROM students; Student expects: firstName column first, then house EThe fields are written in the wrong order. The output shows house first, so the query should be SELECT firstName, house FROM students;
6Query: SELECT firstName FROM students; Student expects: firstName and yearLevel columns FyearLevel is not named after SELECT, so it is left out of the output. It should be SELECT firstName, yearLevel FROM students;

Definitions

SELECT lastName, firstName FROM students;

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
-- Output columns: lastName, firstName SELECT ________, firstName FROM students;
2.
SELECT firstName, points ____ students;
3.
-- Every record, name then price SELECT name, price FROM ________;
4.
-- Show points first, then firstName SELECT ______, firstName FROM students;
5.
-- Only studentId and mark are shown SELECT studentId, ____ FROM results;

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A -- Show house, then firstName
B SELECT firstName, house
C FROM students;
Question 2
A -- Show lastName then points
B SELECT lastName points
C FROM students;
Question 3
A -- Only name and price columns
B SELECT name, price, stock
C FROM products;
Question 4
A -- List every student's name
B -- (all records, no WHERE)
C SELECT firstName, lastName
D FROM student;
Question 5
A SELECT name, price, category
B FROM products;
C -- name, then category, price

Answer Key

Field order in the output of a SELECT
Section A: Quick Questions
Q1
A — lastName
Q2
B — SELECT house, points FROM students;
Q3
A — 1 column
Q4
A — 10 rows
Q5
B — order of the columns
Section B: Match the Pairs
Q1
SELECT lastName, firstName FROM students; → Shows two columns, lastName first and then firstName, for every student, because there is no WHERE
Q2
SELECT name, price FROM products; → Shows only the name and price columns; category and stock are left out of the output
Q3
Swapping the field order makes the columns appear in the opposite order, with the points column before the firstName column → SELECT points, firstName FROM students;
Q4
With no WHERE clause, every record in the table appears in the output, and the heading row shows the field names → SELECT house FROM students;
Q5
Query: SELECT house, firstName FROM students; Student expects: firstName column first, then house → The fields are written in the wrong order. The output shows house first, so the query should be SELECT firstName, house FROM students;
Q6
Query: SELECT firstName FROM students; Student expects: firstName and yearLevel columns → yearLevel is not named after SELECT, so it is left out of the output. It should be SELECT firstName, yearLevel FROM students;
Section C: Definitions
Q1
SELECT lastName, firstName FROM students; — The output has only the fields named, as columns in the order written after SELECT, with a heading row of field names and one row per record kept (all records if there is no WHERE).
Section D: Fill in the Blanks
Q1
-- Output columns: lastName, firstName SELECT lastName, firstName FROM students;
Q2
SELECT firstName, points FROM students;
Q3
-- Every record, name then price SELECT name, price FROM products;
Q4
-- Show points first, then firstName SELECT points, firstName FROM students;
Q5
-- Only studentId and mark are shown SELECT studentId, mark FROM results;
Section E: Spot the Error
Q1
B — The comment says house comes first, but the fields are written in the wrong order, so the columns would be firstName then house. It should be SELECT house, firstName.
Q2
B — Field names in a SELECT list must be separated by commas. The comma is missing, so it should be SELECT lastName, points.
Q3
B — Every field named after SELECT becomes a column in the output. stock is named but not wanted, so it should be SELECT name, price.
Q4
D — The table is called students. The table name after FROM must match exactly, so it should be FROM students;.
Q5
A — The columns appear in the order the fields are written. The comment says category comes before price, so it should be SELECT name, category, price.