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.

May 10, 2025 - 11:41
Updated: 
103.3k

Data Analytics & Data Science in AI & Data · Course information

SQL Basics for MySQL Beginners: SELECT, JOIN and GROUP BY Explained

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?

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 typeRows returned with our data
INNER JOIN3 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;
nameorder_counttotal_spent
Ravi1900.00
Asha2750.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

SQL is the query language for relational databases, while MySQL is a specific database server that understands SQL plus its own extensions.

INNER JOIN returns only rows that match in both tables, while LEFT JOIN returns all rows from the left table with NULLs where no match exists.

WHERE filters individual rows before grouping happens, while HAVING filters the grouped results after aggregate functions have been calculated.

ONLY_FULL_GROUP_BY is enabled by default, so every selected column must be grouped, aggregated or functionally dependent on the grouped columns.

NULL means unknown and is never equal to anything, including NULL, so you must use IS NULL or IS NOT NULL to test it.

COUNT(*) counts all rows in the group, while COUNT(column) counts only rows where that column is not NULL.

No. WHERE is evaluated before the SELECT list, so aliases are not available there, although MySQL allows them in HAVING and ORDER BY.

Create a small schema, insert a few rows you understand, predict each query result on paper and then compare it with what MySQL returns.

What's Your Reaction?

Like Like 0
Dislike Dislike 0
Love Love 0
Funny Funny 0
Wow Wow 0
Sad Sad 0
Angry Angry 0
Vaishnavi

Vaishnavi is a skilled tech professional at the Ethical Hacking Training Institute in Pune, responsible for managing and optimizing the technical infrastructure that supports advanced cybersecurity education. With deep expertise in network security, backend operations, and system performance, she ensures that practical labs, online modules, and assessments run smoothly and securely. Her behind-the-scenes contributions play a vital role in delivering a seamless and secure learning experience for aspiring ethical hackers.