10 WooCommerce SQL Queries Every Store Owner Needs
Content
10 WooCommerce SQL Queries Every Store Owner Needs
10 WooCommerce SQL Queries Every Store Owner Needs
10 WooCommerce SQL Queries Every Store Owner Needs
Running a WooCommerce store means dealing with data. Orders, customers, products, coupons, refunds. Your WordPress dashboard gives you the basics, but when you need specific answers, the built-in reports fall short.
These 10 queries give you direct access to the insights that actually matter for your business.
Your WooCommerce Database Tables
Table
Purpose
wp_wc_orders
Order records (WC 8.0+)
wp_wc_order_stats
Order statistics and totals
wp_wc_order_product_lookup
Products within each order
wp_wc_customer_lookup
Customer data
wp_posts + wp_postmeta
Legacy order storage
Query 1: Total Revenue This Month
Ask: "What's my total revenue this month?"
SELECTSUM(total_sales)as revenue,COUNT(*)as order_count,AVG(total_sales)as avg_order_value
FROM wp_wc_order_statsWHERE status IN(wc-completed,wc-processing)AND date_created >= DATE_FORMAT(NOW(), %Y-%m-01);
Query 2: Top 10 Best-Selling Products
SELECTp.post_titleas product_name,SUM(ol.product_qty)as units_sold,SUM(ol.product_net_revenue)as revenue
FROM wp_wc_order_product_lookup olJOIN wp_posts p ON ol.product_id = p.IDJOIN wp_wc_order_stats os ON ol.order_id = os.order_idWHERE os.statusIN(wc-completed,wc-processing)AND os.date_created >= DATE_SUB(NOW(),INTERVAL 30DAY)GROUP BY ol.product_idORDER BY units_sold DESCLIMIT 10;
Query 3: Customer Lifetime Value (Top Customers)
SELECTcl.first_name,cl.last_name,cl.email,COUNT(os.order_id)as total_orders,SUM(os.total_sales)as lifetime_value,MIN(os.date_created)as first_order,MAX(os.date_created)as last_order
FROM wp_wc_customer_lookup clJOIN wp_wc_order_stats os ON cl.customer_id = os.customer_idWHERE os.statusIN(wc-completed,wc-processing)GROUP BY cl.customer_idORDER BY lifetime_value DESCLIMIT 20;
Query 4: Abandoned Carts (Pending Orders)
SELECTos.order_id,os.total_sales,os.date_created,cl.email,cl.first_nameFROM wp_wc_order_stats osLEFT JOIN wp_wc_customer_lookup cl ON os.customer_id = cl.customer_idWHERE os.status = wc-pendingAND os.date_created >= DATE_SUB(NOW(),INTERVAL 7DAY)ORDER BY os.total_salesDESC;
Query 5: Revenue by Day (Last 30 Days)
SELECTDATE(date_created)as order_date,COUNT(*)as orders,SUM(total_sales)as daily_revenue
FROM wp_wc_order_statsWHERE status IN(wc-completed,wc-processing)AND date_created >= DATE_SUB(NOW(),INTERVAL 30DAY)GROUP BY DATE(date_created)ORDER BY order_date DESC;
Query 6: Which Coupons Actually Work?
SELECTp.post_titleas coupon_code,COUNT(DISTINCT os.order_id)as times_used,SUM(os.total_sales)as total_revenue
FROM wp_posts pJOIN wp_wc_order_coupon_lookup ocl ON p.ID = ocl.coupon_idJOIN wp_wc_order_stats os ON ocl.order_id = os.order_idWHERE p.post_type = shop_couponAND os.statusIN(wc-completed,wc-processing)GROUP BY p.IDORDER BY times_used DESC;
Query 7: Products That Never Sold
SELECT p.ID,p.post_title,p.post_dateFROM wp_posts pLEFT JOIN wp_wc_order_product_lookup ol ON p.ID = ol.product_idWHERE p.post_type = productAND p.post_status = publishAND ol.order_idIS NULLORDER BY p.post_dateASC;
Query 8: Refund Rate by Product
SELECTp.post_title,COUNT(CASE WHEN os.status = wc-completed THEN 1END)as completed,COUNT(CASE WHEN os.status = wc-refunded THEN 1END)as refunded,ROUND(COUNT(CASE WHENos.status = wc-refunded THEN1END) * 100.0 /
NULLIF(COUNT(*),0),1)as refund_rate
FROM wp_wc_order_product_lookup olJOIN wp_posts p ON ol.product_id = p.IDJOIN wp_wc_order_stats os ON ol.order_id = os.order_idGROUP BY ol.product_idHAVING COUNT(*) >= 5ORDER BY refund_rate DESC;
Query 9: Revenue by Country
SELECTcl.country,COUNT(DISTINCT cl.customer_id)as customers,COUNT(os.order_id)as orders,SUM(os.total_sales)as revenue
FROM wp_wc_customer_lookup clJOIN wp_wc_order_stats os ON cl.customer_id = os.customer_idWHERE os.statusIN(wc-completed,wc-processing)GROUP BY cl.countryORDER BY revenue DESCLIMIT 15;
Query 10: Month-over-Month Growth
SELECTDATE_FORMAT(date_created, %Y-%m)as month,COUNT(*)as orders,SUM(total_sales)as revenue,AVG(total_sales)as avg_order_value
FROM wp_wc_order_statsWHERE status IN(wc-completed,wc-processing)AND date_created >= DATE_SUB(NOW(),INTERVAL 6MONTH)GROUP BY DATE_FORMAT(date_created, %Y-%m)ORDER BY month DESC;
Stop Writing These Queries by Hand
With AI2SQL, you describe what you want in plain English:
"Show me this month's revenue by product category" → done
"Find customers who ordered more than 3 times" → done
"Compare coupon performance for Black Friday" → done
No need to memorize table names, remember JOIN syntax, or worry about WooCommerce version differences. Try it free at ai2sql.io and unlock the data hiding in your WooCommerce database.