Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
The Applied SQL Data Analytics Workshop

You're reading from   The Applied SQL Data Analytics Workshop Develop your practical skills and prepare to become a professional data analyst

Arrow left icon
Product type Paperback
Published in Feb 2020
Publisher Packt
ISBN-13 9781800203679
Length 484 pages
Edition 2nd Edition
Languages
Arrow right icon
Authors (3):
Arrow left icon
Upom Malik Upom Malik
Author Profile Icon Upom Malik
Upom Malik
Benjamin Johnston Benjamin Johnston
Author Profile Icon Benjamin Johnston
Benjamin Johnston
Matt Goldwasser Matt Goldwasser
Author Profile Icon Matt Goldwasser
Matt Goldwasser
Arrow right icon
View More author details
Toc

3. Aggregate and Window Functions

Activity 3.01: Analyzing Sales Data Using Aggregate Functions

Solution

  1. Open your favorite SQL client and connect to the sqlda database.
  2. Calculate the number of unit sales the company has achieved using the COUNT function:
    SELECT 
      COUNT(*)
    FROM 
      sales;

    You should get 37,711 sales.

  3. Determine the total sales amount in dollars for each state; we can use the SUM aggregate 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

  4. Determine the top five dealerships in terms of most units sold using the GROUP BY clause. Set the LIMIT to 5:
    SELECT 
      s.dealership_id, 
      COUNT(*)
    FROM 
      sales s
    WHERE 
      ...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime