Do you mean like this?
SELECT SUM(value)
FROM myTable
If you have multiple columns to return, simply add each non-aggregate (i.e., summed) row to the GROUP BY clause:
SELECT firstName, lastName, SUM(value)
FROM myTable
GROUP BY firstName, lastName
Answer from Devin Burke on Stack Overflow Top answer 1 of 6
34
Do you mean like this?
SELECT SUM(value)
FROM myTable
If you have multiple columns to return, simply add each non-aggregate (i.e., summed) row to the GROUP BY clause:
SELECT firstName, lastName, SUM(value)
FROM myTable
GROUP BY firstName, lastName
2 of 6
4
SELECT SUM(`value`) FROM `your_table`
w3resource
w3resource.com › mysql › aggregate-functions-and-grouping › aggregate-functions-and-grouping-sum().php
MySQL SUM() function
March 2, 2026 - The outer query then calculates the sum of the alias 'mysum', effectively giving the total count of rows where 'no_page' is greater than 200 across all rows in the 'book_mast' table. The alias 'bb' is used to reference the result of the subquery within the outer query. The query will return a single value, which is the sum of the count of rows where 'no_page' is greater than 200 in the 'book_mast' table. This provides insights into the total number of books with more than 200 pages in the dataset. ... mysql> SELECT SUM(mysum) -> FROM( -> SELECT COUNT(*) AS mysum -> FROM book_mast -> WHERE no_page>200) AS bb; +------------+ | SUM(mysum) | +------------+ | 12 | +------------+ 1 row in set (0.02 sec)
Javatpoint
javatpoint.com › mysql-sum
MySQL sum() - javatpoint
It allows us to count all rows or only some rows of the table that matches a specified condition. It is a type of aggregate function whose return type is BIGINT. This function... ... MySQL's aggregate function is used to perform calculations on multiple values and return the result in a single value like the average of all values, the sum of all values, and maximum & minimum value among certain groups of values.
DataCamp
datacamp.com › doc › mysql › mysql-sum
MySQL SUM() Function: Usage & Examples
The `SUM()` function in MySQL is used to calculate the total sum of a numeric column. It is particularly useful for aggregating data across rows in queries like sales totals, amounts, or any numerical data. The `SUM()` function is commonly used in `SELECT` statements combined with `GROUP BY` ...
Learning About Electronics
learningaboutelectronics.com › Articles › How-to-get-the-sum-of-all-rows-of-a-MySQL-table-column-using-PHP.php
How to Get the Sum of All Rows of a column of a MySQL Table Using PHP
So the above code adds up all the rows of the orders column. Doing the math, adding up all the orders, we get 27.50 (5.50 + 7.00 + 9.00 + 2.25 + 3.75= 27.50). So to explain the code a little, we first connect to the database. Then we query the database using the $result variable. This $result variable queries the database by getting the SUM of the orders column AS totalsum from the CustomerOrders table.
Scaler
scaler.com › home › topics › mysql sum
MySQL SUM() Function - Scaler Topics
May 18, 2023 - Once all rows have been iterated over, the total sum is returned. We can also use the SUM() function with the GROUP BY and HAVING clauses in MySQL to perform aggregate addition of specific columns based on certain conditions.
TutorialsPoint
tutorialspoint.com › mysql › mysql_aggregate_functions_sum.htm
MySQL - SUM() Function
The MySQL SUM() function is an aggregate function that is used to calculate the sum of all values in a particular column/field. If the specified row(s) doesn't exist this function returns NULL.
GeeksforGeeks
geeksforgeeks.org › python › how-to-compute-the-sum-of-all-rows-of-a-column-of-a-mysql-table-using-python
How to Compute the Sum of All Rows of a Column of a MySQL Table Using Python? - GeeksforGeeks
November 26, 2020 - # import required module import mysql.connector # connect python with mysql with your hostname, # database, user and password db = mysql.connector.connect(host='localhost', database='gfg', user='root', password='') # create cursor object cursor = db.cursor() # get the sum of rows of a column cursor.execute("SELECT SUM(Marks) FROM student") # fetch sum and display it print(cursor.fetchall()[0][0]) # terminate connection db.close()
TutorialsPoint
tutorialspoint.com › get-the-sum-of-a-column-in-all-mysql-rows
Get the sum of a column in all MySQL rows?
July 30, 2019 - Use aggregate function SUM() to get the sum of a column in al rows. Let us first create a table − · mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Amount int ); Query OK, 0 rows affected (0.20 sec)
EDUCBA
educba.com › home › data science › data science tutorials › mysql tutorial › mysql sum()
MySQL sum() | Complete Guide to MySQL sum() with Query Examples
June 6, 2023 - The result is zero because there is no row with supplier id 15 in the table Suppliers. We will take two tables, Products and Suppliers, which: ... SELECT SUM(Unit * CostEach) Product_cost FROM Suppliers INNER JOIN Products ON Supplier_ID WHERE Product_Name = 'Maggie'; ... The result sum value evaluates based on a condition provided by the values in the next table. The MySQL SUM() is similar to a mathematical sum calculation that finds the totality of provided table values in the database.
Call: +917738666252
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
Plus2Net
plus2net.com › sql_tutorial › sql_sum-multiple.php
Sum sql for data in multiple columns and across rows with Total & Percentage
February 5, 2000 - SELECT id, name, class, social, math, science, SUM(social + math + science) AS Total FROM student_sum GROUP BY id UNION SELECT '', '', 'Total', SUM(social), SUM(math), SUM(science), SUM(social) + SUM(math) + SUM(science) FROM student_sum Output ( watch the last row ) We can display grade of the student by using CASE statement in our Query. MySQL Case query executes the statement based on the matching condition. Here we will check total mark against set marks for different levels of grade and allot Grade accordingly.
Top answer 1 of 9
11
You can use WITH ROLLUP modifier:
select coalesce(user_type, 'total') as user, count(*) as count
from users
where user_type in ('driver', 'passenger')
group by user_type with rollup
This will return the same information but in a different format:
user | count
----------|------
driver | 32
passenger | 58
total | 90
db-fiddle
In MySQL 8 you can use COUNT() as window function:
select distinct
user_type,
count(*) over (partition by user_type) as count,
count(*) over () as sum
from users
where user_type in ('driver', 'passenger');
Result:
user_type | count | sum
----------|-------|----
driver | 32 | 90
passenger | 58 | 90
db-fiddle
or use CTE (Common Table Expressions):
with cte as (
select user_type, count(*) as count
from users
where user_type in ('driver', 'passenger')
group by user_type
)
select user_type, count, (select sum(count) from cte) as sum
from cte
db-fiddle
2 of 9
10
You need a subquery:
SELECT user_type,
Count(*) AS count,
(SELECT COUNT(*)
FROM users
WHERE user_type IN ("driver","passenger" )) as sum
FROM users
WHERE user_type IN ("driver","passenger" )
GROUP BY user_type ;
Note you dont need distinct here.
OR
SELECT user_type,
Count(*) AS count,
c.sum
FROM users
CROSS JOIN (
SELECT COUNT(*) as sum
FROM users
WHERE user_type IN ("driver","passenger" )
) as c
WHERE user_type IN ("driver","passenger" )
GROUP BY user_type ;
TutorialsPoint
tutorialspoint.com › sum-values-of-a-single-row-in-mysql
Sum values of a single row in MySQL?
You can use below syntax to sum values of a single row − Case 1 − The following is the syntax if your column does not have NULL value − SELECT yourColumnName1+yourColumnNa
Stack Overflow
stackoverflow.com › questions › 12555195 › return-the-sum-of-a-field-and-all-the-rows-using-mysql
Newest Questions - Stack Overflow
September 24, 2012 - Stack Overflow | The World’s Largest Online Community for Developers