SQL SELECT queries
Querying a Database with SQL
SQL (Structured Query Language) is the standard language used to retrieve and manipulate data in relational databases. A query is an instruction sent to the database specifying what data you want. The database processes the query and returns only the matching results - you never need to look at all the data manually.
The four keywords you need to know for retrieval queries are SELECT, FROM, WHERE, and ORDER BY. These are used together in a specific order to tell the database which columns to return, which table to look in, which rows to include, and how to sort the results.
The Table Used in This Benchmark
All examples below use a Students table from a school database:
| StudentID | Surname | FirstName | YearGroup | Grade |
|---|---|---|---|---|
| 1001 | Khan | Aisha | 10 | A |
| 1002 | Lee | James | 11 | B |
| 1003 | Patel | Priya | 10 | A |
| 1004 | Smith | Tom | 11 | C |
| 1005 | Ahmed | Sara | 10 | B |
SQL Query Examples
SELECT * FROM - Retrieve All Columns
The asterisk * means "all columns". This query retrieves every column and every row from the Students table.
SELECT *
FROM Students;
Result: all 5 rows, all 5 columns - the entire table.
Syntax rule: SQL keywords are conventionally written in uppercase. Each clause on a new line improves readability. The query ends with a semicolon.
SELECT Specific Columns
Instead of *, list the column names you want, separated by commas. Only those columns appear in the results.
SELECT Surname, FirstName, Grade
FROM Students;
Result: all 5 rows, but only the Surname, FirstName, and Grade columns:
| Surname | FirstName | Grade |
|---|---|---|
| Khan | Aisha | A |
| Lee | James | B |
| Patel | Priya | A |
| Smith | Tom | C |
| Ahmed | Sara | B |
WHERE - Filtering Which Rows to Return
WHERE adds a condition: only rows where the condition is true are included in the results. The database checks every row against the condition and returns only the matching ones.
SELECT Surname, FirstName
FROM Students
WHERE YearGroup = 10;
Result: Khan/Aisha, Patel/Priya, Ahmed/Sara (the three Year 10 students only).
WHERE with comparison operators
The condition can use: = (equals), <> or != (not equal), > < >= <= (greater/less than or equal to). Text values must be enclosed in single quotes.
SELECT Surname, Grade
FROM Students
WHERE Grade = 'A';
Result: Khan/A and Patel/A (students with grade A).
ORDER BY - Sorting the Results
ORDER BY sorts the results by a specified column. ASC (ascending) sorts A to Z or smallest to largest; DESC (descending) sorts Z to A or largest to smallest. ASC is the default if neither is specified.
SELECT Surname, FirstName, Grade
FROM Students
ORDER BY Surname ASC;
Result: all students sorted alphabetically by surname:
| Surname | FirstName | Grade |
|---|---|---|
| Ahmed | Sara | B |
| Khan | Aisha | A |
| Lee | James | B |
| Patel | Priya | A |
| Smith | Tom | C |
Combining WHERE and ORDER BY
WHERE and ORDER BY can be used together. WHERE must come before ORDER BY.
SELECT Surname, FirstName, Grade
FROM Students
WHERE YearGroup = 10
ORDER BY Surname ASC;
Result: Year 10 students only, sorted A to Z by surname: Ahmed, Khan, Patel.
Clause Order in a SELECT Query
The keywords must appear in this fixed order:
SELECT column(s) -- which columns to return (* for all)
FROM table -- which table to query
WHERE condition -- (optional) which rows to include
ORDER BY column ASC|DESC -- (optional) how to sort
You must always have SELECT and FROM. WHERE and ORDER BY are optional, but if both are present, WHERE must come before ORDER BY.
Key Takeaways
- SELECT specifies which columns to return;
*means all columns. - FROM specifies which table to query.
- WHERE filters which rows are returned - only rows where the condition is true are included.
- ORDER BY sorts the results; ASC is ascending (A-Z, smallest-largest); DESC is descending (Z-A, largest-smallest).
- Clause order is fixed: SELECT → FROM → WHERE → ORDER BY. Text values in WHERE conditions must be enclosed in single quotes.