SQL Basics in Snowflake โ SELECT, FILTER, GROUP BY
A story-based introduction for modern Snowflake users
Imagine Snowflake as a giant data library.
Inside are millions of books (tables), shelves (schemas), and rooms (databases).
SQL is your magic spell that helps you quickly find, filter, and summarize the exact information you need.
Letโs explore the essentials using simple, real-world examples.
๐ 1. SELECT โ Choosing Your Columnsโ
SELECT is like saying:
"I want to read these specific columns from my table."
Syntaxโ
SELECT column1, column2
FROM schema.table;
Exampleโ
SELECT first_name, last_name, email
FROM SALES_DB.CUSTOMERS;
Story Example: Youโre a store manager looking at just customer names and emails instead of the whole customer file. Thatโs SELECT.
๐ 2. WHERE โ Filtering Rowsโ
WHERE lets you filter only the rows you want.
Syntaxโ
SELECT column1, column2
FROM schema.table
WHERE condition;
Exampleโ
SELECT first_name, last_name, total_purchase
FROM SALES_DB.CUSTOMERS
WHERE total_purchase > 500;
Story Example: You want VIP customers only. WHERE is like a filter in your workflow โ showing only the important people.
๐ฏ 3. FILTER โ Using Conditional Logicโ
FILTER works mostly with aggregates to refine calculations.
Exampleโ
SELECT country, COUNT(*) AS total_customers
FROM SALES_DB.CUSTOMERS
GROUP BY country
HAVING COUNT(*) > 50;
HAVINGis like a filter after grouping.- Only countries with more than 50 customers are displayed.
Story Example: Youโre summarizing sales per country but only want to show active markets, ignoring tiny countries.
๐งฎ 4. GROUP BY โ Summarizing Dataโ
GROUP BY groups rows that have the same value in one or more columns.
Syntaxโ
SELECT column1, AGG_FUNCTION(column2)
FROM schema.table
GROUP BY column1;
Exampleโ
SELECT country, SUM(total_purchase) AS total_sales
FROM SALES_DB.CUSTOMERS
GROUP BY country;
Story Example: Think of it like counting total sales per country. GROUP BY helps you summarize big data into meaningful insights.
โก 5. Combining SELECT, WHERE, GROUP BYโ
You can combine all three for powerful insights.
Exampleโ
SELECT country, COUNT(*) AS vip_customers, SUM(total_purchase) AS total_sales
FROM SALES_DB.CUSTOMERS
WHERE total_purchase > 500
GROUP BY country
ORDER BY total_sales DESC;
Explanation:
- WHERE โ filters VIP customers
- GROUP BY โ groups them by country
- SELECT โ chooses the columns to show
- ORDER BY โ ranks countries by total sales
Story Example: Youโre a regional manager checking where your VIP customers are spending the most. Done in one simple query.
๐ 6. Tips for SQL in Snowflakeโ
-
Always start small: Use
LIMIT 10while testing queries. -
Use aliases: Makes results easy to read.
SELECT country AS region, SUM(total_purchase) AS revenueFROM SALES_DB.CUSTOMERSGROUP BY country; -
Use CTEs for clarity:
WITHclauses make complex queries readable. -
Use consistent naming: Snowflake is case-insensitive by default, but best to be consistent.
-
Preview before aggregating:
SELECT * FROM table LIMIT 5can save compute and time.
๐ฏ 7. One-Sentence Summaryโ
SELECT chooses your columns, WHERE filters rows, GROUP BY summarizes, and FILTER/HAVING refines aggregates โ these are the building blocks of SQL in Snowflake.
๐ Next Step
Internal Stages, Table Stages, User Stages โ Deep Explanation