SnowSql Commands Flashcards
How do you set your context in Snowflake to a specific role, database, and schema?
To set your context in Snowflake to a specific role, database, and schema, you can use the following SQL commands:
* Set a Specific Role: USE ROLE FOUND_ROLE;
* Set a Specific Database: USE DATABASE SNOWBEARITR_DB;
* Set a Specific Schema: USE SCHEMA MODELED;
How do you create a virtual warehouse in Snowflake?
CREATE OR REPLACE WAREHOUSE FOUND_LEARNER_wh WITH
WAREHOUSE_SIZE = XSmall
INITIALLY_SUSPENDED = true;
This SQL command performs the following actions:
* CREATE OR REPLACE WAREHOUSE: This part of the statement ensures that if a warehouse named FOUND_LEARN_WH already exists, it will be replaced. If it doesn’t exist, a new one will be created.
* WAREHOUSE_SIZE = ‘XSmall’: Sets the size of the warehouse to ‘XSmall’, which determines the compute resources allocated to the warehouse.
* INITIALLY_SUSPENDED = true: Specifies that the warehouse should be created in a suspended state. This means it won’t start consuming credits until you explicitly start it.
This command creates a virtual warehouse named FOUND_LEARNER_wh with a size of XSmall and sets it to be initially suspended.
Changing Roles in Snowflake
How do you change your role in Snowflake?
How do you revert to a previously assigned role in Snowflake?
How do you select a virtual warehouse to use for your Snowflake session?
To change your role and select a virtual warehouse for your Snowflake session, you can use the following SQL commands:
1. Change Your Role: USE ROLE {role_name};
2. Replace {role_name} with the name of the role you want to switch to: USE ROLE ANALYST;
3. Select a Virtual Warehouse: USE WAREHOUSE {warehouseName};
4. Replace {warehouseName} with the name of the warehouse you want to use: USE WAREHOUSE COMPUTE_WH;
These commands are key for navigating and manipulating roles and warehouses in Snowflake, ensuring you can manage your resources and permissions effectively.
Showing Tables in Your Current Context
How can you display the tables available in your current Snowflake context?
How do you display tables in your current Snowflake context and what does this command not utilize?
This command lists all the tables that are accessible within the current database and schema that your session is set to. Here’s a breakdown of how it works: SHOW TABLES;
* This command is a metadata operation, meaning it retrieves information about table structures without needing to perform any data processing tasks.
* As such, it does not require the use of your virtual warehouse’s compute resources to execute. Therefore, it will not consume any compute credits, making it cost-effective for administrative or exploratory tasks.
This is crucial for managing and exploring database objects without incurring extra compute costs.
Basic SELECT Query
How do you perform a basic SELECT query on a table in Snowflake?
To query a table in Snowflake, use the SELECT statement followed by the columns you want to retrieve or an asterisk (*) for all columns, then specify the table. For example:
SELECT * FROM members LIMIT 10;
This command retrieves the first 10 rows from the members table.
Specifying a Full Path in Queries
When do you need to specify a full path in a SELECT query in Snowflake?
You need to provide the full path (database.schema.table) in your SELECT statement when the table you want to query is not in the default schema of your current context. For example:
SELECT TOP 5 c.c_lastname, c.c_firstname, c.c_acctbal
FROM snowbearair_db.promo_catalog_sales.customer c
ORDER BY c.c_acctbal DESC;
This query specifies the full path to the customer table, which is in a different schema than the current context.
JOIN Operation in Queries
How do you perform a JOIN operation between tables in Snowflake?
To join tables in Snowflake, use the JOIN keyword and specify the condition for the join. For example:
SELECT c_firstname, c_lastname, o_orderkey, o_totalprice
FROM promo_catalog_sales.orders
JOIN promo_catalog_sales.customer ON o_custkey = c_custkey
ORDER BY o_totalprice DESC LIMIT 10;
This query joins the orders and customer tables on a common key and orders the results by o_totalprice.
Creating a Database
How do you create a database in Snowflake?
To create a database in Snowflake, use the CREATE OR REPLACE DATABASE statement:
CREATE OR REPLACE DATABASE FOUND_LEARNER_db;
This command creates a new database named FOUND_LEARNER_db or replaces it if it already exists.
Creating a Schema
How do you create a schema in Snowflake?
To create a schema within a database in Snowflake, execute:
CREATE OR REPLACE SCHEMA FOUND_LEARNER_db.my_schema;
This creates a schema named my_schema within the FOUND_LEARNER_db database.
Creating a Temporary Table
How do you create a temporary table in Snowflake?
To create a temporary table in Snowflake, use:
CREATE OR REPLACE TEMPORARY TABLE my_favorite_actors (name VARCHAR);
This creates a temporary table named my_favorite_actors that will exist for the duration of the session.
Inserting Data into a Table
How do you insert data into a table in Snowflake?
To insert data into a table, use the INSERT INTO statement:
INSERT INTO my_favorite_actors VALUES
(‘Heath Ledger’),
(‘Michelle Pfeiffer’),
(‘Meryl Streep’),
(‘Anthony Hopkins’),
(‘Bruce Lee’);
This adds a list of actor names into the my_favorite_actors table.
Suspending a Virtual Warehouse
How do you suspend a virtual warehouse in Snowflake?
To suspend a virtual warehouse, use the ALTER WAREHOUSE command followed by the SUSPEND keyword:
ALTER WAREHOUSE FOUND_LEARNER_wh SUSPEND;
This will stop the virtual warehouse, ceasing its compute resources and saving credits. The dot next to your virtual warehouse name will turn gray, indicating it is no longer running.
Setting User Defaults
How do you set user defaults for your role, warehouse, and namespace in Snowflake?
To set your default role, warehouse, and namespace, use the ALTER USER command:
ALTER USER FOUND_LEARNER SET DEFAULT_ROLE = FOUND_ROLE,
DEFAULT_WAREHOUSE = FOUND_LEARNER_wh,
DEFAULT_NAMESPACE = FOUND_LEARNER_db.my_schema;
These settings ensure that every time you start a new session or open a new worksheet, Snowflake will automatically use these defaults, streamlining your workflow.
Generic SQL Statement for UNDROP
If you accidentally drop a table named customer_data, you can recover it using the following statement, provided you’re within the Time Travel retention period:
UNDROP TABLE customer_data;
Generic SQL Statement for CLONE with Time Travel
Research further on this
- To create a clone of a table as it existed at a specific point in the past, you might use.
- Alternatively, to clone an object from just before a specific change:
CREATE OR REPLACE TABLE customer_data_clone
CLONE customer_data
AT (OFFSET => -3600); – Clones the table as it was an hour ago
=========================================================================================
CREATE OR REPLACE TABLE customer_data_clone
CLONE customer_data
BEFORE (STATEMENT => ‘query_id’); – Replace ‘query_id’ with the ID of the query that modified the table
Generic SQL Statement for SELECT with Time Travel
- To query historical data from a specific time in the past, you might use.
- Or to query data as it was before a specific query ran.
SELECT *
FROM customer_data
AT (TIMESTAMP => ‘2024-04-12 08:30:00.000’::timestamp); – Retrieves data as it was on April 12, 2024, at 8:30 AM
=====================================================================================
SELECT *
FROM customer_data
BEFORE (STATEMENT => ‘query_id’); – Replace ‘query_id’ with the ID of the query you want to travel back to before it was executed
Restoring Objects with UNDROP
- How can you restore dropped objects using Snowflake’s Time Travel?
- What types of objects can be restored using the UNDROP command?
Understand the capabilities and limitations of the UNDROP command.
UNDROP is a lifeline for recovering dropped database objects within the Time Travel retention period.
How to Restore Dropped Objects
* Restoring a Table: This will restore the specified table as it was just before it was dropped.
UNDROP TABLE table_name;
-
Restoring a Schema: This will restore the specified schema and all its contents, including tables and views.
UNDROP SCHEMA schema_name; -
Restoring a Database: This will restore the entire database along with all its schemas and contained objects.
UNDROP DATABASE database_name;
Types of Objects That Can Be Restored: Tables, Schemas, Databases, Limitations
- The UNDROP command must be used within the Time Travel retention period specific to your Snowflake edition.
- The command can only restore objects to their state just before they were dropped, not to any other historical state.