Skip to Content
Learning Snowflake SQL and Scripting
book

Learning Snowflake SQL and Scripting

by Alan Beaulieu
October 2023
Beginner
398 pages
9h 36m
English
O'Reilly Media, Inc.
Content preview from Learning Snowflake SQL and Scripting

Chapter 7. Grouping and Aggregates

Databases must store data at the lowest level of granularity needed for any particular operation. For example, the Snowflake sample database stores data about customer orders, which is needed for business operations such as billing and inventory management. However, you can opt to work with the data at higher levels, such as yearly sales per region, which would be helpful for other parts of the business such as marketing and sales. This chapter discusses the ways that data in a Snowflake database can be grouped together to meet various types of business needs.

Grouping Concepts

Let’s say you were asked by the vice president of the marketing department to help with a special promotion to reward the best customers with 25% off their next order. This promotion will be for customers who have spent $1,800,000 or more or who have placed eight or more orders. Your job is to determine which customers will be eligible for the promotion.

The sample database has 115,269 orders, so you certainly don’t want to read through all the data at that level of granularity. What you need to do is group the orders together by customer, then count the number of orders, and sum the total price of the orders. This can be achieved using the group by clause, which is used to group rows together using common values of one or more columns. Here’s the first step in generating the data for the VP of marketing:

PUBLIC>select o_custkey
       from orders
       group by o_custkey; ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Advanced SQL

Advanced SQL

Rui Machado, Helder Russa, Pedro Esmeriz
Practical SQL

Practical SQL

Anthony DeBarros

Publisher Resources

ISBN: 9781098140311Errata Page