|
| 1 | +--- |
| 2 | +title: MODE |
| 3 | +--- |
| 4 | +import FunctionDescription from '@site/src/components/FunctionDescription'; |
| 5 | + |
| 6 | +<FunctionDescription description="Introduced or updated: v1.2.647"/> |
| 7 | + |
| 8 | +Returns the value that appears most frequently in a group of values. |
| 9 | + |
| 10 | +## Syntax |
| 11 | + |
| 12 | +```sql |
| 13 | +MODE(<expr>) |
| 14 | +``` |
| 15 | + |
| 16 | +## Examples |
| 17 | + |
| 18 | +This example identifies the best-selling product for each month from the sales data: |
| 19 | + |
| 20 | +```sql |
| 21 | +CREATE OR REPLACE TABLE sales ( |
| 22 | + sale_date DATE, |
| 23 | + product_id INT, |
| 24 | + quantity INT |
| 25 | +); |
| 26 | + |
| 27 | +INSERT INTO sales (sale_date, product_id, quantity) VALUES |
| 28 | + ('2024-01-01', 101, 10), |
| 29 | + ('2024-01-02', 102, 15), |
| 30 | + ('2024-01-02', 101, 10), |
| 31 | + ('2024-01-03', 103, 8), |
| 32 | + ('2024-01-03', 101, 10), |
| 33 | + ('2024-02-01', 101, 20), |
| 34 | + ('2024-02-02', 102, 15), |
| 35 | + ('2024-02-03', 102, 15); |
| 36 | + |
| 37 | +SELECT MONTH(sale_date) AS month, MODE(product_id) AS most_sold_product |
| 38 | +FROM sales |
| 39 | +GROUP BY month |
| 40 | +ORDER BY month; |
| 41 | + |
| 42 | +┌─────────────────────────────────────┐ |
| 43 | +│ month │ most_sold_product │ |
| 44 | +├─────────────────┼───────────────────┤ |
| 45 | +│ 1 │ 101 │ |
| 46 | +│ 2 │ 102 │ |
| 47 | +└─────────────────────────────────────┘ |
| 48 | +``` |
0 commit comments