All sample questions / SQL

What does SELECT dish FROM menu WHERE price > 10 ORDER BY dish return? SQL Data Modification and Constraints

Block 8, Data Modification and Constraints, question 20 of the SQL sample

CREATE TABLE menu (id INTEGER PRIMARY KEY, dish TEXT, price INTEGER);
INSERT INTO menu VALUES (1, 'soup', 8), (2, 'salad', 7);
INSERT INTO menu VALUES (3, 'steak', 22), (4, 'pasta', 14), (5, 'cake', 9);

SELECT dish FROM menu WHERE price > 10 ORDER BY dish

A restaurant adds new dishes in one statement. The statements above run, then this query. What comes back?

  1. A. pasta
  2. B. pasta steak
  3. C. cake pasta steak
  4. D. steak pasta
Show the answer and why each option is right or wrong

Answer, B. pasta steak

A. pasta
This drops steak, as if only the first of the three rows added by the second INSERT had been kept.
B. pasta steak
Correct. Five menu items exist once both INSERT statements run, and only steak at 22 and pasta at 14 clear the price greater than 10 filter, listed alphabetically.
C. cake pasta steak
This includes cake, which costs 9 and does not pass the price greater than 10 filter.
D. steak pasta
This has the right two dishes but in the wrong order, listing steak before pasta instead of alphabetically.

A WHERE clause is applied after the table already holds every row from every INSERT in the schema, it filters the finished data rather than the statements that built it. Rows from a multi row INSERT are each tested against the condition individually, only cake among the three new rows fails it here.

The answer above is what SQLite and DuckDB both returned when the snippet was run, not what someone expected it to do.

This question comes from a bank of 502 SQL interview questions, all built the same way, sold as one PDF with the full answer key.

Get the 502 SQL questions, 9 euros

Or SQL, Python, pandas and NumPy together, 1,636 questions, 19 euros

Or play the free SQL sample, twenty questions in the browser, also as a free PDF.

Previous question

Did this save you a lookup? Buy me a coffee on Ko-fi.