Counts the number of rows that satisfy a given condition. Provides a more concise and readable syntax compared to COUNT with CASE or IF.
INT64Diperbarui: 13 Jun 2026COUNTIF(condition)The boolean expression to evaluate for each row
1 SELECT 2 COUNTIF(status = 'completed') as completed_orders, 3 COUNTIF(status = 'pending') as pending_orders, 4 COUNTIF(status = 'cancelled') as cancelled_orders, 5 COUNT(*) as total_orders 6 FROM `project.dataset.orders`;
Breaks down orders by status in a single query.
| completed_orders | pending_orders | cancelled_orders | total_orders |
|---|---|---|---|
| 8500 | 1200 | 300 | 10000 |
1 SELECT 2 product_category, 3 COUNTIF(price > 1000000) as premium_products, 4 COUNTIF(price BETWEEN 500000 AND 1000000) as mid_range, 5 COUNTIF(price < 500000) as budget_products 6 FROM `project.dataset.products` 7 GROUP BY product_category;
Groups products by price range per category.
| product_category | premium_products | mid_range | budget_products |
|---|---|---|---|
| Electronics | 45 | 120 | 85 |
| Fashion | 12 | 89 | 234 |
1 SELECT 2 DATE(timestamp) as date, 3 COUNT(*) as total_visits, 4 COUNTIF(converted = TRUE) as conversions, 5 ROUND(COUNTIF(converted = TRUE) * 100.0 / COUNT(*), 2) as conversion_rate 6 FROM `project.dataset.user_sessions` 7 GROUP BY date 8 ORDER BY date DESC 9 LIMIT 7;
Calculates the daily conversion rate.
| date | total_visits | conversions | conversion_rate |
|---|---|---|---|
| 2024-06-25 | 125000 | 3750 | 3.00 |
| 2024-06-24 | 118000 | 3304 | 2.80 |
1 SELECT 2 customer_id, 3 COUNTIF(order_date >= DATE_SUB(CURRENT_DATE(), INTERVAL 30 DAY)) as orders_last_30_days, 4 COUNTIF(order_date >= DATE_SUB(CURRENT_DATE(), INTERVAL 90 DAY)) as orders_last_90_days, 5 COUNT(*) as total_orders 6 FROM `project.dataset.orders` 7 GROUP BY customer_id 8 HAVING COUNTIF(order_date >= DATE_SUB(CURRENT_DATE(), INTERVAL 30 DAY)) > 0 9 LIMIT 10;
Analyzes customer activity by time period.
| customer_id | orders_last_30_days | orders_last_90_days | total_orders |
|---|---|---|---|
| C001 | 3 | 8 | 25 |
| C002 | 5 | 12 | 45 |
Sudah paham COUNTIF? Latih langsung di browser
Latihan interaktif, langsung di browser.