The most useful answer to a search for a guide to MySQL exercise answers is not a single answer key, because "MySQL exercises" refers to dozens of unrelated problem sets: textbook chapters, online course quizzes, tutorial sites, certification practice exams, and instructor-assigned labs. What you can rely on is a method: identify the exact exercise source, derive the answer yourself using a reproducible setup, and use the answer key only to verify your reasoning and syntax. This guide explains how to do that, walks through the answers to the classic exercise types that appear in almost every MySQL course, and shows you how to tell a correct answer from one that merely runs.

First, pin down which exercise set you have

Exercise answers differ completely depending on the source, so the first step is to establish the scope. A chapter exercise from a textbook such as Alan Beaulieu's Learning SQL or the classic SQL Queries for Mere Mortals uses a specific sample schema and expects a specific style of query. A course exercise on a platform expects the exact output columns and ordering the grader checks. A certification practice question expects one of four options. A tutorial exercise on a site like SQLZoo or W3Schools expects a query that satisfies that site's own sample tables.

The practical test is simple: does the exercise statement name its tables, columns, and expected output? If yes, the answer is determinate and you can verify it. If the exercise refers to a schema you do not have ("use the employees database"), you cannot fully answer it without that schema, and no generic answer key will substitute. State that limitation to yourself before you start searching, because it determines whether you need a schema, a version note, or an instructor's rubric.

Version matters too, though less than beginners fear. The core of SQL — SELECT, WHERE, JOIN, GROUP BY, subqueries — behaves identically across MySQL 5.7, 8.0, and 8.4. Differences cluster in specific areas: window functions exist only from 8.0 onward; common table expressions likewise; GROUP BY no longer implicitly sorts results in 8.0; and strict mode changes how invalid dates and oversized strings are handled. If an exercise comes from a book written for 5.7 and you run it on 8.0, a query can be "wrong" only because of these version differences rather than because of your logic.

The universal setup that makes any answer checkable

Before checking answers, reproduce the data. Every worked example below uses a small schema you can create in a minute. This matters because checking an answer against your own predictable data is the only reliable way to know whether the answer is correct — far more reliable than comparing text with a key that may have been written for different data.

Two rows are deliberately awkward: Fay has no department, and not every employee has a manager. Most exercise mistakes happen on exactly these rows, so keep them.

The classic exercise types, with answers and the reasoning that makes them correct

Filtering and sorting

Exercise: list employees earning more than 70,000, highest first.

The answer is Ana (95000), Ben (88000), Cara (72000). The distinction that matters here is > versus >=: "more than 70,000" excludes a salary of exactly 70,000, and exercise statements are usually precise about this. If the grader returns rows in a different order, check whether ORDER BY was included at all — without it, MySQL may return rows in any order, and an answer key that shows sorted output is implicitly assuming a sort that the query may not perform.

Aggregation and grouping

Exercise: show each department's name and average salary.

The result is Engineering 91,500.00, Sales 68,500.00, Marketing 60,000.00 — and no row for Support, because it has no employees. That omission is usually correct for an inner join, but the exercise wording decides: "each department and its average salary" often implies Support should appear with a NULL average, which requires a LEFT JOIN from departments. This is the single most common source of "my answer doesn't match the key" in aggregation exercises.

Grouping by d.dept_id as well as d.dept_name is deliberate. MySQL 8.0's default ONLY_FULL_GROUP_BY mode requires every non-aggregated select column to be functionally dependent on the grouping columns; grouping by the primary key makes the name dependent and the query portable.

Joins, including the self join

Exercise: list each employee alongside their manager's name, including employees who have no manager.

Fay and the three department heads appear with a NULL manager. The self join works because the same table is given two aliases; without aliases MySQL cannot tell which emp_name you mean and raises an ambiguous-column error. Using an inner join here silently drops the NULL-manager rows, which is a logic error, not a syntax error — the query runs, and the answer is wrong.

Subqueries versus joins

Exercise: find employees earning more than the company average.

The answer is Ana and Ben (average salary is 72,500.00; Cara at 72,000 does not qualify). The scalar subquery is evaluated once, which is both simple and efficient at this scale. A join-based alternative groups the whole table and joins back, which is harder to read for this particular question. The lesson for answer-checking: two queries can be logically equivalent and both correct, so a key that shows only one form is showing a style, not a requirement.

HAVING versus WHERE

Exercise: show departments whose average salary exceeds 65,000.

Engineering and Sales qualify. The distinction to internalize: WHERE filters rows before grouping and cannot reference aggregates; HAVING filters groups after aggregation. An answer that puts AVG(salary) > 65000 in WHERE fails with an error in every MySQL version — this is one of the few places where the wrong answer is loudly wrong rather than quietly wrong, which makes it good practice material.

Window functions (MySQL 8.0 and later)

Exercise: rank employees by salary within each department.

