SELECT Fundamentals
The SELECT statement is the most used SQL command — it retrieves data from databases. Every query you write starts with SELECT. Master filtering (WHERE), sorting (ORDER BY), and pattern matching (LIKE) to extract exactly the data you need.
40 min•By Priygop Team•Last updated: Feb 2026
SELECT Syntax & Clauses
- SELECT columns FROM table — Basic query: SELECT name, email FROM users
- SELECT * — Retrieve all columns. Avoid in production code — explicitly list needed columns
- WHERE — Filter rows: WHERE age > 18, WHERE status = 'active', WHERE price BETWEEN 10 AND 50
- AND / OR — Combine conditions: WHERE age > 18 AND status = 'active'
- ORDER BY — Sort results: ORDER BY name ASC (ascending, default), ORDER BY price DESC (descending)
- LIMIT / OFFSET — Pagination: LIMIT 10 OFFSET 20 returns rows 21-30. MySQL/PostgreSQL syntax
- DISTINCT — Remove duplicates: SELECT DISTINCT category FROM products
- AS — Column aliases: SELECT first_name AS name, COUNT(*) AS total
SELECT Query Examples
Example
-- Basic SELECT with filtering
SELECT first_name, last_name, email
FROM users
WHERE status = 'active' AND age >= 18
ORDER BY last_name ASC;
-- Pattern matching with LIKE
SELECT * FROM products
WHERE name LIKE '%phone%' -- Contains 'phone'
AND name LIKE 'Apple%' -- Starts with 'Apple'
AND sku NOT LIKE '%_old'; -- Doesn't end with '_old'
-- % = any characters, _ = single character
-- IN — match against a list
SELECT * FROM orders
WHERE status IN ('pending', 'processing', 'shipped');
-- BETWEEN — range queries
SELECT * FROM products
WHERE price BETWEEN 10.00 AND 99.99
AND created_at BETWEEN '2025-01-01' AND '2025-12-31';
-- NULL handling
SELECT * FROM users
WHERE phone IS NULL; -- Has no phone
SELECT * FROM users
WHERE phone IS NOT NULL; -- Has a phone
-- IMPORTANT: NULL = NULL is FALSE! Always use IS NULL
-- Pagination (MySQL / PostgreSQL)
SELECT id, name, price
FROM products
ORDER BY price ASC
LIMIT 10 OFFSET 0; -- Page 1 (first 10)
-- LIMIT 10 OFFSET 10; -- Page 2 (next 10)
-- Combining everything
SELECT
p.name AS product_name,
p.price,
c.name AS category
FROM products p
WHERE p.price > 25.00
AND p.stock > 0
AND p.category_id IN (1, 2, 5)
ORDER BY p.price DESC
LIMIT 20;