New: 6 free SQL practice datasets with 300+ questions — try the SQL Compiler →
SQL

SQL SELECT Statement: Syntax and Examples

The SQL SELECT statement explained with a sample table and real outputs: choosing columns, WHERE, ORDER BY, LIMIT, DISTINCT, aliases and common mistakes.

Upskly AI Team September 24, 2026 7 min read
SQL SELECT Statement: Syntax and Examples

The SQL SELECT statement reads data from a table. It is the first command every beginner learns and the start of almost every query you will ever write. SELECT says which columns you want, FROM says which table they live in, and the other clauses filter, sort and limit the result.

This guide walks through each part with one small products table and the real output of every query.

In this guide

SELECT syntax

SELECT column1, column2
FROM table_name
WHERE condition
ORDER BY column1
LIMIT number;

Only SELECT and FROM are required. Everything else is optional, and the clauses must appear in this order. Here is the table we will use:

Table: products
product_idnamecategorypricestock
1NotebookStationery60120
2PenStationery10500
3BackpackBags90040
4Water BottleBags25075
5Desk LampElectronics70030
6HeadphonesElectronics150025
7PencilStationery5800
8Laptop SleeveBags45060
9Art SetStationery35020
Want to run this yourself? Copy the setup SQL
CREATE TABLE products (product_id INT, name VARCHAR(40), category VARCHAR(20), price INT, stock INT);
INSERT INTO products VALUES
  (1, 'Notebook', 'Stationery', 60, 120),
  (2, 'Pen', 'Stationery', 10, 500),
  (3, 'Backpack', 'Bags', 900, 40),
  (4, 'Water Bottle', 'Bags', 250, 75),
  (5, 'Desk Lamp', 'Electronics', 700, 30),
  (6, 'Headphones', 'Electronics', 1500, 25),
  (7, 'Pencil', 'Stationery', 5, 800),
  (8, 'Laptop Sleeve', 'Bags', 450, 60),
  (9, 'Art Set', 'Stationery', 350, 20);

Works in MySQL, PostgreSQL and SQLite. Paste it into any of them, then run the queries from this guide.

Choosing columns

List the columns you want, separated by commas:

SELECT name, price
FROM products;
Output: name and price of every product
nameprice
Notebook60
Pen10
Backpack900
Water Bottle250
Desk Lamp700
Headphones1500
Pencil5
Laptop Sleeve450
Art Set350

To get every column, use SELECT *. It is handy for a quick look, but in real work it is better to name the columns you need. The query stays clear, and it stays fast on big tables because the database returns less data.

Filtering rows with WHERE

Most of the time you want a slice of the rows, not all of them. WHERE keeps only the rows where the condition is true:

SELECT name, price
FROM products
WHERE category = 'Bags';
Output: products in the Bags category
nameprice
Backpack900
Water Bottle250
Laptop Sleeve450

Text values go in single quotes ('Bags'), numbers do not. Combine conditions with AND and OR:

SELECT name, price
FROM products
WHERE category = 'Electronics'
  AND price < 1000;
Output: electronics under 1000
nameprice
Desk Lamp700

Three more filters you will use constantly:

  • IN ('Bags', 'Electronics') matches any value in a list.
  • BETWEEN 200 AND 800 matches a range, including both ends.
  • LIKE 'P%' matches a pattern. % stands for “any characters”.
SELECT name, price
FROM products
WHERE price BETWEEN 200 AND 800
ORDER BY price;
Output: price between 200 and 800
nameprice
Water Bottle250
Art Set350
Laptop Sleeve450
Desk Lamp700
SELECT name
FROM products
WHERE name LIKE 'P%';
Output: names starting with P
name
Pen
Pencil

Sorting with ORDER BY

ORDER BY sorts the result. Add DESC for high-to-low; the default is ASC, low-to-high. You can sort by several columns, and later ones break ties in earlier ones:

SELECT name, category, price
FROM products
ORDER BY category, price DESC;
Output: sorted by category, then price high to low
namecategoryprice
BackpackBags900
Laptop SleeveBags450
Water BottleBags250
HeadphonesElectronics1500
Desk LampElectronics700
Art SetStationery350
NotebookStationery60
PenStationery10
PencilStationery5

Sorting happens after filtering, so it only sorts the rows that passed your WHERE.

Limiting rows with LIMIT

Useful for “show me the top 3” questions, and important once a table has millions of rows, since you rarely want them all back at once:

