SQL SELECT, WHERE and ORDER BY Explained for Computer Science Students
Basic SQL is easier than it looks, once each keyword has a job in your head. Students get stuck when they try to read a query like a sentence, left to right, without knowing what each part is for.
So let us give every keyword a plain-English question. Read a query by answering these four questions in turn:
By the end of this lesson, you will be able to:
Our example table
Every query below runs against this Students table.
| StudentID | Name | Age | Score |
|---|---|---|---|
| 101 | Alice | 15 | 81 |
| 102 | Ben | 16 | 65 |
| 103 | Chen | 15 | 90 |
| 104 | Dia | 16 | 72 |
SELECT and FROM
In plain English: “Display the Name and Score fields from the Students table.” SELECT chooses the fields (columns) and FROM names the table. This query returns all four students, but only their Name and Score.
Selecting every field
If you want every field rather than a named list, use an asterisk in place of the field names:
The * means “all fields”, so this returns every column for every student — StudentID, Name, Age and Score. Reach for it when you genuinely need the whole record; name the fields you want when you do not, because a shorter result is easier to read and to mark.
WHERE: filtering records
WHERE filters records (rows), not fields. It keeps only the rows that satisfy the condition. Here, only students aged 15 remain:
| Name | Score |
|---|---|
| Alice | 81 |
| Chen | 90 |
Notice the split of responsibilities: SELECT decided the columns, WHERE decided the rows. Keeping those two jobs separate in your mind prevents most SQL errors.
ORDER BY: sorting results
ORDER BY sorts the results. ASC means ascending and DESC means descending. Ascending is normally the default when no direction is given, consistent with the FutureLogic Topic 9 material. Sorted by Score descending, the students run 90, 81, 72, 65 — Chen, Alice, Dia, Ben.
Putting it together
Read a combined query in stages rather than all at once:
- Table = Students.
- Keep records where Age = 15.
- Display the Name and Score fields.
- Sort by Score, descending.
That leaves Chen (90) then Alice (81). Four keywords, four questions answered in order — and the result is obvious.
Exam Tip: read by role, not left to right
Do not try to read SQL like an English sentence from left to right. Read it by the role of each keyword:
Fields → Table → Filter → Sort.
Answering “what fields, which table, which records, in what order” will get you through almost every basic query question.
- Writing field names after FROM (fields belong after SELECT).
- Confusing records with fields.
- Using WHERE to choose columns — WHERE chooses rows.
- Reversing ASC and DESC.
- Assuming ORDER BY filters records — it only sorts them.
- Forgetting quotes where string values require them.
Where this leads next
Once this structure is secure, the aggregate functions become much easier. Functions such as SUM() and COUNT() simply act on the records your query has already selected — so if you can pick the right rows and fields, you are most of the way to summarising them too.
Ascending is the default
If you leave the direction off, the sort is ascending by default. These two queries do exactly the same thing:
Both list the scores from lowest to highest: 65, 72, 81, 90 — Ben, Dia, Alice, Chen. Write ASC in when you want to be explicit, but know that a bare ORDER BY already means ascending.
A note on quotes
Numbers are written plainly, but text values usually need quotation marks. To find a student by name you would write:
The quotes tell the database that Chen is a piece of text to match, not the name of a field. Forgetting them around a string is one of the most common reasons a simple query fails, so it is worth building the habit now.
Check Your Understanding
- Which keyword chooses the fields to display?
- Does WHERE filter fields or records?
- What does
ORDER BY Score DESCdo?
Check the answers
1. SELECT chooses the fields. 2. WHERE filters records (rows). 3. It sorts the results by the Score field in descending order, highest first.
Final Summary
SELECT
Chooses the fields (columns) to display.
FROM
Names the table to read from.
WHERE
Filters the records (rows) by a condition.
ORDER BY
Sorts results; ASC ascending, DESC descending.