SELECT, WHERE & ORDER BY questions and answers

Filtering, DISTINCT, IN, BETWEEN, LIKE, sorting and LIMIT. Practise select, where & order by questions from the sql section of placement tests, with a worked explanation after every answer. Practice is free and needs no sign-up.

Sample select, where & order by questions with worked answers

Question 1

Table Students:
roll | name | city | marks
1 | Aarav | Pune | 78
2 | Diya | Mumbai | 91
3 | Kabir | Pune | 64
4 | Meena | Chennai | 91
5 | Rohan | Mumbai | NULL
6 | Sneha | Pune | 85

What does this query return (one city per row)?

SELECT DISTINCT city
FROM Students
ORDER BY city;
  1. Chennai; Mumbai; Pune
  2. Pune; Mumbai; Chennai
  3. Chennai; Mumbai; Mumbai; Pune; Pune; Pune
  4. Pune; Mumbai; Pune; Chennai; Mumbai; Pune

Answer: A. Chennai; Mumbai; Pune

Explanation: DISTINCT keeps each city once, leaving Pune, Mumbai and Chennai. ORDER BY city then sorts them alphabetically: Chennai, Mumbai, Pune.

Question 2

A query has the clauses SELECT, FROM, WHERE, ORDER BY and LIMIT. In which logical order are they evaluated?

  1. SELECT, FROM, WHERE, ORDER BY, LIMIT
  2. FROM, SELECT, WHERE, ORDER BY, LIMIT
  3. FROM, WHERE, ORDER BY, SELECT, LIMIT
  4. FROM, WHERE, SELECT, ORDER BY, LIMIT

Answer: D. FROM, WHERE, SELECT, ORDER BY, LIMIT

Explanation: Rows are first read (FROM) and filtered (WHERE), then the SELECT list is computed, the result is sorted, and the row limit is applied last. This is why ORDER BY can use a SELECT alias but WHERE cannot.

Question 3

Table Products:
pid | name | category | price | stock
1 | Kurta | Clothing | 899 | 12
2 | Saree | Clothing | 2499 | 0
3 | Mixer | Kitchen | 3200 | 5
4 | Tawa | Kitchen | 650 | 20
5 | Earbuds | Electronics | 1499 | 0
6 | Charger | Electronics | 499 | 40

What does this query return?

SELECT name, price * stock AS stock_value
FROM Products
WHERE stock > 0
ORDER BY stock_value DESC
LIMIT 2;
  1. Mixer, 16000; Tawa, 13000
  2. Mixer, 3200; Kurta, 899
  3. Nothing; it fails because an alias cannot be used in ORDER BY
  4. Charger, 19960; Mixer, 16000

Answer: D. Charger, 19960; Mixer, 16000

Explanation: ORDER BY runs after SELECT, so the alias stock_value is allowed there. The values are Kurta 10788, Mixer 16000, Tawa 13000 and Charger 499 × 40 = 19960, so the top two are Charger and Mixer.

All 20 select, where & order by questions with answers

SELECT, WHERE & ORDER BY: common questions

What are select, where & order by questions?

Filtering, DISTINCT, IN, BETWEEN, LIKE, sorting and LIMIT. They appear in the sql section of many placement tests.

Where can I practise select, where & order by questions with answers for free?

Right here on LogicWiz Exams. Every select, where & order by question has a worked explanation after you answer. Practice is free and needs no sign-up.

How should I practise select, where & order by?

Start with easy questions, then move to medium and hard. Read the explanation after each answer, and come back to the ones you missed.

Start practising

On this page you can start a fresh set of select, where & order by questions, choose easy, medium or hard, and time yourself if you want to. Or practise the whole subject: SQL practice.

More sql topics

Other subjects

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge · Test from a job description · LogicWiz Open: free team quiz · Free interview prep