Skip to content
TUTORIALBeginner

SQL for AI Analytics

Query, analyze, and extract insights from data

Learn SQL for data analysis and AI workflows. Covers SELECT to complex window functions, CTEs, and AI-assisted query writing. Essential for data, analytics, and AI roles.

22 min4 stepsUpdated 2026-01-22

STEP-BY-STEP GUIDE

How to SQL for AI Analytics

1

Master the SELECT Statement Foundation

-- The anatomy of a SELECT statement (execute in this order)
SELECT   column1, column2, AGG_FUNCTION(column3)  -- 6. What to return
FROM     table_name                                 -- 1. Where to look
JOIN     other_table ON table.id = other_table.id  -- 2. Join data
WHERE    condition                                  -- 3. Filter rows
GROUP BY column1, column2                          -- 4. Group results
HAVING   AGG_FUNCTION(column3) > threshold         -- 5. Filter groups
ORDER BY column1 DESC                              -- 7. Sort
LIMIT    100;                                      -- 8. Limit rows
2

Use Window Functions for Running Calculations

Window functions perform calculations across a set of rows related to the current row — without collapsing rows like GROUP BY does:

-- Running total, rank, lag/lead
SELECT
  date,
  revenue,
  SUM(revenue) OVER (ORDER BY date) AS running_total,
  RANK() OVER (PARTITION BY region ORDER BY revenue DESC) AS rank,
  LAG(revenue, 1) OVER (ORDER BY date) AS prev_day,
  revenue - LAG(revenue,1) OVER (ORDER BY date) AS day_over_day
FROM sales;
3

Write Maintainable Queries with CTEs

Common Table Expressions (CTEs) make complex queries readable by breaking them into named steps:

WITH
applicants AS (
  SELECT user_id, applied_at FROM applications
),
screened AS (
  SELECT user_id FROM phone_screens
),
offered AS (
  SELECT user_id FROM offers
)
SELECT
  COUNT(DISTINCT a.user_id) AS total_applicants,
  COUNT(DISTINCT s.user_id) AS screened,
  COUNT(DISTINCT o.user_id) AS offered,
  ROUND(COUNT(DISTINCT s.user_id)::numeric /
        COUNT(DISTINCT a.user_id) * 100, 1) AS screen_rate_pct
FROM applicants a
LEFT JOIN screened s USING (user_id)
LEFT JOIN offered o USING (user_id);
4

Use AI to Write and Explain SQL

AI dramatically accelerates SQL development. Effective pattern: describe your table schemas and the question you’re trying to answer, then ask the AI to write the query. Always review and test before running in production — AI sometimes generates syntactically valid but logically incorrect queries. After getting a query, ask the AI to explain it clause by clause, then try to write the next query yourself before asking for help.

PRACTICE

Exercises

Write a query that finds the top 10 highest-paying AI jobs from a salary database, broken down by city.

Calculate month-over-month application volume growth using window functions.

Write a CTE chain that builds a funnel analysis: applicants → phone screens → offers → accepted.

Use AI to help you write a complex query, then explain back to the AI what each clause does.

Optimize a slow query using EXPLAIN ANALYZE and appropriate indexing.

CAREER IMPACT

Career Paths That Use This Skill

Career PathHow It's UsedSalary Range
Data AnalystPrimary analysis tool$75K–$120K
Data ScientistData extraction and feature engineering$120K–$190K
ML EngineerData pipeline queries and feature stores$140K–$250K
Finance Analyst (AI)Financial data modeling$90K–$150K

FAQ

Common Questions

Which SQL dialect should I learn first?+
PostgreSQL is the best starting point — it's powerful, widely used, and the concepts transfer to all other dialects (MySQL, BigQuery, Snowflake, SQL Server). BigQuery SQL is worth learning separately for cloud data warehouse roles.
How is SQL used in AI roles?+
ML Engineers use SQL for feature extraction and data pipeline queries. Data Scientists use it for EDA and model input preparation. Analytics roles use it as their primary analysis tool. Even AI Product Managers benefit from reading SQL to validate data assumptions.

Related Academy Tracks

Put this skill into action

Take our quiz to get your personalized learning path and start applying these skills immediately.

Find My Track

Ready to Apply? Use HireKit's Free Tools

AI-powered job search tools for SQL for AI Analytics

hirekit.co — AI-powered job search platform