SQL for Data Analytics - Third Edition

Book description

Take your first steps to becoming a fully qualified data analyst by learning how to explore complex datasets

Key Features

  • Master each concept through practical exercises and activities
  • Discover various statistical techniques to analyze your data
  • Implement everything you’ve learned on a real-world case study to uncover valuable insights

Book Description

Every day, businesses operate around the clock, and a huge amount of data is generated at a rapid pace. This book helps you analyze this data and identify key patterns and behaviors that can help you and your business understand your customers at a deep, fundamental level.

SQL for Data Analytics, Third Edition is a great way to get started with data analysis, showing how to effectively sort and process information from raw data, even without any prior experience.

You will begin by learning how to form hypotheses and generate descriptive statistics that can provide key insights into your existing data. As you progress, you will learn how to write SQL queries to aggregate, calculate, and combine SQL data from sources outside of your current dataset. You will also discover how to work with advanced data types, like JSON. By exploring advanced techniques, such as geospatial analysis and text analysis, you will be able to understand your business at a deeper level. Finally, the book lets you in on the secret to getting information faster and more effectively by using advanced techniques like profiling and automation.

By the end of this book, you will be proficient in the efficient application of SQL techniques in everyday business scenarios and looking at data with the critical eye of analytics professional.

What you will learn

  • Use SQL to clean, prepare, and combine different datasets
  • Aggregate basic statistics using GROUP BY clauses
  • Perform advanced statistical calculations using a WINDOW function
  • Import data into a database to combine with other tables
  • Export SQL query results into various sources
  • Analyze special data types in SQL, including geospatial, date/time, and JSON data
  • Optimize queries and automate tasks
  • Think about data problems and find answers using SQL

Who this book is for

If you're a database engineer looking to transition into analytics or a backend engineer who wants to develop a deeper understanding of production data and gain practical SQL knowledge, you will find this book useful. This book is also ideal for data scientists or business analysts who want to improve their data analytics skills using SQL. Basic familiarity with SQL (such as basic SELECT, WHERE, and GROUP BY clauses) as well as a good understanding of linear algebra, statistics, and PostgreSQL 14 are necessary to make the most of this SQL data analytics book.

