Querying Snowflake with SQL
This guide will walk you through the basics of querying data in Snowflake using SQL. Even if you’re new to SQL, these steps will help you retrieve, filter, and explore the data you have access to in your Snowflake Reader Account.
What is SQL?
SQL (Structured Query Language) is a language used to interact with databases. In Snowflake, SQL allows you to select data, filter results, and combine tables to get the information you need.
Basic Query Structure
A simple SQL query follows this structure:
SELECT [columns]
FROM [table_name]
WHERE [conditions];
Let’s break this down:
1.SELECT - Specifies which columns (pieces of data) you want to retrieve.
2.FROM - Identifies the table that contains the data.
3.WHERE - (Optional) Allows you to add conditions to narrow down your results.
Example 1: Selecting All Data from a Table
To view all data from a table, you can use the * symbol, which means “all columns.”
SELECT *
FROM sample_table;
This query will display every column and row in sample_table.
⚠️ For large tables, this might return a lot of data and take a long time, so be cautious when using * with big tables. You can identify table size by mousing over the table name in the databases sidebar.
If you are just exploring a data set, use LIMIT to return only a few rows.
SELECT *
FROM sample_table
LIMIT 10;
This query will display 10 rows of data from sample_table and include every column. This is helpful when getting to know the data in a table.
Example 2: Selecting Specific Columns
If you’re interested in specific columns, list them in the SELECT part of the query:
SELECT column1, column2
FROM sample_table;
For example, if you want to see only the customer_name and order_date columns:
SELECT customer_name, order_date
FROM orders_table;
Example 3: Filtering Data with WHERE
To get only specific rows, add a WHERE clause with conditions.
SELECT column1, column2
FROM sample_table
WHERE column1 = 'some_value';
For instance, if you want only the orders placed by “Jane Doe”:
SELECT customer_name, order_date
FROM orders_table
WHERE customer_name = 'Jane Doe';
Example 4: Adding More Conditions
You can use AND and OR to add multiple conditions.
SELECT customer_name, order_date
FROM orders_table
WHERE customer_name = 'Jane Doe' AND order_date > '2023-01-01';
This query will show orders by “Jane Doe” after January 1, 2023.
Example 5: Sorting Results with ORDER BY
To sort your results, add an ORDER BY clause.
SELECT customer_name, order_date
FROM orders_table
WHERE customer_name = 'Jane Doe'
ORDER BY order_date DESC;
This query lists “Jane Doe’s” orders, with the latest orders appearing first (DESC stands for descending order).
Putting It All Together
Here’s a query that selects specific columns, filters by conditions, and sorts the results:
SELECT customer_name, order_date, total_amount
FROM orders_table
WHERE total_amount > 100
ORDER BY order_date DESC;
This example shows orders where the total amount is over $100, sorted by the latest order dates first.
Common Tips and Troubleshooting
•Table and Column Names: Table and column names are usually found in the Data Explorer section of Snowflake or can be provided by your team. If you’re unsure, ask a colleague for the exact names.
•Error Messages: If you see an error, double-check spelling and punctuation; SQL is sensitive to exact spelling and structure.
•Limits: For large tables, consider adding a LIMIT clause (e.g., LIMIT 10) to restrict the number of rows returned, which helps keep things manageable.
Need Help?
If you’re unsure of which tables to use or need further guidance, feel free to reach out to your team or a colleague familiar with Snowflake. iSeatz data specialists are also available for custom trainings for your organization. Contact a colleague who manages the iSeatz relationship to discuss training options.
Happy querying!