0
0
MySQLquery~5 mins

SUM function in MySQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the SUM function do in SQL?
The SUM function adds up all the values in a numeric column and returns the total.
Click to reveal answer
beginner
How do you use the SUM function to find the total sales from a 'sales' column in a table named 'orders'?
You write: <br>SELECT SUM(sales) FROM orders;<br>This adds all values in the 'sales' column.
Click to reveal answer
beginner
Can the SUM function be used with non-numeric columns?
No, SUM only works with numeric columns like integers or decimals. Using it on text columns causes an error.
Click to reveal answer
intermediate
What happens if the SUM function is used on an empty set of rows?
It returns NULL if there are no rows to sum. To get zero instead, you can use COALESCE, like COALESCE(SUM(column), 0).
Click to reveal answer
intermediate
How can you use SUM with GROUP BY to find total sales per customer?
Use: <br>SELECT customer_id, SUM(sales) FROM orders GROUP BY customer_id;<br>This groups rows by customer and sums sales for each.
Click to reveal answer
What does the SQL SUM function return?
AThe total of numeric values in a column
BThe average of numeric values in a column
CThe count of rows in a table
DThe maximum value in a column
Which SQL clause is commonly used with SUM to group results?
AORDER BY
BWHERE
CGROUP BY
DHAVING
What will SUM(sales) return if there are no rows matching the query?
A0
BEmpty string
CAn error
DNULL
Can SUM be used on a text column?
AYes, it sums the text length
BNo, it only works on numeric columns
CYes, it concatenates text
DOnly if text is numeric strings
How do you write a query to get total sales from 'orders' table?
ASELECT SUM(sales) FROM orders;
BSELECT COUNT(sales) FROM orders;
CSELECT MAX(sales) FROM orders;
DSELECT AVG(sales) FROM orders;
Explain how the SUM function works in SQL and give an example query.
Think about adding numbers in a list.
You got /3 concepts.
    Describe how to use SUM with GROUP BY to get totals per category.
    Imagine adding sales per customer.
    You got /3 concepts.