Before a quarterly review, someone usually gets asked two questions: who are our top three customers, and which accounts were worth $3,000 or more this quarter? The usual answer is a spreadsheet, a sort, and a lot of adding up by hand.

Both questions are four SQL keywords: GROUP BY, ORDER BY, LIMIT and HAVING. This walkthrough uses a made-up shop, Lakeside Office Supply, with eight orders. You can type all of it into DB Fiddle (free, no account needed, MySQL v9 selected).

Step 1: make the orders table

DB Fiddle has two boxes. The left one, Schema SQL, holds the table. Click inside it and type:

CREATE TABLE orders ( id INT, order_date DATE, customer VARCHAR(40), amount INT); 

Four columns: an id, the order date, the customer and the amount.

Leave a blank line and add the eight orders. Text goes in single quotes, numbers don't. Every row ends with a comma except the last one, which ends with a semicolon so the database knows the statement is finished:

INSERT INTO orders VALUES (1, '2026-07-02', 'Maple Dental', 1250), (2, '2026-07-15', 'Cobalt Legal', 860), (3, '2026-07-28', 'Northside Realty', 2140), (4, '2026-08-06', 'Cobalt Legal', 2310), (5, '2026-08-19', 'Harbor Accounting', 640), (6, '2026-08-27', 'Maple Dental', 980), (7, '2026-09-09', 'Northside Realty', 1890), (8, '2026-09-22', 'Cobalt Legal', 1270); 

Four customers, eight orders, one quarter (July to September).

Step 2: total each customer with GROUP BY

In the right box, Query SQL, type:

SELECT customer, SUM(amount) AS revenue FROM orders GROUP BY customer; 

Click Run. GROUP BY customer collapses the eight rows into one row per customer, and SUM(amount) adds up that customer's orders. AS revenue names the new column.

You get four rows, but in no useful order.

Step 3: biggest first with ORDER BY

Delete the semicolon at the end of the last line, press Enter, and add:

ORDER BY revenue DESC; 

DESC means descending, biggest first. Run it:

customer revenue
Cobalt Legal 4440
Northside Realty 4030
Maple Dental 2230
Harbor Accounting 640

Notice you can sort by revenue, the alias you just created. That works because ORDER BY runs after the totals are calculated.

Step 4: keep the top three with LIMIT

Remove the semicolon again and add one line:

LIMIT 3; 

The full query now reads:

SELECT customer, SUM(amount) AS revenue FROM orders GROUP BY customer ORDER BY revenue DESC LIMIT 3; 

Run it: Cobalt Legal, Northside Realty, Maple Dental. That's question one answered.

One thing to watch: LIMIT without ORDER BY gives you three rows, but not necessarily the top three. Always sort first.

Step 5: key accounts with HAVING

Question two is different. It's not "the top N" but "everyone over a threshold". Replace the whole query (Ctrl+A, then type) with:

SELECT customer, COUNT(*) AS orders, SUM(amount) AS revenue FROM orders GROUP BY customer HAVING SUM(amount) >= 3000 ORDER BY revenue DESC; 

COUNT(*) adds the number of orders next to the revenue. Run it:

customer orders revenue
Cobalt Legal 3 4440
Northside Realty 2 4030

Those are the two accounts that get a thank-you call. Change 3000 to any other number and the list updates.

Why HAVING and not WHERE

This is the part that trips up most beginners. It's tempting to write:

-- This does NOT do what you want SELECT customer, SUM(amount) AS revenue FROM orders WHERE amount >= 3000 GROUP BY customer; 

WHERE filters single rows before they are added up. No single order in this table is $3,000 or more (the biggest is 2,310), so this returns nothing, even though two customers are clearly over $3,000 in total.

HAVING filters the totals after GROUP BY. "Customers worth $3,000 or more" is a question about totals, so it needs HAVING.

You can use both in the same query. WHERE first narrows which orders count (say, one month), then HAVING filters the totals:

SELECT customer, SUM(amount) AS revenue FROM orders WHERE order_date >= '2026-08-01' GROUP BY customer HAVING SUM(amount) >= 2000; 

The order of the clauses

SQL is strict about the order you write things in:

SELECT ... FROM ... WHERE ... -- filters rows GROUP BY ... -- makes one row per group HAVING ... -- filters groups ORDER BY ... -- sorts the result LIMIT ... -- keeps the first N 

If you get a syntax error on a query that looks right, check this order first.

Same table, new question

The table didn't change between question one and question two. Only the query did. That's the real shift from spreadsheets: at work the orders table already exists, and every new question from your manager is just a new query. GROUP BY works on any column: customer, month, region or salesperson.

Checklist

  • [ ] Every row in INSERT ends with a comma, the last with a semicolon
  • [ ] GROUP BY names the column you want one row per
  • [ ] ORDER BY ... DESC before LIMIT, or the "top three" is random
  • [ ] Thresholds on totals go in HAVING, thresholds on single rows go in WHERE
  • [ ] Clause order: SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY, LIMIT
  • [ ] One total checked by hand (Cobalt Legal: 860 + 2310 + 1270 = 4440)

Practice it

This is based on a free step-by-step lesson where you type every line yourself in a practice copy of DB Fiddle that checks your work as you go (14 steps, about 12 minutes): SQL: Top Customers and Key Accounts with GROUP BY and HAVING.