Skip to lesson

Practice: aggregates and grouping

A review of this module. Skip it if you feel confident; the next module does not need it.

Fix the mistakes

The slips that cost marks in this module. Spot each one, then check the fix.

Most grouping bugs come from four habits: selecting a column that is not grouped, putting a filter in the wrong clause, forgetting what NULL does to counts and averages, and counting rows when the question was about something else. Each has a short, reliable fix.

Mistake 1: a selected column that is not grouped

Adding "just one more column" to a working report can break it with error 1055. The example fixes it with GROUP_CONCAT(DISTINCT ...) and COUNT(DISTINCT ...).

Example 1 · Fixing 1055 with GROUP_CONCAT(DISTINCT ...)

SQL
DROP TABLE IF EXISTS opd_visits;CREATE TABLE opd_visits (  visit_id   INT PRIMARY KEY,  doctor     VARCHAR(20)  NOT NULL,  department VARCHAR(20)  NOT NULL,  fee        DECIMAL(7,2) NULL,        -- NULL: free health-camp visit  status     VARCHAR(10)  NOT NULL     -- DONE or NO_SHOW);
INSERT INTO opd_visits VALUES  (1,  'Dr. Rao',   'Cardiology',    800.00, 'DONE'),  (2,  'Dr. Rao',   'Cardiology',    800.00, 'DONE'),  (3,  'Dr. Iyer',  'Cardiology',   1000.00, 'NO_SHOW'),  (4,  'Dr. Iyer',  'Cardiology',   1000.00, 'DONE'),  (5,  'Dr. Khan',  'Orthopaedics',  600.00, 'DONE'),  (6,  'Dr. Khan',  'Orthopaedics',    NULL, 'DONE'),  (7,  'Dr. Bose',  'Orthopaedics',  700.00, 'NO_SHOW'),  (8,  'Dr. Mehta', 'Paediatrics',   500.00, 'DONE'),  (9,  'Dr. Mehta', 'Paediatrics',     NULL, 'DONE'),  (10, 'Dr. Mehta', 'Paediatrics',   500.00, 'DONE'),  (11, 'Dr. Sen',   'Dermatology',   900.00, 'NO_SHOW'),  (12, 'Dr. Sen',   'Dermatology',   900.00, 'NO_SHOW');
-- Wrong (error 1055): SELECT department, doctor, COUNT(*) ... GROUP BY department-- Right: decide what "doctor" should show for a whole departmentSELECT department,       COUNT(*)                                                AS visits,       COUNT(DISTINCT doctor)                                  AS doctors,       GROUP_CONCAT(DISTINCT doctor ORDER BY doctor SEPARATOR ', ') AS doctor_listFROM opd_visitsGROUP BY departmentORDER BY department;
Output
+--------------+--------+---------+--------------------+
| department   | visits | doctors | doctor_list        |
+--------------+--------+---------+--------------------+
| Cardiology   |      4 |       2 | Dr. Iyer, Dr. Rao  |
| Dermatology  |      2 |       1 | Dr. Sen            |
| Orthopaedics |      3 |       2 | Dr. Bose, Dr. Khan |
| Paediatrics  |      3 |       1 | Dr. Mehta          |
+--------------+--------+---------+--------------------+

What this code does

  • A department has several visits and possibly several doctors, so a plain doctor column has no single value per group and MySQL raises error 1055.
  • COUNT(DISTINCT doctor) gives how many doctors, and GROUP_CONCAT(DISTINCT doctor ...) lists each one once. Without DISTINCT, Dr. Mehta would be listed three times.

The fix is a decision: one row per value too, add it to GROUP BY; one summary value, use MIN, MAX or COUNT; all the values, use GROUP_CONCAT. Never turn off ONLY_FULL_GROUP_BY to hide the error.

Fix the error

Fix the grouped query

Read the error, then pick the line that fixes it.

Pick the line that replaces line 1 so the query works.

SELECT dept_id, name, COUNT(*) AS people
FROM employees
GROUP BY dept_id;

Error

ERROR 1055 (42000): Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'practice.employees.name' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

