SELECT: Reading Data

Site Admin · 11 Sep 2026 · 2 views

The SELECT Statement

SELECT name, email FROM customers;
SELECT * FROM customers;
SELECT DISTINCT city FROM customers;

* means every column. DISTINCT removes duplicate rows from the result.

Filtering With WHERE

SELECT * FROM customers WHERE city = 'Mumbai';
SELECT * FROM customers WHERE city = 'Mumbai' AND id > 2;
SELECT * FROM customers WHERE city IN ('Mumbai', 'Pune');
SELECT * FROM customers WHERE name LIKE 'A%';
  • =, <>, >, <= compare values.
  • AND and OR combine conditions.
  • IN (...) tests membership in a list.
  • LIKE \u0027A%\u0027 matches names beginning with A.

Sorting With ORDER BY

SELECT name, city FROM customers ORDER BY name;
SELECT name, city FROM customers ORDER BY city DESC, name ASC;

The default sort order is ascending (ASC). Use DESC for descending.

Limiting Results

SELECT * FROM customers ORDER BY id DESC LIMIT 5;
SELECT * FROM customers LIMIT 10 OFFSET 20;   -- page 3 of 10

Aliases

SELECT name AS customer_name, email AS contact FROM customers;

Key Points

  • SELECT retrieves rows; WHERE filters them.
  • ORDER BY sorts the result set.
  • LIMIT and OFFSET implement pagination.
  • Aliases with AS make output (and later JOINs) readable.
Share this post:

Comments (0)

Please login or register to comment.