SELECT, WHERE, ORDER BY and LIMIT

Beginner  Nov 3, 2026

Read exactly the rows you need, in the order you need.

SELECT, WHERE, ORDER BY and LIMIT
The steps of this tutorial at a glance.
The steps of this tutorial at a glance.

Sample table

CREATE TABLE products (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(80) NOT NULL,
  category VARCHAR(30) NOT NULL,
  price DECIMAL(8,2) NOT NULL
);
INSERT INTO products (name, category, price) VALUES
 ('Pen', 'office', 1.50), ('Notebook', 'office', 4.00),
 ('Mouse', 'tech', 15.00), ('Keyboard', 'tech', 30.00);
The table described by the SQL above.
The table described by the SQL above.
How the code in this section fits into the page.
How the code in this section fits into the page.

Choose columns and rows

SELECT name, price FROM products WHERE category = 'tech';
SELECT * FROM products WHERE price BETWEEN 2 AND 20;
SELECT name FROM products WHERE name LIKE 'K%';

Sort and limit

SELECT name, price FROM products ORDER BY price DESC LIMIT 2;

Combine conditions

SELECT name FROM products
WHERE category = 'tech' AND price < 20 OR category = 'office';

Use brackets when you mix AND and OR, because AND is evaluated first.

Tips

  • Name the columns you need instead of SELECT *.
  • Without ORDER BY, the order of rows is not guaranteed.

More tutorials