Database
Window Functions
QueryValidateHard
What you will learn
- ROW_NUMBER() OVER (ORDER BY ...) assigns sequence โ test stable ordering across ties (pick deterministic tiebreaker)
- Window functions execute AFTER WHERE but BEFORE ORDER BY + LIMIT โ can't filter on window results in WHERE, use subquery
- RANK() vs DENSE_RANK() vs ROW_NUMBER(): each handles ties differently โ test with tied data, not only unique rows
Description
Rank products by price within each category.
Test Scenario
-- Your query:
SELECT
name,
category,
price,
ROW_NUMBER() OVER (PARTITION BY category ORDER BY price DESC) as rank_in_category
FROM products;
-- Expected: Products ranked within their categoryTesting Tips
- โขOVER() defines the window
- โขPARTITION BY groups without collapsing
- โขROW_NUMBER(), RANK(), DENSE_RANK()
// Playwright API Testing - Databaseimport { test, expect } from '@playwright/test';test('Database query via API', async ({ request }) => {const response = await request.post('/api/database/query', {data: {sql: 'SELECT * FROM users WHERE id = ?',params: [1]}});expect(response.ok()).toBeTruthy();const { rows } = await response.json();expect(rows.length).toBeGreaterThan(0);});
Challenge ID: sql-window-functions
2 frameworks available