SQL SELECT Basics
The SELECT statement is how you read data out of a SQL database. It is the single most-used statement in SQL — you will write it thousands of times. At its simplest it says "give me these columns from this table, matching these conditions, in this order." Master SELECT and you have unlocked most of day-to-day database work.
The basic shape
A SELECT query is built from clauses, written in this order:
SELECT column_list -- which columns you want
FROM table_name -- where the data lives
WHERE condition -- which rows to keep
ORDER BY column_list -- how to sort the result
LIMIT n; -- how many rows to return
Let us use a student table for examples:
-- PostgreSQL / MySQL compatible
-- 1. Get every column, every row (the * means "all columns")
SELECT * FROM student;
-- 2. Get only the columns you care about (better practice than *)
SELECT full_name, city FROM student;
-- 3. Filter rows with WHERE: only students from Jalgaon
SELECT full_name, city
FROM student
WHERE city = 'Jalgaon';
-- 4. Sort the result: alphabetical by name
SELECT full_name, city
FROM student
WHERE city = 'Jalgaon'
ORDER BY full_name ASC; -- ASC = ascending (default); DESC = descending
-- 5. Limit how many rows come back: top 5 only
SELECT full_name, city
FROM student
ORDER BY full_name
LIMIT 5;
Note on LIMIT: this works in PostgreSQL and MySQL. SQL Server uses TOP, and the SQL standard form is FETCH FIRST 5 ROWS ONLY (also supported by PostgreSQL). When portability matters, the FETCH FIRST form is the standard one.
Filtering with WHERE
The WHERE clause is where queries get useful. Operators you will use constantly:
-- Comparison
SELECT * FROM student WHERE age >= 18;
-- Multiple conditions with AND / OR
SELECT * FROM student WHERE city = 'Jalgaon' AND age >= 18;
-- Range with BETWEEN (inclusive on both ends)
SELECT * FROM student WHERE age BETWEEN 18 AND 25;
-- Membership with IN
SELECT * FROM student WHERE city IN ('Jalgaon', 'Pune', 'Nashik');
-- Pattern matching with LIKE (% = any characters, _ = one character)
SELECT * FROM student WHERE full_name LIKE 'A%'; -- names starting with A
-- NULL must use IS NULL, never = NULL
SELECT * FROM student WHERE phone IS NULL;
That last one matters: NULL means "unknown," and column = NULL is never true. You must use IS NULL or IS NOT NULL.
Want to learn this properly?
Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.
Browse coursesDISTINCT: removing duplicates
DISTINCT collapses duplicate rows in the result:
-- List each city once, even if many students live there
SELECT DISTINCT city FROM student;
Aliasing columns and expressions
You can compute values and rename output columns with AS:
-- Combine columns and rename the output
SELECT
full_name AS name,
age,
age + 1 AS age_next_year -- a computed column
FROM student;
The clause that surprises everyone: execution order
You write a query starting with SELECT, but the database executes the clauses in a different order:
FROM— find the source table(s)WHERE— filter rowsGROUP BY— group them (covered in GROUP BY & aggregates)HAVING— filter the groupsSELECT— pick/compute the output columnsORDER BY— sortLIMIT— cut to size
This order explains a common error: you cannot use a column alias defined in SELECT inside the WHERE clause, because WHERE runs before SELECT. You can use it in ORDER BY, which runs after.
-- WORKS: alias used in ORDER BY (runs after SELECT)
SELECT full_name, age + 1 AS age_next_year
FROM student
ORDER BY age_next_year;
-- FAILS in standard SQL: alias used in WHERE (runs before SELECT)
-- SELECT full_name, age + 1 AS age_next_year
-- FROM student
-- WHERE age_next_year > 20; -- error: age_next_year unknown here
Common mistakes
- Using
= NULL. It silently matches nothing. Always useIS NULL/IS NOT NULL. - Selecting
*in production code. It pulls every column (slower, fragile if columns change). Name the columns you actually need. - Expecting a stable row order without
ORDER BY. The database may return rows in any order, and that order can change. If order matters, say so explicitly. - Referencing a SELECT alias in WHERE. Because of execution order, the alias does not exist yet. Repeat the expression or use a subquery / CTE.
- Confusing
LIKEwildcards.%matches many characters,_matches exactly one. Forgetting which is which leads to wrong matches.
FAQ
Is SQL case-sensitive?
Keywords are not (SELECT = select). String comparisons depend on the database's collation — MySQL is often case-insensitive by default, PostgreSQL is case-sensitive. Identifiers vary too. When in doubt, be consistent and test.
What is the difference between WHERE and HAVING?
WHERE filters individual rows before grouping; HAVING filters groups after GROUP BY. See GROUP BY & aggregate functions.
Why use column names instead of *?
Naming columns is faster, makes intent clear, and protects you if someone adds or reorders columns later.
Keep learning
Once you can read data, learn to change it with INSERT, UPDATE & DELETE, combine tables with SQL joins, and summarise it with GROUP BY & aggregate functions.
Want to practise writing queries against real datasets? Join the waitlist for the DBMS & SQL course at Infoplanet, Jalgaon.
Want to learn this properly?
Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.
Browse coursesFounder, Infoplanet
Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.
Related guides
The ER Model Explained
A plain-language guide to the Entity-Relationship model — the conceptual blueprint you draw before you build any database. Covers entities, attributes, keys, relationships, cardinality, and how to convert an ER diagram into tables.
Keys in DBMS Explained
Every type of key in a relational database explained simply: super, candidate, primary, alternate, composite, foreign, and surrogate keys — with SQL you can run.
Database Normalization (1NF–BCNF)
A step-by-step walk through normalization from 1NF to BCNF, using one example table refactored stage by stage. Covers functional dependencies, the anomalies normalization prevents, and when denormalization makes sense.
