Skip to content
beginner2 min readFree guide

SQL SELECT and WHERE: ask for the rows and columns you need

InterviewSolutionHub learning library · Updated

On this page

A SELECT query describes a result. Learn to separate column selection, row filtering and ordering so a query returns a deliberate answer instead of a coincidental-looking sample.

Choose columns with a purpose

SELECT lists the values you want to return. Naming columns makes the result easier to understand and reduces reliance on the current table layout. A database row is not automatically a public response: choose which fields the caller is allowed to see before returning data through an API.

Write a condition for each included row

WHERE filters individual rows. Combine conditions with AND and OR carefully, using parentheses when grouping changes the meaning. NULL represents missing or unknown information and requires special handling, such as IS NULL rather than equality to NULL. Keep the database dialect in mind when using functions or date expressions.

Request an order when order matters

Without ORDER BY, do not rely on a particular row order. Add a stable tie-breaker when equal values could make pagination ambiguous. Limit a result only after you know what “first” means for the task. Test a query with rows that should be included and similar rows that should be excluded.

Worked example

The fictional results table contains scores of 72, 91 and 85. Filtering for scores of at least 80 keeps two learners; ordering by score descending places Noah before Mia. The lower score is deliberately included in the fixture to test the boundary.

CREATE TABLE results (name TEXT, score INTEGER);
INSERT INTO results VALUES ('Ava',72), ('Noah',91), ('Mia',85);
SELECT name, score FROM results WHERE score >= 80 ORDER BY score DESC, name;

Expected output

Noah | 91
Mia | 85

Check your understanding

Try each question before opening its explanation. These are free practice prompts; they do not use game chances.

Does SELECT * always return data in insertion order?

No. It selects columns, not an ordering rule. Use ORDER BY when the order is part of the required result.

Why can AND and OR need parentheses?

Their precedence can make an ungrouped condition mean something different from the intended rule. Write the logical groups explicitly and test both included and excluded examples.

Would a learner with score 80 appear here?

Yes. The condition uses greater than or equal to 80. Changing it to strictly greater than 80 would exclude that boundary value.

Sources and further reading

These original examples explain foundational concepts. Use the official references for complete, current details and version-specific behaviour.

About this guide: AI-assisted educational content with original examples and linked primary references. Found an issue? Tell us what to improve.

Keep exploring Databases

All subtopics →