Skip to main content
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: 2

Testing Tips

  • โ€ขCOUNT(*) counts all rows
  • โ€ขGROUP BY groups results
  • โ€ขAggregate function per group
// Playwright API Testing - Database
import { 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