Mistake 2: a filter in the wrong clause

A missing row filter fails quietly: no-shows are counted as earnings and the report looks normal. The example runs a wrong query and the right one, with WHERE before GROUP BY and HAVING after.

Example 2 · No-shows counted as earnings

SQL
DROP TABLE IF EXISTS opd_visits;CREATE TABLE opd_visits (  visit_id   INT PRIMARY KEY,  doctor     VARCHAR(20)  NOT NULL,  department VARCHAR(20)  NOT NULL,  fee        DECIMAL(7,2) NULL,        -- NULL: free health-camp visit  status     VARCHAR(10)  NOT NULL     -- DONE or NO_SHOW);
INSERT INTO opd_visits VALUES  (1,  'Dr. Rao',   'Cardiology',    800.00, 'DONE'),  (2,  'Dr. Rao',   'Cardiology',    800.00, 'DONE'),  (3,  'Dr. Iyer',  'Cardiology',   1000.00, 'NO_SHOW'),  (4,  'Dr. Iyer',  'Cardiology',   1000.00, 'DONE'),  (5,  'Dr. Khan',  'Orthopaedics',  600.00, 'DONE'),  (6,  'Dr. Khan',  'Orthopaedics',    NULL, 'DONE'),  (7,  'Dr. Bose',  'Orthopaedics',  700.00, 'NO_SHOW'),  (8,  'Dr. Mehta', 'Paediatrics',   500.00, 'DONE'),  (9,  'Dr. Mehta', 'Paediatrics',     NULL, 'DONE'),  (10, 'Dr. Mehta', 'Paediatrics',   500.00, 'DONE'),  (11, 'Dr. Sen',   'Dermatology',   900.00, 'NO_SHOW'),  (12, 'Dr. Sen',   'Dermatology',   900.00, 'NO_SHOW');
-- Wrong: no-show visits are added to the earningsSELECT department, SUM(fee) AS earnedFROM opd_visitsGROUP BY departmentHAVING SUM(fee) >= 1000ORDER BY department;
-- Right: completed visits only, then the group conditionSELECT department, SUM(fee) AS earnedFROM opd_visitsWHERE status = 'DONE'GROUP BY departmentHAVING SUM(fee) >= 1000ORDER BY department;
Output
+--------------+---------+
| department   | earned  |
+--------------+---------+
| Cardiology   | 3600.00 |
| Dermatology  | 1800.00 |
| Orthopaedics | 1300.00 |
| Paediatrics  | 1000.00 |
+--------------+---------+
+-------------+---------+
| department  | earned  |
+-------------+---------+
| Cardiology  | 2600.00 |
| Paediatrics | 1000.00 |
+-------------+---------+

What this code does

  • The wrong query looks fine but lists Dermatology with Rs 1,800, earned entirely from two no-shows, and adds Rs 1,000 to Cardiology for a patient who never came.
  • The right query removes no-shows in WHERE before grouping; HAVING then keeps departments that earned at least Rs 1,000: Cardiology and Paediatrics.

A row condition belongs in WHERE, a group condition in HAVING. An aggregate in WHERE fails with error 1111; a plain ungrouped column in HAVING fails with error 1054 ('having clause'), so move it to WHERE.

Find the bug

Find the wrong line

Select the line that is wrong.

This should total delivered orders only, but cancelled orders are included. Which line is wrong?

Round 1 of 2

Mistake 3: forgetting what NULL does

A free health-camp visit has a NULL fee. The example counts the visits and averages the fees in two ways, and each way answers a different question.

Example 3 · Free visits: counts and averages with NULL fees

