Querying with SQL: SELECT
1 min read3 lessons in topic
Learning objective
By the end of this lesson you will be able to retrieve data with SELECT, WHERE, ORDER BY and LIMIT.
SQL (Structured Query Language) is how you talk to a relational database. The SELECT statement retrieves data.
Selecting columns
SELECT name, programme FROM students;SELECT chooses which columns to return and FROM says which table to read from.
Filtering with WHERE
SELECT name FROM students
WHERE programme = 'Information Technology';- = equal
- <> not equal
- > < greater / less than
- LIKE '%Ama%' — pattern match
- AND / OR — combine conditions
Sorting & limiting
SELECT name, score FROM results
WHERE score >= 50
ORDER BY score DESC
LIMIT 5;ORDER BY sorts the results (ASC ascending, DESC descending); LIMIT caps how many rows are returned.
Practice exercise
Write a query that returns the names of students with a score of at least 70, sorted alphabetically.
Check your understanding
1. Which clause filters rows before returning them?
2. How would you return the 10 highest scores?
Self-graded — answer then check below.