SQL for beginners: SELECT, SUM and GROUP BY
SQL is the standard language for asking questions of a table. A few keywords cover most business questions.
The four keywords
SELECT chooses the columns you want to see. FROM names the table. GROUP BY makes one row per group, such as per city. ORDER BY sorts the result. Functions like SUM and COUNT calculate totals and counts.
An example
To see money by city, biggest first, you write: SELECT city, SUM(amount) AS money FROM sales GROUP BY city ORDER BY money DESC. Read it aloud: show each city and the sum of amount, from the sales table, one row per city, biggest first.
Practise safely
Reading data never changes it, so you can experiment. Change the column names and see what happens. Add LIMIT 5 to see only the top five rows.
Related guides
- How to clean sales data before you analyse it
- How to group customers by spending
- Average, median and why one number can mislead
General information for learning. It is not financial, tax, legal or investment advice.