SQL Basics for MySQL Beginners: SELECT, JOIN and GROUP BY Explained
As a MySQL beginner you need three building blocks. SELECT reads rows from a table, with WHERE to filter, ORDER BY to sort and LIMIT to cap the number of rows. JOIN combines rows from related tables using matching key columns; INNER JOIN keeps only matches and LEFT JOIN keeps every row from the left table.
Topic: Data Analytics & Data Science in AI & Data · Course information
Quick answer: As a MySQL beginner you need three building blocks. SELECT reads rows from a table, with WHERE to filter, ORDER BY to sort and LIMIT to cap the number of rows. JOIN combines rows from related tables using matching key columns; INNER JOIN keeps only matches and LEFT JOIN keeps every row from the left table. GROUP BY collapses rows into groups so aggregate functions such as COUNT, SUM and AVG can summarise them, and HAVING filters those groups. Practise all three on a small two-table schema, check the results by hand and you will understand most everyday SQL queries.
Key takeaways
- SELECT, WHERE, ORDER BY and LIMIT cover most single-table questions.
- INNER JOIN returns matches only; LEFT JOIN also keeps unmatched left rows.
- WHERE filters rows before grouping; HAVING filters groups after.
- Every non-aggregated column in SELECT should appear in GROUP BY.
- NULL is not equal to anything; test it with IS NULL.
Table of Contents
- How Do You Create Practice Tables?
- How Does SELECT Work?
- How Do JOINs Combine Tables?
- How Do GROUP BY and HAVING Work?
- Why Does NULL Cause Surprises?
- What Should You Learn After the Basics?
- References
How Do You Create Practice Tables?
Run these statements with any MySQL client. They use CREATE TABLE and INSERT:
CREATE DATABASE practice;
USE practice;
CREATE TABLE customers (
customer_id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
city VARCHAR(50)
);
CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_id INT NOT NULL,
amount DECIMAL(10,2) NOT NULL,
FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);
INSERT INTO customers VALUES
(1, 'Asha', 'Pune'), (2, 'Ravi', 'Mumbai'), (3, 'Meera', NULL);
INSERT INTO orders VALUES
(101, 1, 500.00), (102, 1, 250.00), (103, 2, 900.00);
How Does SELECT Work?
The SELECT statement picks columns and rows:
SELECT order_id, amount
FROM orders
WHERE amount >= 300
ORDER BY amount DESC
LIMIT 10;
This returns orders 103 (900.00) and 101 (500.00). MySQL logically processes FROM, then WHERE, then SELECT, then ORDER BY and LIMIT, which is why a column alias defined in SELECT cannot be used in WHERE. Name the columns you need instead of using SELECT *, so results stay clear and queries do less work as tables grow.
How Do JOINs Combine Tables?
The JOIN clause matches rows on a condition:
SELECT c.name, o.order_id, o.amount
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.customer_id
ORDER BY c.name;
| Join type | Rows returned with our data |
|---|---|
| INNER JOIN | 3 rows: Asha twice, Ravi once |
| LEFT JOIN (customers on the left) | 4 rows: also Meera, with NULL order columns |
To find customers with no orders, add WHERE o.order_id IS NULL to the LEFT JOIN.
How Do GROUP BY and HAVING Work?
GROUP BY works with aggregate functions:
SELECT c.name,
COUNT(o.order_id) AS order_count,
COALESCE(SUM(o.amount), 0) AS total_spent
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.customer_id
GROUP BY c.customer_id, c.name
HAVING total_spent > 0
ORDER BY total_spent DESC;
| name | order_count | total_spent |
|---|---|---|
| Ravi | 1 | 900.00 |
| Asha | 2 | 750.00 |
COUNT(o.order_id) counts only non-NULL values, so Meera would get 0, and HAVING then removes her. MySQL enables ONLY_FULL_GROUP_BY by default, which rejects queries that select columns not grouped or aggregated; see MySQL handling of GROUP BY.
Why Does NULL Cause Surprises?
NULL means unknown, so city = NULL is never true. Use city IS NULL or city IS NOT NULL instead, as the manual explains in working with NULL values. Aggregates such as SUM and AVG ignore NULLs, and COUNT(*) counts rows while COUNT(column) skips NULLs.
What Should You Learn After the Basics?
Next, learn subqueries, common table expressions and window functions, covered in MySQL for data analysts. For a full staged plan, follow our self-study MySQL path. If you want a trainer to review your queries, contact WebAsha about beginner MySQL training.
References
Frequently Asked Questions
What's Your Reaction?
Like
0
Dislike
0
Love
0
Funny
0
Wow
0
Sad
0
Angry
0