Notifications  
  Closing in ·· seconds...

SQL for Financial Data: Queries Every Data Analyst Intern Should Know

Practical SQL for stock market data: filtering, aggregation, JOINs, 52-week highs, daily returns with LAG, moving averages and ranking with window functions.

Bluestock Team2 min readData Analyst Internship

SQL is the most requested skill in data analyst job descriptions, and financial data is a great way to practise it. The examples below use standard SQL that works, with minor changes, in PostgreSQL, MySQL 8 and most modern databases.

The sample tables

Assume two tables:

  • daily_prices(symbol, trade_date, open, high, low, close, volume)
  • companies(symbol, company_name, sector, market_cap)

1. Filtering and sorting

The most active stocks on a given day:

SELECT symbol, close, volume
FROM daily_prices
WHERE trade_date = '2026-11-30'
ORDER BY volume DESC
LIMIT 10;

2. Aggregation with GROUP BY

The 52-week high and low for each stock:

SELECT symbol,
       MAX(high) AS high_52w,
       MIN(low)  AS low_52w
FROM daily_prices
WHERE trade_date >= CURRENT_DATE - INTERVAL '365 days'
GROUP BY symbol;

3. Joining tables

Average daily traded volume by sector:

SELECT c.sector,
       AVG(p.volume) AS avg_volume
FROM daily_prices p
JOIN companies c ON c.symbol = p.symbol
WHERE p.trade_date >= CURRENT_DATE - INTERVAL '30 days'
GROUP BY c.sector
ORDER BY avg_volume DESC;

4. Daily returns with LAG

Window functions let you compare a row with the previous row in the same group, which is perfect for time series:

SELECT symbol,
       trade_date,
       close,
       close / LAG(close) OVER (PARTITION BY symbol ORDER BY trade_date) - 1
         AS daily_return
FROM daily_prices;

PARTITION BY symbol makes sure returns are calculated within each stock, never across two different stocks.

5. Moving averages

SELECT symbol,
       trade_date,
       close,
       AVG(close) OVER (
         PARTITION BY symbol
         ORDER BY trade_date
         ROWS BETWEEN 19 PRECEDING AND CURRENT ROW
       ) AS sma_20
FROM daily_prices;

Note that the first 19 rows of each stock average fewer than 20 days. Filter them out if you need a full window.

6. Ranking top gainers per sector

WITH returns AS (
  SELECT p.symbol, c.sector, p.trade_date,
         p.close / LAG(p.close) OVER (PARTITION BY p.symbol ORDER BY p.trade_date) - 1
           AS daily_return
  FROM daily_prices p
  JOIN companies c ON c.symbol = p.symbol
)
SELECT *
FROM (
  SELECT r.*,
         RANK() OVER (PARTITION BY sector ORDER BY daily_return DESC) AS rnk
  FROM returns r
  WHERE trade_date = '2026-11-30'
) ranked
WHERE rnk <= 3;

Common table expressions (WITH) keep complex queries readable by breaking them into named steps.

7. Data quality checks

-- Duplicate rows for the same stock and date
SELECT symbol, trade_date, COUNT(*)
FROM daily_prices
GROUP BY symbol, trade_date
HAVING COUNT(*) > 1;

-- Impossible prices
SELECT *
FROM daily_prices
WHERE high < low OR close > high OR close < low;

Checks like these catch bad data before it reaches a report or an app.

Practise and apply

Load a public price dataset into a free database such as PostgreSQL or SQLite and try each query. Then combine SQL with Python analysis. These are core skills for the Data Analyst Internship at Bluestock.

SQLWindow FunctionsFinancial DataData Analyst Internship
Share this article: