Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
SQL for Data Analytics

You're reading from  SQL for Data Analytics

Product type Book
Published in Aug 2019
Publisher Packt
ISBN-13 9781789807356
Pages 386 pages
Edition 1st Edition
Languages
Authors (3):
Upom Malik Upom Malik
Profile icon Upom Malik
Matt Goldwasser Matt Goldwasser
Profile icon Matt Goldwasser
Benjamin Johnston Benjamin Johnston
Profile icon Benjamin Johnston
View More author details

Table of Contents (11) Chapters

Preface 1. Understanding and Describing Data 2. The Basics of SQL for Analytics 3. SQL for Data Preparation 4. Aggregate Functions for Data Analysis 5. Window Functions for Data Analysis 6. Importing and Exporting Data 7. Analytics Using Complex Data Types 8. Performant SQL 9. Using SQL to Uncover the Truth – a Case Study Appendix

4. Aggregate Functions for Data Analysis

Activity 6: 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 by 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 4.23: Total sales in dollars by US state
  4. Determine the top five dealerships in terms of most units sold, using the GROUP BY clause and set LIMIT as 5:
    SELECT s.dealership_id, COUNT(*)
    FROM sales s
    WHERE channel='dealership'
    GROUP BY 1
    ORDER BY 2 DESC
    LIMIT 5

    You should get the following output:

    Figure 4.24: Top five dealerships by units sold
  5. Calculate...
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 €14.99/month. Cancel anytime}