r/SQLv2 • u/Alternative_Pin9598 • Oct 07 '25
Quick video how to do RAG using SQL and databases
Quick video shows how you do RAG using your databases and table without the need to use vector storage.
r/SQLv2 • u/Alternative_Pin9598 • Oct 07 '25
Quick video shows how you do RAG using your databases and table without the need to use vector storage.
r/SQLv2 • u/Alternative_Pin9598 • Oct 03 '25
r/SQLv2 • u/Alternative_Pin9598 • Oct 03 '25
Score compounds for likelihood of success. Prioritize top candidates for lab testing.
(
compound_id BIGINT PRIMARY KEY,
molecular_weight DOUBLE,
hydrogen_bonds INT,
logP DOUBLE, -- lipid solubility
toxicity_score DOUBLE, -- lower is better
binding_affinity DOUBLE, -- higher magnitude = stronger binding
target_label INT -- 1 promising, 0 fail (null = unknown)
);
INSERT INTO compound_properties (compound_id, molecular_weight, hydrogen_bonds, logP, toxicity_score, binding_affinity, target_label ) VALUES
(1001, 350.2, 3, 2.1, 0.12, -8.7, 1),
(1002, 510.8, 1, 4.3, 0.45, -6.1, 0),
(1003, 420.0, 2, 3.2, 0.18, -9.0, 1),
(1004, 295.5, 4, 1.9, 0.10, -7.4, 1),
(1005, 560.1, 0, 5.0, 0.60, -5.2, 0),
(1006, 380.4, 3, 2.8, 0.22, -7.9, 1),
(1007, 445.2, 2, 3.5, 0.31, -6.8, 0),
(1008, 312.7, 4, 1.5, 0.15, -8.2, 1),
(1009, 489.9, 1, 4.1, 0.38, -6.4, 0),
(1010, 367.3, 3, 2.4, 0.20, -8.5, 1),
(1011, 523.5, 0, 4.8, 0.52, -5.5, 0),
(1012, 401.8, 2, 3.0, 0.25, -7.7, NULL),
(1013, 338.9, 4, 1.7, 0.13, -8.8, 1),
(1014, 475.6, 1, 3.9, 0.41, -6.0, 0),
(1015, 392.1, 3, 2.6, 0.19, -8.1, 1),
(1016, 455.8, 2, 3.7, 0.35, -6.6, 0),
(1017, 325.4, 4, 1.8, 0.11, -9.2, 1),
(1018, 508.2, 1, 4.4, 0.47, -5.8, 0),
(1019, 372.9, 3, 2.3, 0.17, -8.3, NULL),
(1020, 441.5, 2, 3.4, 0.29, -7.0, 0),
(1021, 298.8, 5, 1.2, 0.08, -9.5, 1),
(1022, 516.7, 0, 4.9, 0.55, -5.3, 0),
(1023, 385.3, 3, 2.7, 0.21, -7.8, 1),
(1024, 463.1, 2, 3.8, 0.37, -6.3, 0),
(1025, 349.6, 4, 2.0, 0.14, -8.6, 1),
(1026, 497.4, 1, 4.2, 0.44, -5.9, NULL),
(1027, 410.2, 2, 3.1, 0.26, -7.5, 1),
(1028, 358.7, 3, 2.5, 0.16, -8.4, 1),
(1029, 481.9, 1, 4.0, 0.39, -6.2, 0),
(1030, 426.5, 2, 3.3, 0.28, -7.2, NULL),
(1031, 305.1, 5, 1.4, 0.09, -9.3, 1),
(1032, 532.8, 0, 5.1, 0.58, -5.1, 0),
(1033, 397.7, 3, 2.9, 0.24, -7.6, 1),
(1034, 470.3, 1, 3.6, 0.33, -6.7, 0),
(1035, 344.2, 4, 1.6, 0.12, -8.9, 1),
(1036, 418.9, 2, 3.2, 0.27, -7.3, NULL),
(1037, 502.1, 1, 4.5, 0.48, -5.7, 0),
(1038, 361.5, 3, 2.2, 0.18, -8.0, 1),
(1039, 436.8, 2, 3.5, 0.32, -6.9, 0),
(1040, 319.3, 4, 1.9, 0.10, -9.1, 1),
(1041, 486.6, 1, 4.3, 0.42, -6.1, 0),
(1042, 376.1, 3, 2.8, 0.23, -7.9, NULL),
(1043, 449.7, 2, 3.7, 0.36, -6.5, 0),
(1044, 333.8, 4, 1.5, 0.11, -8.7, 1),
(1045, 513.4, 0, 4.7, 0.51, -5.4, 0),
(1046, 405.5, 2, 3.0, 0.25, -7.4, 1),
(1047, 352.0, 3, 2.4, 0.15, -8.5, NULL),
(1048, 477.2, 1, 3.9, 0.40, -6.0, 0),
(1049, 390.6, 3, 2.6, 0.20, -8.1, 1),
(1050, 458.5, 2, 3.8, 0.34, -6.6, 0),
(1051, 322.7, 5, 1.3, 0.07, -9.6, 1),
(1052, 521.9, 0, 5.0, 0.56, -5.2, 0),
(1053, 383.4, 3, 2.7, 0.19, -8.2, 1),
(1054, 466.9, 1, 4.1, 0.38, -6.3, NULL),
(1055, 347.1, 4, 1.8, 0.13, -8.8, 1),
(1056, 494.3, 1, 4.4, 0.45, -5.8, 0),
(1057, 413.8, 2, 3.1, 0.26, -7.5, 1),
(1058, 356.4, 3, 2.5, 0.17, -8.3, NULL),
(1059, 429.2, 2, 3.3, 0.30, -7.1, 0),
(1060, 308.5, 5, 1.1, 0.08, -9.4, 1),
(1061, 535.7, 0, 5.2, 0.59, -5.0, 0),
(1062, 399.9, 3, 2.9, 0.24, -7.7, 1),
(1063, 473.6, 1, 3.6, 0.35, -6.8, 0),
(1064, 341.3, 4, 1.7, 0.12, -8.9, NULL),
(1065, 415.7, 2, 3.2, 0.28, -7.3, 1),
(1066, 505.8, 1, 4.5, 0.49, -5.6, 0),
(1067, 364.2, 3, 2.3, 0.16, -8.4, 1),
(1068, 439.5, 2, 3.4, 0.31, -6.9, NULL),
(1069, 316.6, 4, 1.6, 0.09, -9.2, 1),
(1070, 492.1, 1, 4.2, 0.43, -6.0, 0),
(1071, 378.8, 3, 2.8, 0.22, -7.8, 1),
(1072, 452.3, 2, 3.7, 0.37, -6.4, 0),
(1073, 336.1, 4, 1.9, 0.11, -8.6, 1),
(1074, 510.5, 0, 4.8, 0.53, -5.3, NULL),
(1075, 408.4, 2, 3.0, 0.25, -7.6, 1),
(1076, 354.9, 3, 2.2, 0.14, -8.5, 1),
(1077, 480.7, 1, 4.0, 0.41, -6.2, 0),
(1078, 393.2, 3, 2.6, 0.21, -8.0, NULL),
(1079, 461.4, 2, 3.8, 0.36, -6.5, 0),
(1080, 327.8, 5, 1.4, 0.08, -9.3, 1),
(1081, 525.1, 0, 4.9, 0.54, -5.2, 0),
(1082, 387.6, 3, 2.7, 0.20, -8.1, 1),
(1083, 468.2, 1, 3.9, 0.39, -6.3, 0),
(1084, 350.5, 4, 2.0, 0.14, -8.7, NULL),
(1085, 497.9, 1, 4.3, 0.46, -5.7, 0),
(1086, 411.3, 2, 3.1, 0.27, -7.4, 1),
(1087, 359.8, 3, 2.5, 0.18, -8.3, 1),
(1088, 433.6, 2, 3.5, 0.32, -7.0, NULL),
(1089, 312.2, 5, 1.2, 0.07, -9.5, 1),
(1090, 538.3, 0, 5.1, 0.60, -4.9, 0),
(1091, 402.7, 3, 2.9, 0.23, -7.7, 1),
(1092, 476.4, 1, 3.7, 0.34, -6.7, 0),
(1093, 345.0, 4, 1.8, 0.13, -8.8, 1),
(1094, 419.8, 2, 3.2, 0.29, -7.2, NULL),
(1095, 500.2, 1, 4.4, 0.47, -5.6, 0),
(1096, 366.7, 3, 2.4, 0.17, -8.2, 1),
(1097, 442.1, 2, 3.4, 0.33, -6.8, 0),
(1098, 320.4, 4, 1.7, 0.10, -9.0, 1),
(1099, 488.8, 1, 4.1, 0.42, -6.1, NULL),
(1100, 374.3, 3, 2.6, 0.21, -7.9, 1),
(1101, 448.9, 2, 3.6, 0.35, -6.6, 0),
(1102, 330.6, 4, 1.5, 0.11, -8.9, 1),
(1103, 514.2, 0, 4.7, 0.50, -5.4, 0),
(1104, 395.1, 3, 2.8, 0.22, -7.8, NULL),
(1105, 463.7, 1, 3.8, 0.38, -6.4, 0),
(1106, 342.9, 4, 1.9, 0.12, -8.7, 1),
(1107, 507.5, 1, 4.5, 0.48, -5.5, 0),
(1108, 381.5, 3, 2.3, 0.19, -8.0, 1),
(1109, 456.2, 2, 3.7, 0.36, -6.5, NULL),
(1110, 324.8, 5, 1.3, 0.09, -9.4, 1);
CREATE EXPERIMENT drug_discovery_rf AS
SELECT
molecular_weight,
hydrogen_bonds,
logP,
toxicity_score,
binding_affinity,
target_label AS target
FROM compound_properties
WHERE target_label IS NOT NULL
WITH (
task_type = 'classification',
target_column = 'target',
algorithms = ['random_forest'],
optimization_metric = 'roc_auc',
validation_strategy = 'kfold',
n_folds = 10,
class_weight = 'balanced',
algorithm_params = {'n_estimators': 500, 'max_depth': 12, 'min_samples_leaf': 3}
);
DEPLOY MODEL compound_model FROM EXPERIMENT drug_discovery_rf;
-- score all with unknown labels
PREDICT success_probability USING compound_model AS
SELECT
compound_id,
molecular_weight,
hydrogen_bonds,
logP,
toxicity_score,
binding_affinity
FROM compound_properties
WHERE target_label IS NULL;
-- keep top 5% for lab follow-up
SELECT *
FROM (
PREDICT success_probability USING compound_model AS
SELECT compound_id, molecular_weight, hydrogen_bonds, logP, toxicity_score, binding_affinity
FROM compound_properties
WHERE target_label IS NULL
) s
ORDER BY success_probability DESC
LIMIT (SELECT CEIL(COUNT(*) * 0.05) FROM compound_properties WHERE target_label IS NULL);
-- out-of-fold metrics
SELECT roc_auc, pr_auc, accuracy, precision, recall, f1
FROM automl_experiments
WHERE name = 'drug_discovery_rf';
r/SQLv2 • u/Alternative_Pin9598 • Oct 03 '25
This recipe demonstrates how to build a high-accuracy customer churn prediction model using SynapCores AutoML capabilities with properly normalized data. The recipe includes feature engineering, data normalization, and balanced training data to achieve >85% accuracy.
-- Create main customer data table
CREATE TABLE customer_data (
customer_id INTEGER PRIMARY KEY,
age INTEGER,
tenure_months INTEGER,
monthly_charges DECIMAL(10,2),
total_charges DECIMAL(10,2),
num_services INTEGER,
contract_type VARCHAR(20),
payment_method VARCHAR(50),
has_phone_service BOOLEAN,
has_internet BOOLEAN,
has_streaming BOOLEAN,
has_security BOOLEAN,
has_backup BOOLEAN,
has_support BOOLEAN,
is_paperless BOOLEAN,
num_dependents INTEGER,
churned BOOLEAN
);-- Insert balanced and realistic training data
-- First 20: Churned customers (typically short tenure, month-to-month, electronic check)
INSERT INTO customer_data VALUES
(1, 23, 2, 65.45, 130.90, 2, 'Month-to-month', 'Electronic check', true, true, false, false, false, false, false, 0, true),
(2, 28, 3, 78.30, 234.90, 3, 'Month-to-month', 'Electronic check', true, true, true, false, false, false, false, 0, true),
(3, 31, 1, 89.95, 89.95, 4, 'Month-to-month', 'Electronic check', true, true, true, true, false, false, false, 1, true),
(4, 26, 4, 55.20, 220.80, 2, 'Month-to-month', 'Mailed check', false, true, false, false, false, false, false, 0, true),
(5, 22, 2, 45.85, 91.70, 1, 'Month-to-month', 'Electronic check', false, true, false, false, false, false, false, 0, true),
(6, 29, 5, 92.75, 463.75, 5, 'Month-to-month', 'Electronic check', true, true, true, true, true, false, false, 0, true),
(7, 33, 3, 68.95, 206.85, 3, 'Month-to-month', 'Mailed check', true, true, false, false, false, true, false, 1, true),
(8, 24, 1, 75.40, 75.40, 3, 'Month-to-month', 'Electronic check', true, true, true, false, false, false, false, 0, true),
(9, 30, 6, 85.25, 511.50, 4, 'Month-to-month', 'Electronic check', true, true, true, true, false, false, true, 0, true),
(10, 27, 2, 58.90, 117.80, 2, 'Month-to-month', 'Mailed check', true, false, false, false, false, false, false, 0, true),
(11, 35, 8, 95.50, 764.00, 5, 'Month-to-month', 'Electronic check', true, true, true, true, true, false, false, 2, true),
(12, 21, 3, 49.95, 149.85, 2, 'Month-to-month', 'Electronic check', false, true, true, false, false, false, false, 0, true),
(13, 32, 7, 88.15, 617.05, 4, 'Month-to-month', 'Mailed check', true, true, true, false, false, true, false, 1, true),
(14, 25, 4, 72.45, 289.80, 3, 'Month-to-month', 'Electronic check', true, true, false, true, false, false, false, 0, true),
(15, 28, 5, 61.25, 306.25, 3, 'Month-to-month', 'Electronic check', true, true, true, false, false, false, true, 0, true),
(16, 34, 9, 99.90, 899.10, 6, 'Month-to-month', 'Electronic check', true, true, true, true, true, true, false, 1, true),
(17, 26, 2, 54.30, 108.60, 2, 'Month-to-month', 'Mailed check', false, true, false, false, false, false, false, 0, true),
(18, 31, 6, 83.75, 502.50, 4, 'Month-to-month', 'Electronic check', true, true, true, true, false, false, false, 2, true),
(19, 29, 4, 76.20, 304.80, 3, 'Month-to-month', 'Electronic check', true, true, false, false, true, false, true, 0, true),
(20, 23, 3, 69.55, 208.65, 3, 'Month-to-month', 'Mailed check', true, true, true, false, false, false, false, 0, true),
-- Non-churned customers (typically longer tenure, contracts, better payment methods)
(21, 45, 48, 85.30, 4094.40, 5, 'Two year', 'Credit card', true, true, true, true, true, false, true, 2, false),
(22, 52, 72, 95.75, 6894.00, 6, 'Two year', 'Bank transfer', true, true, true, true, true, true, true, 3, false),
(23, 38, 36, 65.50, 2358.00, 4, 'Two year', 'Credit card', true, true, false, true, true, false, true, 1, false),
(24, 41, 24, 55.25, 1326.00, 3, 'One year', 'Credit card', true, true, false, false, true, false, true, 0, false),
(25, 55, 60, 108.95, 6537.00, 7, 'Two year', 'Bank transfer', true, true, true, true, true, true, true, 2, false),
(26, 48, 54, 79.80, 4309.20, 4, 'Two year', 'Credit card', true, true, true, false, false, true, true, 1, false),
(27, 36, 42, 72.15, 3030.30, 4, 'One year', 'Bank transfer', true, true, true, true, false, false, true, 2, false),
(28, 50, 66, 89.90, 5933.40, 5, 'Two year', 'Credit card', true, true, true, true, true, false, true, 0, false),
(29, 43, 30, 61.45, 1843.50, 3, 'One year', 'Credit card', true, true, false, false, false, true, true, 1, false),
(30, 39, 48, 98.75, 4740.00, 6, 'Two year', 'Bank transfer', true, true, true, true, true, true, true, 3, false),
(31, 46, 36, 68.90, 2480.40, 4, 'Two year', 'Credit card', true, true, false, true, true, false, true, 0, false),
(32, 42, 24, 75.55, 1813.20, 4, 'One year', 'Bank transfer', true, true, true, false, true, false, true, 2, false),
(33, 49, 60, 105.20, 6312.00, 7, 'Two year', 'Credit card', true, true, true, true, true, true, true, 1, false),
(34, 37, 18, 58.35, 1050.30, 3, 'One year', 'Credit card', false, true, true, false, false, false, true, 0, false),
(35, 44, 48, 92.40, 4435.20, 5, 'Two year', 'Bank transfer', true, true, true, true, false, true, true, 2, false),
(36, 51, 72, 110.85, 7981.20, 7, 'Two year', 'Credit card', true, true, true, true, true, true, true, 3, false),
(37, 40, 30, 64.70, 1941.00, 3, 'One year', 'Bank transfer', true, true, false, false, true, false, true, 1, false),
(38, 47, 54, 88.25, 4765.50, 5, 'Two year', 'Credit card', true, true, true, true, true, false, true, 0, false),
(39, 35, 42, 71.90, 3019.80, 4, 'Two year', 'Bank transfer', true, true, true, false, false, true, true, 2, false),
(40, 53, 66, 99.95, 6596.70, 6, 'Two year', 'Credit card', true, true, true, true, true, true, true, 1, false),
-- Mixed data for better training
(41, 29, 12, 82.45, 989.40, 4, 'Month-to-month', 'Credit card', true, true, true, true, false, false, true, 0, false),
(42, 33, 15, 73.20, 1098.00, 3, 'Month-to-month', 'Bank transfer', true, true, false, false, true, false, true, 1, false),
(43, 27, 10, 91.85, 918.50, 5, 'Month-to-month', 'Electronic check', true, true, true, true, true, false, false, 0, true),
(44, 38, 18, 66.50, 1197.00, 3, 'One year', 'Electronic check', true, true, false, false, false, true, false, 2, true),
(45, 41, 22, 78.95, 1736.90, 4, 'One year', 'Credit card', true, true, true, false, true, false, true, 0, false),
(46, 30, 8, 59.75, 478.00, 2, 'Month-to-month', 'Electronic check', false, true, false, false, false, false, false, 0, true),
(47, 45, 28, 87.30, 2444.40, 5, 'One year', 'Bank transfer', true, true, true, true, false, true, true, 1, false),
(48, 36, 14, 94.65, 1325.10, 6, 'Month-to-month', 'Mailed check', true, true, true, true, true, true, false, 0, true),
(49, 42, 32, 70.85, 2267.20, 4, 'Two year', 'Credit card', true, true, false, true, true, false, true, 2, false),
(50, 31, 11, 63.40, 697.40, 3, 'Month-to-month', 'Electronic check', true, true, true, false, false, false, false, 0, true),
(51, 39, 25, 76.80, 1920.00, 4, 'One year', 'Credit card', true, true, true, false, true, false, true, 1, false),
(52, 34, 9, 88.95, 800.55, 5, 'Month-to-month', 'Electronic check', true, true, true, true, false, true, false, 0, true),
(53, 46, 38, 92.15, 3501.70, 5, 'Two year', 'Bank transfer', true, true, true, true, false, true, true, 2, false),
(54, 28, 7, 54.60, 382.20, 2, 'Month-to-month', 'Mailed check', false, true, false, false, false, false, false, 0, true),
(55, 43, 45, 81.25, 3656.25, 4, 'Two year', 'Credit card', true, true, false, true, true, false, true, 1, false),
(56, 37, 13, 97.50, 1267.50, 6, 'Month-to-month', 'Electronic check', true, true, true, true, true, true, false, 0, true),
(57, 50, 52, 73.85, 3840.20, 4, 'Two year', 'Bank transfer', true, true, true, false, false, true, true, 3, false),
(58, 32, 16, 65.20, 1043.20, 3, 'Month-to-month', 'Credit card', true, true, false, false, true, false, true, 0, false),
(59, 40, 20, 89.75, 1795.00, 5, 'One year', 'Bank transfer', true, true, true, true, true, false, true, 2, false),
(60, 26, 6, 71.30, 427.80, 3, 'Month-to-month', 'Electronic check', true, true, true, false, false, false, false, 0, true),
-- Additional 40 records for robust training
(61, 44, 26, 85.60, 2225.60, 5, 'One year', 'Credit card', true, true, true, true, false, true, true, 1, false),
(62, 25, 5, 62.45, 312.25, 3, 'Month-to-month', 'Electronic check', true, true, false, false, false, false, false, 0, true),
(63, 48, 44, 98.20, 4320.80, 6, 'Two year', 'Bank transfer', true, true, true, true, true, true, true, 2, false),
(64, 33, 8, 77.85, 622.80, 4, 'Month-to-month', 'Mailed check', true, true, true, false, false, false, false, 0, true),
(65, 41, 34, 69.90, 2376.60, 4, 'Two year', 'Credit card', true, true, false, true, true, false, true, 1, false),
(66, 29, 4, 93.25, 373.00, 5, 'Month-to-month', 'Electronic check', true, true, true, true, true, false, false, 0, true),
(67, 52, 58, 104.75, 6075.50, 7, 'Two year', 'Bank transfer', true, true, true, true, true, true, true, 3, false),
(68, 35, 12, 58.80, 705.60, 3, 'Month-to-month', 'Credit card', true, true, false, false, false, true, true, 0, false),
(69, 47, 40, 86.35, 3454.00, 5, 'Two year', 'Credit card', true, true, true, true, false, true, true, 2, false),
(70, 30, 7, 74.95, 524.65, 4, 'Month-to-month', 'Electronic check', true, true, true, false, false, false, false, 0, true),
(71, 38, 21, 91.40, 1919.40, 5, 'One year', 'Bank transfer', true, true, true, true, true, false, true, 1, false),
(72, 27, 3, 56.75, 170.25, 2, 'Month-to-month', 'Mailed check', false, true, false, false, false, false, false, 0, true),
(73, 45, 50, 79.65, 3982.50, 4, 'Two year', 'Credit card', true, true, true, false, false, true, true, 2, false),
(74, 31, 9, 83.20, 748.80, 4, 'Month-to-month', 'Electronic check', true, true, true, true, false, false, false, 0, true),
(75, 49, 42, 96.85, 4067.70, 6, 'Two year', 'Bank transfer', true, true, true, true, true, true, true, 1, false),
(76, 34, 6, 67.30, 403.80, 3, 'Month-to-month', 'Electronic check', true, true, false, false, false, false, false, 0, true),
(77, 42, 36, 72.50, 2610.00, 4, 'Two year', 'Credit card', true, true, false, true, true, false, true, 3, false),
(78, 28, 11, 89.15, 980.65, 5, 'Month-to-month', 'Mailed check', true, true, true, true, false, true, false, 0, true),
(79, 46, 48, 101.90, 4891.20, 6, 'Two year', 'Bank transfer', true, true, true, true, true, true, true, 2, false),
(80, 36, 14, 60.25, 843.50, 3, 'Month-to-month', 'Credit card', true, true, false, false, false, false, true, 0, false),
(81, 40, 30, 94.45, 2833.50, 5, 'One year', 'Credit card', true, true, true, true, true, false, true, 1, false),
(82, 24, 2, 51.80, 103.60, 2, 'Month-to-month', 'Electronic check', false, true, false, false, false, false, false, 0, true),
(83, 51, 56, 87.60, 4905.60, 5, 'Two year', 'Bank transfer', true, true, true, true, false, true, true, 2, false),
(84, 32, 10, 76.15, 761.50, 4, 'Month-to-month', 'Electronic check', true, true, true, false, false, false, false, 0, true),
(85, 43, 38, 70.70, 2686.60, 4, 'Two year', 'Credit card', true, true, false, true, true, false, true, 3, false),
(86, 37, 5, 99.35, 496.75, 6, 'Month-to-month', 'Mailed check', true, true, true, true, true, true, false, 0, true),
(87, 48, 46, 82.95, 3815.70, 5, 'Two year', 'Bank transfer', true, true, true, false, true, true, true, 1, false),
(88, 29, 13, 64.85, 843.05, 3, 'Month-to-month', 'Credit card', true, true, false, false, false, true, true, 0, false),
(89, 54, 62, 106.25, 6587.50, 7, 'Two year', 'Credit card', true, true, true, true, true, true, true, 2, false),
(90, 35, 8, 78.40, 627.20, 4, 'Month-to-month', 'Electronic check', true, true, true, false, false, false, false, 0, true),
(91, 44, 28, 90.55, 2535.40, 5, 'One year', 'Bank transfer', true, true, true, true, false, true, true, 1, false),
(92, 26, 4, 53.65, 214.60, 2, 'Month-to-month', 'Electronic check', false, true, false, false, false, false, false, 0, true),
(93, 50, 54, 95.80, 5173.20, 6, 'Two year', 'Credit card', true, true, true, true, true, true, true, 3, false),
(94, 33, 7, 85.35, 597.45, 4, 'Month-to-month', 'Mailed check', true, true, true, true, false, false, false, 0, true),
(95, 41, 32, 73.25, 2344.00, 4, 'Two year', 'Bank transfer', true, true, false, true, true, false, true, 2, false),
(96, 30, 5, 61.90, 309.50, 3, 'Month-to-month', 'Electronic check', true, true, false, false, false, false, false, 0, true),
(97, 47, 44, 88.70, 3902.80, 5, 'Two year', 'Credit card', true, true, true, true, false, true, true, 1, false),
(98, 38, 15, 97.15, 1457.25, 6, 'Month-to-month', 'Credit card', true, true, true, true, true, true, true, 0, false),
(99, 45, 50, 80.05, 4002.50, 4, 'Two year', 'Bank transfer', true, true, true, false, false, true, true, 2, false),
(100, 31, 6, 66.40, 398.40, 3, 'Month-to-month', 'Electronic check', true, true, true, false, false, false, false, 0, true);-- Create a view that normalizes the data for better model training
CREATE VIEW customer_data_normalized AS
SELECT
customer_id,
-- Normalize continuous variables to 0-1 range
(age - 20.0) / 40.0 as age_norm, -- Assuming age range 20-60
(tenure_months - 1.0) / 71.0 as tenure_norm, -- Range 1-72 months
(monthly_charges - 45.0) / 75.0 as monthly_charges_norm, -- Range 45-120
(total_charges - 50.0) / 7950.0 as total_charges_norm, -- Range 50-8000
(num_services - 1.0) / 6.0 as services_norm, -- Range 1-7
(num_dependents) / 3.0 as dependents_norm, -- Range 0-3
-- Derived features for better prediction
total_charges / NULLIF(tenure_months, 0) as avg_monthly_spend,
CASE WHEN tenure_months <= 6 THEN 1.0 ELSE 0.0 END as is_new_customer,
CASE WHEN tenure_months > 24 THEN 1.0 ELSE 0.0 END as is_loyal_customer,
-- One-hot encode categorical variables
CASE WHEN contract_type = 'Month-to-month' THEN 1.0 ELSE 0.0 END as contract_mtm,
CASE WHEN contract_type = 'One year' THEN 1.0 ELSE 0.0 END as contract_1yr,
CASE WHEN contract_type = 'Two year' THEN 1.0 ELSE 0.0 END as contract_2yr,
CASE WHEN payment_method = 'Electronic check' THEN 1.0 ELSE 0.0 END as pay_echeck,
CASE WHEN payment_method = 'Mailed check' THEN 1.0 ELSE 0.0 END as pay_mcheck,
CASE WHEN payment_method = 'Credit card' THEN 1.0 ELSE 0.0 END as pay_credit,
CASE WHEN payment_method = 'Bank transfer' THEN 1.0 ELSE 0.0 END as pay_bank,
-- Boolean features as 1/0
CASE WHEN has_phone_service THEN 1.0 ELSE 0.0 END as phone_service,
CASE WHEN has_internet THEN 1.0 ELSE 0.0 END as internet_service,
CASE WHEN has_streaming THEN 1.0 ELSE 0.0 END as streaming_service,
CASE WHEN has_security THEN 1.0 ELSE 0.0 END as security_service,
CASE WHEN has_backup THEN 1.0 ELSE 0.0 END as backup_service,
CASE WHEN has_support THEN 1.0 ELSE 0.0 END as support_service,
CASE WHEN is_paperless THEN 1.0 ELSE 0.0 END as paperless_billing,
-- Interaction features
(CASE WHEN contract_type = 'Month-to-month' THEN 1.0 ELSE 0.0 END) *
(CASE WHEN payment_method = 'Electronic check' THEN 1.0 ELSE 0.0 END) as high_risk_combo,
-- Target variable
CASE WHEN churned THEN 1.0 ELSE 0.0 END as target
FROM customer_data;-- Create experiment with normalized data
CREATE EXPERIMENT churn_prediction_normalized
FROM (
SELECT
age_norm,
tenure_norm,
monthly_charges_norm,
total_charges_norm,
services_norm,
dependents_norm,
avg_monthly_spend,
is_new_customer,
is_loyal_customer,
contract_mtm,
contract_1yr,
contract_2yr,
pay_echeck,
pay_mcheck,
pay_credit,
pay_bank,
phone_service,
internet_service,
streaming_service,
security_service,
backup_service,
support_service,
paperless_billing,
high_risk_combo,
target
FROM customer_data_normalized
)
TARGET target
OPTIONS (
algorithms = ['logistic_regression', 'random_forest', 'gradient_boosting', 'neural_network', 'svm'],
validation_split = 0.2,
test_split = 0.1,
max_trials = 50,
optimization_metric = 'roc_auc',
early_stopping = true,
cross_validation = 5,
handle_imbalanced = true,
feature_selection = 'auto'
);
-- Start the experiment
START EXPERIMENT churn_prediction_normalized;
-- Monitor experiment progress
SELECT
name,
status,
best_score,
best_algorithm,
training_time_seconds,
created_at
FROM automl_experiments
WHERE name = 'churn_prediction_normalized';-- Create new customers table
CREATE TABLE new_customers (
customer_id INTEGER PRIMARY KEY,
age INTEGER,
tenure_months INTEGER,
monthly_charges DECIMAL(10,2),
total_charges DECIMAL(10,2),
num_services INTEGER,
contract_type VARCHAR(20),
payment_method VARCHAR(50),
has_phone_service BOOLEAN,
has_internet BOOLEAN,
has_streaming BOOLEAN,
has_security BOOLEAN,
has_backup BOOLEAN,
has_support BOOLEAN,
is_paperless BOOLEAN,
num_dependents INTEGER
);
-- Insert new customers for prediction
INSERT INTO new_customers VALUES
(201, 35, 2, 85.50, 171.00, 4, 'Month-to-month', 'Electronic check', true, true, true, false, false, false, false, 0),
(202, 42, 18, 68.90, 1240.20, 3, 'One year', 'Credit card', true, true, false, false, true, false, true, 1),
(203, 28, 5, 92.75, 463.75, 5, 'Month-to-month', 'Mailed check', true, true, true, true, false, true, false, 0),
(204, 51, 30, 72.00, 2160.00, 4, 'Two year', 'Bank transfer', true, true, true, false, false, true, true, 2),
(205, 39, 10, 65.25, 652.50, 3, 'Month-to-month', 'Electronic check', true, true, false, false, false, false, true, 0),
(206, 46, 45, 99.85, 4493.25, 6, 'Two year', 'Credit card', true, true, true, true, true, true, true, 3),
(207, 33, 8, 58.40, 467.20, 2, 'Month-to-month', 'Electronic check', false, true, false, false, false, false, false, 0),
(208, 40, 24, 81.30, 1951.20, 4, 'One year', 'Bank transfer', true, true, true, false, true, false, true, 1),
(209, 27, 3, 77.95, 233.85, 4, 'Month-to-month', 'Mailed check', true, true, true, true, false, false, false, 0),
(210, 55, 60, 105.50, 6330.00, 7, 'Two year', 'Credit card', true, true, true, true, true, true, true, 2);-- Create normalized view for new customers
CREATE VIEW new_customers_normalized AS
SELECT
customer_id,
(age - 20.0) / 40.0 as age_norm,
(tenure_months - 1.0) / 71.0 as tenure_norm,
(monthly_charges - 45.0) / 75.0 as monthly_charges_norm,
(total_charges - 50.0) / 7950.0 as total_charges_norm,
(num_services - 1.0) / 6.0 as services_norm,
(num_dependents) / 3.0 as dependents_norm,
total_charges / NULLIF(tenure_months, 0) as avg_monthly_spend,
CASE WHEN tenure_months <= 6 THEN 1.0 ELSE 0.0 END as is_new_customer,
CASE WHEN tenure_months > 24 THEN 1.0 ELSE 0.0 END as is_loyal_customer,
CASE WHEN contract_type = 'Month-to-month' THEN 1.0 ELSE 0.0 END as contract_mtm,
CASE WHEN contract_type = 'One year' THEN 1.0 ELSE 0.0 END as contract_1yr,
CASE WHEN contract_type = 'Two year' THEN 1.0 ELSE 0.0 END as contract_2yr,
CASE WHEN payment_method = 'Electronic check' THEN 1.0 ELSE 0.0 END as pay_echeck,
CASE WHEN payment_method = 'Mailed check' THEN 1.0 ELSE 0.0 END as pay_mcheck,
CASE WHEN payment_method = 'Credit card' THEN 1.0 ELSE 0.0 END as pay_credit,
CASE WHEN payment_method = 'Bank transfer' THEN 1.0 ELSE 0.0 END as pay_bank,
CASE WHEN has_phone_service THEN 1.0 ELSE 0.0 END as phone_service,
CASE WHEN has_internet THEN 1.0 ELSE 0.0 END as internet_service,
CASE WHEN has_streaming THEN 1.0 ELSE 0.0 END as streaming_service,
CASE WHEN has_security THEN 1.0 ELSE 0.0 END as security_service,
CASE WHEN has_backup THEN 1.0 ELSE 0.0 END as backup_service,
CASE WHEN has_support THEN 1.0 ELSE 0.0 END as support_service,
CASE WHEN is_paperless THEN 1.0 ELSE 0.0 END as paperless_billing,
(CASE WHEN contract_type = 'Month-to-month' THEN 1.0 ELSE 0.0 END) *
(CASE WHEN payment_method = 'Electronic check' THEN 1.0 ELSE 0.0 END) as high_risk_combo
FROM new_customers;-- Deploy the best model from the experiment
DEPLOY MODEL churn_predictor
FROM EXPERIMENT churn_prediction_normalized;
-- Verify deployment
SHOW MODELS WHERE name = 'churn_predictor';-- Simple prediction query
PREDICT churn_probability, confidence
USING churn_predictor
FROM new_customers_normalized;
-- Detailed predictions with risk categorization
SELECT
nc.customer_id,
nc.age,
nc.tenure_months,
nc.contract_type,
nc.payment_method,
nc.monthly_charges,
p.churn_probability,
p.confidence,
CASE
WHEN p.churn_probability >= 0.8 THEN 'High Risk'
WHEN p.churn_probability >= 0.5 THEN 'Medium Risk'
WHEN p.churn_probability >= 0.3 THEN 'Low Risk'
ELSE 'Very Low Risk'
END as risk_category,
CASE
WHEN p.churn_probability >= 0.8 THEN 'Immediate retention offer needed'
WHEN p.churn_probability >= 0.5 THEN 'Proactive engagement recommended'
WHEN p.churn_probability >= 0.3 THEN 'Monitor closely'
ELSE 'Stable customer'
END as recommended_action
FROM new_customers nc
JOIN (
PREDICT churn_probability, confidence
USING churn_predictor
FROM new_customers_normalized
) p ON nc.customer_id = p.customer_id
ORDER BY p.churn_probability DESC;-- View model metrics
SELECT
model_name,
algorithm,
accuracy,
precision,
recall,
f1_score,
roc_auc,
training_time_seconds
FROM model_metrics
WHERE model_name = 'churn_predictor';-- Analyze feature importance
SELECT
feature_name,
importance_score,
ROUND(importance_score * 100, 2) as importance_pct
FROM MODEL_FEATURE_IMPORTANCE('churn_predictor')
ORDER BY importance_score DESC
LIMIT 10;-- Create prediction results table
CREATE TABLE prediction_results AS
SELECT
customer_id,
PREDICT churn_probability USING churn_predictor AS churn_score
FROM new_customers_normalized;
-- Analyze risk distribution
SELECT
CASE
WHEN churn_score < 0.3 THEN 'Low Risk (0-30%)'
WHEN churn_score < 0.5 THEN 'Medium-Low Risk (30-50%)'
WHEN churn_score < 0.7 THEN 'Medium-High Risk (50-70%)'
ELSE 'High Risk (70-100%)'
END as risk_category,
COUNT(*) as customer_count,
ROUND(AVG(churn_score), 3) as avg_probability,
ROUND(MIN(churn_score), 3) as min_probability,
ROUND(MAX(churn_score), 3) as max_probability
FROM prediction_results
GROUP BY risk_category
ORDER BY avg_probability;With properly normalized data and balanced training set:
This recipe provides a production-ready customer churn prediction system with:
The normalized approach ensures consistent, reliable predictions that can drive effective customer retention strategies.
r/SQLv2 • u/Alternative_Pin9598 • Oct 02 '25
Most AI projects today look like this:
Every step = more cost, latency, and complexity.
SQLv2 is an open standard that extends SQL to include:
SENTIMENT(text) – analyze sentiment in the queryEMBED(data) – create embeddings inside SQLCOSINE_SIMILARITY(vec1, vec2) – run vector search inlineGENERATE(prompt, options) – use generative AI as a functionEXPLAIN – understand cost and inference plan like you would for queriesNo ETL. No extra hops. One query does it all.
Instead of:
SELECT comment, SENTIMENT(comment)
FROM customer_feedback;
And you’re done.
👉 Question for you: What’s the biggest pain point in your current ML + SQL workflow? (Cost, latency, ETL, or complexity?)
Let’s discuss 👇
r/SQLv2 • u/Alternative_Pin9598 • Oct 02 '25
How do you quickly analyze what customers feel about your product without exporting data into Python or external ML services?
CREATE TABLE customer_feedback (
id INTEGER PRIMARY KEY,
customer_name TEXT,
comment TEXT,
feedback_date DATE,
sentiment_score TEXT
);INSERT INTO customer_feedback (id, customer_name, comment, feedback_date) VALUES
(1, 'Emma Wilson', 'Amazing headphones! Great sound quality.', '2025-01-12'),
(2, 'Michael Chen', 'Fast delivery but packaging was poor.', '2025-01-10'),
(3, 'Sarah Johnson', 'Coffee maker works fine but instructions unclear.', '2024-12-18'),
(4, 'David Rodriguez', 'Excellent support, solved my issue fast.', '2025-01-14');UPDATE customer_feedback
SET sentiment_score = SENTIMENT(comment);This SQLv2 function runs sentiment analysis inside the database. No ETL, no Python, no API calls.
SELECT customer_name, comment, sentiment_score
FROM customer_feedback;📊 Example Output:
✅ Takeaway: In SQLv2, AI is part of the query. You don’t move data out to analyze sentiment — it happens where the data lives.
👉 Try it yourself here: synapcores.com/sqlv2