Your First Finance-Focused SQL Query

From curiosity to insight—in just a few lines of code
If you’re reading this, you probably already know that SQL is a valuable skill in finance. But knowing why isn’t the same as knowing how. So let’s change that—right now.
In this post, I’ll walk you through your very first finance-focused SQL query. You’ll see exactly how to ask a business question using SQL and get a clean, reliable answer in seconds. No programming experience required.
Let’s go!
The Scenario: Revenue by Product Line
Imagine your manager asks:
“Can you tell me total revenue by product line for last quarter?”
It's a simplified example but let's assume that this data is not readily available from a BI dashboard. If you’re in Excel, this means:
- Downloading the right data
- Filtering by dates
- Creating a pivot table
- Checking for missing data
- Copy-pasting for slides or dashboards
Now imagine doing that with one query, always up to date, straight from the source system, without having to involve a BI developer.
The SQL Query
Here’s how we’d ask that same question in SQL:
SELECT
product_line,
SUM(revenue) AS total_revenue
FROM
finance.sales_data
WHERE
invoice_date BETWEEN '2024-10-01' AND '2024-12-31'
GROUP BY
product_line
ORDER BY
total_revenue DESC;
Breaking It Down
The first thing you will notice is how close this is to plain English. It's very readable even if you have never seen SQL before. Follow that tangent for a moment before diving into each part of the query, because the readability is another of SQL's strengths compared to spreadsheet formulas: because references to data and operations on it have descriptive names, it's easier to follow than the often obscure spreadsheet cell references, and even though it's avoidable many finance professionals (perhaps you have a colleague like this!) almost seem to take pride in writing "clever" formulas. I've seen something along these lines way too often:
=IFERROR(SUMIFS(FILTER(C2:C847, (A2:A847>=E1)*(A2:A847<=E2)*(D2:D847="External")), B2:B847, G2, E2:E847, H2, F2:F847, I2), 0)
Now let’s go through the SQL line by line:
SELECT
– This defines what you want to see in the result:product_line
and the sum ofrevenue
.FROM
– This tells SQL where to find the data. In this example, it’s in thefinance.sales_data
table, which we assume is available in your data warehouse.WHERE
– A filter to focus only on the relevant time period (last quarter).GROUP BY
– This tells SQL to group all matching rows by product line before summing the revenue.ORDER BY
– This sorts the results so the highest revenue product lines appear first.
That’s it. No macros. No dragging formulas. And in a later tutorial we will show how to make date ranges dynamic, meaning that you can run the script
What You’ll See
When you run the query, you’ll get a table like this:
product_line | total_revenue |
---|---|
Widgets | 3,200,000 |
Gadgets | 2,750,000 |
Spare Parts | 950,000 |
Clean, structured, and ready to visualize or automate.
Try It Yourself
If you have access to BigQuery (or another data warehouse), try running the query in your browser, making sure to adjust the column names (revenue
, product_line
, invoice_date
) based on your company’s schema. Don’t have access yet? Ask your BI or data team for read-only access to a safe dataset like sales orders or invoices.
What You Just Learned
With just a few lines of SQL, you now know how to:
- Select specific columns
- Filter by some criterion such as date
- Group by a business dimension
- Aggregate values with SUM
- Sort your results for clarity
This single query pattern can power 80% of the reporting needs finance teams face every day.
What’s Next
Next, we’ll look at how to join multiple datasets—for example, linking actuals with budgets, or finance data with operational KPIs.
If you’re following this learning path, you’re already ahead of the curve. Just keep going!