SELECT: Querying Your Data
The most important SQL command — retrieve, sort, and limit exactly the data you want.
SELECT: the heart of SQL
The SELECT statement retrieves data from a table, and you'll use it more than every other command combined. Its basic form picks which columns you want from which table:
-- Get specific columns
SELECT name, age
FROM students;
-- Get ALL columns with a star
SELECT *
FROM students;
The pattern is SELECT the columns you want, FROM the table they're in. List column names separated by commas, or use * to mean "every column". The two dashes -- start a comment — a note for humans that the database ignores. By convention, SQL keywords are written in uppercase (SELECT, FROM) to stand out, though SQL isn't case-sensitive about them.
WHERE: filtering rows
Usually you don't want every row — you want the ones matching some condition. The WHERE clause filters rows, keeping only those where the condition is true:
-- Students from a specific city
SELECT name, city
FROM students
WHERE city = 'Ahmedabad';
-- Students older than 18
SELECT *
FROM students
WHERE age > 18;
Note an important detail: in SQL, you compare with a single equals = (not == like in most programming languages), and text values go in single quotes ('Ahmedabad'). Numbers don't need quotes. The WHERE clause is where SQL's power begins — you can ask precise questions of huge tables and get back exactly the rows you care about.
Comparison operators in WHERE
WHERE supports all the comparisons you'd expect, letting you express a wide range of conditions:
SELECT * FROM students WHERE age = 19; -- equal
SELECT * FROM students WHERE age != 19; -- not equal (also <>)
SELECT * FROM students WHERE age > 18; -- greater than
SELECT * FROM students WHERE age <= 20; -- less than or equal
These work on numbers and, for ordering, on text and dates too. Each query reads almost like a sentence describing the rows you want — that clarity is what makes SQL so approachable.
ORDER BY: sorting the results
To control the order of your results, add ORDER BY. By default it sorts ascending (smallest first); add DESC for descending (largest first):
-- Students sorted by age, youngest first
SELECT name, age
FROM students
ORDER BY age;
-- Sorted by age, oldest first
SELECT name, age
FROM students
ORDER BY age DESC;
-- Sort alphabetically by name
SELECT name
FROM students
ORDER BY name ASC;
ASC (ascending) is the default so you can omit it; DESC reverses the order. You can even sort by multiple columns — ORDER BY city, age sorts by city first, then by age within each city. Sorting is essential for any "top" or "ranked" question.
LIMIT: just the top few
Often you only want a handful of rows — the top 5, the newest 10. LIMIT caps how many rows come back, and it's especially powerful combined with ORDER BY:
-- The 3 oldest students
SELECT name, age
FROM students
ORDER BY age DESC
LIMIT 3;
This combination — sort by something, then limit — is how you answer "top N" questions: the top 5 best-selling products, the 10 most recent orders, the 3 highest scorers. It's one of the most useful patterns in everyday SQL.
Putting it together
The real power comes from combining these clauses. SQL has a strict order they must appear in — SELECT, FROM, WHERE, ORDER BY, LIMIT — and together they let you ask remarkably precise questions:
-- The 5 oldest students from Ahmedabad, named, oldest first
SELECT name, age
FROM students
WHERE city = 'Ahmedabad'
ORDER BY age DESC
LIMIT 5;
Read it as one clear request: "give me the name and age, from students, where the city is Ahmedabad, sorted oldest first, just the top 5." This single pattern — select, filter, sort, limit — answers a huge proportion of real-world data questions. Next, we'll learn more powerful ways to filter data.
Finished "SELECT: Querying Your Data"?
Mark this chapter complete so you can pick up exactly where you left off. Your progress saves locally — sign in to sync across devices.
Was this chapter clear?
