MySQL Basics: Writing Your First SELECT Statements

Every MySQL journey starts with SELECT, and while the basic syntax is simple, a few clauses used together cover the vast majority of real-world queries.

SELECT name, price, category
FROM products
WHERE category = 'electronics' AND price < 100
ORDER BY price DESC
LIMIT 10;

-- Counting and grouping
SELECT category, COUNT(*) AS total, AVG(price) AS avg_price
FROM products
GROUP BY category
HAVING COUNT(*) > 5;

Note the difference between WHERE and HAVING: WHERE filters rows before grouping, HAVING filters groups after aggregation — a distinction that trips up a lot of SQL beginners since they look superficially similar.