Use tiny sample tables and 8–12 beginner MySQL queries, each with a solved answer and expected rows, so you can check results row by row. Without checked answers, beginners often memorize syntax but cannot tell whether a query is actually correct. This guide gives MySQL practice exercises for beginners with sample tables, completed SQL, expected results, and one-line explanations for each answer.

Why beginners should practice MySQL

MySQL is a popular open-source relational database, and practice is how beginners turn syntax into usable skill. A good exercise set starts small, moves in order, and shows the result shape for each query so the learner can spot mistakes quickly.

W3resource organizes practice from basic SELECT to later topics. Its sections include Basic SELECT statement with 19 exercises, Restricting and Sorting Data with 11 exercises, Aggregate Functions and Group by with 14 exercises, MySQL JOINS with 13 exercises, MySQL Subqueries with 22 exercises, MySQL String Functions with 17 exercises, and MySQL Date Time with 21 exercises. It also describes the material as for beginners to advanced users and says users can download the structure and data of the database used there.

This set follows that idea, but keeps the path narrow. It starts with SELECT, WHERE, and ORDER BY, then moves into JOINs, aggregates, subqueries, string work, and date handling.

What this exercise set covers

Each exercise uses the same tiny data set, so every answer can be checked against a fixed result. That keeps the focus on reading rows, matching keys, grouping correctly, and spotting NULLs.

How the difficulty increases

The first tasks ask for columns and filtered rows. Later tasks add sort order, joins, grouped totals, nested SELECTs, and functions on text and dates.

How to load the sample tables

Create the three tables below, load the seed rows, and run the exercises against them. The names and values are fixed so every result here stays repeatable.

  1. Verify the load. Run a simple row count for each table before starting the exercises. The expected counts are about 4 users, 4 orders, and 6 order items.

Insert the seed rows. Keep the values exactly as shown so the answer key stays valid.

users user_id name email city created_at
row 11Ana[email protected]Austin2024-01-02 09:15:00
row 22Ben[email protected]NULL2024-01-03 14:20:00
row 33Cara[email protected]Boston2024-01-05 08:00:00
row 44Dan[email protected]Denver2024-01-07 18:45:00
orders order_id user_id order_date status
row 110112024-02-01 10:00:00shipped
row 210212024-02-03 16:30:00pending
row 310322024-02-05 11:10:00cancelled
row 410442024-02-07 09:25:00shipped
order_items item_id order_id product_name quantity unit_price
row 11001101Pencil21.50
row 21002101Notebook14.00
row 31003102Eraser30.75
row 41004103Marker22.25
row 51005104Ruler13.50
row 61006104Compass25.00

Create the tables. Use one table for users, one for orders, and one for order items. The data types below are enough for beginner practice and support SELECT, WHERE, ORDER BY, GROUP BY, and JOIN work.

Table Column Type Notes
users user_id INT Primary key
users name VARCHAR(30) User name
users email VARCHAR(60) Email address
users city VARCHAR(30) NULL Allows NULL practice
users created_at DATETIME Signup time
orders order_id INT Primary key
orders user_id INT Matches users.user_id
orders order_date TIMESTAMP Order time
orders status VARCHAR(20) Shipment state
order_items item_id INT Primary key
order_items order_id INT Matches orders.order_id
order_items product_name VARCHAR(30) Item name
order_items quantity INT Units ordered
order_items unit_price DECIMAL(6,2) Price per unit

Seed data and repeatability

The data is small on purpose. There appear to be two orders with two items each: order 101 and order 104. That matters later when a join repeats order rows, because the repeat is correct, not a mistake.

How to verify the tables

  1. Check counts first. Use SELECT COUNT(*) on each table. If the numbers differ from 4, 4, and 6, reload the seed rows before practicing.
  2. Check key matches. Run a quick join between orders and users, then between orders and order_items. That confirms the foreign keys line up as expected.

Sample tables exercise pack

This pack moves from one-table queries to joins and then to functions. The sequence matters. A beginner who can read a filtered result set can handle joins and grouped answers with less guesswork.

The users table is the cleanest place to start. It has one NULL value in city, which is useful for WHERE practice.

The orders table introduces a one-to-many link back to users. It also gives a TIMESTAMP column for date practice.

The order_items table is where duplication shows up in joins. One order can have more than one item, so a joined query can return more rows than the orders table itself.

What should you practice first?

Start with SELECT, then add WHERE, then ORDER BY, and only then add LIMIT-style filtering.

Basic SELECT exercises

Exercise 1. Return each user’s name and email, ordered by name.

