What you will be able to do
- Set, use, list and drop SQL session variables, and explain how long they last
- Set and inspect the session's context: role, warehouse, database and schema
- Distinguish account, session and object parameters and the levels where each can be set
- Work out which value of a parameter wins when it is set at several levels, and confirm it with SHOW PARAMETERS
1.SQL variables live and die with the session
A Snowflake session keeps its own state. The simplest part of that state is a set of SQL variables that you define yourself. You create them with SET, remove them with UNSET, and list them with SHOW VARIABLES. You can also pass them in the connection string when you connect, which helps with tools where the connection string is the only thing you can customise. A variable's data type comes from whatever expression you assign to it. String and binary variables are limited to 16KB.
When you reference a variable, prefix it with $. That's how Snowflake tells it apart from bind values and column names. You can use a variable anywhere a literal constant is allowed.
SET (min, max)=(40, 70);
SELECT $min;
SELECT AVG(salary) FROM emp WHERE age BETWEEN $min AND $max;A variable can also hold the name of an object, such as a table. In that case $my_table_name on its own won't work in DDL. You have to wrap it in IDENTIFIER(). In a FROM clause, TABLE($my_table_name) also works.
Variables are private to the session. Nobody can read a variable that was set in another session, and closing the session drops every variable created in it. Variable names are case-insensitive in SQL, but Snowflake stores them in uppercase. That matters for the convenience functions GETVARIABLE, SESSION_CONTEXT and SYS_CONTEXT, which take and return values as strings. GETVARIABLE('var_artist_name') returns NULL, while GETVARIABLE('VAR_ARTIST_NAME') returns the value.
Checkpoint 1 of 6· Exam question
A Snowflake administrator wants to create a new resource monitor to cap credit spend across every warehouse in the account. At which level of the object hierarchy must this resource monitor be created, and why?
Correct answer: B — At the account level, because resource monitors are account objects not contained in any database or schema, and can be attached to the account or to individual warehouses.
- A. Resource monitors are not schema-level objects and do not inherit schema privileges; they sit at the account level, outside any database or schema container, which is why this description is incorrect.
- B. Resource monitors are account-level objects, created with ACCOUNTADMIN privileges and then optionally attached to the account as a whole or to one or more specific warehouses, which is exactly what an account-wide credit cap requires.
- C. Resource monitors do not live inside a database and are not tied to where ACCOUNT_USAGE views are queryable; ACCOUNT_USAGE is a shared schema in the SNOWFLAKE database and has no bearing on where a monitor is created.
- D. Warehouses do not each own a private resource monitor object; a single resource monitor is an account-level object that can be assigned to multiple warehouses at once, the opposite of a per-warehouse private configuration.
Checkpoint 2 of 6· Fill the gap
my_table_name holds the string 'table1'. Which keyword lets the variable be used as the table name?
CREATE TABLE ? ($my_table_name) (i INTEGER);To use a variable as an object name, you wrap it in IDENTIFIER(). GETVARIABLE and the context functions return the value as a string, not as an identifier.
Source: docs.snowflake.comSources1
2.Session context: role, warehouse, database, schema
Besides your own variables, every session has a context: the active role, warehouse, database and schema. Snowflake groups the commands that set these with its account and session DDL:
- USE ROLE sets the primary role, and USE SECONDARY ROLES sets secondary roles;
- USE WAREHOUSE sets the virtual warehouse for the session;
- USE DATABASE and USE SCHEMA set where unqualified names resolve. The schema must be in the session's current database.
To read the context back, Snowflake's reference points you to context functions. You call them in a plain SELECT, with no need to query any view. The SQL variables page even shows context functions being captured into variables:
SET (current_user, current_warehouse) = ((SELECT CURRENT_USER()), (SELECT CURRENT_WAREHOUSE()));The organization has a context function too: any role can run CURRENT_ORGANIZATION_NAME to find out which organization the current account belongs to. Context matters because it decides what a statement does. The active role sets which privileges apply. The current database and schema decide how a short name like orders resolves to a full database.schema.object name.
Checkpoint 3 of 6· Check yourself
A session runs USE SCHEMA analytics; and gets an error, even though a schema named ANALYTICS exists in another database. What's the most likely cause?
USE SCHEMA with a bare schema name resolves inside the session's current database. A schema in another database needs a USE DATABASE first, or a fully-qualified name.
“Specifies the schema to use in the session (specified schema must be in the current database for the session).”Source: docs.snowflake.com
3.Three parameter types and where each can be set
Variables are your own. Parameters are Snowflake's settings, and every parameter has a default value. There are three types, and each can be set at different levels.
- Account parameters apply to the whole account. You can set them *only* at the account level, with ALTER ACCOUNT, using a role that has the necessary privilege.
- Session parameters make up most parameters. Their hierarchy is Account » User » Session. An account administrator sets an account-wide default with ALTER ACCOUNT. An administrator (typically with SECURITYADMIN) or the user themselves can override it for one user with ALTER USER, and that becomes the default for every session the user starts. Any user can then override it again for the current session with ALTER SESSION.
- Object parameters default to objects such as warehouses, databases, schemas and tables. Setting one at the account level makes it the default for objects created in the account. A user with the right privileges can then override it on an individual object with CREATE <object> or ALTER <object>.
| Parameter | Object type | Note |
|---|---|---|
| DATA_RETENTION_TIME_IN_DAYS | Database, Schema, Table | Settable on each of the three containers |
| STATEMENT_TIMEOUT_IN_SECONDS | Warehouse | Also a session parameter (object and session levels) |
| MAX_CONCURRENCY_LEVEL | Warehouse | Object parameter on a warehouse |
| PIPE_EXECUTION_PAUSED | Schema, Pipe | Pauses pipes at schema or pipe level |
| NETWORK_POLICY | User | User-level value overrides the account-level value |
Checkpoint 4 of 6· Match them up
Match each parameter type to the levels where it can be set
Tap a term, then the definition that fits it.
Account parameters have one level only. Session parameters cascade account → user → session. Object parameters cascade from the account default to the individual object.
“Session — Can be set for Account » User » Session”Source: docs.snowflake.com
4.Precedence: the most specific level wins, and how to prove it
Precedence follows the hierarchy: the most specific level that has a value wins. For session parameters, a session setting beats a user setting, and a user setting beats an account setting. If nothing is set at any level, the Snowflake default applies. Object parameters work the same way: a value set on the object with CREATE or ALTER overrides the account-level default. NETWORK_POLICY is a clear example: when it's set on both the account and a user, the user-level policy overrides the account-level one. STATEMENT_TIMEOUT_IN_SECONDS is worth remembering because it's both a warehouse object parameter and a session parameter.
To see which value is actually in effect, use SHOW PARAMETERS. With no clause it shows only session parameters. Add IN <object> to see one object's parameters, or IN ACCOUNT to see all of them. If you filter by name with LIKE, it must come before IN.
SHOW PARAMETERS IN DATABASE mydb;
SHOW PARAMETERS IN WAREHOUSE mywh;The output has a level column that shows where the current value comes from, for example ACCOUNT. If level is empty, nobody has set the parameter and you're seeing the default. To return an account-level setting to its default, run ALTER ACCOUNT UNSET <param>.
Checkpoint 5 of 6· Exam question
A data engineering team needs a reusable location inside Snowflake to stage CSV files before running COPY INTO on multiple tables, without tying the location to any single table or user. Which object should they create?
Correct answer: C — A named internal stage, because it is an independent database object that can hold files for loading into any table, and is referenced by name in COPY INTO.
- A. A user stage is tied to one specific user, is not shareable with teammates, and cannot be referenced by other users' COPY INTO statements, which rules it out for a team-wide reusable location.
- B. A table stage is implicitly bound to its own table and can only be used to load data into that same table, so it cannot serve as a shared staging area for multiple different target tables.
- C. A named internal stage is a standalone database object independent of any specific table or user, and it is exactly the object type COPY INTO statements reference by name when loading data into any table, matching this reusable, team-shared requirement.
- D. A file format object only defines how to parse file contents, such as delimiters and header handling; it does not define or hold a storage location, so it cannot replace a stage.
Checkpoint 6 of 6· Check yourself
The account has STATEMENT_TIMEOUT_IN_SECONDS = 3600. A user's profile doesn't set it, but in one session they run ALTER SESSION SET STATEMENT_TIMEOUT_IN_SECONDS = 300. What does SHOW PARAMETERS LIKE 'STATEMENT_TIMEOUT%' report as the level in that session?
ALTER SESSION overrides the account default for the current session, so the effective value is 300 and it comes from the session level.
“Users can run the ALTER SESSION command to override session parameters for the current session.”Source: docs.snowflake.com
Exam traps
Each one states something that sounds right. Open it to see what is actually true.
1.A variable set with SET in one worksheet session can be read by other sessions or users, or survives after the session closes.Why is that wrong?
SQL variables are private to the session that created them and are dropped when that session closes.
Covered in SQL variables live and die with the session
2.GETVARIABLE('my_var') finds a variable created with SET my_var = ..., because variable names are case-insensitive.Why is that wrong?
$references are case-insensitive, but Snowflake stores variable names in uppercase, so GETVARIABLE needs the uppercase name.Covered in SQL variables live and die with the session
3.Any parameter can be overridden with ALTER SESSION.Why is that wrong?
Only session parameters cascade down to the session. Account parameters can be set only at the account level.
Practise it for real
Create, use, inspect and drop session variables, then check where a parameter's value comes from
1.Run
SET (min, max)=(40, 70);and thenSELECT $min;Why: This shows that variables are referenced with the $ prefix wherever a literal is allowed
You should see: The SELECT returns 40
2.Run
SHOW VARIABLES;Why: This lists every variable defined in the current session
You should see: Two rows, named MAX and MIN (stored in uppercase), with values 70 and 40
3.Run
SHOW PARAMETERS LIKE 'time%' IN ACCOUNT;Why: IN ACCOUNT includes account and object parameters, and LIKE must come before IN
You should see: Parameters whose names start with TIME, with value, default and level columns
4.Run
UNSET min;and thenSHOW VARIABLES;againWhy: UNSET drops a variable explicitly, without waiting for the session to end
You should see: Only MAX remains in the list
Stuck? Get a nudge
If the level column is empty for a parameter, nothing has set it at any level and you're seeing Snowflake's default.
Sources
Every claim above is drawn from one of these pages, quoted as it was written on the date shown.
- 1.
“To distinguish them from bind values and column names, all variables must be prefixed with a $ sign.”
↩︎ SQL variables live and die with the session“When a Snowflake session is closed, all variables created during the session are dropped.”
↩︎ SQL variables live and die with the session“The size of string or binary variables is limited to 16KB.”
↩︎ SQL variables live and die with the session“To use a variable as an identifier, you must wrap it inside IDENTIFIER()”
↩︎ SQL variables live and die with the session“All of these functions accept and return session variable values as strings”
↩︎ SQL variables live and die with the session“SQL variables are private to a session.”
↩︎ Exam trap 1“In this example, the output is NULL because Snowflake stores variables with all uppercase letters.”
↩︎ Exam trap 2 - 2.
“Using a role, warehouse, database, or schema within a session.”
↩︎ Session context: role, warehouse, database, schema“Specifies the primary role to use in the session.”
↩︎ Session context: role, warehouse, database, schema“Specifies the schema to use in the session (specified schema must be in the current database for the session).”
↩︎ Checkpoint - 3.
“Users with any role can execute the CURRENT_ORGANIZATION_NAME function to return the organization of the current account.”
↩︎ Session context: role, warehouse, database, schema - 4.
“Most parameters are session parameters, which you can set at the following levels:”
↩︎ Three parameter types and where each can be set“The values that you set for a user become the default values in any session started by that user.”
↩︎ Three parameter types and where each can be set“Users with the appropriate privileges can run the CREATE <object> or ALTER <object> commands to override object parameters for an individual object.”
↩︎ Three parameter types and where each can be set“If this parameter is set on the account and a user in the same account, the user-level network policy overrides the account-level network policy.”
↩︎ Precedence: the most specific level wins, and how to prove it“You must specify the LIKE clause before the IN clause.”
↩︎ Precedence: the most specific level wins, and how to prove it“You can only set account parameters at the account level”
↩︎ Exam trap 3“Session — Can be set for Account » User » Session”
↩︎ Checkpoint“Users can run the ALTER SESSION command to override session parameters for the current session.”
↩︎ Checkpoint - 5.
“Object parameters that default to objects (warehouses, databases, schemas, and tables).”
↩︎ Three parameter types and where each can be set“If the level column is empty for a parameter, the parameter is not explicitly set and the current value is the default value.”
↩︎ Precedence: the most specific level wins, and how to prove it“this date display format is only the default and can be overridden for any individual user or session.”
↩︎ Prediction