Ana ranks 1 in Engineering, Ben 2; Cara 1 in Sales, Dev 2; Eli 1 in Marketing. This exercise cannot be answered on MySQL 5.7, and any answer key that includes window functions presupposes 8.0 or later. If you are on 5.7, the equivalent requires a correlated subquery counting higher earners — a useful exercise in itself, and worth trying precisely because it shows what the window function replaced.

Data modification with a safety check

Exercise: give every Sales employee a 10% raise.

Cara moves to 79,200 and Dev to 71,500. Two cautions belong with any UPDATE or DELETE exercise. First, run the SELECT with the same WHERE clause first to confirm the row count. Second, MySQL refuses to let you modify a table while selecting from it in a subquery in some forms, but the scalar subquery against departments here is fine; if the exercise involves selecting from the same table you are updating, MySQL 8.0 permits it via a derived table wrapper, while older behavior differed. Answers that wrap the subquery in SELECT * FROM (...) AS t are adapting to that constraint, not adding unnecessary complexity.

How to verify an answer instead of trusting it

A query that runs is not necessarily correct, and a query that matches a key is not necessarily understood. Verification has three levels, and good exercise practice touches all three.

Level one: does it run, and does it return the expected columns? Column names, order, and data types are what automated graders check first. Use column aliases to match the requested output exactly. COUNT(*) returns a different type from SUM(), and a grader expecting a decimal may reject an integer.

Level two: does it behave correctly on the edge rows? Re-run the query against your sample data and ask what happens to Fay (NULL department) and to the department heads (NULL manager). If the exercise implies those rows should appear, an inner join is wrong. If it implies they should be excluded, the inner join is right. The answer key rarely tells you which; the wording does.

Level three: can you predict the result before running it? This is the level that produces learning. Write down the rows you expect, run the query, and investigate any difference. If you cannot predict the output, you have found the concept to review — usually NULL handling, join type, or grouping granularity.

A concrete habit makes this fast: keep a file of your exercise queries with a comment above each stating the exercise number and the expected row count. When you return to the exercises later, the row counts let you spot regressions immediately, and the file becomes your own answer key, written for your schema and your MySQL version — which is more useful than any published key.

Why published answer keys so often disagree with your correct query

Several legitimate reasons explain a mismatch, and distinguishing them saves hours.

Different sample data. Most textbook keys are written against the book's schema. If your instructor substituted a different dataset, the correct query is identical but the output rows differ. Compare the query, not the output.

Implied requirements the statement leaves out. "List all departments" usually means all four, forcing a LEFT JOIN; "list departments with employees" means the three with staff. The key reflects the author's reading. When the wording is genuinely ambiguous, note both interpretations and the query for each — that note is the real answer.

Dialect differences disguised as errors. A key written for PostgreSQL may use ILIKE, :: casts, or LIMIT/OFFSET in forms MySQL handles differently. MySQL uses LIKE with a case-insensitive default collation for most character sets, and CAST(x AS ...) rather than ::. If the key's query does not run in MySQL, it is a dialect answer, not a MySQL answer.

Version drift. Keys showing GROUP BY without listing all selected columns, or relying on implicit sorting, were valid under older defaults and may fail or reorder under MySQL 8.0. Writing the explicit, standards-compliant form is the safer answer.

Style presented as requirement. JOIN ... ON versus comma joins with WHERE, subquery versus EXISTS, COUNT(*) versus COUNT(1) — these are equivalent in MySQL for practical purposes. If a key marks one "the answer," treat it as one valid form among several.

When you genuinely cannot derive the answer

Sometimes the exercise depends on material you do not have: a proprietary schema, a specific stored procedure definition, or an instructor's expected output format. The honest response is to state what is missing and answer the part that is determinate. You can still write the query structure, name the tables and columns it would reference as described in the statement, and verify the syntax against your own sample tables. What you should not do is invent the missing schema or pretend a unique output exists. In a course setting, submitting a query with a short note explaining the assumption you made is usually stronger than submitting a guessed output.

The same applies to certification practice questions: they are multiple-choice and their "correct" answers are defined by the exam's own documentation, not by SQL semantics. If a practice option seems wrong on technical grounds, check it against MySQL's official reference manual for the version the exam targets rather than against forum answers, which frequently reflect older versions or other database products.

A short checklist before you call an exercise finished

The query runs without error on the MySQL version you are actually using. The output columns, aliases, and order match what the exercise requests. You have checked NULL-bearing rows and decided deliberately whether they belong. You can explain why each clause is there, including the join type and the grouping columns. You have predicted the result before running it at least once. If the answer differs from a published key, you can name the reason: data, wording, dialect, version, or style.

That last item is the real measure of progress. The goal of MySQL exercises is not to accumulate answer keys; it is to reach the point where you can produce and defend the answer yourself, and where a key that disagrees with you prompts a specific, checkable question rather than doubt.