Share E-Book
Scan to open this page

Scan with your phone to open this page

AuthorCathy Tanimura

With the explosion of data, computing power, and cloud data warehouses, SQL has become an even more indispensable tool for the savvy analyst or data scientist. This practical book reveals new and hidden ways to improve your SQL skills, solve problems, and make the most of SQL as part of your workflow. You'll learn how to use both common and exotic SQL functions such as joins, window functions, subqueries, and regular expressions in new, innovative ways--as well as how to combine SQL techniques to accomplish your goals faster, with understandable code. If you work with SQL databases, this is a must-have reference. • Learn the key steps for preparing your data for analysis • Perform time series analysis using SQL's date and time manipulations • Use cohort analysis to investigate how groups change over time • Use SQL's powerful functions and operators for text analysis • Detect outliers in your data and replace them with alternate values • Establish causality using experiment analysis, also known as A/B testing

AI Reading Assistant

Whole-book reading guide from stratified index samples; jump to passages in the text

AI guide
【One-Line Pitch】 A practical, advanced SQL reference for analysts and data scientists who want to move beyond basic queries and use SQL as a complete toolkit for data preparation, time series analysis, cohort studies, text mining, and A/B testing—turning raw data into actionable insights. 【Book Arc】 - **Opening (~0%–10%)**: Introduces SQL's role in modern data analysis, framing it as a core skill within a broader data-informed workflow. Covers ethical considerations (GDPR, CCPA) and sets expectations for the book's practical, code-first approach. - **Early (~10%–30%)**: Builds foundational knowledge: database types (row-store vs. column-store like Redshift, Snowflake), data types (BOOLEAN, DATE, TIMESTAMP), and query structure. Emphasizes data profiling—checking distributions, sampling, and understanding schemas before analysis. - **Early–Middle (~30%–50%)**: Focuses on data preparation and shaping. Covers CASE statements for cleaning and categorizing (e.g., standardizing gender values, NPS scoring), handling nulls and empty strings, and techniques like pivoting/unpivoting. Introduces window functions (e.g., ntile) for ranking and binning. - **Middle (~50%–70%)**: Delves into advanced analysis techniques: time series analysis with date/time manipulations, cohort analysis (retention curves, survivorship, repeat purchase behavior), and text analysis using SQL string functions and regular expressions. Emphasizes combining techniques for efficient, readable code. - **Late (~70%–90%)**: Covers outlier detection and replacement, and experiment analysis (A/B testing) to establish causality. Discusses how to structure complex SQL for maintainability and how to output data for BI tools, statistics (R), or machine learning (Python). - **Ending (~90%–100%)**: Wraps up with best practices for integrating SQL into a repeatable analysis workflow, emphasizing that data analysis is a continuous process, not a one-off project. (Excerpts do not cover the final chapters in detail.) 【Key Takeaways】 - **SQL is a complete analysis tool, not just a query language** (Early): Beyond fetching data, SQL can profile, clean, shape, and model data—reducing reliance on external tools. This saves time and leverages database computing power. - **Data profiling is the first step in any analysis** (Early): Before querying, inspect schemas, column names, and distributions to build a mental model of relationships (e.g., order_detail → order → customer). This prevents errors and guides query design. - **Column-store databases change how you write SQL** (Early): Databases like Redshift and Snowflake compress data efficiently and lack primary keys, so duplicates can sneak in. Understanding their write-only policies and lack of indexes is crucial for quality checking and query optimization. - **CASE statements are the Swiss Army knife for data cleaning** (Middle): Use them to standardize messy values (e.g., 'F', 'female', 'femme' → 'Female') or create new categories (e.g., NPS detractors/passives/promoters). They handle multiple columns and AND/OR logic, making them versatile for enrichment. - **Window functions like ntile are essential for ranking and binning** (Middle): ntile splits data into ordered bins (e.g., deciles of order amounts), enabling percentile analysis and cohort segmentation. Combined with PARTITION BY and ORDER BY, they unlock powerful aggregations without collapsing rows. - **Handling nulls and empty strings is a deliberate choice** (Middle): Nulls mean "unknown," while empty strings mean "known blank." Business users often misinterpret nulls, so decide whether to replace them with defaults or meaningful values to avoid confusing output. - **Shaping data depends on the downstream tool** (Middle): Output as few rows as possible while meeting granularity needs—whether for BI dashboards, spreadsheets, R, or Python ML models. This reduces data transfer time and processing overhead. - **Time series and cohort analysis are core analytical patterns** (Late): SQL's date/time functions enable trend analysis, while cohort analysis (e.g., retention curves, survivorship) reveals how groups change over time. These techniques turn raw event logs into strategic insights. 【Reading Tips】 - **Skim the early chapters (0–30%)** if you're already comfortable with SQL basics; focus on the sections about column-store databases and data profiling, as they're unique and often overlooked. - **Deep-read the middle chapters (30–70%)** on data shaping, window functions, and cohort analysis—these are the book's core value. Practice the code examples with your own data to internalize the patterns. - **Pay special attention to the "why" behind each technique** (e.g., why use ntile vs. CASE for binning). The book explains trade-offs, which helps you choose the right tool for future problems. - **Use the GitHub repository** (linked in the preface) to download code examples and exercises. Run them in your own database environment to see how syntax varies across SQL dialects (e.g., LIMIT vs. TOP). - **Skip the preface and early ethics discussion** if you're time-pressed; they're context-setting but not actionable. Jump straight to Chapter 2 for data preparation techniques. 【Coverage Limits】 This guide synthesizes the book's core themes—data prep, time series, cohorts, text analysis, and A/B testing—but does not cover every code example or the final chapters' detailed case studies. For exhaustive syntax and edge cases, refer to the book directly.
Page 7
146 Defining Cohorts from Dates Other Than the First Date 151 Related Cohort Analyses 153 Survivorship 154 Returnship, or Repeat Purchase Behavior 158 Cumula...
View in text
Page 19
llenges. Communicating results is a skill. Learning to make good recommendations and becoming a trusted partner to an organization take time. In my experienc...
View in text
Excerpt 3
eturns the remainder when one integer is divided by another. If the ID field is an integer, mod can be used to find the last one, two, or more digits and fil...
View in text
Excerpt 4
propriate. Empty string can also be used as a default value instead of null, or as a way to overcome a NOT NULL constraint by inserting a value, even if empt...
View in text
Excerpt 5
l databases 70 | Chapter 3: Time Series Analysis Figure 3-2. Trend of monthly retail and food services sales This data clearly has some patterns, but it also...
View in text
Excerpt 6
JOIN. In this example, the two table aliases are JOINed on the year of the sales_month to ensure that the aggregated values are for the same year, resetting...
View in text
Excerpt 7
e end date relative to the next start date—can be used when no end date is present and we have a reasonable expectation that most customers or users will sta...
View in text
Excerpt 8
aa JOIN ( SELECT generate_series as period FROM generate_series(0,20,1) ) cc on 1 = 1 ) aaa LEFT JOIN ( SELECT d.first_state, g.gender ,coalesce(date_part('y...
View in text
Tags
AI categories
DataDatabaseSQL
ISBN: 1492088781
Publisher: O'Reilly Media
Publish Year: 2021
Language: English
Pages: 342
File Format: PDF
File Size: 10.7 MB
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Generating text preview…