>_ IzzyLab

Topic breakdown

A CTE (WITH name AS (...)) is a named subquery declared at the start of a query and referenced further on like a table. Formally it's syntactic sugar over a nested subquery, but it makes multi-step computations readable: first you compute an intermediate result, then work with it by name. A typical scenario — aggregate the data in the first step (GROUP BY plus SUM), and in the main query join a reference table and pick the right row with a sort plus LIMIT. In the final SELECT it's worth keeping exactly the columns the task requires.

What's checked

  1. customer with the maximum total selected

What it looks like