A SQL SELECT statement chooses which columns to return, WHERE filters which rows to include, ORDER BY sorts the results, LIMIT caps how many rows come back, and DISTINCT removes duplicates. Together, they answer most everyday questions about a single table.
In Part 1, you learned what SQL is and ran your first queries. This part goes deeper into the statement you’ll write more than any other. By the end, you’ll be able to find exactly the rows you need, in the order you want, from any table.
All examples use the series’ sample database. If you haven’t set it up, download shop.sql and follow the setup steps in Part 1. The examples were tested on PostgreSQL, MySQL, SQL Server, and SQLite; differences are noted where they matter.
Choosing Columns with SELECT
SELECT lists the columns you want, and FROM names the table:
SELECT first_name, last_name, city
FROM customers;
SELECT * returns every column. It’s handy for a quick look, but in real queries name the columns you need: the results are easier to read, faster to return, and won’t change unexpectedly if someone adds a column.
Aliases and Calculated Columns
You can rename a column in the results with AS, and calculate new values from existing ones:
SELECT name AS product, price, price * 1.10 AS price_plus_10
FROM products;
product | price | price_plus_10
--------------+--------+---------------
Laptop | 899.00 | 988.9000
Monitor | 249.00 | 273.9000
Keyboard | 45.00 | 49.5000
Mouse | 25.00 | 27.5000
Desk | 320.00 | 352.0000
Office Chair | 180.00 | 198.0000
The calculation only changes the results, not the data stored in the table. SQLite shows 988.9 instead of 988.9000, because it doesn’t keep a fixed number of decimal places.
Filtering Rows with WHERE
WHERE keeps only the rows that match a condition:
SELECT name, price
FROM products
WHERE price > 200;
name | price
---------+--------
Laptop | 899.00
Monitor | 249.00
Desk | 320.00
Comparison Operators
| Operator | Meaning | Example |
|---|---|---|
= | Equal to | state = 'CA' |
<> or != | Not equal to | status <> 'shipped' |
> / < | Greater / less than | price > 200 |
>= / <= | Greater / less than or equal to | quantity >= 3 |
Text and dates go in single quotes ('CA', '2025-09-01'); numbers don’t.
Combining Conditions: AND, OR, NOT
AND requires every condition to be true:
SELECT first_name, last_name, city
FROM customers
WHERE state = 'CA' AND city = 'San Diego';
first_name | last_name | city
------------+-----------+-----------
James | Carter | San Diego
Omar | Haddad | San Diego
OR requires at least one:
SELECT name, category, price
FROM products
WHERE category = 'Furniture' OR price > 500;
name | category | price
--------------+-------------+--------
Laptop | Electronics | 899.00
Desk | Furniture | 320.00
Office Chair | Furniture | 180.00
NOT reverses a condition, as in WHERE NOT state = 'CA' or WHERE state NOT IN (...).
Use Parentheses When Mixing AND and OR
AND is evaluated before OR, just like multiplication before addition. This is one of the most common beginner bugs. Say you want shipped or pending orders with a quantity above 3:
-- Wrong: AND is applied first
SELECT order_id, status, quantity
FROM orders
WHERE status = 'shipped' OR status = 'pending' AND quantity > 3;
This returns 9 rows: every shipped order, plus pending orders over 3, because SQL reads it as shipped OR (pending AND quantity > 3). Add parentheses to say what you mean:
-- Right
SELECT order_id, status, quantity
FROM orders
WHERE (status = 'shipped' OR status = 'pending') AND quantity > 3
ORDER BY order_id;
order_id | status | quantity
----------+---------+----------
107 | shipped | 4
108 | pending | 5
When a condition mixes AND and OR, always use parentheses, even when they aren’t strictly needed.
Matching Lists and Ranges: IN and BETWEEN
IN is a shorter way to write several OR conditions on the same column:
SELECT order_id, status
FROM orders
WHERE status IN ('pending', 'returned', 'cancelled')
ORDER BY order_id;
order_id | status
----------+-----------
106 | returned
108 | pending
111 | cancelled
112 | pending
NOT IN does the opposite: WHERE state NOT IN ('CA', 'AZ') returns the two Nevada customers.
BETWEEN matches a range, and includes both ends:
SELECT name, price
FROM products
WHERE price BETWEEN 45 AND 249
ORDER BY price;
name | price
--------------+--------
Keyboard | 45.00
Office Chair | 180.00
Monitor | 249.00
It works with dates too. Orders placed in September 2025:
SELECT order_id, order_date
FROM orders
WHERE order_date BETWEEN '2025-09-01' AND '2025-09-30'
ORDER BY order_date;
order_id | order_date
----------+------------
105 | 2025-09-01
106 | 2025-09-07
107 | 2025-09-15
108 | 2025-09-22
Be careful with BETWEEN on columns that also store a time: '2025-09-30' means midnight at the start of that day, so later times on September 30 are missed. SQL Date Functions explains the safer >= start AND < next day pattern.
Matching Text Patterns with LIKE
LIKE matches text with two wildcards:
%matches any number of characters, including none._matches exactly one character.
SELECT name FROM products WHERE name LIKE 'Mo%'; -- starts with "Mo"
name
---------
Monitor
Mouse
SELECT name FROM products WHERE name LIKE '_esk'; -- one character, then "esk"
name
------
Desk
Common patterns:
| Pattern | Matches |
|---|---|
'C%' | Starts with C |
'%son' | Ends with “son” |
'%chair%' | Contains “chair” anywhere |
'_____' | Exactly five characters |
Case sensitivity differs. In PostgreSQL, LIKE is case-sensitive, so 'mo%' finds nothing; use ILIKE to ignore case. In MySQL, SQL Server, and SQLite, LIKE ignores case by default, so 'mo%' still finds Monitor and Mouse. SQL String Functions covers this and more text tools.
Finding Missing Values: IS NULL
NULL means “no value”: not zero, and not an empty string. Daniel has no email on file:
SELECT first_name, last_name, email
FROM customers
WHERE email IS NULL;
first_name | last_name | email
------------+-----------+-------
Daniel | Brooks |
Here’s the trap: = NULL doesn’t work.
SELECT first_name, last_name FROM customers WHERE email = NULL; -- returns no rows
NULL means “unknown,” so comparing anything to it with = gives “unknown,” not true, and the row is left out. Always use IS NULL and IS NOT NULL.
Sorting Results with ORDER BY
Without ORDER BY, a database can return rows in any order, and the order can change between runs. If order matters, sort explicitly.
SELECT name, price
FROM products
ORDER BY price DESC;
name | price
--------------+--------
Laptop | 899.00
Desk | 320.00
Monitor | 249.00
Office Chair | 180.00
Keyboard | 45.00
Mouse | 25.00
ASC (ascending, the default) sorts from smallest to largest or A to Z; DESC reverses it.
Sort by several columns to break ties. Customers by state, then last name:
SELECT state, last_name, first_name
FROM customers
ORDER BY state, last_name;
state | last_name | first_name
-------+-----------+------------
AZ | Brooks | Daniel
AZ | Nguyen | Sofia
CA | Carter | James
CA | Haddad | Omar
CA | Lopez | Maria
CA | Martinez | Ana
NV | Chen | Laura
NV | Patel | Kevin
Where Do NULLs Go?
Laura’s city is NULL. Sorting by city puts her in a different place depending on the database:
| Database | NULLs in ascending order |
|---|---|
| PostgreSQL | Last |
| MySQL, SQL Server, SQLite | First |
PostgreSQL (and SQLite 3.30 and later) let you choose with ORDER BY city NULLS FIRST or NULLS LAST.
Limiting the Number of Rows
To get only the first few rows, such as the three most expensive products, add a limit after sorting:
SELECT name, price
FROM products
ORDER BY price DESC
LIMIT 3;
name | price
---------+--------
Laptop | 899.00
Desk | 320.00
Monitor | 249.00
The syntax depends on the database:
| Database | Top 3 rows | Skip 3, then take 3 (page 2) |
|---|---|---|
| PostgreSQL, MySQL, SQLite | LIMIT 3 | LIMIT 3 OFFSET 3 |
| SQL Server | SELECT TOP 3 ... | OFFSET 3 ROWS FETCH NEXT 3 ROWS ONLY |
| PostgreSQL, Oracle (standard SQL) | FETCH FIRST 3 ROWS ONLY | OFFSET 3 ROWS FETCH NEXT 3 ROWS ONLY |
SQL Server doesn’t accept LIMIT at all; you’ll get Incorrect syntax near 'LIMIT'.
Always combine a limit with ORDER BY. “The first 3 rows” means nothing unless you’ve said what order they’re in.
Removing Duplicates with DISTINCT
DISTINCT returns each unique value (or combination of values) once:
SELECT DISTINCT state
FROM customers
ORDER BY state;
state
-------
AZ
CA
NV
With several columns, it removes duplicate combinations:
SELECT DISTINCT city, state
FROM customers
ORDER BY state, city;
city | state
-----------+-------
Phoenix | AZ
Yuma | AZ
El Centro | CA
San Diego | CA
Las Vegas | NV
| NV
Eight customers live in six distinct city-and-state combinations (one of them with a missing city). To count distinct values instead of listing them, use COUNT(DISTINCT state), covered in SQL Aggregate Functions.
Putting It All Together
A full SELECT can use every clause from this guide. Their written order is fixed:
SELECT DISTINCT columns
FROM table
WHERE conditions
ORDER BY columns
LIMIT n;
The database runs them in a different order: FROM, then WHERE, then SELECT, then DISTINCT, then ORDER BY, then LIMIT. That explains a common surprise: you can’t use a column alias from SELECT inside WHERE, because WHERE runs first. (You can use it in ORDER BY.)
Here’s a practical example: the three most recent orders that haven’t shipped yet.
SELECT order_id, customer_id, order_date, status
FROM orders
WHERE status <> 'shipped'
ORDER BY order_date DESC
LIMIT 3;
Common Mistakes to Avoid
- Using double quotes for text. Write
WHERE state = 'CA'. In PostgreSQL and SQLite,"CA"means a column named CA. - Mixing
ANDandORwithout parentheses. - Writing
= NULLinstead ofIS NULL. - Expecting a fixed order without
ORDER BY. - Using
LIMITin SQL Server, which needsTOPorOFFSET ... FETCH. - Assuming
LIKEignores case everywhere. It doesn’t in PostgreSQL. - Using
SELECT *in saved queries and applications, where named columns are clearer and safer.
Practice Exercises
Try these on the sample database. Each needs only what’s in this guide.
- List all products in the Accessories category, cheapest first.
- Find customers whose last name starts with “M”.
- Show orders with a quantity of 3 or more that were placed in August or September 2025.
- List the distinct order statuses.
- Find the two cheapest products that cost more than $40.
Answers are at the end of the FAQ.
Frequently Asked Questions
What does SELECT * mean?
It returns every column in the table. It’s fine for exploring, but naming columns is better in saved queries, because the result stays the same even if the table changes.
What’s the difference between WHERE and HAVING?
WHERE filters individual rows before any grouping. HAVING filters groups after aggregation, as in HAVING COUNT(*) > 2. See SQL Aggregate Functions.
Is BETWEEN inclusive?
Yes. BETWEEN 45 AND 249 includes 45 and 249.
Why doesn’t WHERE column = NULL work?
Because NULL means “unknown,” and comparing anything to an unknown value is unknown, not true. Use IS NULL or IS NOT NULL.
How do I get the top 10 rows in SQL?
Sort with ORDER BY, then use LIMIT 10 in PostgreSQL, MySQL, and SQLite, or SELECT TOP 10 in SQL Server.
Practice exercise answers
-- 1
SELECT name, price FROM products WHERE category = 'Accessories' ORDER BY price;
-- 2
SELECT first_name, last_name FROM customers WHERE last_name LIKE 'M%';
-- 3
SELECT order_id, quantity, order_date FROM orders
WHERE quantity >= 3 AND order_date BETWEEN '2025-08-01' AND '2025-09-30';
-- 4
SELECT DISTINCT status FROM orders ORDER BY status;
-- 5
SELECT name, price FROM products WHERE price > 40 ORDER BY price LIMIT 2;
Conclusion
SELECT, WHERE, ORDER BY, LIMIT, and DISTINCT are the core of almost every SQL query. Choose the columns you need, filter with comparison operators, AND, OR, IN, BETWEEN, LIKE, and IS NULL, sort explicitly, and limit the results when you only need the top few.
Next in the series, you’ll move from individual rows to summaries: counting, adding, and averaging with SQL Aggregate Functions.
