COUNT and SUM
Two of the most useful SQL tools for summarizing data are COUNT()
and SUM()
.
They help you answer questions like:
- How many orders were placed?
- What's the total amount spent?
COUNT: How Many Rows?
COUNT()
is used to count rows in a table.
Count total clients
SELECT COUNT(*)
FROM clients;
This returns the total number of rows in the clients
table.
To count only non-null values in a column, you can use the COUNT()
function with a column name.
Count clients with email
SELECT COUNT(email)
FROM clients;
SUM: Add Up Values
SUM()
adds together values from a numeric column.
Total sales
SELECT SUM(order_total)
FROM client_orders;
This returns the total amount spent across all orders.
Note: Works only with numeric types like
INT
orREAL
.
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.