SELECT, WHERE & ORDER BY Questions with Answers

23 practice questions on SELECT, WHERE & ORDER BY (SQL) with answers and step-by-step solutions. Try 10 samples below, then practise all 23 free in the AptiNest app.

Sample questions

Question 1Easy

What is the logical processing order of these clauses in a SELECT statement?

  1. FROM, WHERE, SELECT, GROUP BY, HAVING, ORDER BY
  2. SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY
  3. FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY
  4. FROM, GROUP BY, WHERE, HAVING, SELECT, ORDER BY

Answer: C. FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY

Explanation

Rows are fetched (FROM), filtered (WHERE), grouped, group-filtered (HAVING), projected (SELECT) and finally sorted (ORDER BY).

Step-by-step solution

  1. Rows come from FROM, then WHERE filters them.
  2. GROUP BY groups the rows, then HAVING filters the groups.
  3. SELECT picks the columns, then ORDER BY sorts; the order is option C.

Why the other options are wrong

  • A. SELECT is not evaluated before GROUP BY and HAVING.
  • B. SELECT is not first; FROM is read first.
  • D. WHERE comes before GROUP BY, not after it.

Tip: Remember the order: FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY.

Question 2Easy

Which names does this query return?

students
id | name  | city    | marks
1  | Kiran | Pune    | 78
2  | Neha  | Delhi   | 91
3  | Arjun | Pune    | NULL
4  | Divya | Chennai | 65
5  | Rohan | NULL    | 91
6  | Sana  | Delhi   | 54
SELECT name FROM students
WHERE marks BETWEEN 65 AND 78
ORDER BY name;
  1. Divya, Kiran
  2. Divya, Kiran, Sana
  3. Kiran, Neha
  4. Kiran

Answer: A. Divya, Kiran

Explanation

BETWEEN is inclusive at both ends, so 65 (Divya) and 78 (Kiran) both qualify; NULL marks never satisfy BETWEEN.

Step-by-step solution

  1. BETWEEN 65 AND 78 includes both ends.
  2. Kiran 78 and Divya 65 qualify.
  3. NULL marks fail, so Arjun is out.
  4. Sorted by name: Divya, Kiran.

Why the other options are wrong

  • B. Sana has 54, outside the range.
  • C. Neha has 91, outside the range.
  • D. Divya at 65 is included too.

Tip: BETWEEN includes both ends.

Question 3Easy

Which names does this query return?

students
id | name  | city    | marks
1  | Kiran | Pune    | 78
2  | Neha  | Delhi   | 91
3  | Arjun | Pune    | NULL
4  | Divya | Chennai | 65
5  | Rohan | NULL    | 91
6  | Sana  | Delhi   | 54
SELECT name FROM students
WHERE name LIKE '%n'
ORDER BY id;
  1. Kiran, Rohan
  2. Neha, Sana
  3. Kiran, Arjun, Rohan, Sana
  4. Kiran, Arjun, Rohan

Answer: D. Kiran, Arjun, Rohan

Explanation

'%n' matches names ending in n: Kiran, Arjun and Rohan.

Step-by-step solution

  1. The pattern %n matches names that end in n.
  2. Kiran, Arjun and Rohan end in n; Neha, Divya and Sana do not.
  3. Ordered by id, that is Kiran, Arjun, Rohan, option D.

Why the other options are wrong

  • A. Arjun also ends in n, so leaving him out is wrong.
  • B. Neha and Sana do not end in n.
  • C. Sana ends in a, so including her is wrong.

Tip: LIKE with a leading percent matches the end of the text; check the last letter.

Question 4Easy

Which titles does this query return?

books
id | title  | author | price | pages
1  | Atlas  | Rao    | 300   | 120
2  | Bloom  | NULL   | 250   | NULL
3  | Cipher | Iyer   | NULL  | 340
4  | Delta  | Rao    | 300   | 210
5  | Ember  | Khan   | 150   | 90
SELECT title FROM books
WHERE author = 'Rao' AND price >= 300
ORDER BY title DESC;
  1. Delta, Atlas
  2. Delta
  3. Atlas
  4. Atlas, Delta

Answer: A. Delta, Atlas

Explanation

Both Rao books cost 300, which satisfies >= 300; DESC lists Delta before Atlas.

Step-by-step solution

  1. author = 'Rao' and price >= 300: Atlas (300) and Delta (300).
  2. ORDER BY title DESC: Delta, then Atlas.

