Advanced SQL & Transactions
Read a clear definition, compare the concepts, then try the SQL or client command. Detailed explanations and diagrams remain available when you need them.
Before you begin
Intermediate · 16 lessons · About 324 minutes including detailed reading.
Prerequisite: Database Objects & SQL Foundations. Reading needs no account. Manual SQL practice requires an authorized Snowflake account, suitable role and warehouse. Commands are examples, not executed by this website.
Complete Module 03 first. Most SQL includes independent VALUES inputs. Transaction exercises use disposable objects in L2_OBJECTS_LAB.SALES; the concurrency exercise requires two separate sessions and explicit cleanup after its intentional error. The final lab combines deduplication, analytics and a commit/rollback decision.
Begin Module 04 →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.
What this module covers
- Joins: inner, left, right, full outer, cross, self, and semi/anti patterns.
- Subqueries, correlated subqueries, CTEs, and recursive CTEs.
- Set operators: UNION, UNION ALL, INTERSECT, and MINUS.
- Window functions: ROW_NUMBER, RANK, DENSE_RANK, LAG, LEAD, running totals, and moving averages.
- QUALIFY, PIVOT, UNPIVOT, GENERATOR, and lateral joins.
- ACID, autocommit, BEGIN/COMMIT/ROLLBACK, isolation, concurrent DML, locking, and transaction conflicts.
- Lab: Solve deduplication and analytical queries; test a multi-statement transaction.
Your lesson path
- Lesson 1 · About 18 min
Inner, left, right and full outer joins
A join combines rows from two inputs using a relationship. Inner joins keep matches; outer joins also keep unmatched rows from a chosen side.
Read and practise → - Lesson 2 · About 18 min
Cross joins and self joins
A cross join creates every combination of two inputs. A self join uses two aliases for the same input to compare related rows.
Read and practise → - Lesson 3 · About 18 min
Subqueries, correlated queries and semi/anti patterns
A subquery answers a smaller question inside a larger query. EXISTS asks whether a match exists; NOT EXISTS asks whether no match exists.
Read and practise → - Lesson 4 · About 18 min
CTEs and bounded recursion
A CTE names an intermediate query within one statement. A recursive CTE repeatedly uses its previous result to build a hierarchy or sequence.
Read and practise → - Lesson 5 · About 18 min
Combine result sets with set operators
Set operators combine compatible query results vertically. They answer questions about membership and duplicates, rather than matching columns as a join does.
Read and practise → - Lesson 6 · About 24 min
Window functions: keep the rows, add context
A window function adds a calculation to each row while preserving the original row detail.
Read and practise → - Lesson 7 · About 24 min
ROW_NUMBER, RANK and DENSE_RANK
ROW_NUMBER numbers rows; RANK and DENSE_RANK preserve ties with different rules for the following rank.
Read and practise → - Lesson 8 · About 24 min
QUALIFY, latest records and deduplication
QUALIFY filters rows after window calculations, making latest-record and top-per-group queries easier to read.
Read and practise → - Lesson 9 · About 24 min
Window frames: ROWS versus RANGE
A window frame specifies which nearby rows or ordering values contribute to the current calculation.
Read and practise → - Lesson 10 · About 24 min
Running totals and moving averages
Running totals accumulate values; moving averages summarize a limited neighborhood around each row.
Read and practise → - Lesson 11 · About 24 min
LAG, LEAD and changes over time
LAG reads an earlier row and LEAD reads a later row within an ordered partition.
Read and practise → - Lesson 12 · About 18 min
PIVOT and UNPIVOT: reshape reports
PIVOT turns category values into columns using an aggregate. UNPIVOT turns selected columns into rows; it cannot recover detail lost during aggregation.
Read and practise → - Lesson 13 · About 18 min
GENERATE rows and evaluate lateral queries
GENERATOR supplies synthetic rows. A lateral query can refer to a preceding input row and return zero, one or several related rows for it.
Read and practise → - Lesson 14 · About 18 min
ACID, autocommit and transaction boundaries
A transaction groups changes into one commit or rollback decision. Use it when several writes represent one business operation.
Read and practise → - Lesson 15 · About 18 min
Isolation, concurrent DML and lock waits
Concurrent sessions can read and write at the same time, but conflicting writes may wait or fail. Isolation defines which changes a statement can see.
Read and practise → - Lesson 16 · About 18 min
Lab: deduplicate, analyse and commit a batch
Apply deterministic ranking to a small event batch, calculate customer totals, then commit the chosen records as one controlled operation.
Read and practise →