Schema and sample data
| order_id | customer_id | amount |
|---|---|---|
| 1 | 101 | 120.00 |
| 2 | 102 | 75.50 |
| 3 | 101 | 200.00 |
| 4 | 103 | 49.99 |
| 5 | 102 | 310.00 |
| 6 | 104 | 15.25 |
Show setup SQL (copy to run)
CREATE TABLE Orders (
order_id INT PRIMARY KEY,
customer_id INT NOT NULL,
amount DECIMAL(10,2) NOT NULL
);
INSERT INTO Orders (order_id, customer_id, amount) VALUES
(1, 101, 120.00),
(2, 102, 75.50),
(3, 101, 200.00),
(4, 103, 49.99),
(5, 102, 310.00),
(6, 104, 15.25);
From the Orders table, return a single row containing the total of all order amounts. Add up every value in the amount column and alias the result as total_sales.
Expected result
| total_sales |
|---|
| 770.74 |
Show hint
You want one number for the whole table, not one row per order. Reach for an aggregate function over the amount column, and give it a name with AS. No GROUP BY is needed when you are collapsing every row into a single total.
Try to write the query yourself before you open this.
Show solution and explanation
SELECT SUM(amount) AS total_sales
FROM Orders;
SUM(amount) is an aggregate function: it reads every value in the amount column and adds them together, returning one number. Because there is no GROUP BY clause, the whole table is treated as a single group, so you get exactly one row back. Adding the six amounts (120.00 + 75.50 + 200.00 + 49.99 + 310.00 + 15.25) gives 770.74. See the SUM function reference for the full syntax and edge cases.
The AS total_sales alias renames the output column. Without it, SQL Server would return an unnamed column (labelled something like (No column name)), which is awkward to reference. Aliasing aggregates is a good habit that makes results and downstream queries readable.
One common mistake is to add another column to the SELECT, such as customer_id, without a GROUP BY. That fails, because a bare column cannot be mixed with an aggregate unless it appears in GROUP BY. Also note that SUM ignores NULL amounts rather than treating them as zero, so a NULL row simply does not contribute to the total.