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
SQL for Data Analytics

You're reading from   SQL for Data Analytics Perform fast and efficient data analysis with the power of SQL

Arrow left icon
Product type Paperback
Published in Aug 2019
Publisher Packt
ISBN-13 9781789807356
Length 386 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (3):
Arrow left icon
Benjamin Johnston Benjamin Johnston
Author Profile Icon Benjamin Johnston
Benjamin Johnston
Matt Goldwasser Matt Goldwasser
Author Profile Icon Matt Goldwasser
Matt Goldwasser
Upom Malik Upom Malik
Author Profile Icon Upom Malik
Upom Malik
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Understanding and Describing Data 2. The Basics of SQL for Analytics FREE CHAPTER 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 €18.99/month. Cancel anytime