Database
Count Records
QueryValidateMedium
What you will learn
- COUNT(*) counts all rows including NULL; COUNT(col) skips NULL โ different answers, easy bug
- COUNT(DISTINCT col) dedups โ useful for unique-user counts. Test that performance degrades predictably on large sets
- COUNT in subquery vs JOIN โ pick based on EXPLAIN output, not intuition. Same result, order of magnitude different perf
Description
Count the number of products in each category.
Test Scenario
-- Your query:
SELECT category, COUNT(*) as product_count
FROM products
GROUP BY category;
-- Expected:
-- Electronics: 4
-- Accessories: 2
-- Office: 2Testing Tips
- โขCOUNT(*) counts all rows
- โขGROUP BY groups results
- โขAggregate function per group
// 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-count
2 frameworks available