Last updated: August 27, 2026.
SQL retrieves and changes data in relational databases. Begin with SELECT, filtering, and sorting, then move to joins, grouping, aggregates, and data modification.
Core query clauses
- SELECT rows and columns
- Filter rows with WHERE
- Combine conditions with AND and OR
- Filter ranges with BETWEEN
- Remove duplicate rows with DISTINCT
Combine and summarize data
A practical query
SELECT
c.customer_name,
COUNT(o.order_id) AS order_count,
SUM(o.total_amount) AS revenue
FROM customers AS c
JOIN orders AS o ON o.customer_id = c.customer_id
WHERE o.order_date >= '2026-01-01'
GROUP BY c.customer_id, c.customer_name
HAVING SUM(o.total_amount) >= 1000
ORDER BY revenue DESC;Change database data
SQL dialects differ in details, so check the documentation for your database engine. The PostgreSQL SELECT reference is a thorough standards-oriented reference.
Building a database application? See how PHPRunner turns database tables and queries into web applications.