SQL Multiple-Choice Questions with Answers
Read 15 SQL questions on filtering, sorting, joins, nulls, keys, and aggregation, with worked answers and separate printable PDFs.
Introductory databases and SQL interview practiceA Books table has the columns title, author, and price. Which query returns only the title and author of each book?
Ready to play?
Open at Step 3 with every question loaded. Play solo, host live, or customize your game.
Play & Customize GameQuestions by ladder level
A Books table has the columns title, author, and price. Which query returns only the title and author of each book?
- SELECT title, author FROM Books
- SELECT * FROM Books
- SELECT author, price FROM Books
- SELECT title FROM Books
Show answer and explanation
Correct answer: A. SELECT title, author FROM Books
The comma-separated select list names the exact columns to return. A star returns all columns.
Which SQL clause filters individual rows according to a condition such as price < 20?
- GROUP BY
- ORDER BY
- WHERE
- SELECT DISTINCT
Show answer and explanation
Correct answer: C. WHERE
WHERE keeps rows whose condition evaluates to true. The selected columns and order are handled by other clauses.
Which clause sorts a result so a numeric score column runs from highest to lowest?
- ORDER BY score ASC
- ORDER BY score DESC
- ORDER BY score
- GROUP BY score
Show answer and explanation
Correct answer: B. ORDER BY score DESC
DESC reverses the ascending sort order. A GROUP BY clause groups rows and does not by itself guarantee an order.
A Pets table contains three rows with species Cat, Dog, and Cat. How many rows does SELECT DISTINCT species FROM Pets return?
- Zero
- Three
- One
- Two
Show answer and explanation
Correct answer: D. Two
For the selected species column, duplicate values collapse into one result row. The two values are Cat and Dog.
A Tickets table contains six rows. Every row counts as a ticket even when its optional status is NULL. What does SELECT COUNT(*) FROM Tickets return?
- The number of distinct statuses
- Six
- The number of rows that have a status
- A separate count for every status
Show answer and explanation
Correct answer: B. Six
COUNT(*) counts all input rows, including rows that have null values in one or more columns.
A Parcel table stores NULL in delivered_on while a shipment is undelivered. Which condition selects the undelivered rows?
- WHERE delivered_on IS NULL
- WHERE delivered_on = NULL
- WHERE delivered_on IS NOT NULL
- WHERE delivered_on = 'NULL'
Show answer and explanation
Correct answer: A. WHERE delivered_on IS NULL
IS NULL tests for a missing value. Comparing with NULL using ordinary equality gives an unknown result, so it does not match in WHERE.
A Sales table has columns store_id, status, and total. You need the sum of total for completed sales per store. Which clause limits the input to completed sales before grouping?
- GROUP BY status
- HAVING SUM(total) > 0
- ORDER BY status DESC
- WHERE status = 'completed'
Show answer and explanation
Correct answer: D. WHERE status = 'completed'
Filter by status with WHERE to remove other sale rows first. Group the remaining rows by store_id and sum total within each group.
A table’s points column contains 0, NULL, 4, and 4. What does COUNT(points) return over those rows?
- Two
- Four
- Three
- Eight
Show answer and explanation
Correct answer: C. Three
COUNT(expression) ignores nulls. Zero is a non-null value, and repeated non-null values each count unless DISTINCT is specified.
An order has a customer_id. In a Customers table, id is the primary key. Which join condition associates each order with its own customer?
- Orders.customer_id = Customers.id
- Orders.id = Customers.id
- Orders.customer_id = Customers.name
- Orders.customer_id = Orders.id
Show answer and explanation
Correct answer: A. Orders.customer_id = Customers.id
A key-based join compares the order’s customer reference with the matching identifier in Customers.
You want a result containing every customer, including customers who have placed no orders. The Customers table appears first. What type of join to Orders keeps those customers?
- RIGHT JOIN
- INNER JOIN
- CROSS JOIN
- LEFT JOIN
Show answer and explanation
Correct answer: D. LEFT JOIN
A LEFT JOIN keeps all rows from Customers, the left table. Where no matching order exists, order columns are null in the result.
Four rows store the measurements 0, 6, 9, and NULL. What value does AVG(measurement) return?
- 7.5
- 3.75
- 5
- 15
Show answer and explanation
Correct answer: C. 5
The average uses the three non-null values. Their sum is 15, and 15 divided by 3 is 5.
A list must show its highest score first. Players who share a score must be listed from A to Z by player_name. Which clause does both?
- ORDER BY player_name ASC, score DESC
- ORDER BY score DESC, player_name ASC
- ORDER BY score ASC, player_name ASC
- ORDER BY score DESC, player_name DESC
Show answer and explanation
Correct answer: B. ORDER BY score DESC, player_name ASC
SQL sorts by the first expression and uses the second expression to order rows that tie on the first.
A query groups shipments by warehouse_id. You only want warehouses with at least five shipments. Which clause can follow GROUP BY warehouse_id to filter the groups?
- HAVING COUNT(*) > 5
- WHERE COUNT(*) >= 5
- ORDER BY COUNT(*) >= 5
- HAVING COUNT(*) >= 5
Show answer and explanation
Correct answer: D. HAVING COUNT(*) >= 5
HAVING applies a condition after groups are formed. Greater than or equal to includes warehouses that have exactly five shipments.
A Branches table contains IDs 1, 2, and 3. In Loans, there are three matching rows for branch 1, two for branch 2, and none for branch 3. How many rows come back from a LEFT JOIN from Branches to Loans on the branch ID, with no grouping or filters?
- Six
- Five
- Three
- Seven
Show answer and explanation
Correct answer: A. Six
Branches 1 and 2 contribute one result for each matching loan. Branch 3 contributes one result with null loan columns, giving 3 + 2 + 1 = 6.
The Grade table has a primary key on (student_id, course_id). Which pair of rows can coexist under that key?
- (12, NULL) and (12, 42)
- (12, 41) and (12, 41)
- (12, 41) and (12, 42)
- (NULL, 41) and (12, 41)
Show answer and explanation
Correct answer: C. (12, 41) and (12, 42)
A composite primary key requires every component to be non-null and the entire combination to be unique. The same student can appear for different courses.