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
sql: "-- Basic SQL Demo\n-- Create a simple employees table\nDROP TABLE IF EXISTS employees;\nCREATE TABLE employees (\n id INTEGER PRIMARY KEY,\n name TEXT NOT NULL,\n department TEXT,\n salary NUMERIC,\n hire_date DATE\n);\n\n-- Insert sample data\nINSERT INTO employees (name, department, salary, hire_date) VALUES\n ('Alice Smith', 'Engineering', 85000, '2020-01-15'),\n ('Bob Johnson', 'Marketing', 72000, '2019-03-20'),\n ('Carol Williams', 'Engineering', 92000, '2018-11-07'),\n ('Dave Brown', 'Finance', 115000, '2017-05-12'),\n ('Eve Davis', 'Engineering', 110000, '2021-08-30');\n\n-- Query the data\nSELECT \n department, \n COUNT(*) as employee_count,\n ROUND(AVG(salary), 2) as avg_salary\nFROM employees\nGROUP BY department\nORDER BY avg_salary DESC;"
35
+
},
36
+
'schema': {
37
+
name: 'Show Schema',
38
+
sql: "-- Show all tables in the database\nSELECT name, sql\nFROM sqlite_master\nWHERE type='table';"
39
+
},
40
+
'blog-app': {
41
+
name: 'Blog App Schema',
42
+
sql: "-- Complete Blog Application Schema\n\n-- Users table\nDROP TABLE IF EXISTS users;\nCREATE TABLE users (\n id INTEGER PRIMARY KEY,\n username TEXT NOT NULL UNIQUE,\n email TEXT UNIQUE,\n password_hash TEXT NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\n-- Insert sample users\nINSERT INTO users (username, email, password_hash, created_at) VALUES\n ('alice', '[email protected]', 'hash1', '2022-01-10'),\n ('bob', '[email protected]', 'hash2', '2022-01-15'),\n ('carol', '[email protected]', 'hash3', '2022-02-20');\n\n-- Posts table\nDROP TABLE IF EXISTS posts;\nCREATE TABLE posts (\n id INTEGER PRIMARY KEY,\n user_id INTEGER NOT NULL,\n title TEXT NOT NULL,\n content TEXT NOT NULL,\n published BOOLEAN DEFAULT 0,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE\n);\n\n-- Insert sample posts\nINSERT INTO posts (user_id, title, content, published, created_at) VALUES\n (1, 'First Post', 'This is my first post content', 1, '2022-01-12'),\n (1, 'Second Post', 'This is another post by Alice', 1, '2022-01-18'),\n (2, 'Hello World', 'Bob\\'s first post content', 1, '2022-01-20'),\n (3, 'Introduction', 'Hello from Carol', 1, '2022-02-25'),\n (2, 'Draft Post', 'This is a draft', 0, '2022-02-28');\n\n-- Comments table\nDROP TABLE IF EXISTS comments;\nCREATE TABLE comments (\n id INTEGER PRIMARY KEY,\n post_id INTEGER NOT NULL,\n user_id INTEGER NOT NULL,\n content TEXT NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (post_id) REFERENCES posts(id) ON DELETE CASCADE,\n FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE\n);\n\n-- Insert sample comments\nINSERT INTO comments (post_id, user_id, content, created_at) VALUES\n (1, 2, 'Great post!', '2022-01-13'),\n (1, 3, 'I agree with Bob', '2022-01-14'),\n (3, 1, 'Welcome Bob!', '2022-01-21'),\n (4, 2, 'Nice to meet you Carol', '2022-02-26');\n\n-- Query: Show posts with comment counts\nSELECT \n p.id, \n p.title, \n u.username as author,\n COUNT(c.id) as comment_count\nFROM posts p\nJOIN users u ON p.user_id = u.id\nLEFT JOIN comments c ON c.post_id = p.id\nWHERE p.published = 1\nGROUP BY p.id\nORDER BY p.created_at DESC;"
43
+
},
44
+
'e-commerce': {
45
+
name: 'E-commerce Schema',
46
+
sql: "-- E-commerce Database Schema\n\n-- Products table\nDROP TABLE IF EXISTS products;\nCREATE TABLE products (\n id INTEGER PRIMARY KEY,\n name TEXT NOT NULL,\n description TEXT,\n price DECIMAL(10,2) NOT NULL,\n stock_quantity INTEGER NOT NULL DEFAULT 0,\n category TEXT\n);\n\n-- Insert sample products\nINSERT INTO products (name, description, price, stock_quantity, category) VALUES\n ('Smartphone', 'Latest model smartphone', 699.99, 50, 'Electronics'),\n ('Laptop', 'High performance laptop', 1299.99, 25, 'Electronics'),\n ('Headphones', 'Noise cancelling headphones', 199.99, 100, 'Electronics'),\n ('T-shirt', 'Cotton t-shirt', 19.99, 200, 'Clothing'),\n ('Jeans', 'Blue denim jeans', 49.99, 150, 'Clothing');\n\n-- Customers table\nDROP TABLE IF EXISTS customers;\nCREATE TABLE customers (\n id INTEGER PRIMARY KEY,\n name TEXT NOT NULL,\n email TEXT UNIQUE,\n address TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\n-- Insert sample customers\nINSERT INTO customers (name, email, address) VALUES\n ('John Doe', '[email protected]', '123 Main St'),\n ('Jane Smith', '[email protected]', '456 Oak Ave'),\n ('Mike Johnson', '[email protected]', '789 Pine Rd');\n\n-- Orders table\nDROP TABLE IF EXISTS orders;\nCREATE TABLE orders (\n id INTEGER PRIMARY KEY,\n customer_id INTEGER NOT NULL,\n order_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT DEFAULT 'pending',\n total DECIMAL(10,2) NOT NULL,\n FOREIGN KEY (customer_id) REFERENCES customers(id)\n);\n\n-- Insert sample orders\nINSERT INTO orders (customer_id, order_date, status, total) VALUES\n (1, '2023-01-15', 'completed', 919.98),\n (2, '2023-01-20', 'completed', 1299.99),\n (3, '2023-02-01', 'processing', 249.98),\n (1, '2023-02-15', 'pending', 199.99);\n\n-- Order items table\nDROP TABLE IF EXISTS order_items;\nCREATE TABLE order_items (\n id INTEGER PRIMARY KEY,\n order_id INTEGER NOT NULL,\n product_id INTEGER NOT NULL,\n quantity INTEGER NOT NULL,\n price DECIMAL(10,2) NOT NULL,\n FOREIGN KEY (order_id) REFERENCES orders(id),\n FOREIGN KEY (product_id) REFERENCES products(id)\n);\n\n-- Insert sample order items\nINSERT INTO order_items (order_id, product_id, quantity, price) VALUES\n (1, 1, 1, 699.99),\n (1, 3, 1, 199.99),\n (2, 2, 1, 1299.99),\n (3, 4, 5, 19.99),\n (3, 5, 3, 49.99),\n (4, 3, 1, 199.99);\n\n-- Query: Show customer orders with items\nSELECT \n c.name as customer,\n o.id as order_id,\n o.order_date,\n o.status,\n p.name as product,\n oi.quantity,\n oi.price,\n (oi.quantity * oi.price) as subtotal\nFROM customers c\nJOIN orders o ON c.id = o.customer_id\nJOIN order_items oi ON o.id = oi.order_id\nJOIN products p ON oi.product_id = p.id\nORDER BY o.order_date DESC, c.name;"
47
+
},
48
+
'recursive-query': {
49
+
name: 'Recursive Query',
50
+
sql: "-- Employee Hierarchy with Recursive CTE\n\n-- Create employees table with manager relationship\nDROP TABLE IF EXISTS employees;\nCREATE TABLE employees (\n id INTEGER PRIMARY KEY,\n name TEXT NOT NULL,\n title TEXT NOT NULL,\n manager_id INTEGER,\n salary NUMERIC,\n FOREIGN KEY (manager_id) REFERENCES employees(id)\n);\n\n-- Insert sample hierarchical data\nINSERT INTO employees (id, name, title, manager_id, salary) VALUES\n (1, 'Mark Johnson', 'CEO', NULL, 250000),\n (2, 'Sarah Williams', 'CTO', 1, 180000),\n (3, 'Michael Brown', 'CFO', 1, 175000),\n (4, 'Patricia Davis', 'Engineering Director', 2, 150000),\n (5, 'Robert Wilson', 'Finance Director', 3, 145000),\n (6, 'Linda Miller', 'Senior Developer', 4, 120000),\n (7, 'James Taylor', 'Senior Developer', 4, 120000),\n (8, 'Elizabeth Anderson', 'Accountant', 5, 95000),\n (9, 'David Thomas', 'Junior Developer', 6, 85000),\n (10, 'Jennifer Jackson', 'Junior Developer', 7, 85000);\n\n-- Recursive query to show employee hierarchy\nWITH RECURSIVE employee_hierarchy AS (\n -- Base case: top-level employees (no manager)\n SELECT \n id, \n name, \n title, \n manager_id, \n salary,\n 0 AS level,\n name AS path\n FROM employees\n WHERE manager_id IS NULL\n \n UNION ALL\n \n -- Recursive case: employees with managers\n SELECT \n e.id, \n e.name, \n e.title, \n e.manager_id, \n e.salary,\n eh.level + 1 AS level,\n eh.path || ' > ' || e.name AS path\n FROM employees e\n JOIN employee_hierarchy eh ON e.manager_id = eh.id\n)\n\n-- Query the hierarchy\nSELECT \n id,\n printf('%.' || (level * 4) || 's%s', '', name) AS employee,\n title,\n level,\n salary,\n path\nFROM employee_hierarchy\nORDER BY path;"
51
+
},
52
+
'complex-subquery': {
53
+
name: 'Complex Subqueries',
54
+
sql: "-- Complex Subqueries Example\n\n-- Create sample tables\nDROP TABLE IF EXISTS departments;\nCREATE TABLE departments (\n id INTEGER PRIMARY KEY,\n name TEXT NOT NULL\n);\n\nDROP TABLE IF EXISTS employees;\nCREATE TABLE employees (\n id INTEGER PRIMARY KEY,\n name TEXT NOT NULL,\n department_id INTEGER,\n salary NUMERIC,\n hire_date DATE,\n FOREIGN KEY (department_id) REFERENCES departments(id)\n);\n\nDROP TABLE IF EXISTS projects;\nCREATE TABLE projects (\n id INTEGER PRIMARY KEY,\n name TEXT NOT NULL,\n budget NUMERIC\n);\n\nDROP TABLE IF EXISTS employee_projects;\nCREATE TABLE employee_projects (\n employee_id INTEGER,\n project_id INTEGER,\n hours_worked NUMERIC,\n PRIMARY KEY (employee_id, project_id),\n FOREIGN KEY (employee_id) REFERENCES employees(id),\n FOREIGN KEY (project_id) REFERENCES projects(id)\n);\n\n-- Insert sample data\nINSERT INTO departments (id, name) VALUES\n (1, 'Engineering'),\n (2, 'Marketing'),\n (3, 'Finance'),\n (4, 'HR');\n\nINSERT INTO employees (id, name, department_id, salary, hire_date) VALUES\n (1, 'Alice Smith', 1, 85000, '2020-01-15'),\n (2, 'Bob Johnson', 2, 72000, '2019-03-20'),\n (3, 'Carol Williams', 1, 92000, '2018-11-07'),\n (4, 'Dave Brown', 3, 115000, '2017-05-12'),\n (5, 'Eve Davis', 1, 110000, '2021-08-30'),\n (6, 'Frank Miller', 2, 68000, '2020-04-18'),\n (7, 'Grace Wilson', 3, 95000, '2019-12-01'),\n (8, 'Henry Garcia', 4, 75000, '2021-02-15');\n\nINSERT INTO projects (id, name, budget) VALUES\n (1, 'Website Redesign', 150000),\n (2, 'Mobile App', 200000),\n (3, 'Database Migration', 100000),\n (4, 'Marketing Campaign', 80000);\n\nINSERT INTO employee_projects (employee_id, project_id, hours_worked) VALUES\n (1, 1, 120), (1, 2, 80),\n (2, 4, 150),\n (3, 1, 100), (3, 2, 120), (3, 3, 40),\n (4, 3, 60),\n (5, 2, 180), (5, 3, 30),\n (6, 4, 140),\n (7, 3, 80);\n\n-- Complex query 1: Find employees who work on projects with a budget greater than average\nSELECT DISTINCT e.name, e.salary\nFROM employees e\nJOIN employee_projects ep ON e.id = ep.employee_id\nWHERE ep.project_id IN (\n SELECT id FROM projects WHERE budget > (\n SELECT AVG(budget) FROM projects\n )\n)\nORDER BY e.salary DESC;\n\n-- Complex query 2: Find departments with employees who have above-average salaries\nSELECT \n d.name AS department,\n COUNT(*) AS employee_count,\n ROUND(AVG(e.salary), 2) AS avg_salary\nFROM departments d\nJOIN employees e ON d.id = e.department_id\nWHERE e.salary > (\n SELECT AVG(salary) FROM employees\n)\nGROUP BY d.id\nORDER BY avg_salary DESC;\n\n-- Complex query 3: Find employees who work on the most projects\nSELECT \n e.name,\n COUNT(ep.project_id) AS project_count,\n SUM(ep.hours_worked) AS total_hours\nFROM employees e\nJOIN employee_projects ep ON e.id = ep.employee_id\nGROUP BY e.id\nHAVING COUNT(ep.project_id) = (\n SELECT MAX(project_count)\n FROM (\n SELECT COUNT(project_id) AS project_count\n FROM employee_projects\n GROUP BY employee_id\n )\n)\nORDER BY total_hours DESC;"
55
+
},
56
+
'window-functions': {
57
+
name: 'Window Functions',
58
+
sql: "-- Window Functions Example\n\n-- Create sales table\nDROP TABLE IF EXISTS sales;\nCREATE TABLE sales (\n id INTEGER PRIMARY KEY,\n salesperson TEXT NOT NULL,\n region TEXT NOT NULL,\n amount NUMERIC NOT NULL,\n sale_date DATE NOT NULL\n);\n\n-- Insert sample data\nINSERT INTO sales (salesperson, region, amount, sale_date) VALUES\n ('Alice', 'North', 12500, '2023-01-05'),\n ('Bob', 'South', 8700, '2023-01-10'),\n ('Carol', 'East', 15200, '2023-01-12'),\n ('Dave', 'West', 7300, '2023-01-15'),\n ('Alice', 'North', 9800, '2023-02-03'),\n ('Bob', 'South', 11600, '2023-02-08'),\n ('Carol', 'East', 14100, '2023-02-15'),\n ('Dave', 'West', 9200, '2023-02-20'),\n ('Alice', 'North', 16700, '2023-03-05'),\n ('Bob', 'South', 10300, '2023-03-12'),\n ('Carol', 'East', 12800, '2023-03-18'),\n ('Dave', 'West', 8500, '2023-03-25');\n\n-- Window function queries\n\n-- 1. Running total of sales by salesperson\nSELECT\n salesperson,\n region,\n sale_date,\n amount,\n SUM(amount) OVER (\n PARTITION BY salesperson \n ORDER BY sale_date\n ) AS running_total\nFROM sales\nORDER BY salesperson, sale_date;\n\n-- 2. Rank salespeople by amount within each region\nSELECT\n region,\n salesperson,\n amount,\n RANK() OVER (\n PARTITION BY region \n ORDER BY amount DESC\n ) AS region_rank\nFROM sales\nORDER BY region, region_rank;\n\n-- 3. Calculate moving average of last 2 sales\nSELECT\n salesperson,\n sale_date,\n amount,\n AVG(amount) OVER (\n PARTITION BY salesperson \n ORDER BY sale_date \n ROWS BETWEEN 1 PRECEDING AND CURRENT ROW\n ) AS moving_avg_2\nFROM sales\nORDER BY salesperson, sale_date;\n\n-- 4. Compare each sale to the previous sale\nSELECT\n salesperson,\n sale_date,\n amount,\n LAG(amount, 1) OVER (\n PARTITION BY salesperson \n ORDER BY sale_date\n ) AS previous_amount,\n amount - LAG(amount, 1) OVER (\n PARTITION BY salesperson \n ORDER BY sale_date\n ) AS amount_change\nFROM sales\nORDER BY salesperson, sale_date;"
59
+
},
60
+
'json-functions': {
61
+
name: 'JSON Functions',
62
+
sql: "-- SQLite JSON Functions Example\n\n-- Create table with JSON data\nDROP TABLE IF EXISTS users;\nCREATE TABLE users (\n id INTEGER PRIMARY KEY,\n name TEXT NOT NULL,\n profile JSON\n);\n\n-- Insert sample data with JSON\nINSERT INTO users (name, profile) VALUES\n ('Alice', '{\"age\": 28, \"city\": \"New York\", \"skills\": [\"Python\", \"SQL\", \"JavaScript\"], \"contact\": {\"email\": \"[email protected]\", \"phone\": \"555-1234\"}}'),\n ('Bob', '{\"age\": 35, \"city\": \"San Francisco\", \"skills\": [\"Java\", \"C++\", \"Ruby\"], \"contact\": {\"email\": \"[email protected]\", \"phone\": \"555-5678\"}}'),\n ('Carol', '{\"age\": 42, \"city\": \"Chicago\", \"skills\": [\"SQL\", \"R\", \"Tableau\"], \"contact\": {\"email\": \"[email protected]\"}}'),\n ('Dave', '{\"age\": 31, \"city\": \"Boston\", \"skills\": [\"Python\", \"SQL\"], \"contact\": {\"email\": \"[email protected]\", \"phone\": \"555-9012\"}}');\n\n-- JSON queries\n\n-- 1. Extract simple values\nSELECT\n name,\n json_extract(profile, '$.age') AS age,\n json_extract(profile, '$.city') AS city\nFROM users\nORDER BY age;\n\n-- 2. Filter based on JSON values\nSELECT name, profile\nFROM users\nWHERE json_extract(profile, '$.age') > 30\nORDER BY json_extract(profile, '$.age');\n\n-- 3. Check for array elements\nSELECT name\nFROM users\nWHERE json_extract(profile, '$.skills') LIKE '%SQL%'\nORDER BY name;\n\n-- 4. Extract nested values\nSELECT\n name,\n json_extract(profile, '$.contact.email') AS email,\n json_extract(profile, '$.contact.phone') AS phone\nFROM users\nORDER BY name;\n\n-- 5. Count array elements\nSELECT\n name,\n json_array_length(json_extract(profile, '$.skills')) AS skill_count\nFROM users\nORDER BY skill_count DESC;"
0 commit comments