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;
- Chennai; Mumbai; Pune
- Pune; Mumbai; Chennai
- Chennai; Mumbai; Mumbai; Pune; Pune; Pune
- 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?
- SELECT, FROM, WHERE, ORDER BY, LIMIT
- FROM, SELECT, WHERE, ORDER BY, LIMIT
- FROM, WHERE, ORDER BY, SELECT, LIMIT
- 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;
- Mixer, 16000; Tawa, 13000
- Mixer, 3200; Kurta, 899
- Nothing; it fails because an alias cannot be used in ORDER BY
- 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
- Table Students: roll | name | city | marks 1 | Aarav | Pune | 78 2 | Diya |…
- In the query below, what is stock_value?
- Table Products: pid | name | category | price | stock 1 | Kurta | Clothing |…
- Table Payments: txn_id | payer | app | amount | status 1 | Farhan | GPay | 250…
- Table Students: roll | name | city | marks 1 | Aarav | Pune | 78 2 | Diya |…
- Table Products: pid | name | category | price | stock 1 | Kurta | Clothing |…
- Table Products: pid | name | category | price | stock 1 | Kurta | Clothing |…
- Table Products: pid | name | category | price | stock 1 | Kurta | Clothing |…
- Table Students: roll | name | city | marks 1 | Aarav | Pune | 78 2 | Diya |…
- Table Students: roll | name | city | marks 1 | Aarav | Pune | 78 2 | Diya |…
- Table Payments: txn_id | payer | app | amount | status 1 | Farhan | GPay | 250…
- Table Products: pid | name | category | price | stock 1 | Kurta | Clothing |…
- A query has the clauses SELECT, FROM, WHERE, ORDER BY and LIMIT. In which…
- Table Students: roll | name | city | marks 1 | Aarav | Pune | 78 2 | Diya |…
- Table Payments: txn_id | payer | app | amount | status 1 | Farhan | GPay | 250…
- Table Products: pid | name | category | price | stock 1 | Kurta | Clothing |…
- Table Students: roll | name | city | marks 1 | Aarav | Pune | 78 2 | Diya |…
- Table Payments: txn_id | payer | app | amount | status 1 | Farhan | GPay | 250…
- Table Students: roll | name | city | marks 1 | Aarav | Pune | 78 2 | Diya |…
- Table Students: roll | name | city | marks 1 | Aarav | Pune | 78 2 | Diya |…
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
- SQL Joins: Inner, left, right, full, cross and self joins, and row counts.
- Aggregates & GROUP BY: COUNT, SUM, AVG, grouping and HAVING versus WHERE.
- Subqueries & CTEs: Scalar and correlated subqueries, IN, EXISTS and WITH.
- Window Functions: ROW_NUMBER, RANK, DENSE_RANK, PARTITION BY, LAG and running totals.
- NULL Handling: Three-valued logic, IS NULL, COALESCE and NULL in aggregates.
- SQL Functions & CASE: CASE WHEN, conditional aggregation, string and numeric functions.
- UNION, INTERSECT & EXCEPT: Combining results, duplicates and column rules.
- Tables, Keys & Constraints: CREATE, ALTER, primary and foreign keys, UNIQUE and CHECK.
- DML & Transactions: INSERT, UPDATE, DELETE, COMMIT, ROLLBACK and ACID.
Other subjects
- Quantitative Aptitude: Arithmetic, percentages, time and work, speed, probability and data interpretation: the numerical section of every fresher test.
- Logical Reasoning: Series, coding-decoding, arrangements, syllogisms and puzzles: the section that separates prepared candidates from the rest.
- Verbal Ability: Vocabulary, grammar, reading comprehension and sentence order: tested in nearly every IT services hiring exam.
- Technical & Programming: CS fundamentals, output prediction and pseudo-code: the technical MCQ round for IT roles.
- Python: Python quizzes by topic, from types and strings to classes and generators: output prediction and interview concepts.
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