LogoInterview Master

SQL Learning

SQL SUM Function

Understanding the SQL SUM Function

The SUM function is a fundamental SQL aggregate function used for calculating the total sum of values in a specified numeric column. It's essential for financial reporting, performance analysis, inventory tracking, and any scenario where you need to aggregate quantitative data.

Basic Syntax

SELECT SUM(numeric_column_name)
FROM table_name
[WHERE condition];

SUM operates only on columns with numeric data types (like INTEGER, REAL, DECIMAL, FLOAT, etc.).

Common SQL SUM Interview Questions

  • Explain how the SUM function handles NULL values.
  • What happens if you try to use SUM on a non-numeric column?
  • How can you calculate sums for different categories within a single query?
  • Show how to calculate a running total or cumulative sum using SUM (often involves window functions).
  • When would you use SUM in a HAVING clause versus a WHERE clause?

SUM Function Basics

Simple Total Calculation

At its core, SUM adds all non-NULL values in the specified column for the rows selected by the query.

-- Calculate the total views across all videos on TokTuk
SELECT SUM(views) AS total_platform_views
FROM Videos;

Result: A single row with a single column (total_platform_views) containing the sum of all values in the views column.

SUM with Conditions (WHERE Clause)

Use the WHERE clause to filter rows before applying the SUM function, allowing you to calculate totals for specific subsets of your data.

-- Calculate total views just for videos uploaded by user_id 5
SELECT SUM(views) AS user_5_total_views
FROM Videos
WHERE user_id = 5;

Only the views from rows where user_id is 5 are included in the sum.

SUM and NULL Value Handling

Crucially, the SUM function ignores NULL values. It sums only the non-NULL numeric values it encounters. If all values in the column for the selected rows are NULL, SUM typically returns NULL (or sometimes 0, depending on the database system and context, though NULL is standard SQL behavior).

-- Example: If 'bonus_points' can be NULL
SELECT SUM(bonus_points) AS total_bonuses
FROM UserRewards;
-- This only sums rows where bonus_points is not NULL.

To treat NULLs as 0 in a sum, you often need to use a function like COALESCE or IFNULL: SUM(COALESCE(bonus_points, 0)).

SUM with DISTINCT

You can use SUM(DISTINCT column_name) to sum only the unique non-NULL values in a column. This is less common than COUNT(DISTINCT) but can be useful in specific scenarios.

-- Calculate the sum of unique view counts (less common, illustrative)
SELECT SUM(DISTINCT views) AS sum_of_distinct_view_counts
FROM Videos;

If view counts were 100, 200, 100, 300, this would return 100 + 200 + 300 = 600.

Data Types and SUM

SUM is designed for numeric data types (INTEGER, FLOAT, DECIMAL, etc.). Applying it to non-numeric types like strings or dates will usually result in an error. The data type of the result of SUM is typically large enough to hold the potential sum (e.g., summing INTEGER might return a BIGINT or DECIMAL).

Using SUM with GROUP BY

The real analytical power of SUM emerges when combined with the GROUP BY clause. This allows you to calculate subtotals for different categories or groups within your data.

Calculating Subtotals by Group

Aggregate numeric values for each distinct group defined in the GROUP BY clause.

-- Calculate total views per user (creator)
SELECT
  user_id,
  SUM(views) AS total_views_per_user
FROM Videos
GROUP BY user_id -- Group rows by the user who uploaded the video
ORDER BY total_views_per_user DESC;

This query calculates the sum of views separately for each unique user_id, showing the total view count achieved by each content creator.

Combining with Joins for Richer Grouping

Join tables to group by descriptive names rather than just IDs.

-- Calculate total views by username
SELECT
  u.username,
  SUM(COALESCE(v.views, 0)) AS total_views -- Use COALESCE if using LEFT JOIN to treat NULLs as 0
FROM Users u
LEFT JOIN Videos v ON u.user_id = v.user_id
GROUP BY u.user_id, u.username -- Important: Group by PK and other selected non-aggregated cols
ORDER BY total_views DESC;

Joining with Users allows us to display the username. Using LEFT JOIN and COALESCE ensures users with no videos appear with a total view count of 0.

Filtering Groups Based on SUM (HAVING Clause)

Use the HAVING clause to filter the results based on the calculated sum for each group.

-- Find users whose videos have collectively received more than 10,000 views
SELECT
  u.username,
  SUM(v.views) AS total_views
FROM Users u
JOIN Videos v ON u.user_id = v.user_id -- INNER JOIN is fine if we only want users WITH videos
GROUP BY u.user_id, u.username
HAVING SUM(v.views) > 10000 -- Filter the groups based on the aggregated SUM
ORDER BY total_views DESC;

The HAVING clause acts after the grouping and summation, allowing us to select only the high-performing user groups based on their total views.

Advanced SUM Techniques

Multiple Aggregations (SUM, COUNT, AVG)

Combine SUM with other aggregate functions like COUNT and AVG for more comprehensive summaries.

-- User content performance summary
SELECT
  u.username,
  COUNT(v.video_id) AS number_of_videos,
  SUM(v.views) AS total_views_generated,
  AVG(v.views) AS average_views_per_video,
  MAX(v.views) AS highest_view_count
FROM Users u
LEFT JOIN Videos v ON u.user_id = v.user_id
GROUP BY u.user_id, u.username
ORDER BY total_views_generated DESC;

This provides a multi-faceted view of each user's content performance.

SUM with Expressions

You can apply SUM to expressions involving columns, not just single columns.

-- Example: Calculate total potential revenue (views * estimated_ad_rate)
-- Assuming an 'estimated_ad_rate' column or variable exists
SELECT
  user_id,
  SUM(views * 0.001) AS estimated_revenue -- Summing the result of an expression
FROM Videos
GROUP BY user_id;

This calculates an estimated revenue per user based on their total views and a hypothetical ad rate.

Summary

The SUM function is a cornerstone of numeric data aggregation in SQL:

  • It calculates the total sum of non-NULL values in a numeric column.
  • It ignores NULL values during calculation. Use COALESCE or similar functions if you need to treat NULLs as zero.
  • Use with a WHERE clause to calculate sums for specific subsets of data.
  • Combine with GROUP BY to calculate subtotals for different categories.
  • Use with HAVING to filter groups based on their calculated sum.
  • Can be applied to expressions involving numeric columns.

Understanding how and when to use SUM is crucial for performing meaningful quantitative analysis and generating insightful reports from your database.

Loading SQL editor...