Skip to main content
TutorialApplies to: Unified engine deployments
Use window functions to keep detail rows while comparing related values.

Before you start

You need Workbench and a running SQL engine. Every example includes its own input rows. No catalog, files, credentials, or external data is needed. These queries create no stored tables. Copy the SQL into Workbench and run the statements in order. Compare the result with the table below the query. WITH names input data for one statement only. Each example can run on its own.

1. Number rows within groups

What is each row position within its team?
Expected result An ID tie-breaker makes row numbering stable. Change one thing: Use score ASC. Team A positions become 3, 1, and 2.

2. Rank tied values

How does rank handle a tie?
Expected result RANK gives ties the same rank and leaves a gap after them. Change one thing: Use DENSE_RANK. ID 5 gets rank 4.

3. Use dense ranks

Can tied ranks have no gaps?
Expected result DENSE_RANK counts distinct ordering values. Change one thing: Order score ASC. IDs 1 through 5 get ranks 3, 2, 2, 4, and 1.

4. Keep the top two per group

Which rows are top two within each team?
Expected result Filter a window result in an outer query. Change one thing: Use position = 1. Expect IDs 1 and 4.

5. Read the previous value

What score came before each row?
Expected result LAG reads an earlier row within the same partition. Change one thing: Use LEAD. Expect 20, 20, NULL, 10, NULL.

6. Calculate a running total

What is the cumulative score per team?
Expected result The frame includes the current row and every earlier row in the partition. Change one thing: Remove PARTITION BY team. Expect totals 30, 50, 70, 110, 120.

7. Calculate a moving average

What is the average of this row and the preceding row?
Expected result At the start of a partition the frame has only one row. Change one thing: Use CURRENT ROW as both frame bounds. Each average equals its score.

8. Read the final group value

What is the last score in each team?
Expected result The explicit full frame lets LAST_VALUE read the end of the partition. Change one thing: Use FIRST_VALUE. Team A returns 30 and team B returns 40.

Continue

Continue to Read JSON and nested values, or return to Learn Laminar.
Last modified on November 4, 2026