Advanced SQL Capabilities in Snowflake for Data Analysis

Photo by Caspar Camille Rubin on Unsplash

SQL, or Structured Query Language, is a standard language for managing data held in relational database management systems. It is particularly useful for handling structured data, i.e., data incorporating relations among entities and variables.

Snowflake, a cloud-based data warehousing platform, supports a wide range of advanced SQL capabilities, making it a powerful tool for data analysis.

Advanced SQL Capabilities

Advanced SQL operations refer to queries that are more intricate and require more thought to design and write. They often involve advanced techniques and features such as:

  • Subqueries: These are queries nested within another SQL query. They can return data that will be used in the main query as a condition to further restrict the data to be retrieved.
  • Joining to many tables: This involves combining rows from two or more tables based on a related column between them.
  • Using different join types: There are several types of joins in SQL, including INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN. Each type of join returns a different subset of the combined data.
  • Functions within functions: This refers to using SQL functions inside other functions to perform complex calculations or transformations on the data.
  • Group by and having: The GROUP BY statement groups rows that have the same values in specified columns into aggregated data. The HAVING clause is used instead of WHERE clause with SQL COUNT(), SUM(), AVG(), MAX() or MIN() functions.

New Horizon: Clustering Support for Dynamic Tables: The Public Preview of Clustering Support for Dynamic Tables is a notable addition. It complements dynamic tables, streamlining complex data pipelines. This feature automatically clusters data, facilitating faster query execution by enabling effective pruning, reducing scanned data, and leading to quicker SQL queries and reduced compute costs.

Diving Deeper into Snowflake’s SQL Prowess: Snowflake’s SQL prowess is extensive: Materialized Views: To accelerate query performance through pre-aggregated data.

  • Time Travel: For querying historical data states and safeguarding against loss.
  • Snowpipe: For real-time data ingestion, enhancing the speed of data availability.
  • Task Scheduling: Automates SQL statements, orchestrating intricate data pipelines and routine maintenance tasks efficiently.
  • Data Sharing: Offers real-time secure data sharing capabilities across Snowflake accounts.

Advanced SQL techniques for complex queries include features such as Common Table Expressions (CTEs) and Window Functions. A CTE is a named temporary result set that you can reference within a subsequent SELECT, INSERT, UPDATE, or DELETE statement. Window Functions allow you to perform calculations on a set of rows related to the current row.

Use Cases of Advanced SQL Capabilities

Advanced SQL capabilities and complex operations are widely used in various scenarios for data analysis. Here are some use cases:

  1. Complex Data Manipulation: Techniques like pivoting and unpivoting allow you to reshape data between row-based and column-based formats for better analysis.
  2. Automation and Reusability: Concepts like stored procedures and triggers let you automate repetitive tasks and promote code reusability.
  3. Sales Data Analysis: For instance, you can use window functions to create running totals of sales data over time. This can help businesses identify sales trends and patterns, which can inform their operations and strategies.
  4. Customer Data Analysis: You can use Common Table Expressions (CTEs) to divide a challenging query into smaller, easier-to-handle parts. For example, determining the average age of customers who have bought a particular product.
  5. Data Retrieval: Advanced SQL functions like ROW_NUMBER, RANK, DENSE_RANK, LEAD, and LAG are used for tasks like row numbering, ranking results within a partition, and retrieving data from preceding or following rows in the same result set.

Snowflake and Advanced SQL

Snowflake supports most of these advanced SQL capabilities and complex operations:

  • Standard and Extended SQL Support: Snowflake supports most DDL defined in SQL:1999, including databases, schemas, tables, and related objects, core data types, SET operations, and CAST functions. It also supports advanced DML like multi-table INSERT, MERGE, and multi-merge, DML for bulk data loading/unloading, transactions, temporary and transient tables for transitory data, lateral views, materialized views, statistical aggregate functions, and analytical aggregates.
  • Windowing Functions and Grouping Sets: Snowflake supports parts of the SQL:2003 analytic extensions, including windowing functions and grouping sets.
  • User-Defined Functions (UDFs): Snowflake supports scalar and tabular user-defined functions (UDFs), with support for Java, JavaScript, Python, Scala, and SQL.
  • Stored Procedures and Procedural Language Support: Snowflake supports stored procedures and procedural language support (Snowflake Scripting).
  • Recursive Queries: Snowflake supports recursive queries, including CONNECT BY and Recursive CTE (common table expressions).
  • Geospatial Data Support: Snowflake supports geospatial data, which can be used for location-based analysis.
  • Advanced Analytical Functions: Snowflake utilizes a dialect of SQL that closely adheres to ANSI SQL, making it familiar to most database professionals. Its SQL capabilities include robust support for JSON and semi-structured data, advanced analytical functions, and efficient data warehousing operations.
  • Advanced Query Optimization: Snowflake’s cloud data platform offers advanced query optimization capabilities, essential for handling large datasets efficiently.

