If you work finance, it's crucial to keep track of metrics to stay on top of business health. Let's take a look at some popular metrics to track and sample SQL queries:
Revenue over a period
Total revenue over a period of time
SELECT SUM(revenue)
FROM sales
WHERE date >= '2022-01-01' AND date <= '2022-12-31';
💡 Explanation
This query retrieves the total revenue for the year 2022 by summing up the revenue column in the sales table where the date falls within the specified time period.
Expenses over a period
Total expenses over a period of time
SELECT SUM(expenses)
FROM expenses
WHERE date >= '2022-01-01' AND date <= '2022-12-31';
💡 Explanation
This query retrieves the total expenses for the year 2022 by summing up the expenses column in the expenses table where the date falls within the specified time period.
Gross profit margin over a period
SELECT (SUM(revenue) - SUM(cost)) / SUM(revenue) * 100 AS gross_profit_margin
FROM sales
WHERE date >= '2022-01-01' AND date <= '2022-12-31';
💡 Explanation
This query calculates the gross profit margin for the year 2022 by subtracting the total cost of sales from the total revenue, dividing the result by the total revenue, and multiplying by 100.
Total assets
SELECT SUM(value) AS total_assets
FROM assets;
💡 Explanation
This query retrieves the total value of assets by summing up the value column in the assets table.
Total liabilities
SELECT SUM(value) AS total_liabilities
FROM liabilities;
💡 Explanation
This query retrieves the total value of liabilities by summing up the value column in the liabilities table.
Debt to equity ratio
SELECT SUM(value) / (SELECT SUM(value) FROM equity) AS debt_to_equity_ratio
FROM liabilities;
💡 Explanation
This query calculates the debt-to-equity ratio by dividing the total value of liabilities by the total value of equity.
Return on investment (ROI)
SELECT (SUM(revenue) - SUM(cost)) / SUM(cost) * 100 AS ROI
FROM sales
WHERE date >= '2022-01-01' AND date <= '2022-12-31';
💡 Explanation
This query calculates the return on investment for the year 2022 by subtracting the total cost of sales from the total revenue, dividing the result by the total cost of sales, and multiplying by 100.
Top customers by revenue
SELECT customer_name, SUM(revenue) AS total_revenue
FROM sales
GROUP BY customer_name
ORDER BY total_revenue DESC
LIMIT 10;
💡 Explanation
This query retrieves the top 10 customers by total revenue by grouping the sales data by customer name, summing up the revenue for each customer, ordering the results by total revenue in descending order, and limiting the results to the top 10.
Top products by revenue
SELECT product_name, (SUM(revenue) - SUM(cost)) / SUM(revenue) * 100 AS profit_margin
FROM sales
GROUP BY product_name
ORDER BY profit_margin DESC
LIMIT 10;
💡 Explanation
This query retrieves the top 10 products by profit margin by grouping the sales data by product name, subtracting the total cost of sales from the total revenue for each product, dividing the result by the total revenue for each product, ordering the results by profit margin in descending order, and limiting the results to the top 10.
These queries are just a starting point and can be modified or expanded upon based on the specific needs of the finance analyst and the organization they are working for.