SELECT name, price
FROM products
ORDER BY price DESC
LIMIT 3;
Output: the 3 most expensive products
nameprice
Headphones1500
Backpack900
Desk Lamp700

Always pair LIMIT with ORDER BY. Without a sort, “the first 3 rows” is not a defined set, and the database may return any 3.

Limiting rows in different databases
DatabaseHow to limit rows
MySQL, PostgreSQL, SQLiteLIMIT 3
SQL ServerSELECT TOP 3 …
Oracle (12c and later)FETCH FIRST 3 ROWS ONLY

Removing duplicates with DISTINCT

Nine products, but how many categories? DISTINCT returns each value once:

SELECT DISTINCT category
FROM products
ORDER BY category;
Output: the unique categories
category
Bags
Electronics
Stationery

Aliases and calculated columns

A column does not have to come straight from the table. You can calculate one, and rename it with AS so the result is easy to read:

SELECT name, price * stock AS stock_value
FROM products
ORDER BY stock_value DESC
LIMIT 3;
Output: stock value = price x stock, top 3
namestock_value
Headphones37500
Backpack36000
Laptop Sleeve27000

price * stock AS stock_value works out the value of the stock on hand and labels the column. Note that the alias can be used in ORDER BY.

The order SQL runs your query in

SQL reads top to bottom, but it does not run top to bottom. Roughly, the database works out FROM first, then WHERE, then SELECT, then ORDER BY, then LIMIT. That explains two things beginners find odd:

  • An alias defined in SELECT can be used in ORDER BY (which runs later) but, in standard SQL, not in WHERE (which runs earlier). MySQL, PostgreSQL and SQL Server reject it. SQLite is more lenient and accepts it, so if you test this in a browser SQLite editor it may appear to work. Do not rely on it.
  • ORDER BY sorts only the rows that survived WHERE.

For the full picture, including GROUP BY and HAVING, see SQL WHERE vs HAVING.

Common beginner mistakes

1. Mixing AND and OR without parentheses

AND is evaluated before OR. Say you want cheap items (under 100) that are Bags or Stationery. This looks right but is not:

SELECT name, category, price
FROM products
WHERE category = 'Bags' OR category = 'Stationery'
  AND price < 100;
Output: 6 rows, expensive bags included
namecategoryprice
NotebookStationery60
PenStationery10
BackpackBags900
Water BottleBags250
PencilStationery5
Laptop SleeveBags450

It reads as “all Bags, or Stationery under 100”. The expensive bags slipped in. Add parentheses to say what you mean:

SELECT name, category, price
FROM products
WHERE (category = 'Bags' OR category = 'Stationery')
  AND price < 100;
Output: 3 rows, as intended
namecategoryprice
NotebookStationery60
PenStationery10
PencilStationery5

2. Using LIMIT without ORDER BY

The rows you get back are arbitrary, and can change between runs or databases.

3. Comparing to NULL with =

WHERE phone = NULL never matches anything. Use IS NULL. See NULL in SQL.

4. Forgetting quotes around text

WHERE category = Bags makes the database look for a column called Bags, and it fails.

Next, learn to combine tables with SQL JOINs, then follow the full SQL learning roadmap.

Frequently asked questions

What does SELECT * mean in SQL?

It means “select all columns”. It is convenient for a quick look at a table, but it is better practice to list only the columns you need.

What is the difference between SELECT and SELECT DISTINCT?

SELECT returns every matching row, including duplicates. SELECT DISTINCT returns each unique combination of the selected columns once.

Can I use a column alias in the WHERE clause?

In standard SQL, and in MySQL, PostgreSQL and SQL Server, no. WHERE runs before SELECT, so the alias does not exist yet. Repeat the expression instead. SQLite is lenient and allows it, but the query then will not port to other databases.

Is SQL case sensitive?

Keywords such as SELECT and from are not case sensitive. Whether table names, column names and text comparisons are case sensitive depends on the database and its settings, so keep your naming consistent.

How do I select only the first 10 rows?

Use LIMIT 10 in MySQL, PostgreSQL and SQLite, TOP 10 in SQL Server, or FETCH FIRST 10 ROWS ONLY in Oracle. Add ORDER BY so the 10 rows are well defined.

Try these queries yourself

A free SQL editor in your browser. Run SELECT statements instantly, no install needed.

Open SQL Compiler →

Test what you have learned

Free, timed SQL quiz questions with an explanation for every answer.

Take the SQL Quiz →

Want a structured path instead?

Structured SQL courses – built for real data roles.

Browse SQL Courses →
Upskly AI Team
Learning made simple
Scroll to Top