Complete the code to select all sales amounts from the sales table.
SELECT [1] FROM sales;The column that stores the sales amount is named amount. Selecting this column will show all sales amounts.
Complete the code to calculate the running total of sales amounts using a correlated subquery.
SELECT s1.id, s1.amount, (SELECT SUM(s2.amount) FROM sales s2 WHERE s2.id <= [1]) AS running_total FROM sales s1 ORDER BY s1.id;s2.id instead of the outer s1.id.The correlated subquery compares s2.id to s1.id to sum all amounts up to the current row. So, s1.id is the correct reference.
Fix the error in the code to correctly calculate the running total without window functions.
SELECT id, amount, (SELECT SUM(amount) FROM sales WHERE id [1] id) AS running_total FROM sales ORDER BY id;= which sums only the current row.> which sums rows after the current one.To get the running total up to the current row, we sum amounts where id <= current id. Using <= includes the current row.
Fill both blanks to create a running total query that sums amounts up to the current row id.
SELECT s1.id, s1.amount, (SELECT SUM([1]) FROM sales s2 WHERE s2.id [2] s1.id) AS running_total FROM sales s1 ORDER BY s1.id;
The subquery sums the amount column where s2.id <= s1.id to include all rows up to the current one.
Fill all three blanks to write a running total query without window functions, using aliases and correct comparison.
SELECT [1], [2], (SELECT SUM([3]) FROM sales s2 WHERE s2.id <= s1.id) AS running_total FROM sales s1 ORDER BY s1.id;
The outer query selects s1.id and s1.amount. The subquery sums the amount column from s2 where s2.id <= s1.id to get the running total.