Why the other options are wrong

  • B. Atlas also satisfies both conditions.
  • C. Delta also qualifies.
  • D. DESC puts Delta first.

Tip: DESC = reverse.

Question 5Medium

What does this query return?

books
id | title  | author | price | pages
1  | Atlas  | Rao    | 300   | 120
2  | Bloom  | NULL   | 250   | NULL
3  | Cipher | Iyer   | NULL  | 340
4  | Delta  | Rao    | 300   | 210
5  | Ember  | Khan   | 150   | 90
SELECT title FROM books
ORDER BY price DESC, pages ASC
LIMIT 3;

(SQLite: NULL sorts as the smallest value.)

  1. Cipher, Atlas, Delta
  2. Delta, Atlas, Bloom
  3. Atlas, Delta, Bloom
  4. Atlas, Delta, Ember

Answer: C. Atlas, Delta, Bloom

Explanation

Atlas and Delta tie on price 300, and pages ASC puts Atlas (120) before Delta (210). Bloom (250) is next; Cipher with a NULL price sorts last in DESC.

Step-by-step solution

  1. ORDER BY price DESC sorts 300 (Atlas, Delta), then 250 (Bloom), then 150 (Ember), with Cipher's NULL last.
  2. Ties on price 300 are broken by pages ASC: Atlas (120) before Delta (210).
  3. LIMIT 3 gives Atlas, Delta, Bloom, option C.

Why the other options are wrong

  • A. Cipher has a NULL price, so it sorts last in DESC and is not in the top three.
  • B. Delta has 210 pages, so it comes after Atlas (120) on the tie.
  • D. Ember (150) is below Bloom (250), so it is not third.

Tip: Use the second ORDER BY column to break ties; in SQLite a NULL sorts as the smallest value.

Question 6Medium

How many rows does this query return?

students
id | name  | city    | marks
1  | Kiran | Pune    | 78
2  | Neha  | Delhi   | 91
3  | Arjun | Pune    | NULL
4  | Divya | Chennai | 65
5  | Rohan | NULL    | 91
6  | Sana  | Delhi   | 54
SELECT DISTINCT city, marks FROM students;
  1. 5
  2. 4
  3. 6
  4. 3

Answer: C. 6

Explanation

DISTINCT applies to the whole (city, marks) combination, and every pair here is different, so all 6 rows remain.

Step-by-step solution

  1. DISTINCT applies to the city and marks pair together.
  2. The pairs are Pune 78, Delhi 91, Pune NULL, Chennai 65, NULL 91 and Delhi 54; all six differ.
  3. So all 6 rows remain, option C.

Why the other options are wrong

  • A. 5 would mean two pairs were merged, but no two pairs are the same.
  • B. 4 would mean pairs were merged, but none are identical.
  • D. 3 is wrong because all six pairs are different.

Tip: DISTINCT over several columns removes only rows where every listed column matches.

Question 7Medium

What does this query return?

matches
mid | home   | away   | hg   | ag
1   | Lions  | Tigers | 2    | 1
2   | Bears  | Lions  | 0    | 0
3   | Tigers | Bears  | 3    | 2
4   | Lions  | Bears  | 1    | 3
5   | Tigers | Lions  | NULL | NULL
SELECT mid, hg - ag AS margin FROM matches
ORDER BY ABS(hg - ag) DESC, mid
LIMIT 2;

(SQLite: NULL sorts as the smallest value.)

  1. 4 -2, 1 1
  2. 1 1, 3 1
  3. 4 -2, 3 1
  4. 3 1, 1 1

Answer: A. 4 -2, 1 1

Explanation

ORDER BY may use an expression not in the SELECT list. Absolute margins: 1, 0, 1, 2, NULL; the largest is match 4, then the tie at 1 goes to the lower mid (1).

Step-by-step solution

  1. Margins hg − ag: match 1 is 1, match 2 is 0, match 3 is 1, match 4 is −2, match 5 is NULL.
  2. Largest absolute margin: match 4 (2). The tie at 1 goes to the lower mid, match 1.
  3. Result: 4 −2, then 1 1 (option A).

Why the other options are wrong

  • B. Match 4 has the largest absolute margin of 2, so it must come first.
  • C. Match 1 has the same margin as match 3 and the lower mid, so it comes before 3.
  • D. Match 4 has the largest absolute margin, and match 1 comes before match 3.

Tip: ORDER BY ABS(expression) sorts by size, not sign; add mid as a tie-break.

