You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SQLite-JS examples for custom SQL logic written in JavaScript.
category
platform
status
publish
slug
sqlite-js-examples
Examples
Example 1: String Manipulation
-- Create a function to extract domain from emailSELECT js_create_scalar('get_domain', '(function(args) { const email = args[0]; return email.split("@")[1] || null;})');
-- Use it in a querySELECT email, get_domain(email) AS domain FROM users;
Example 2: Statistical Aggregation
-- Create a function to calculate standard deviationSELECT js_create_aggregate('stddev',
'sum = 0; sumSq = 0; count = 0;',
'(function(args) { const val = args[0]; sum += val; sumSq += val * val; count++; })',
'(function() { if (count < 2) return null; const variance = (sumSq - (sum * sum) / count) / (count - 1); return Math.sqrt(variance); })'
);
-- Use it in a querySELECT department, stddev(salary) FROM employees GROUP BY department;
Example 3: Custom Window Function
-- Create a window function to calculate percentile within a windowSELECT js_create_window('percentile_rank',
'values = [];',
'(function(args) { values.push(args[0]); })',
'(function() { values.sort((a, b) => a - b); })',
'(function() { const current = values[values.length - 1]; const rank = values.indexOf(current); return (rank / (values.length - 1)) * 100; })',
'(function(args) { const index = values.indexOf(args[0]); if (index !== -1) { values.splice(index, 1); } })'
);
-- Use it in a querySELECT name, score,
percentile_rank(score) OVER (ORDER BY score)
FROM exam_results;