The QUERY formula in Google Sheets is a powerful tool for filtering, grouping, and summarizing data. With this formula, you can simplify data management, perform advanced analysis, and create detailed reports.
In this blog, we’ll explore four practical use cases to demonstrate how the QUERY formula can transform your workflows.
1. Streamlining inventory management
Scenario: A warehouse manager needs to retrieve details about oversold products and calculate daily out-of-stock totals from inventory records.
|
A |
B |
C |
|
Product |
Stock |
Sales |
|
Product A |
50 |
60 |
|
Product B |
30 |
15 |
|
Product C |
10 |
20 |
=QUERY(A1:C4, “SELECT A, B, C, (C – B) WHERE (C – B) > 0 LABEL (C – B) ‘Oversold by’ “, 1)
Breakdown:
- Data Range: (A1:C4).
- The formula processes data from the entire table.
- SELECT clause: SELECT A, B, C, (C – B).
- Retrieves columns A, B, C, and the difference between sales and stock (C – B).
- WHERE clause: (C – B) > 0.
- Filters rows where sales are higher than stock, showing only oversold products.
- LABEL clause: LABEL (C – B) ‘Oversold by’.
- Renames (C – B) to “Oversold by” in the output.
- Header indicator: (1).
- Treats the first row as headers for proper formatting.
| Product | Stock | Sales | Oversold by |
| Product A | 50 | 60 | 10 |
| Product C | 10 | 20 | 10 |
How it works
The QUERY formula selects products where sales exceed stock, automatically calculating the oversold amount.
Benefits
- Identify oversold products in seconds.
- Eliminate manual filtering and calculation errors.
2. Summarizing Financial Data by Category
Scenario: A finance team needs to analyze expenses by category for daily and monthly reporting.
|
A |
B |
C |
|
Date |
Category |
Expense ($) |
|
01/01/2024 |
Marketing |
200 |
|
01/01/2024 |
Travel |
300 |
|
02/01/2024 |
Marketing |
150 |
=QUERY(A1:C5, “SELECT B, SUM(C) WHERE B IS NOT NULL GROUP BY B LABEL SUM(C) ‘Total Expense ($)'”, 1)
Breakdown
- Data Range: (A1:C5).
- The formula processes data from columns A, B, and C in this range.
- SELECT clause: SELECT B, SUM(C).
- Retrieves column B (Category) and calculates the sum of column C (Expense).
- WHERE clause:WHERE B IS NOT NULL.
- Excludes rows where column B (Category) is blank.
- GROUP clause: GROUP BY B LABEL SUM(C)
- Groups the data by category (B) to calculate totals for each unique category.
- LABEL clause: LABEL SUM(C) ‘Total Expense ($)’.
- Renames the calculated total (SUM(C)) to “Total Expense ($)” for clarity.
- Header indicator: 1.
- Treats the first row as headers for proper formatting.
| Category | Total Expense ($) |
| Marketing | 350 |
| Travel | 300 |
How it works
The QUERY formula groups expenses by category and calculates their total.
Benefits
- Gain insights into spending patterns.
- Quickly prepare data for financial analysis or presentations.
Breakdown
- Data Range: (A1:C5).
- The formula processes data from columns A, B, and C in this range.
- SELECT clause: SELECT B, SUM(C).
- Retrieves column B (Category) and calculates the sum of column C (Expense).
- WHERE clause:WHERE B IS NOT NULL.
- Excludes rows where column B (Category) is blank.
- GROUP clause: GROUP BY B LABEL SUM(C)
- Groups the data by category (B) to calculate totals for each unique category.
- LABEL clause: LABEL SUM(C) ‘Total Expense ($)’.
- Renames the calculated total (SUM(C)) to “Total Expense ($)” for clarity.
- Header indicator: 1.
- Treats the first row as headers for proper formatting.
| Category | Total Expense ($) |
| Marketing | 350 |
| Travel | 300 |
3. Tracking Stock Movement
Scenario: A store manager wants to monitor stock added and sold per product over a week.
|
A |
B |
C |
|
Product |
Date |
Stock ($) |
|
Product A |
01/01/2024 |
10 |
|
Product A |
02/01/2024 |
20 |
|
Product B |
01/01/2024 |
5 |
=QUERY(A1:C6, “SELECT A, SUM(C) WHERE A IS NOT NULL GROUP BY A LABEL SUM(C) ‘Total Stock Added ($)’ “, 1)
Breakdown:
- Data Range: (A1:C6).
- The formula processes data from columns A, B, and C in this range.
- SELECT clause: SELECT A, SUM(C).
- Retrieves column A (Product Name) and the sum of column C (Stock Added).
- WHERE clause: WHERE A IS NOT NULL.
- Excludes rows where column A (Product Name) is blank.
- GROUP clause: GROUP BY A.
- Groups the data by product (A) to calculate totals for each unique product.
- LABEL clause: LABEL SUM(C) ‘Total Stock Added ($)’.
- Renames the calculated total (SUM(C)) to “Total Stock Added ($)” for clarity.
- Header indicator: 1.
- Treats the first row as headers for proper formatting.
| Product | Total Stock Added ($) |
| Product A | 30 |
| Product B | 5 |
How it works
The QUERY formula groups stock entries by product and calculates the total.
Benefits
- Summarize stock trends dynamically.
- Simplify stock reconciliation tasks.
4. Creating Detailed Timesheet Reports
Scenario: An HR team needs to generate reports of employee hours by project and week.
|
A |
B |
C |
|
Employee |
Project |
Hours ($) |
|
Alice |
Project X |
10 |
|
Bob |
Project Y |
15 |
|
Alice |
Project Y |
20 |
=QUERY(A1:C5, “SELECT B, SUM(C) WHERE B IS NOT NULL GROUP BY B LABEL SUM(C) ‘Total Hours'”, 1)
Breakdown
- Data Range: A1:C5.
- Processes data from columns A, B, and C in this range.
- SELECT clause: SELECT B, SUM(C).
- Retrieves column B (Project Name) and calculates the sum of column C (Hours Worked).
- WHERE clause: WHERE B IS NOT NULL.
- Excludes rows where column B (Project Name) is blank.
- GROUP clause: GROUP BY B.
- Groups the data by project (B) to calculate totals for each unique project.
- LABEL clause: LABEL SUM(C) ‘Total Hours’.
- Renames the calculated total (SUM(C)) to “Total Hours” for clarity
- Header indicator: 1.
- Treats the first row as headers for proper formatting.
| Project | Total Hours |
| Project X | 10 |
| Project Y | 35 |
How it works
The QUERY formula groups hours by project and calculates their total, offering a detailed view of time allocation.
Benefits
- Automate timesheet summaries.
- Minimize manual reporting errors.
Unlock the Power of QUERY Formulas
The QUERY formula is a must-have tool for anyone working with data in Google Sheets. Its ability to filter, group, and summarize data dynamically simplifies workflows and unlocks powerful insights.
Have a unique use case for the QUERY formula? Share it in the comments—we’d love to hear how you’re using this versatile tool!
