SELECT is the SQL statement you will write more than any other. It reads data without changing it, which makes it the safest place to build confidence. Good sql select query examples all share the same skeleton: choose columns, name the table, filter rows, sort what remains, and limit the output when you only need a sample. Once that order feels automatic, you can answer most everyday reporting questions yourself.
The examples below use two simple tables you can picture easily. The customers table has id, first_name, last_name, email, city, and created_at. The orders table has id, customer_id, product_name, quantity, total_price, status, and order_date. Column and table names vary in real projects, but the patterns transfer directly. When you are ready to combine tables like these, continue with our guide where sql joins explained walks through matching rows across tables.
Selecting Columns and Reading a Whole Table
Start with the simplest form. Name the columns you want, then the table after FROM. Listing columns explicitly is better than using a star in shared code because the result stays stable when someone adds a new column to the table later, and the intent is clear to the next reader.
SELECT id, first_name, last_name, email FROM customers; SELECT * FROM customers;
The first query returns four named columns for every customer. The second query uses * to return all columns, which is fine for a quick look in a console but risky in application code. End each statement with a semicolon so tools know where it stops, especially when you run several statements together. If a column name contains spaces or matches a reserved word in your database, follow your system rules for quoting identifiers, but most well-named schemas never need that.
Aliases rename a column in the result only. They help when you compute a value or when a long column name would make reports hard to read. Aliases do not change the stored data.
SELECT first_name AS given_name,
total_price * quantity AS line_total
FROM orders;
Here AS given_name labels the first column in the output, and the second column is a calculation: total_price multiplied by quantity, labelled line_total. You can use that label in ORDER BY in most databases, which keeps sorting code readable. Keep aliases short, consistent, and free of spaces unless you quote them.
Filtering Rows With WHERE
WHERE keeps only rows that meet a condition. It is evaluated before sorting and limiting, so it decides which rows enter the rest of the pipeline. Comparison operators include equals, not equals, greater than, less than, and their or-equal forms. Text values go in single quotes in standard SQL, while numbers do not.
SELECT id, product_name, total_price FROM orders WHERE status = 'paid'; SELECT id, first_name, city FROM customers WHERE city <> 'London' AND created_at >= '2026-01-01';
The first query returns only orders whose status is exactly paid. The second query combines two conditions with AND, so both must be true: the city is not London, written with the standard not-equals operator, and the account was created on or after 1 January 2026. Use OR when either condition may be true, and wrap mixed AND and OR logic in parentheses so your intent is unambiguous. Databases evaluate AND before OR by default, which surprises beginners when parentheses are missing.
Three helpers cover most remaining filters. BETWEEN checks a range inclusively, IN checks membership in a list, and LIKE matches text patterns with wildcards. The percent sign matches any run of characters and the underscore matches exactly one character.
SELECT id, product_name, total_price
FROM orders
WHERE total_price BETWEEN 20 AND 100;
SELECT id, first_name, city
FROM customers
WHERE city IN ('Paris', 'Berlin', 'Madrid');
SELECT id, email
FROM customers
WHERE email LIKE '%@example.com';
The BETWEEN example keeps orders priced from 20 to 100 including both ends, which is shorter and clearer than writing two comparisons joined by AND. The IN example replaces a chain of OR checks with one tidy list. The LIKE example finds example.com email addresses; the leading percent means anything may come before the domain. LIKE can be slow on very large tables when the pattern starts with a wildcard, because indexes help less in that shape, so prefer exact or prefix searches for performance-critical lookups. For summaries built on filtered rows, see how sql group by having turns the same data into counts and totals.
Handling NULL Values Correctly
NULL means missing or unknown, not zero and not an empty string. Comparisons with NULL do not return true or false; they return unknown, so rows quietly disappear if you test NULL with equals. SQL provides IS NULL and IS NOT NULL for this job, and they are the only reliable way to find gaps.
SELECT id, first_name, email FROM customers WHERE email IS NULL; SELECT id, product_name FROM orders WHERE shipped_at IS NOT NULL;
The first query lists customers with no email on file, a common data-cleaning task. The second lists orders that have a shipping timestamp, meaning they have actually shipped. Remember this when a count looks wrong: a WHERE total_price > 0 filter excludes rows where total_price is NULL without telling you. When you display results, the COALESCE function can substitute a fallback, for example COALESCE(email, ‘no email’), so reports show readable text instead of blanks.
Removing Duplicates With DISTINCT
DISTINCT removes duplicate rows from the result after the selected columns are considered. It answers questions like which cities appear at least once, or which statuses are in use, without repeating the same value hundreds of times.
SELECT DISTINCT city FROM customers ORDER BY city; SELECT DISTINCT status, city FROM customers ORDER BY status, city;
The first query returns each city once, sorted alphabetically. The second returns each unique pair of status and city, because DISTINCT applies to the whole selected row, not to a single column. That distinction matters: adding a second column can bring back rows you thought were duplicates. DISTINCT can cost performance on huge tables since the database must sort or hash rows to spot repeats, so use it when you need unique values, not as a blanket fix for a join that produced extra rows.
Sorting Results With ORDER BY
Without ORDER BY, a database owes you no particular row order, even if results often look sorted in practice. Add ORDER BY whenever order matters for a report, a leaderboard, or a newest-first list. ASC is ascending and the default; DESC is descending. You can sort by several columns, applied left to right.
SELECT id, product_name, total_price FROM orders ORDER BY total_price DESC; SELECT id, last_name, first_name FROM customers ORDER BY last_name ASC, first_name ASC;
The first query puts the most expensive orders at the top, a typical top-sales check. The second sorts customers by family name, then by first name within the same family name, which is how address books are usually ordered. You may also sort by column position, such as ORDER BY 2, but naming the column is clearer and survives edits to the SELECT list. Sorting happens after filtering, so a large unsorted table with no matching index may take time; that is normal behaviour to note, not a reason to skip sorting when the reader needs it.
Limiting Results With LIMIT and OFFSET
LIMIT caps how many rows come back. OFFSET skips a number of rows first. Together they power pagination and top-N reports. Syntax differs slightly between databases: MySQL, PostgreSQL, and SQLite use LIMIT and OFFSET as shown here, SQL Server uses TOP and OFFSET with FETCH, and Oracle uses FETCH FIRST. Learn the variant for your system and keep the idea the same.
SELECT id, product_name, total_price FROM orders ORDER BY total_price DESC LIMIT 10; SELECT id, first_name, last_name FROM customers ORDER BY id LIMIT 20 OFFSET 40;
The first query returns the ten highest-priced orders, and the ORDER BY is essential: top ten has no meaning until you define the ranking. The second query skips 40 customers and returns the next 20, which is page three when a page holds 20 rows and pages start at offset zero. Always pair pagination with a stable sort such as id, otherwise rows can repeat or vanish between pages when new data arrives. For very deep pages, keyset pagination using a last-seen id performs better, but LIMIT with OFFSET is the right starting point. When you need to change the rows you found, our notes on sql insert update delete show the safe update patterns.
Combining Conditions and Counting What You Found
Real questions rarely need only one clause. A typical support query filters by status and date, sorts newest first, and limits the output to a reviewable batch. Build such queries one clause at a time, running after each step to confirm the row count moves in the direction you expect.
SELECT id, customer_id, product_name, order_date
FROM orders
WHERE status IN ('pending', 'processing')
AND order_date >= '2026-11-01'
ORDER BY order_date ASC
LIMIT 25;
SELECT COUNT(*) AS paid_orders
FROM orders
WHERE status = 'paid';
The first query lists the 25 oldest open orders placed since 1 November 2026, exactly the queue a fulfilment team would work through. The IN list covers both open statuses, the date condition removes older history, and ascending date order surfaces the longest-waiting orders first. The second query introduces COUNT(*), which counts rows matching the filter and returns a single number labelled paid_orders. Counts are the quickest sanity check before exporting data: if the count surprises you, fix the filter before anyone uses the full result. Similar counting habits appear throughout mysql common queries for routine admin checks.
Common SELECT Mistakes to Avoid
A few errors account for most confusing results. The first is using double quotes for text in standard SQL; double quotes usually mean identifiers, so use single quotes for string values and let your database settings guide any exception. The second is forgetting that WHERE runs before grouping and sorting logic in more advanced queries, so a filter on a computed alias may fail; repeat the expression or restructure the query instead. The third is leaving out ORDER BY and then assuming the first row is the earliest or largest. The fourth is comparing dates to text in the wrong format; store dates in date columns and write literals as YYYY-MM-DD for clarity.
Also watch for accidental full-table reads on production data. Before running a broad query on a large table, run the same WHERE clause with COUNT(*) first, and add a small LIMIT while exploring. Read the column list back to yourself as a sentence: select these fields, from this table, where these facts are true, in this order, only this many. If the sentence matches the question someone asked, the SQL usually does too.
Conclusion
SELECT, FROM, WHERE, ORDER BY, and LIMIT will answer the majority of reading questions you meet. Filter precisely, treat NULL with IS NULL, sort deliberately, and check counts before trusting a result set. Practise each clause on a small table until the sentence-like structure feels natural, then move on to joins and grouping with the same careful, step-by-step approach.
Tagged: database basics order by sql examples sql queries sql select where clause



5 thoughts on “SQL SELECT Queries: Filter, Sort and Limit With Examples”