Skip to main content
Youngmin Ko
HomeWritingProjectsPlaygroundKnowledge GraphCV
More ⌄
AboutNowCoursesLinksPrivacy
Search ↗
Writing/Computer science study · Data, SQL & systems

SQL for AI Engineers: JOINs, Window Functions, and the Rest

2025-08-12·2 min read
SQLDatabaseData EngineeringFeature Engineering
← Previous Post My SAP Final Interview (And Why I Didn’t Get the Offer) Next Post →LG Aimers Demand Forecasting Hackathon: What I Did (Ensemble Strategy & Experiments)

Loading comments...

← Writing

On this page

  • Why SQL Again?
  • Core Functions: From an AI Engineer's Perspective
  • JOIN/GROUP BY: Foundation of Training Datasets
  • Window Functions: DB-Level Feature Engineering
  • Subqueries/CTEs: Staging Complexity
  • Workflow Impact
© 2023-2026 Youngmin. All rights reserved.
rhdudals0505@naver.comGitHubLinkedIn
Built with Next.js.Updated 2026.11.07

There's a moment in every ML project when Pandas starts feeling slow and you realize most of the heavy lifting should have stayed in the database. This is a quick note on revisiting SQL from an AI engineer's angle: specifically JOIN/GROUP BY, window functions, and CTEs.

Why SQL Again?

Model performance depends on the quality and quantity of data, and most of that data is in databases. Instead of pulling large volumes into Pandas for processing, it's much more efficient to create necessary aggregations or features at the DB level and then transfer only the minimum data needed.

Core Functions: From an AI Engineer's Perspective

JOIN/GROUP BY: Foundation of Training Datasets

Combines multiple tables to create normalized views in units that models can learn from.

Window Functions: DB-Level Feature Engineering

Generates time-series and rank-based features using LAG/LEAD/RANK, etc. Example: Time elapsed since the user's last purchase.

sql
1SELECT
2  user_id,
3  order_timestamp,
4  EXTRACT(EPOCH FROM (
5    order_timestamp - LAG(order_timestamp, 1) OVER (PARTITION BY user_id ORDER BY order_timestamp)
6  )) AS time_since_last_order
7FROM orders;

Subqueries/CTEs: Staging Complexity

Improves readability and reusability, making collaboration and maintenance easier.

Workflow Impact

  • Shift in feature engineering focus: Heavy operations handled by SQL
  • Faster EDA: Understand distributions and outlier outlines with GROUP BY/COUNT
  • Improved communication: Can clearly point out problem areas at schema/partition level