This document describes the reusable SQL analysis scripts available in the scripts/analysis/ directory. These scripts are designed to be parameterized and reusable for financial analysis of companies in the database.
Returns general company information for a given CIK.
Parameters:
:cik- The CIK of the company to analyze (e.g., '0000320193' for Apple)
Returns:
legal_name- Company's legal namesector- Business sectorindustry- Specific industrycountry- Country of incorporationearliest_fiscal_year- First year with available datalatest_fiscal_year- Most recent year with available datatotal_facts- Total number of financial facts in the databasetotal_filings- Total number of SEC filings
Usage:
\set cik '0000320193'
\i scripts/analysis/company_overview.sqlReturns yearly financial time series data with growth rates and margins.
Parameters:
:cik- The CIK of the company to analyze
Returns:
fiscal_year- The fiscal yearrevenue- Total revenuenet_income- Net income/losstotal_assets- Total assetsstockholders_equity- Shareholders' equitytotal_liabilities- Total liabilitiesoperating_cash_flow- Cash flow from operationscapex- Capital expendituresfree_cash_flow- Operating cash flow minus capital expendituresrevenue_growth_pct- Year-over-year revenue growth percentagenet_income_growth_pct- Year-over-year net income growth percentagetotal_assets_growth_pct- Year-over-year total assets growth percentagenet_margin_pct- Net income as percentage of revenueoperating_margin_pct- Operating cash flow as percentage of revenuefcf_margin_pct- Free cash flow as percentage of revenue
Usage:
\set cik '0000320193'
\i scripts/analysis/financial_series.sqlReturns advanced financial ratios including profitability, leverage, and valuation metrics.
Parameters:
:cik- The CIK of the company to analyze
Returns:
fiscal_year- The fiscal yearrevenue- Total revenuenet_income- Net income/losstotal_assets- Total assetsstockholders_equity- Shareholders' equitytotal_liabilities- Total liabilitiesroe_pct- Return on Equity (Net Income / Shareholders' Equity)roa_pct- Return on Assets (Net Income / Total Assets)net_margin_pct- Net Profit Margin (Net Income / Revenue)debt_to_equity- Debt to Equity Ratio (Total Liabilities / Shareholders' Equity)debt_to_assets- Debt to Assets Ratio (Total Liabilities / Total Assets)current_ratio- Current Ratio (Current Assets / Current Liabilities)market_cap- Market Capitalization (Share Price × Shares Outstanding)pe_ratio- Price to Earnings Ratio (Market Cap / Net Income)
Note: Valuation ratios (market_cap, pe_ratio) require stock price data to be available in the prices table.
Usage:
\set cik '0000320193'
\i scripts/analysis/ratios_advanced.sqlCompares key financial metrics across multiple companies for their latest fiscal year.
Parameters:
:ciks- Array of CIKs to compare (e.g., ARRAY['0000320193', '0000789019'] for Apple and Microsoft)
Returns:
cik- The CIK of the companylatest_fiscal_year- Most recent fiscal year with datarevenue- Total revenue for latest yearnet_income- Net income for latest yearroe_pct- Return on Equity for latest yearnet_margin_pct- Net profit margin for latest yeardebt_to_equity- Debt to equity ratio for latest yearrevenue_growth_pct- Year-over-year revenue growth percentage
Usage:
\set ciks '{"0000320193","0000789019"}'
\i scripts/analysis/compare_companies.sqlAll scripts follow these design principles:
- Parameterized - Use PostgreSQL parameter syntax (:param_name) for safe substitution
- Idempotent - Can be run multiple times without side effects
- Well-documented - Clear comments explaining each section
- Efficient - Use CTEs and proper joins for optimal performance
- Robust - Handle NULL values and edge cases appropriately
- Readable - Formatted for clarity and maintainability
- PostgreSQL 9.5+ (for CTE support)
- Financial database schema with companies, financial_facts, filings, etc.
- For valuation ratios: stock price data in the prices table from Yahoo Finance provider
\set cik '0000320193'
\i scripts/analysis/company_overview.sql\set cik '0000789019'
\i scripts/analysis/financial_series.sql\set ciks '{"0000320193","0000789019"}'
\i scripts/analysis/compare_companies.sqlWhen modifying these scripts:
- Keep backward compatibility with existing parameter names
- Add clear comments for complex logic
- Test with known CIKs (Apple: 0000320193, Microsoft: 0000789019)
- Ensure proper handling of edge cases (NULL values, zero denominators)