W3. SQL Sum Flashcards

1
Q

Q: What does the SQL SUM() function do?

A

A: It returns the total sum of a numeric column.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Q: Write a query to find the total quantity in the “Quantity” column of the “OrderDetails” table.

A

SELECT SUM(Quantity)
FROM OrderDetails;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Q: What is the syntax for using SUM() with a condition?

A

SELECT SUM(column_name)
FROM table_name
WHERE condition;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Q: How can you use the WHERE clause with SUM()?

A

A: Add a WHERE clause to specify which rows to include in the sum calculation, e.g., WHERE ProductID = 11.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Q: Write a query to return the sum of “Quantity” for records with ProductID = 11.

A
SELECT SUM(Quantity) 
FROM OrderDetails 
WHERE ProductID = 11;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Q: How can you give a descriptive name to the result column of SUM()?

A

A: Use the AS keyword to set an alias, e.g., AS total.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Q: Write a query to find the total quantity and label it as “total.”

A
SELECT SUM(Quantity) AS total 
FROM OrderDetails;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Q: How do you use SUM() with GROUP BY?

A

A: Combine SUM() with GROUP BY to get the sum for each group, e.g., GROUP BY OrderID.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Q: Write a query to return the total quantity for each OrderID in “OrderDetails” and label it “Total Quantity.”

A
SELECT OrderID, SUM(Quantity) AS [Total Quantity] 
FROM OrderDetails 
GROUP BY OrderID;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Q: Can you use an expression inside the SUM() function?

A

A: Yes, e.g., SUM(Quantity * 10) to multiply each quantity by 10 before summing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Q: Write a query that multiplies each quantity by 10 and then sums it in the “OrderDetails” table.

A
SELECT SUM(Quantity * 10) 
FROM OrderDetails;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Q: How can you join tables to calculate a sum with related data?

A

A: Use a JOIN to combine tables, then apply SUM() to calculate based on data from both tables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Q: Write a query to find the total earnings by joining “OrderDetails” and “Products” to use actual prices.

A
SELECT SUM(Price * Quantity) 
FROM OrderDetails 
LEFT JOIN Products ON OrderDetails.ProductID = Products.ProductID;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly