How to Export Shopify Data to a SQL Database for Custom Analytics
Content
How to Export Shopify Data to a SQL Database for Custom Analytics
How to Export Shopify Data to a SQL Database for Custom Analytics
How to Export Shopify Data to a SQL Database for Custom Analytics
Shopify's admin dashboard shows you sales trends and basic metrics. But try answering these questions with it:
What's the average time between a customer's first and second purchase?
Which product combinations are most frequently bought together?
How does customer lifetime value differ by acquisition source?
What's my true profit margin after returns, discounts, and shipping?
You can't. These require SQL access to your raw data. Here's how to set it up.
Option 1: CSV Export (Quick & Free)
The simplest approach. Export data from Shopify Admin and import into a local database.
What You Can Export
Orders (Admin → Orders → Export)
Products (Admin → Products → Export)
Customers (Admin → Customers → Export)
Discount codes (Admin → Discounts)
Financial reports (Admin → Analytics → Reports)
Import into MySQL
-- Create orders tableCREATE TABLE shopify_orders(id BIGINT PRIMARY KEY,email VARCHAR(255),financial_status VARCHAR(50),fulfillment_status VARCHAR(50),total_price DECIMAL(10,2),subtotal_price DECIMAL(10,2),total_discounts DECIMAL(10,2),total_tax DECIMAL(10,2),currency VARCHAR(3),created_at DATETIME,updated_at DATETIME,cancelled_at DATETIME,referring_site VARCHAR(500),landing_site VARCHAR(500),source_name VARCHAR(100),discount_codes TEXT,shipping_country VARCHAR(100),shipping_city VARCHAR(100));
-- Load CSVLOAD DATA LOCAL INFILE '/path/to/orders_export.csv'INTO TABLE shopify_ordersFIELDS TERMINATED BY ','ENCLOSED BY '"'LINES TERMINATED BY '
'
IGNORE 1ROWS;
Pros & Cons
Pro: Free, no tools needed
Pro: Works with any database
Con: Manual process, data gets stale
Con: Limited to what Shopify exports
Option 2: Shopify API + Script (Automated)
Write a script that pulls data from Shopify's REST or GraphQL API on a schedule.
Key API Endpoints
Endpoint
Data
Rate Limit
/admin/api/orders.json
Orders with line items
2 req/sec
/admin/api/products.json
Product catalog
2 req/sec
/admin/api/customers.json
Customer profiles
2 req/sec
/admin/api/inventory_levels.json
Stock levels
2 req/sec
Example: Sync Orders to PostgreSQL
-- Table structure matching Shopify API responseCREATE TABLE shopify_orders(id BIGINTPRIMARY KEY,order_number INTEGER,customer_id BIGINT,email VARCHAR(255),total_price DECIMAL(10,2),subtotal_price DECIMAL(10,2),total_discounts DECIMAL(10,2),total_tax DECIMAL(10,2),financial_status VARCHAR(50),fulfillment_status VARCHAR(50),referring_site TEXT,landing_site TEXT,source_name VARCHAR(100),created_at TIMESTAMPTZ,updated_at TIMESTAMPTZ,cancelled_at TIMESTAMPTZ,tags TEXT,note TEXT);CREATE INDEX idx_orders_created ON shopify_orders(created_at);CREATE INDEX idx_orders_customer ON shopify_orders(customer_id);CREATE INDEX idx_orders_status ON shopify_orders(financial_status);
Option 3: ETL Tools (Best for Scale)
For stores with 1,000+ orders/month, use a managed ETL pipeline:
Tool
Destination
Price
Setup Time
Stitch
BigQuery, Redshift, PostgreSQL
From $100/mo
15 minutes
Fivetran
Snowflake, BigQuery, Redshift
From $250/mo
10 minutes
Airbyte
Any SQL database
Free (self-hosted)
30 minutes
Coupler.io
BigQuery, PostgreSQL
From $49/mo
5 minutes
These tools handle incremental syncs, schema changes, and API pagination automatically.
Option 4: ShopifyQL (Shopify Plus)
If you're on Shopify Plus, you get ShopifyQL Notebooks for direct querying:
FROM ordersSHOW total_sales,order_countGROUP BY daySINCE -30dORDER BY day
ShopifyQL is simpler than SQL but limited in capabilities. For complex analysis, export to a real database.
Queries You Can Run Once Data Is in SQL
Customer Acquisition Cost by Channel
SELECTCASEWHEN referring_site LIKE '%google%'THEN 'Google'WHEN referring_site LIKE '%facebook%'OR referring_site LIKE '%instagram%'THEN 'Meta'WHEN referring_site LIKE '%tiktok%'THEN 'TikTok'WHEN referring_site IS NULL THEN 'Direct'ELSE 'Other'ENDas channel,COUNT(DISTINCT customer_id)as new_customers,SUM(total_price)as revenue,AVG(total_price)as avg_order
FROM shopify_ordersWHERE financial_status = 'paid'AND cancelled_at IS NULLAND created_at >= DATE_SUB(CURRENT_DATE,INTERVAL 90DAY)GROUP BY channelORDER BY new_customers DESC;
Product Pair Analysis (Bought Together)
SELECTli1.titleas product_a,li2.titleas product_b,COUNT(DISTINCT li1.order_id)as times_together
FROM order_line_items li1JOIN order_line_items li2ON li1.order_id = li2.order_idAND li1.product_id < li2.product_idGROUP BY li1.title,li2.titleHAVING times_together >= 5ORDER BY times_together DESCLIMIT 20;
Time Between Purchases
SELECTROUND(AVG(days_between),0)as avg_days_between_orders,ROUND(PERCENTILE_CONT(0.5)WITHIN GROUP(ORDER BYdays_between),0)as median_daysFROM(SELECT
customer_id,DATEDIFF(created_at,LAG(created_at) OVER (PARTITION BYcustomer_idORDERBYcreated_at))as days_between
FROM shopify_ordersWHERE financial_status = 'paid'AND cancelled_at IS NULL)intervalsWHERE days_between IS NOT NULL;
Let AI Generate Your Queries
Setting up the data pipeline is the hard part. Once your Shopify data is in SQL, getting insights should be easy.
AI2SQL turns your business questions into SQL queries instantly. No syntax memorization, no JOIN debugging, no date function lookups.
Set up your data pipeline. Let AI handle the queries. Start at ai2sql.io.