3. Aggregate and Window Functions
Activity 3.01: Analyzing Sales Data Using Aggregate Functions
Solution
- Open your favorite SQL client and connect to the
sqldadatabase. - Calculate the number of unit sales the company has achieved using the
COUNTÂ function:SELECT Â Â COUNT(*) FROM Â Â sales;
You should get
37,711sales. - Determine the total sales amount in dollars for each state; we can use the
SUMaggregate function here:SELECT   c.state, SUM(sales_amount) as total_sales_amount FROM   sales s INNER JOIN   customers c     ON c.customer_id=s.customer_id GROUP BY   1 ORDER BY   1;
You will get the following output:
Figure 3.30: Total sales in dollars by US state
- Determine the top five dealerships in terms of most units sold using the
GROUP BYclause. Set theLIMITto5:SELECT Â Â s.dealership_id, Â Â COUNT(*) FROM Â Â sales s WHERE Â Â ...