SQL SELECT SUM: Understanding the SUM() Function
The SQL SUM() function is a powerful aggregate function that returns the total of a numeric column. It is widely used in data analysis and reporting to calculate totals across multiple records.
Complete Python Course with Advance topics:-
SQL Tutorial:-
SQL SUM() Syntax
SELECT SUM(expression)
FROM table_name
WHERE conditions;
Example 1: SUM with a Single Column
SELECT SUM(salary) AS "Total Salary"
FROM employees
WHERE salary > 25000;
-- Result: 112000
Example 2: SUM with DISTINCT
SELECT SUM(DISTINCT salary) AS "Total Salary"
FROM employees
WHERE salary > 25000;
This ensures duplicate salary values are not counted more than once.
Example 3: SUM with GROUP BY
SELECT department, SUM(daily_sales) AS "Total Sales"
FROM sales_data
GROUP BY department;
| DEPARTMENT | TOTAL SALES |
|---|---|
| IT | 1100 |
| HR | 500 |
| FINANCE | 400 |
Example 4: SUM with GROUP BY and HAVING
Use HAVING to filter grouped results ÔÇö for example, only departments with total sales above 500:
SELECT department, SUM(daily_sales) AS total
FROM sales_data
GROUP BY department
HAVING SUM(daily_sales) > 500;
Download New Real Time Projects:- Click here
Complete Advance AI topics:-
Conclusion
The SUM() function is crucial for aggregate calculations. Whether summing single columns, distinct values, or grouped reports with HAVING, it simplifies data analysis. For more SQL insights, stay connected with .
sql sum multiple columns
sql sum group by
sql sum with condition
sql sum having clause
sql average group by
sql sum distinct
sql select sum example
sql select sum w3schools