Sequences generate identifiers
A sequence is an object that supplies numeric values. It is useful for generated identifiers, but its values are not a gap-free business counter.
Understand the idea
An identifier distinguishes a record; a count measures how many records exist. Those jobs should not be confused. Concurrent work and unused generated values can leave gaps in a sequence, and sequence generation is not a guarantee of business arrival order.
| Concept | What it means |
|---|---|
| Sequence | A reusable generator of numeric values. |
| NEXTVAL | Requests the next generated value. |
| Gap | A missing number can occur without a missing business record. |
A concrete example
Northstar assigns an internal identifier to imported orders. It must not use the largest generated ID as its count of successful orders.
Remember: Count records with COUNT; do not infer the count from a sequence.
Explain it step by step · full theory
Understand the idea
An identifier distinguishes a record; a count measures how many records exist. Those jobs should not be confused. Concurrent work and unused generated values can leave gaps in a sequence, and sequence generation is not a guarantee of business arrival order.
Using a sequence does not remove the need to design key integrity. Other code can supply an explicit ID, and applications can retry a business event. Define how duplicate business keys are handled separately from generating surrogate IDs.
Follow the example
The example creates a new sequence in the lab schema and asks for one value. Run the CREATE once with a fresh name. Avoid changing the increment direction or recreating an in-use sequence as a way to fill gaps.
For this newly created sequence with no other consumers, the first returned identifier is 1000. Later identifiers may have gaps. A missing ID should trigger a business-data investigation only when separate evidence shows that a record is missing.
The first request against this fresh sequence returns 1000. This statement generates a value but does not insert an order.
Words used in this lesson
- Sequence
- A reusable generator of numeric values.
- NEXTVAL
- Requests the next generated value.
- Gap
- A missing number can occur without a missing business record.
See how it works
Choose a step to explore the idea at your own pace.
Open the interactive explanation
Sequences generate identifiers
Start with the requirement
Northstar assigns an internal identifier to imported orders. It must not use the largest generated ID as its count of successful orders.
Automatic playback is off for reduced motion. Select any step or use Previous and Next. Illustrations explain behavior; they are not live measurements.
Read the complete diagram explanation
- Start with the requirement
Northstar assigns an internal identifier to imported orders. It must not use the largest generated ID as its count of successful orders.
- Choose the objects
Compare the responsibilities: Sequence: A reusable generator of numeric values.; NEXTVAL: Requests the next generated value.; Gap: A missing number can occur without a missing business record.
- Explain the outcome
Count records with COUNT; do not infer the count from a sequence.
Try the SQL or command
Read → predict → run in your learning environment → compare. You can also learn by reading without an account.
New to SQL? Where to run it and how to read it
A query is a question written in SQL. Snowflake runs it and returns a result table. This website shows examples; it does not connect to your account.
- No account yet? Read and predict the output first. Follow the account and Snowsight lesson when you are ready.
- For SQL practice, use a learning account in Snowsight and select an authorized role and warehouse. A role determines permissions; a warehouse supplies processing power. Follow the example’s database/schema setup when it refers to stored objects.
- Read the entire example, including comments beginning with
--. Run the selected statement or labeled block in the stated order. Examples labeled Python, shell or HTTP belong in that client, not in a SQL editor. - Compare values and row counts with the explained result. If an example deliberately causes an error, follow its rollback and cleanup steps before continuing.
- SELECT / FROM
- Choose the values to return / identify their input.
- VALUES / COLUMN1
- VALUES supplies tiny example rows. COLUMN1 means their first column, COLUMN2 their second.
- AS / WITH
- AS gives a result a readable name. WITH names a temporary query step within one statement.
- NULL / ::
- NULL means missing or unknown. The :: notation converts a value to a named data type.
- Session
- One connection and its settings. Keep related transaction steps in the same session.
- Database.schema.table
- A full address for stored data. It is separate from the warehouse that processes queries.
Need the L2_OBJECTS_LAB setup? Open the first database-object lesson →
Help with common practice errors
- No active warehouse: select a warehouse your role can use. Do not assume you need to create a new one.
- Object missing or not authorized: check the full name, required setup and active role. Ask the account owner for access if needed.
- Object already exists: setup may already have run. Read the current state or choose a fresh lab name consistently; do not add OR REPLACE blindly.
- Different result: check whether an earlier INSERT or UPDATE changed the data. VALUES examples provide their own fresh input for each statement.
Before running this lesson
Examples are for reading first. Optional execution requires an authorized learning account, a selected warehouse for queries, and privileges on the named objects. Nothing runs on this website.
Read the example
CREATE SEQUENCE L2_OBJECTS_LAB.SALES.ORDER_IDS START = 1000 INCREMENT = 1;
SELECT L2_OBJECTS_LAB.SALES.ORDER_IDS.NEXTVAL AS NEW_ID;What to expect and why
The first request against this fresh sequence returns 1000. This statement generates a value but does not insert an order.
Check your understanding
Choose an answer, then check your reasoning. This is practice, not a scored test.
Try it yourself
Choose the correct way to count successfully stored orders.
Compare your reasoning
Count the qualifying rows in the table. A sequence allocates values independently of the final business outcome, so MAX(ORDER_ID) is not a reliable row count.