SELECT name, email
FROM users
ORDER BY name;

Expected output shape: 4 rows, 2 columns: name, email.

Answer explanation: This reads two columns from every row and sorts alphabetically by name.

Exercise 2. Return each user’s name and city where city is not NULL, ordered by city descending and then name ascending.

SELECT name, city
FROM users
WHERE city IS NOT NULL
ORDER BY city DESC, name ASC;

Expected output shape: 3 rows, 2 columns: name, city.

Answer explanation: The WHERE clause removes the NULL city, and the two-level sort controls the tie order.

WHERE and ORDER BY exercises

Exercise 3. Return the first two users by signup time.

SELECT user_id, name, created_at
FROM users
ORDER BY created_at ASC
LIMIT 2;

Expected output shape: 2 rows, 3 columns: user_id, name, created_at.

Answer explanation: ORDER BY sorts the timestamps, and LIMIT keeps only the first two rows.

Exercise 4. Return shipped orders placed on or after 2024-02-01.

SELECT order_id, order_date, status
FROM orders
WHERE status = 'shipped'
AND order_date >= '2024-02-01'
ORDER BY order_date;

Expected output shape: 2 rows, 3 columns: order_id, order_date, status.

Answer explanation: Both shipped rows meet the date filter, so the result contains orders 101 and 104.

LIMIT-style practice

Exercise 5. Return the first three order items when ordered by unit price ascending, then item_id ascending.

SELECT item_id, product_name, unit_price
FROM order_items
ORDER BY unit_price ASC, item_id ASC
LIMIT 3;

Expected output shape: 3 rows, 3 columns: item_id, product_name, unit_price.

Answer explanation: The sort brings the cheapest rows to the top, and LIMIT trims the list to three.

How do you practice joins and aggregates?

Joins connect tables by matching keys, and aggregates turn many rows into counts or totals. These are the two topics where beginners most often mix up row multiplication and grouping rules.

INNER JOIN practice

Exercise 6. Show each order with the user name who placed it.

SELECT o.order_id, u.name, o.status
FROM orders AS o
INNER JOIN users AS u
ON o.user_id = u.user_id
ORDER BY o.order_id;

Expected output shape: 4 rows, 3 columns: order_id, name, status.

Answer explanation: Each order matches one user, so this is a one-to-many lookup from users to orders.

Common mistake: Joining on the wrong key column returns no rows or the wrong user names.

Exercise 7. List every ordered item with its order id and line total.

SELECT oi.order_id, oi.product_name, oi.quantity, oi.unit_price,
oi.quantity * oi.unit_price AS line_total
FROM order_items AS oi
ORDER BY oi.order_id, oi.item_id;

Expected output shape: 6 rows, 5 columns: order_id, product_name, quantity, unit_price, line_total.

Answer explanation: This uses one table only, but it prepares the same order-item structure used in later joins.

COUNT, SUM, and AVG

Exercise 8. Count orders by status.

SELECT status, COUNT(*) AS order_count
FROM orders
GROUP BY status
ORDER BY status;

Expected output shape: 3 rows, 2 columns: status, order_count.

Answer explanation: GROUP BY collects rows with the same status, and COUNT(*) totals each group.

Common mistake: Leaving out GROUP BY while selecting status causes an error in MySQL’s default SQL mode.

Exercise 9. Show the total value of items in each order.

SELECT order_id,
SUM(quantity * unit_price) AS order_total
FROM order_items
GROUP BY order_id
ORDER BY order_id;

Expected output shape: 4 rows, 2 columns: order_id, order_total.

Answer explanation: SUM adds the line totals inside each order group, so orders 101 and 104 each roll up two item rows.

Common mistake: Grouping by product_name instead of order_id changes the question and splits one order into separate lines.

Exercise 10. Find the average unit price across all order items.

SELECT AVG(unit_price) AS average_unit_price
FROM order_items;

Expected output shape: 1 row, 1 column: average_unit_price.

Answer explanation: AVG works on all six rows at once because there is no GROUP BY clause.

Common mistake: Adding quantity to this query changes the average to a different measure; keep the expression aligned with the question.

Subqueries, strings, and dates

This section finishes the beginner path with nested queries and common scalar functions. The goal is not volume. It is confidence in reading one query block at a time.

Simple subqueries

Exercise 11. Return users who have placed at least two orders.

SELECT name
FROM users
WHERE user_id IN (
SELECT user_id
FROM orders
GROUP BY user_id
HAVING COUNT(*) >= 2
)
ORDER BY name;