Example: Geospatial Data Analysis in Snowflake

Here’s an example of how you can use Snowflake for geospatial data analysis. This example demonstrates how to visualize all Starbucks locations in the U.S. using data from SafeGraph’s Core Places data set, which is available through Snowflake Data Marketplace.

First, you can create a point for each Starbucks location with the following SQL command:

SELECT ST_MAKEPOINT(LONGITUDE, LATITUDE) as geom

FROM CDA26920_STARBUCKS_CORE_PLACES_SAMPLE.PUBLIC.CORE_POI

This will return a geometry column (geom) that represents the location of each Starbucks store.

Next, you can compute an aggregation of these locations using quadkeys at resolution 15, which lets you visualize the result as a heatmap. Here’s the SQL command for that:

with qks as

(

SELECT sfcarto.quadkey.LONGLAT_ASQUADINT(LONGITUDE, LATITUDE, 15) as qk

FROM CDA26920_STARBUCKS_CORE_PLACES_SAMPLE.PUBLIC.CORE_POI

)

SELECT count(*) as num_stores, sfcarto.quadkey.ST_BOUNDARY(qk) as geom

from qks

GROUP BY qk

This will return a count of Starbucks stores (num_stores) and a geometry column (geom) that represents the boundary of each quadkey.

Finally, you can enhance your analysis by computing a 3 km buffer around each store with the following SQL command:

SELECT sfcarto.constructors.ST_MAKEELLIPSE(ST_POINT(LONGITUDE,LATITUDE),3,3,0,’kilometers’,12) as geom

FROM CDA26920_STARBUCKS_CORE_PLACES_SAMPLE.PUBLIC.CORE_POI

This will return a geometry column (geom) that represents a 3 km buffer around each Starbucks store.

These features make Snowflake a powerful tool for data analysis, allowing users to perform complex operations and derive valuable insights from their data.

Case Study 2:

HP Employee Retention Solution

Industry Background:

HP, a leading technology company, operates a large call center handling customer inquiries and support. However, they faced a persistent challenge in retaining employees, leading to high turnover rates.

Challenge:

Despite hiring over 200 new employees annually, HP struggled to maintain a stable headcount in their call center. This revolving door phenomenon resulted in significant time and revenue losses for the company.

Solution:

To address this challenge, HP partnered with a consulting firm to develop a tailored hiring plan. The key component of this plan was the deployment of an employee retention specialist, provided at the consulting firm's expense. The specialist was tasked with managing attendance, performance, and engagement of call center employees.

Outcome:

Implementing the hiring plan resulted in substantial cost savings for HP, exceeding $200k annually. Moreover, the improved employee retention positively impacted productivity and customer satisfaction. As a testament to the success of the project, HP began offering quarterly bonuses for effectively managing the call center's workforce.

Case Study 1:

HP Employee Retention Solution

Industry Background:

HP, a leading technology company, operates a large call center handling customer inquiries and support. However, they faced a persistent challenge in retaining employees, leading to high turnover rates.

Challenge:

Despite hiring over 200 new employees annually, HP struggled to maintain a stable headcount in their call center. This revolving door phenomenon resulted in significant time and revenue losses for the company.

Solution:

To address this challenge, HP partnered with a consulting firm to develop a tailored hiring plan. The key component of this plan was the deployment of an employee retention specialist, provided at the consulting firm's expense. The specialist was tasked with managing attendance, performance, and engagement of call center employees.

Outcome:

Implementing the hiring plan resulted in substantial cost savings for HP, exceeding $200k annually. Moreover, the improved employee retention positively impacted productivity and customer satisfaction. As a testament to the success of the project, HP began offering quarterly bonuses for effectively managing the call center's workforce.