SQL queries read data from tables. The order of clauses matters: SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY. Examples use a table called customers.
Choosing and filtering
- SELECT name, email FROM customers; picks columns
- SELECT * FROM customers; picks every column
- WHERE city = 'Dhaka' filters rows
- AND, OR and NOT combine conditions
- LIKE 'A%' matches text starting with A
Sorting and limiting
- ORDER BY name ASC sorts A to Z
- ORDER BY created DESC sorts newest first
- LIMIT 10 returns the first ten rows
- DISTINCT removes duplicate values
Grouping and joining
- COUNT(*) counts rows
- GROUP BY city groups rows before aggregating
- HAVING COUNT(*) > 5 filters groups
- JOIN orders ON customers.id = orders.customer_id combines tables