MySQL Practice Exercises

Build confidence writing queries with MySQL practice problems designed for MySQL 8.4. Start with selecting and filtering records, then explore joins, aggregate functions, and other SQL features. Your queries execute in a real MySQL database, and the trainer automatically checks the output against expected results using hidden data. Problems in this set: 324, free: 106.

Problems in this set: 324, free: 106

Start with a free problem

MySQL problems

  • EasyFree
    PostgreSQL · MySQL · ClickHouse

    Unique Friend Request Pairs

    A social media platform is counting unique connections based on friend requests. Count the number of unique pairs of users, ignoring the direction of each request: A→B and B→A count as the same pair.

  • EasyFree
    PostgreSQL · MySQL · ClickHouse

    Each Player's First Login Date

    The game analytics team is studying when players first log in. For each player, return player_id and the date of their first login in a column named first_login.

  • EasyFree
    PostgreSQL · MySQL · ClickHouse

    Each Player's First Device

    The game analytics team is studying which devices players use when they first log in to the game. For each player, find the device they used for their first login.

  • EasyFree
    PostgreSQL · MySQL · ClickHouse

    People and Their Addresses

    The delivery department is preparing a contact list with addresses. For every person in the Person table, return their first and last names, along with their city and state from the Address table.

  • EasyFree
    PostgreSQL · MySQL

    Index for Employee Name Searches

    An HR service frequently searches for employees by name and needs these queries to run faster. Create an index named idx_employees_name on the name column of the employees table.

  • EasyFree
    PostgreSQL · MySQL

    Create an Active Employees View

    The HR department needs a separate view containing only active employees so that reports don't have to filter them manually every time.

  • EasyFree
    PostgreSQL · MySQL · ClickHouse

    Posts Reported on July 4, 2024

    A social media moderation team is analyzing reports received on a single day. Each row in Actions represents a user's action on a post: view, like, repost, report, or delete.

  • EasyFree
    PostgreSQL · MySQL · ClickHouse

    Team Size for Each Employee

    An HR analyst is preparing a report on team composition. For each employee, return employee_id and the number of employees on that employee's team in a column named team_size.

  • EasyFree
    PostgreSQL · MySQL

    Create the projects Table

    Create a new table named projects to track project work. The table must have two columns: id — an INT primary key, and name — a required string column that can hold up to 100 characters.

  • EasyFree
    PostgreSQL · MySQL

    Swap the Sex Values for All Employees

    An HR system stores employee records in the Salary table: id UUID, name VARCHAR(100), sex lc_swap_sex_t — an ENUM with the two values 'm' and 'f' — and salary NUMERIC(15,2).

What you will practice

  • Retrieve and limit query results using SELECT and LIMIT
  • Handle missing values with the IFNULL function
  • Combine grouped values into strings using GROUP_CONCAT
  • Filter and transform dates using MySQL date functions
  • Work with joins and window functions in MySQL

Common mistakes

  1. 1. Trying to use FULL OUTER JOIN

    MySQL 8.4 does not support FULL OUTER JOIN directly. Copying a query that uses this operator from another SQL dialect will fail. Before choosing an alternative, decide whether you need matching rows, all records from one table, or unmatched records from both sides.

  2. 2. Expecting GROUP_CONCAT to sort automatically

    GROUP_CONCAT combines values into a string, but their order is not guaranteed unless you specify it. Two results can contain the same elements in different sequences. If order matters, define it inside the aggregate function and test groups containing several distinct values.

  3. 3. Replacing NULL without considering its meaning

    IFNULL replaces a NULL value with a chosen alternative. However, zero, an empty string, and missing data do not always mean the same thing. Replacing missing values before calculating an aggregate can change the metric. Decide when the substitution should happen and why.

  4. 4. Using LIMIT without ORDER BY

    LIMIT controls how many records are returned, not which ones rank highest or lowest. Without ORDER BY, MySQL does not guarantee a particular row order. Specify the sorting criteria first, and consider what should happen when multiple records have equal values.

Theory and courses

Frequently asked questions

Which version of MySQL do these exercises use?

The exercises in this collection support MySQL 8.4. Queries run in a real database, and results are automatically checked against expected output using hidden data. When adapting SQL from another database system, pay attention to differences in functions, joins, and date handling.

Is there a free way to practice MySQL?

Yes. Free MySQL problems: 106. After signing in, you can solve up to 3 new problems per day on the free plan. Premium exercises are visible in the list but require a subscription. Reference SQL solutions are not published.

Does MySQL 8.4 support window functions?

Yes. MySQL has supported window functions since version 8.0, so they are available in MySQL 8.4. They let you calculate ranks, running values, and other metrics across related rows without necessarily reducing each group to a single result row.

How is IFNULL different from WHERE?

IFNULL changes the value returned by an expression when that value is NULL. WHERE determines which rows belong in the result. Use a replacement when you want to keep the record with a fallback value, and a filter when records should be excluded.

How can I handle a full outer join in MySQL?

MySQL 8.4 has no native FULL OUTER JOIN operator. If you need matching and unmatched records from both tables, you must combine supported SQL operations to produce that result. Be careful with duplicates and determine whether each matching pair should appear once.