Skip to main content
Practice

COUNT and SUM

Two of the most common SQL functions for summarizing data are COUNT() and SUM(). They help you answer questions such as:

  • How many orders were placed?
  • What's the total amount spent?

COUNT: How Many Rows?

COUNT() returns the number of 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 specific column, use COUNT() with the column name.

Count clients with email
SELECT COUNT(email)
FROM clients;

SUM: Add Up Values

SUM() adds up all values from a numeric column.

Total sales
SELECT SUM(order_total)
FROM client_orders;

This returns the total amount from all orders.

Note: SUM() works only with numeric types such as INT or REAL.

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.