Question 8Medium

What happens when this query runs?

products
id | name     | category    | price | stock
1  | Pen      | Stationery  | 10    | 100
2  | Notebook | Stationery  | 45    | NULL
3  | Mouse    | Electronics | 450   | 20
4  | Keyboard | Electronics | 900   | 0
5  | Bottle   | NULL        | 120   | 35
6  | Charger  | Electronics | NULL  | 12
SELECT name, price FROM products
ORDER BY 3;
  1. An error: the ORDER BY position 3 is out of range
  2. Rows sorted by the third table column, category
  3. Only the first 3 rows are returned
  4. Rows returned in insertion order

Answer: A. An error: the ORDER BY position 3 is out of range

Explanation

A number in ORDER BY refers to a position in the SELECT list, not the table. The list has only 2 columns, so position 3 is invalid.

Step-by-step solution

  1. ORDER BY 3 refers to the third column in the SELECT list.
  2. The select list is name, price - only 2 columns.
  3. Position 3 is out of range: error.

Why the other options are wrong

  • B. Numbers refer to the SELECT list, not the table.
  • C. ORDER BY does not limit rows.
  • D. Sort is requested, and an invalid position gives an error.

Tip: Position = select list.

Question 9Medium

What does this query return?

products
id | name     | category    | price | stock
1  | Pen      | Stationery  | 10    | 100
2  | Notebook | Stationery  | 45    | NULL
3  | Mouse    | Electronics | 450   | 20
4  | Keyboard | Electronics | 900   | 0
5  | Bottle   | NULL        | 120   | 35
6  | Charger  | Electronics | NULL  | 12
SELECT name, price * 2 AS doubled
FROM products
WHERE doubled > 800;

(Run in SQLite.)

  1. Keyboard 1800
  2. Mouse 900, Keyboard 1800, Charger NULL
  3. Mouse 900, Keyboard 1800
  4. An error in SQLite because aliases cannot be used in WHERE

Answer: C. Mouse 900, Keyboard 1800

Explanation

SQLite allows a SELECT alias in WHERE as an extension (standard SQL, MySQL and PostgreSQL do not). Mouse (900) and Keyboard (1800) exceed 800; NULL price gives NULL.

Step-by-step solution

  1. SQLite lets a WHERE clause use the SELECT alias doubled as an extension.
  2. Doubled prices: Pen 20, Notebook 90, Mouse 900, Keyboard 1800, Bottle 240, Charger NULL.
  3. Only Mouse and Keyboard are above 800, so the result is option C.

Why the other options are wrong

  • A. Mouse also passes, so the result has two rows, not one.
  • B. Charger's doubled value is NULL, which is not above 800, so it is not returned.
  • D. SQLite allows the alias in WHERE, so there is no error.

Tip: Aliases in WHERE work in SQLite but not in standard SQL; repeat the expression for portable code.

Question 10Medium

What does this query return?

students
id | name  | city    | marks
1  | Kiran | Pune    | 78
2  | Neha  | Delhi   | 91
3  | Arjun | Pune    | NULL
4  | Divya | Chennai | 65
5  | Rohan | NULL    | 91
6  | Sana  | Delhi   | 54
SELECT name FROM students
ORDER BY marks DESC
LIMIT 2 OFFSET 2;

(SQLite: NULL sorts as the smallest value.)

  1. Kiran, Divya
  2. Neha, Rohan
  3. Rohan, Kiran
  4. Divya, Sana

Answer: A. Kiran, Divya

Explanation

Sorted by marks descending: 91, 91, 78, 65, 54, NULL. OFFSET 2 skips the two 91s and LIMIT 2 takes 78 (Kiran) and 65 (Divya).

Step-by-step solution

  1. Sorted by marks descending: 91 (Neha), 91 (Rohan), 78 (Kiran), 65 (Divya), 54 (Sana), NULL (Arjun).
  2. OFFSET 2 skips the first two rows, the two 91s.
  3. LIMIT 2 then takes Kiran and Divya, option A.

Why the other options are wrong

  • B. Neha and Rohan are the two rows that OFFSET 2 skips.
  • C. Rohan is skipped by OFFSET, and Kiran comes third in the sorted order.
  • D. Divya is right, but Sana (54) is fifth, after the two rows that LIMIT takes.

Tip: OFFSET skips rows first, then LIMIT takes the next rows; ORDER BY is applied before both.

Practise all 23 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all SQL topics · All topics