CertSafari
    Snowflake SnowPro Advanced: Administrator (ADA-C02)· Lessons

    Domain 3 · Lesson 15/24

    Snowflake sequences, persisted results and cancelling queries

    Perform queries in Snowflake .

    15 min read
    3% of exam
    4 sources
    Published 20 Oct 2026
    Docs as of 18 Oct 2026

    What you will be able to do

    • Explain what a Snowflake sequence guarantees (unique values) and what it does not (no gap-free or ordered values)
    • Identify sequence limitations: no CURRVAL, interval sign changes, the 64-bit range and delayed ALTER SEQUENCE effects
    • Use a sequence as a column default and predict what happens when the sequence is dropped
    • Post-process a previous statement's output with RESULT_SCAN(LAST_QUERY_ID())
    • Cancel one query, every query in a session, or every query of a user

    Key concept

    Unique, not gap-free — A Snowflake sequence promises that no two callers, even concurrent ones, ever receive the same value. It does not promise that the values are contiguous or that they arrive in the order rows were loaded.

    1.What a sequence guarantees, and what it does not

    A sequence is a schema object that hands out unique numbers across sessions and statements, including statements running at the same moment. Teams use one for surrogate primary keys, or for any column whose values must never collide. The guarantee is about uniqueness only. Concurrent queries never see the same value, and every value within a single query is distinct.

    Gaps are built into how values are handed out. Each generated value also reserves a block sized by the sequence's step, and a reserved value is never handed out while the step stays the same. Snowflake may also work out the next value as soon as the current one is used. Holes in an id column after a few concurrent loads are therefore normal behaviour. They are not a sign of rollback trouble or lost rows.

    Values reserved when a sequence generates the value 100
    Step (INCREMENT)Values reserved
    2100 and 101
    10100 to 109
    -596 to 100

    Ordering is narrower still. A new statement gets values greater than the previous statement's maximum only when two things are both true: the sequence lacks the NOORDER property, and the previous statement had completed and been acknowledged before the new one was submitted. NOORDER drops the increasing-order promise, so a START 1 INCREMENT 2 sequence might produce 1, 3, 101, 5, 103. It can improve performance when many clients insert concurrently. Overlapping loads from several sessions fail the second condition, so their ids interleave whatever the property says. The only way to assign values to rows in a chosen order is single-row statements, and even those can leave gaps.

    Checkpoint 1 of 9· Check yourself

    A sequence was created without the NOORDER property. Session 1 starts a large INSERT ... SELECT seq1.NEXTVAL load. While that load is still running, session 2 submits its own load against the same sequence. What can you rely on for the ids that session 2 receives?

    Checkpoint 2 of 9· Exam question

    An ETL team loads order rows with INSERT INTO orders (id, item) SELECT seq1.NEXTVAL, item FROM staging; from several concurrent sessions. Auditors notice that id values contain holes and do not always ascend in load order. The team asks for the MOST accurate explanation of this behavior.

    Sources1

    2.Sequence limitations: interval changes, range and no CURRVAL

    Most sequence surprises come from four limitations. Each one breaks a habit carried over from another database.

    Flipping the sign of the interval can produce duplicates. Suppose a sequence has returned 1, 2 and 3 and you change the increment from 1 to -1. The next values include 2 and 1 again, because uniqueness only holds while the sign of the step stays the same. Snowflake keeps no history of issued values to protect you.

    ALTER SEQUENCE ... SET INCREMENT may not take effect right away. Snowflake may have computed the next value already, so the very next operation can still use the old step.

    The range is finite. A sequence value is a 64-bit two's complement integer, from -2^63 to 2^63 - 1. If the next value's internal representation leaves that range, the query fails with an error and those values may be lost. Numbers never wrap around. Because of gaps, the internal value can overflow even when every returned value is in range. The fix is a smaller increment or a new sequence with a smaller start. A sequence with start 1 and increment 1 is extremely unlikely to run out.

    There is no CURRVAL. In PostgreSQL or Oracle, people often insert a parent row with NEXTVAL and then read CURRVAL to stamp child rows. A Snowflake script written that way fails. Snowflake prefers bulk statements to that single-row pattern. The same parent-child linking is done with a multi-table INSERT and sequence references in nested subqueries, or by letting a column default generate the key.

    Every occurrence of seq1.NEXTVAL in a query also produces its own values, so SELECT seq1.NEXTVAL a, seq1.NEXTVAL b FROM DUAL returns two different numbers. To give two columns the same value, generate it once in a subquery and refer to it twice.

    Generating one sequence value in a subquery and reusing it in two columnssql
    SELECT seqRef.a a, seqRef.a b FROM (SELECT seq1.NEXTVAL a FROM DUAL) seqRef;

    The GETNEXTVAL table function avoids the extra nesting. It is a one-row table function that must be aliased, and its alias exposes a NEXTVAL attribute that many columns can read. Where it sits in the FROM clause matters. Values are generated over the join of every object listed before it, and joins cannot be reordered around it. Snowflake recommends placing it at the end of the FROM clause where possible.

    Checkpoint 3 of 9· Fill the gap

    A migration from Oracle needs two columns of the same row to hold the same generated key. Which expression completes the second column?

    SELECT seqRef.a a,  ?  b FROM (SELECT seq1.NEXTVAL a FROM DUAL) seqRef;

    Checkpoint 4 of 9· Check yourself

    An engineer runs ALTER SEQUENCE seq1 SET INCREMENT = 10 on a sequence that used a step of 1. The very next insert still receives a value that is only 1 higher than the last one. What is the correct explanation?

    Checkpoint 5 of 9· Exam question

    A developer migrating from PostgreSQL writes a Snowflake script that inserts a parent row using seq_ord.NEXTVAL and then fetches the value just used with SELECT seq_ord.CURRVAL. Select TWO statements about Snowflake sequences that apply to this design.(Select 2)

    0 of 2 selected

    Sources1

    3.Using a sequence as a column default

    The simplest way to generate keys is to make seq.NEXTVAL the column's default expression. Any insert that leaves the column out, or sets it to DEFAULT, gets a fresh value. This also covers the parent-key need that CURRVAL serves elsewhere.

    A table whose key column defaults to the next sequence valuesql
    CREATE OR REPLACE TABLE foo (k NUMBER DEFAULT seq1.NEXTVAL, v NUMBER); -- insert rows with unique keys (generated by seq1) and explicit values INSERT INTO foo (v) VALUES (100); INSERT INTO foo VALUES (DEFAULT, 101);

    A default is only a fallback. INSERT INTO foo VALUES (1000, 1001) stores 1000 exactly as given, and the sequence does nothing to stop that number clashing with one it issues later. Because the sequence is a separate object, one sequence can be the default for several columns, even across tables, and they will all draw from the same unique stream.

    That independence works against you if the sequence is dropped. The table keeps its default expression, and every insert or update that relies on the default then fails with an error saying the identifier cannot be found.

    Checkpoint 6 of 9· Fill the gap

    The second INSERT names every column of foo but should still get its key from seq1. Which token goes in the blank?

    CREATE OR REPLACE TABLE foo (k NUMBER DEFAULT seq1.NEXTVAL, v NUMBER); -- insert rows with unique keys (generated by seq1) and explicit values INSERT INTO foo (v) VALUES (100); INSERT INTO foo VALUES ( ? , 101);

    Sources1

    4.Post-processing persisted results with RESULT_SCAN

    Snowflake keeps every query result for 24 hours, whatever its size. It reuses that result only when the query text matches exactly, contains no non-reusable functions such as RANDOM, and reads table data that has not changed; Result cache (persisted query results) covers the reuse rules in full. The 100 KB threshold you may see applies only to the security token for large results, which expires after 6 hours. It has nothing to do with whether a result is stored. The reuse conditions concern the query text, its functions, the underlying data, the role's privileges and result-affecting configuration. None of them concerns the warehouse size.

    You can also query a stored result directly. RESULT_SCAN returns an earlier result as a table, and LAST_QUERY_ID() points it at the statement you just ran. This is how you filter output that SQL cannot otherwise reach. SHOW, DESCRIBE and CALL all return results you cannot embed in a larger query. A stored procedure, for example, cannot be called inside a SELECT the way a function can. You can also build a complex query in steps, putting a new layer on the previous result without recomputing it.

    Run immediately after SHOW TABLES: keep only the empty tables from its outputsql
    SELECT "schema_name", "name" as "table_name", "rows" FROM table(RESULT_SCAN(LAST_QUERY_ID())) WHERE "rows" = 0;

    The pipe operator ->> does the same job without displaying the first command's output. Either way, the stored result must still exist. Each reuse resets the 24-hour retention, but only up to 31 days after the query first ran, and after that the result is purged.

    Checkpoint 7 of 9· Fill the gap

    Right after SHOW TABLES, you want only the empty tables. You can't put SHOW in a larger query, so its stored result has to be read as a table. Which function goes in the blank?

    SELECT "schema_name", "name" as "table_name", "rows" FROM table( ? (LAST_QUERY_ID())) WHERE "rows" = 0;

    Checkpoint 8 of 9· Exam question

    A BI analyst reruns an identical, expensive aggregation on a large table several times during the morning and expects instant results. The administrator wants to confirm which condition would force Snowflake to ignore the persisted result and recompute. Select TWO.(Select 2)

    0 of 2 selected

    Sources2

    5.Cancelling one query, one session or one user

    Cancel from the place the query is running wherever you can. In Snowsight that is the worksheet, and in code it is the ODBC or JDBC driver's cancellation API. SQL cancellation is for cases where that interface is out of reach, such as a runaway job started by another client. What you pick depends on how wide the cancellation should be.

    SQL options for cancelling statements, from narrowest to widest
    ScopeStatementWhat it targets
    One querySYSTEM$CANCEL_QUERYA single running statement
    One sessionSYSTEM$CANCEL_ALL_QUERIESAll running statements in the session whose identifier you pass
    One userALTER USER ... ABORT ALL QUERIESEvery query the user has running or scheduled, on any warehouse

    Snowflake's own JDBC sample shows the session-level function at work. It reads the session id with CURRENT_SESSION(), starts a 120-second generator query, and five seconds later passes that id to SYSTEM$CANCEL_ALL_QUERIES. The query then fails with the query-cancelled SQL state.

    ABORT ALL QUERIES clears everything the user has running, but it does not lock them out. The user can still sign in and start new queries a moment later. If a compromised or misbehaving account must stop entirely, use ALTER USER ... SET DISABLED = TRUE. That aborts the running and scheduled queries and also blocks new logins and new queries.

    Checkpoint 9 of 9· Check yourself

    Several runaway statements are running in one session that a batch client opened, and you can't reach that client's interface. You have the session identifier. The same user also has other sessions doing legitimate work that must keep running. Which SQL option fits?

    Sources34

    Sources

    (4)

    Every claim above is drawn from one of these pages, quoted as it was written on the date shown.

    1. 1.
      “Concurrent queries never observe the same value, and values within a single query are always distinct.”
      ↩︎ What a sequence guarantees, and what it does not
      “NOORDER specifies that the values are not guaranteed to be in increasing order.”
      ↩︎ What a sequence guarantees, and what it does not
      “A reserved value is never generated by the sequence as long as the step/interval is never modified.”
      ↩︎ What a sequence guarantees, and what it does not
      “Many databases provide a currval sequence reference; however, Snowflake does not.”
      ↩︎ Sequence limitations: interval changes, range and no CURRVAL
      “If the internal representation of a sequence’s next value exceeds this range (in either direction) an error results and the query fails.”
      ↩︎ Sequence limitations: interval changes, range and no CURRVAL
      “Changing the sequence interval from positive to negative (e.g. from 1 to -1), or vice versa may result in duplicates.”
      ↩︎ Sequence limitations: interval changes, range and no CURRVAL
      “A call to GETNEXTVAL must be aliased; otherwise, the generated values cannot be referenced.”
      ↩︎ Sequence limitations: interval changes, range and no CURRVAL
      “setting the value to DEFAULT in an insert or update statement will generate a new sequence value for the row.”
      ↩︎ Using a sequence as a column default
      “any attempt to insert/update the table using the default value will result in an error saying the identifier cannot be found.”
      ↩︎ Using a sequence as a column default
      “Snowflake does not guarantee generating sequence numbers without gaps.”
      ↩︎ Key concept
      “There is no guarantee that values from a sequence are contiguous (gap-free) or that the sequence values are assigned in a particular order.”
      ↩︎ Exam trap 1
      “an ALTER SEQUENCE ... SET INCREMENT ... command might not affect the next operation that uses the sequence.”
      ↩︎ Exam trap 2
      “The previous statement completed, and an acknowledgment was received, prior to submitting the current statement.”
      ↩︎ Checkpoint
      “A consequence of this is that an ALTER SEQUENCE ... SET INCREMENT ... command might not affect the next operation that uses the sequence.”
      ↩︎ Checkpoint
    2. 2.
      “You can perform post-processing by using the RESULT_SCAN table function.”
      ↩︎ Post-processing persisted results with RESULT_SCAN
      “UUID_STRING, RANDOM, and RANDSTR are good examples of non-reusable functions.”
      ↩︎ Post-processing persisted results with RESULT_SCAN
      “The table data contributing to the query result has not changed.”
      ↩︎ Post-processing persisted results with RESULT_SCAN
      “For persisted query results of all sizes, the cache expires after 24 hours.”
      ↩︎ Exam trap 3
    3. 3.
      “The recommended way to cancel a statement is to use the interface of the application in which the query is running”
      ↩︎ Cancelling one query, one session or one user
      “This task uses the session identifier as a parameter to SYSTEM$CANCEL_ALL_QUERIES.”
      ↩︎ Cancelling one query, one session or one user
    4. 4.
      “Aborts all the queries and other SQL statements currently running or scheduled by the user, regardless of the warehouse”
      ↩︎ Cancelling one query, one session or one user
      “Note that the user can still log into Snowflake and initiate new queries.”
      ↩︎ Exam trap 4

    Continue to part 2 of 2

    Snowsight Query History filters, charts and dashboards

    Spotted a mistake, or was something unclear? Tell us.