SQL
DROP TABLE IF EXISTS opd_visits;CREATE TABLE opd_visits (  visit_id   INT PRIMARY KEY,  doctor     VARCHAR(20)  NOT NULL,  department VARCHAR(20)  NOT NULL,  fee        DECIMAL(7,2) NULL,        -- NULL: free health-camp visit  status     VARCHAR(10)  NOT NULL     -- DONE or NO_SHOW);
INSERT INTO opd_visits VALUES  (1,  'Dr. Rao',   'Cardiology',    800.00, 'DONE'),  (2,  'Dr. Rao',   'Cardiology',    800.00, 'DONE'),  (3,  'Dr. Iyer',  'Cardiology',   1000.00, 'NO_SHOW'),  (4,  'Dr. Iyer',  'Cardiology',   1000.00, 'DONE'),  (5,  'Dr. Khan',  'Orthopaedics',  600.00, 'DONE'),  (6,  'Dr. Khan',  'Orthopaedics',    NULL, 'DONE'),  (7,  'Dr. Bose',  'Orthopaedics',  700.00, 'NO_SHOW'),  (8,  'Dr. Mehta', 'Paediatrics',   500.00, 'DONE'),  (9,  'Dr. Mehta', 'Paediatrics',     NULL, 'DONE'),  (10, 'Dr. Mehta', 'Paediatrics',   500.00, 'DONE'),  (11, 'Dr. Sen',   'Dermatology',   900.00, 'NO_SHOW'),  (12, 'Dr. Sen',   'Dermatology',   900.00, 'NO_SHOW');
SELECT department,       COUNT(*)                          AS visits,       COUNT(fee)                        AS paid_visits,       ROUND(AVG(fee), 2)                AS avg_paid_fee,       ROUND(AVG(COALESCE(fee, 0)), 2)   AS avg_fee_per_visitFROM opd_visitsWHERE status = 'DONE'GROUP BY departmentORDER BY department;
Output
+--------------+--------+-------------+--------------+-------------------+
| department   | visits | paid_visits | avg_paid_fee | avg_fee_per_visit |
+--------------+--------+-------------+--------------+-------------------+
| Cardiology   |      3 |           3 |       866.67 |            866.67 |
| Orthopaedics |      2 |           1 |       600.00 |            300.00 |
| Paediatrics  |      3 |           2 |       500.00 |            333.33 |
+--------------+--------+-------------+--------------+-------------------+

What this code does

  • Paediatrics had 3 completed visits, but one was free (NULL fee), so COUNT(fee) is 2 and AVG(fee) is 500.00: the average of the paid visits only.
  • AVG(COALESCE(fee, 0)) counts the free visit as 0 and gives 333.33, the average income per visit. Both numbers are correct; they answer different questions.

COUNT(column) skips NULLs, AVG divides by the non-NULL count only, and SUM of no rows is NULL. Use COUNT(*) for rows, AVG(COALESCE(x, 0)) when missing means zero, and COALESCE(SUM(x), 0) when an empty total must print as 0.

Predict the output

Counts and averages with NULL

Read the code and pick what it prints.

Round 1 of 2

Course 5 has marks 80 and NULL.

SELECT COUNT(*), COUNT(marks)
FROM enrollments
WHERE course_id = 5;

Mistake 4: counting the wrong thing

In order_items a row is one line of an order, not one order. The query below counts the lines and the orders, and averages per line and per order.

Example 4 · Order lines versus orders

SQL
SELECT COUNT(*)                                       AS order_lines,       COUNT(DISTINCT order_id)                       AS orders,       ROUND(AVG(quantity * unit_price), 2)           AS avg_line_value,       ROUND(SUM(quantity * unit_price)             / COUNT(DISTINCT order_id), 2)           AS avg_order_valueFROM order_items;
Output
+-------------+--------+----------------+-----------------+
| order_lines | orders | avg_line_value | avg_order_value |
+-------------+--------+----------------+-----------------+
|          25 |     15 |       12373.72 |        20622.87 |
+-------------+--------+----------------+-----------------+

What this code does

  • order_items has 25 lines from 15 orders. A report that says "25 orders" has counted lines.
  • The average line is smaller than the average order, because many orders have two or three lines. Averaging the wrong unit gives a believable but wrong number.

COUNT(DISTINCT order_id) gives orders, and the average order value is SUM(quantity * unit_price) / COUNT(DISTINCT order_id). Before you write an aggregate, say out loud what one row of the table is.

Finish up

Mark complete & continue

Next: Mini project: ShopLite monthly sales report