Table of contents

  1. SQL for Data Analytics
  2. Third edition
  3. Preface
    1. About the Book
    2. About the Authors
    3. Audience
    4. About the Chapters
    5. Conventions
    6. Setting up Your Environment
      1. Installing PostgreSQL 14
      2. Downloading and Installing PostgreSQL on Windows
      3. Setting the PATH Variable
      4. Installation on macOS
      5. Installing Python
        1. Installing Python on Windows
        2. Installing Python on Linux
        3. Installing Python on macOS
    7. Installing Git
      1. Installing Git on Windows or macOS
      2. Installing Git on Linux
    8. Loading the Sample Datasets – Windows
    9. Loading the Sample Datasets – Linux
    10. Loading the Sample Datasets – macOS
    11. Running SQL files
    12. Accessing the Code Files
  4. 1. Understanding and Describing Data
    1. Introduction
    2. Data Analytics and Statistics
      1. Activity 1.01: Classifying a New Dataset
    3. Types of Statistics
      1. Methods of Descriptive Statistics
      2. Univariate Analysis
        1. Data Frequency Distribution
      3. Exercise 1.01: Creating a Histogram
        1. Quantiles
      4. Exercise 1.02: Calculating the Quartiles for Add-On Sales
        1. Central Tendency
      5. Exercise 1.03: Calculating the Central Tendency of Add-On Sales
        1. Dispersion
      6. Exercise 1.04: Dispersion of Add-On Sales
      7. Bivariate Analysis
        1. Scatterplots
        2. Linear Trend Analysis and Pearson Correlation Coefficient
      8. Exercise 1.05: Calculating the Pearson Correlation Coefficient for Two Variables
      9. Interpreting and Analyzing the Correlation Coefficient
        1. Time Series Data
      10. Activity 1.02: Exploring Dealership Sales Data
    4. Working with Missing Data
    5. Statistical Significance Testing
      1. Common Statistical Significance Tests
    6. SQL and Analytics
    7. Summary
  5. 2. The Basics of SQL for Analytics
    1. Introduction
    2. The World of Data
      1. Types of Data
    3. Relational Databases and SQL
      1. Advantages and Disadvantages of SQL Databases
    4. PostgreSQL Relational Database Management System (RDBMS)
      1. Exercise 2.01: Running Your First SELECT Query
      2. SELECT Statement
      3. The WHERE Clause
      4. The AND/OR Clause
      5. The IN/NOT IN Clause
      6. ORDER BY Clause
      7. The LIMIT Clause
      8. IS NULL/IS NOT NULL Clause
      9. Exercise 2.02: Querying the salespeople Table Using Basic Keywords in a SELECT Query
      10. Activity 2.01: Querying the customers Table Using Basic Keywords in a SELECT Query
    5. Creating Tables
      1. Creating Blank Tables
    6. Basic Data Types of SQL
      1. Numeric
      2. Character
      3. Boolean
      4. Datetime
    7. Data Structures: JSON and Arrays
    8. Column Constraints
      1. Simple CREATE Statement
      2. Exercise 2.03: Creating a Table in SQL
      3. Creating Tables with SELECT
    9. Updating Tables
      1. Adding and Removing Columns
      2. Adding New Data
      3. Updating Existing Rows
      4. Exercise 2.04: Updating the Table to Increase the Price of a Vehicle
      5. Deleting Data and Tables
      6. Deleting Values from a Row
      7. Deleting Rows from a Table
      8. Deleting Tables
      9. Exercise 2.05: Deleting an Unnecessary Reference Table
      10. Activity 2.02: Creating and Modifying Tables for Marketing Operations
    10. SQL and Analytics
    11. Summary
  6. 3. SQL for Data Preparation
    1. Introduction
    2. Assembling Data
      1. Connecting Tables Using JOIN
      2. Types of Joins
        1. Inner Joins
        2. Outer Joins
        3. Cross Joins
      3. Exercise 3.01: Using Joins to Analyze a Sales Dealership
      4. Subqueries
      5. Unions
      6. Exercise 3.02: Generating an Elite Customer Party Guest List Using UNION
      7. Common Table Expressions
    3. Cleaning Data
      1. The CASE WHEN Function
      2. Exercise 3.03: Using the CASE WHEN Function to Get Regional Lists
      3. The COALESCE Function
      4. The NULLIF Function
      5. The LEAST/GREATEST Functions
      6. The Casting Function
    4. Transforming Data
      1. The DISTINCT and DISTINCT ON Functions
      2. Activity 3.01: Building a Sales Model Using SQL Techniques
    5. Summary
  7. 4. Aggregate Functions for Data Analysis
    1. Introduction
    2. Aggregate Functions
      1. Exercise 4.01: Using Aggregate Functions to Analyze Data
    3. Aggregate Functions with the GROUP BY Clause
      1. The GROUP BY Clause
      2. Multiple-Column GROUP BY
      3. Exercise 4.02: Calculating the Cost by Product Type Using GROUP BY
      4. Grouping Sets
      5. Ordered Set Aggregates
    4. Aggregate Functions with the HAVING Clause
      1. Exercise 4.03: Calculating and Displaying Data Using the HAVING Clause
    5. Using Aggregates to Clean Data and Examine Data Quality
      1. Finding Missing Values with GROUP BY
      2. Measuring Data Uniqueness with Aggregates
      3. Activity 4.01: Analyzing Sales Data Using Aggregate Functions
    6. Summary
  8. 5. Window Functions for Data Analysis
    1. Introduction
    2. Window Functions
      1. The Basics of Window Functions
      2. Exercise 5.01: Analyzing Customer Data Fill Rates over Time
      3. The WINDOW Keyword
    3. Statistics with Window Functions
      1. Exercise 5.02: Rank Order of Hiring
    4. Window Frame
      1. Exercise 5.03: Team Lunch Motivation
      2. Activity 5.01: Analyzing Sales Using Window Frames and Window Functions
    5. Summary
  9. 6. Importing and Exporting Data
    1. Introduction
    2. The COPY Command
      1. Running the psql Command
      2. The COPY Statement
      3. \COPY with psql
      4. Creating Temporary Views
      5. Configuring COPY and \COPY
      6. Using COPY and \COPY to Bulk Upload Data to Your Database
      7. Exercise 6.01: Exporting Data to a File for Further Processing in Excel
    3. Using Python with your Database
      1. Getting Started with Python
      2. Improving PostgreSQL Access in Python with SQLAlchemy and pandas
      3. What is SQLAlchemy?
      4. Using Python with SQLAlchemy and pandas
      5. Reading and Writing to a Database with pandas
      6. Writing Data to the Database Using Python
      7. Exercise 6.02: Reading, Visualizing, and Saving Data in Python
      8. Improving Python Write Speed with COPY
      9. Reading and Writing CSV Files with Python
      10. Best Practices for Importing and Exporting Data
    4. Going Passwordless
      1. Activity 6.01: Using an External Dataset to Discover Sales Trends
    5. Summary
  10. 7. Analytics Using Complex Data Types
    1. Introduction
    2. Date and Time Data types for Analysis
      1. The DATE Data type
      2. Transforming Date Data types
      3. Intervals
      4. Exercise 7.01: Analytics with Time Series Data
    3. Performing Geospatial Analysis in PostgreSQL
      1. Latitude and Longitude
      2. Representing Latitude and Longitude in PostgreSQL
      3. Exercise 7.02: Geospatial Analysis
    4. Using Array Data types in PostgreSQL
      1. Starting with Arrays
      2. Exercise 7.03: Analyzing Sequences Using Arrays
    5. Using JSON Data types in PostgreSQL
      1. JSONB: Pre-Parsed JSON
      2. Accessing Data from a JSON or JSONB Field
      3. Leveraging the JSON Path Language for JSONB Fields
      4. Creating and Modifying Data in a JSONB Field
      5. Exercise 7.04: Searching through JSONB
    6. Text Analytics Using PostgreSQL
      1. Tokenizing Text
      2. Exercise 7.05: Performing Text Analytics
      3. Performing Text Search
      4. Optimizing Text Search on PostgreSQL
      5. Activity 7.01: Sales Search and Analysis
    7. Summary
  11. 8. Performant SQL
    1. Introduction
    2. The Importance of Highly Efficient SQL
    3. Database Scanning Methods
      1. Query Planning
      2. Exercise 8.01: Interpreting the Query Planner
      3. Activity 8.01: Query Planning
      4. Index Scanning
      5. The B-Tree Index
      6. Exercise 8.02: Creating an Index Scan
      7. Activity 8.02: Implementing Index Scans
      8. The Hash Index
      9. Exercise 8.03: Generating Several Hash Indexes to Investigate Performance
      10. Activity 8.03: Implementing Hash Indexes
      11. Effective Index Use
    4. Killing Queries
      1. Exercise 8.04: Canceling a Long-Running Query
    5. Functions and Triggers
      1. Function Definitions
      2. Exercise 8.05: Creating Functions without Arguments
      3. Activity 8.04: Defining a Largest Sale Value Function
      4. Exercise 8.06: Creating Functions with Arguments
        1. The \df and \sf commands
      5. Activity 8.05: Creating Functions with Arguments
      6. Triggers
      7. Exercise 8.07: Creating Triggers to Update Fields
      8. Activity 8.06: Creating a Trigger to Track Average Purchases
    6. Summary
  12. 9. Using SQL to Uncover the Truth: A Case Study
    1. Introduction
    2. Case Study
      1. The Scientific Method
      2. Exercise 9.01: Preliminary Data Collection Using SQL Techniques
      3. Exercise 9.02: Extracting the Sales Information
      4. Activity 9.01: Quantifying the Sales Drop
      5. Exercise 9.03: Launch Timing Analysis
      6. Activity 9.02: Analyzing the Difference in the Sales Price Hypothesis
      7. Exercise 9.04: Analyzing Sales Growth by Email Opening Rate
      8. Exercise 9.05: Analyzing the Performance of the Email Marketing Campaign
      9. Conclusions
      10. In-Field Testing
    3. Summary
  13. Appendix
    1. 1. Understanding and Describing Data
      1. Activity 1.01: Classifying a New Dataset
      2. Activity 1.02: Exploring Dealership Sales Data
    2. 2. The Basics of SQL for Analytics
      1. Activity 2.01: Querying the customers Table Using Basic Keywords in a SELECT Query
      2. Activity 2.02: Creating and Modifying Tables for Marketing Operations
    3. 3. SQL for Data Preparation
      1. Activity 3.01: Building a Sales Model Using SQL Techniques
    4. 4. Aggregate Functions for Data Analysis
      1. Activity 4.01: Analyzing Sales Data Using Aggregate Functions
    5. 5. Window Functions for Data Analysis
      1. Activity 5.01: Analyzing Sales Using Window Frames and Window Functions
    6. 6. Importing and Exporting Data
      1. Activity 6.01: Using an External Dataset to Discover Sales Trends
    7. 7. Analytics Using Complex Data Types 
      1. Activity 7.01: Sales Search and Analysis
    8. 8. Performant SQL
      1. Activity 8.01: Query Planning
      2. Activity 8.02: Implementing Index Scans
      3. Activity 8.03: Implementing Hash Indexes
      4. Activity 8.04: Defining a Largest Sale Value Function
      5. Activity 8.05: Creating Functions with Arguments
      6. Activity 8.06: Creating a Trigger to Track Average Purchases
    9. 9. Using SQL to Uncover the Truth: A Case Study 
      1. Activity 9.01: Quantifying the Sales Drop
      2. Activity 9.02: Analyzing the Difference in the Sales Price Hypothesis
      3. Hey!

Product information

  • Title: SQL for Data Analytics - Third Edition
  • Author(s): Jun Shan, Matt Goldwasser, Upom Malik, Benjamin Johnston
  • Release date: August 2022
  • Publisher(s): Packt Publishing
  • ISBN: 9781801812870