Expected output shape: 1 row, 1 column: name.

Answer explanation: The inner query finds user IDs with two or more orders, and the outer query turns those IDs into names.

Common mistake: Putting COUNT(*) in WHERE instead of HAVING breaks the aggregate filter.

Exercise 12. Return users who have at least one shipped order.

SELECT name
FROM users AS u
WHERE EXISTS (
SELECT 1
FROM orders AS o
WHERE o.user_id = u.user_id
AND o.status = 'shipped'
)
ORDER BY name;

Expected output shape: 2 rows, 1 column: name.

Answer explanation: EXISTS checks whether the correlated subquery finds a shipped order for each user.

Common mistake: Using IN with a status filter on the wrong column can return the wrong set or no rows at all.

String function exercises

Picture a report that needs clean labels. The names and emails are already simple, but the functions still matter because they show how to transform text without changing the data itself.

SELECT name,
CONCAT(UPPER(TRIM(name)), ' | ', LOWER(TRIM(email))) AS label,
LENGTH(email) AS email_length
FROM users
ORDER BY user_id;

Expected output shape: 4 rows, 3 columns: name, label, email_length.

Answer explanation: TRIM removes extra spaces if they exist, UPPER and LOWER standardize case, CONCAT joins the text, and LENGTH counts characters.

Date and time exercises

Suppose a report only wants February orders, regardless of time of day. DATE() lets the query compare the calendar day while the underlying column stays a TIMESTAMP.

SELECT order_id,
DATE(order_date) AS order_day
FROM orders
WHERE order_date >= '2024-02-03'
ORDER BY order_date;

Expected output shape: 3 rows, 2 columns: order_id, order_day.

Answer explanation: The timestamp filter keeps later orders, and DATE() removes the time part from the display.

Common mistake: Comparing a DATETIME or TIMESTAMP column as text can hide time-based rows if the format is not consistent.

How do you check your answers?

Check each result in the same order every time: row count, column count, column names, then row values. If the shape is right but the values are wrong, the problem is usually a filter, join key, or sort order.

Expected row counts and output shape

The safest way to compare results is to match the output shape first. For this set, the exact shapes are 4, 3, 2, 2, 3, 4, 6, 3, 4, 1, 1, 2 rows, in exercise order.

Common mistake notes

  • NULL handling: use IS NULL or IS NOT NULL, not = NULL.
  • Join duplication: two orders have two items each, so item joins return repeated order IDs by design.
  • Grouping: every selected non-aggregate column must fit the GROUP BY rule.
  • Subqueries: choose HAVING for group filters, not WHERE.
  • Date comparisons: compare the full date or timestamp value consistently.

How to compare results

  1. Run one query only. Do not stack exercises in one session at first. A single result set is easier to compare against the answer key.
  2. Check the shape first. Compare the number of rows and columns to the expected output shape before looking at the actual values.
  3. Inspect mismatches. If a join returns extra rows, check whether the query should be on orders or order_items. If a filter returns too many rows, check NULL handling and date boundaries.

Keep reading

Frequently asked questions

Where can I find MySQL practice exercises for beginners with answers?

Look for a set that starts with basic SELECT and then moves through filtering, sorting, joins, aggregates, subqueries, strings, and dates. W3resource is one public example with beginner-to-advanced sections and solved exercises, but a good beginner path should also show the expected rows so answers are easy to verify.

What are some easy MySQL exercises to practice SELECT, WHERE, and ORDER BY?

Start with selecting two columns from a small table, then add a filter such as city IS NOT NULL, then sort by one or two columns. A beginner should also try a short LIMIT task, because it teaches how ordering affects which rows appear first.

Can I get MySQL sample tables to practice on as a beginner?

Yes. A good beginner set uses three small tables with clear keys: users, orders, and order_items. The data should include at least one NULL value, one one-to-many join, and a few repeated values so grouping and sorting produce visible results.

How do I check my MySQL answers against sample solutions?

Compare four things in order: row count, column count, column names, and then row values. If the shape is wrong, the query structure is usually wrong. If the shape is right but the values are off, check the filter, join condition, or group rule.

How do I practice MySQL joins with sample data?

Use two related tables and start with an INNER JOIN on matching keys. First join users to orders, then orders to order_items. That setup teaches how one row in the parent table can match many rows in the child table.

Are there beginner MySQL exercises for aggregate functions and group by?

Yes. Count orders by status, sum item totals by order, and calculate an average unit price across all items. These exercises teach when to use COUNT, SUM, and AVG, and they show why GROUP BY changes a flat result set into grouped totals.