Skip to content

SELECT Queries

Master the SELECT statement: choosing columns, limiting rows, sorting, and aliases.

What you'll learn

  • How to select specific columns instead of SELECT *
  • Using LIMIT to restrict the number of rows returned
  • Sorting results with ORDER BY (ASC and DESC)
  • Renaming columns in output with AS aliases
  • Removing duplicates with DISTINCT

Concept

The SELECT Statement

SELECT is the most-used SQL statement. Its basic form is SELECT columns FROM table. You can list specific columns (SELECT name, email FROM users) or use * to get all columns. In production code, prefer listing columns explicitly — SELECT * is fine for exploration but returns unnecessary data and breaks if the schema changes.

LIMIT

Large tables can have millions of rows. LIMIT restricts how many rows the query returns. This is essential for performance and for pagination — page 1 shows rows 1–20, page 2 shows 21–40, and so on. In PostgreSQL and MySQL the syntax is LIMIT n; in SQL Server it is TOP n; in Oracle it is FETCH FIRST n ROWS ONLY.

ORDER BY

ORDER BY sorts the result set. You can sort by one or more columns, ascending (ASC, the default) or descending (DESC). Sorting is applied after filtering but before LIMIT. You can sort by a column that is not in the SELECT list.

Aliases with AS

AS renames a column or table in the output. This makes results more readable and is required when selecting expressions. For example, SELECT price * 1.2 AS price_with_tax FROM products. The AS keyword is optional — price * 1.2 price_with_tax works too, but AS is clearer.

DISTINCT

DISTINCT removes duplicate rows from the result. SELECT DISTINCT category FROM products returns each category only once. Use it sparingly — it requires a sort internally and can be slow on large datasets.

Example

              -- Select specific columns, sorted by price descending
SELECT name, price, in_stock
FROM products
ORDER BY price DESC
LIMIT 10;

-- Use an alias for a computed column
SELECT name, price, price * 1.08 AS price_with_tax
FROM products
WHERE in_stock = TRUE
ORDER BY price_with_tax ASC
LIMIT 5;

-- Get unique categories
SELECT DISTINCT category FROM products ORDER BY category;
            

Three common patterns: top-N query with ORDER BY + LIMIT, a computed column with an alias, and DISTINCT to list unique values.

Try it

  • SQL Formatter

    Format multi-clause SELECT statements for readability.

  • JSON Formatter

    Query results often come back as JSON — format them here.

Common mistakes

The mistake

Using SELECT * in production code

The fix

List only the columns you need. SELECT * transfers more data than necessary and silently breaks if columns are added or removed.

The mistake

Forgetting that LIMIT without ORDER BY is non-deterministic

The fix

Without ORDER BY, the database may return rows in any order. Always pair LIMIT with ORDER BY for consistent pagination.

Practice SELECT Queries

20 exercises

Practice SELECT Queries

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors