|
| 1 | +-- SQL Parser Go - VIEW Examples |
| 2 | +-- Demonstrates CREATE VIEW and DROP VIEW parsing across different dialects |
| 3 | + |
| 4 | +-- ============================================================================ |
| 5 | +-- Simple CREATE VIEW |
| 6 | +-- ============================================================================ |
| 7 | + |
| 8 | +-- Basic view |
| 9 | +CREATE VIEW active_users AS |
| 10 | +SELECT id, name, email, created_at |
| 11 | +FROM users |
| 12 | +WHERE active = 1; |
| 13 | + |
| 14 | +-- View with aggregate functions |
| 15 | +CREATE VIEW user_stats AS |
| 16 | +SELECT user_id, COUNT(*) as order_count, SUM(total) as total_spent |
| 17 | +FROM orders |
| 18 | +GROUP BY user_id; |
| 19 | + |
| 20 | +-- ============================================================================ |
| 21 | +-- CREATE VIEW with schema |
| 22 | +-- ============================================================================ |
| 23 | + |
| 24 | +CREATE VIEW myschema.customer_orders AS |
| 25 | +SELECT u.name, o.order_id, o.total, o.created_at |
| 26 | +FROM users u |
| 27 | +JOIN orders o ON u.id = o.user_id |
| 28 | +WHERE o.status = 'completed'; |
| 29 | + |
| 30 | +-- ============================================================================ |
| 31 | +-- CREATE OR REPLACE VIEW |
| 32 | +-- ============================================================================ |
| 33 | + |
| 34 | +CREATE OR REPLACE VIEW high_value_orders AS |
| 35 | +SELECT * FROM orders |
| 36 | +WHERE total > 1000 |
| 37 | +ORDER BY total DESC; |
| 38 | + |
| 39 | +-- ============================================================================ |
| 40 | +-- CREATE VIEW IF NOT EXISTS |
| 41 | +-- ============================================================================ |
| 42 | + |
| 43 | +CREATE VIEW IF NOT EXISTS recent_orders AS |
| 44 | +SELECT * FROM orders |
| 45 | +WHERE created_at > DATE_SUB(NOW(), INTERVAL 30 DAY); |
| 46 | + |
| 47 | +-- ============================================================================ |
| 48 | +-- CREATE VIEW with column list |
| 49 | +-- ============================================================================ |
| 50 | + |
| 51 | +CREATE VIEW user_summary (user_id, total_orders, total_amount, avg_amount) AS |
| 52 | +SELECT user_id, COUNT(*), SUM(total), AVG(total) |
| 53 | +FROM orders |
| 54 | +GROUP BY user_id; |
| 55 | + |
| 56 | +-- ============================================================================ |
| 57 | +-- CREATE VIEW with WITH CHECK OPTION |
| 58 | +-- ============================================================================ |
| 59 | + |
| 60 | +-- MySQL/PostgreSQL: WITH CHECK OPTION ensures inserts/updates through view respect WHERE clause |
| 61 | +CREATE VIEW premium_customers AS |
| 62 | +SELECT * FROM users |
| 63 | +WHERE subscription_type = 'premium' |
| 64 | +WITH CHECK OPTION; |
| 65 | + |
| 66 | +-- ============================================================================ |
| 67 | +-- CREATE MATERIALIZED VIEW (PostgreSQL) |
| 68 | +-- ============================================================================ |
| 69 | + |
| 70 | +-- Materialized views store the query result physically |
| 71 | +CREATE MATERIALIZED VIEW sales_summary AS |
| 72 | +SELECT product_id, SUM(amount) as total_sales, COUNT(*) as sale_count |
| 73 | +FROM sales |
| 74 | +GROUP BY product_id; |
| 75 | + |
| 76 | +-- With IF NOT EXISTS |
| 77 | +CREATE MATERIALIZED VIEW IF NOT EXISTS monthly_revenue AS |
| 78 | +SELECT |
| 79 | + DATE_TRUNC('month', order_date) as month, |
| 80 | + SUM(total) as revenue, |
| 81 | + COUNT(*) as order_count |
| 82 | +FROM orders |
| 83 | +GROUP BY DATE_TRUNC('month', order_date); |
| 84 | + |
| 85 | +-- ============================================================================ |
| 86 | +-- CREATE OR REPLACE MATERIALIZED VIEW |
| 87 | +-- ============================================================================ |
| 88 | + |
| 89 | +CREATE OR REPLACE MATERIALIZED VIEW top_products AS |
| 90 | +SELECT product_id, product_name, SUM(quantity) as total_sold |
| 91 | +FROM order_items |
| 92 | +JOIN products ON order_items.product_id = products.id |
| 93 | +GROUP BY product_id, product_name |
| 94 | +ORDER BY total_sold DESC |
| 95 | +LIMIT 100; |
| 96 | + |
| 97 | +-- ============================================================================ |
| 98 | +-- Complex Views with Subqueries |
| 99 | +-- ============================================================================ |
| 100 | + |
| 101 | +CREATE VIEW above_average_salaries AS |
| 102 | +SELECT * FROM employees |
| 103 | +WHERE salary > (SELECT AVG(salary) FROM employees); |
| 104 | + |
| 105 | +CREATE VIEW department_stats AS |
| 106 | +SELECT |
| 107 | + d.name as department, |
| 108 | + COUNT(e.id) as employee_count, |
| 109 | + AVG(e.salary) as avg_salary, |
| 110 | + (SELECT MAX(salary) FROM employees WHERE department_id = d.id) as max_salary |
| 111 | +FROM departments d |
| 112 | +LEFT JOIN employees e ON d.id = e.department_id |
| 113 | +GROUP BY d.id, d.name; |
| 114 | + |
| 115 | +-- ============================================================================ |
| 116 | +-- Views with CTEs (Common Table Expressions) |
| 117 | +-- ============================================================================ |
| 118 | + |
| 119 | +CREATE VIEW quarterly_sales AS |
| 120 | +WITH quarterly_data AS ( |
| 121 | + SELECT |
| 122 | + EXTRACT(YEAR FROM order_date) as year, |
| 123 | + EXTRACT(QUARTER FROM order_date) as quarter, |
| 124 | + SUM(total) as revenue |
| 125 | + FROM orders |
| 126 | + GROUP BY year, quarter |
| 127 | +) |
| 128 | +SELECT * FROM quarterly_data |
| 129 | +ORDER BY year DESC, quarter DESC; |
| 130 | + |
| 131 | +-- ============================================================================ |
| 132 | +-- Views with Window Functions |
| 133 | +-- ============================================================================ |
| 134 | + |
| 135 | +CREATE VIEW employee_rankings AS |
| 136 | +SELECT |
| 137 | + id, |
| 138 | + name, |
| 139 | + department_id, |
| 140 | + salary, |
| 141 | + ROW_NUMBER() OVER (PARTITION BY department_id ORDER BY salary DESC) as dept_rank, |
| 142 | + RANK() OVER (ORDER BY salary DESC) as company_rank |
| 143 | +FROM employees; |
| 144 | + |
| 145 | +-- ============================================================================ |
| 146 | +-- DROP VIEW |
| 147 | +-- ============================================================================ |
| 148 | + |
| 149 | +-- Simple DROP |
| 150 | +DROP VIEW active_users; |
| 151 | + |
| 152 | +-- DROP IF EXISTS |
| 153 | +DROP VIEW IF EXISTS user_stats; |
| 154 | + |
| 155 | +-- DROP with schema |
| 156 | +DROP VIEW myschema.customer_orders; |
| 157 | + |
| 158 | +-- DROP with CASCADE (PostgreSQL - removes dependent views) |
| 159 | +DROP VIEW IF EXISTS sales_summary CASCADE; |
| 160 | + |
| 161 | +-- ============================================================================ |
| 162 | +-- DROP MATERIALIZED VIEW (PostgreSQL) |
| 163 | +-- ============================================================================ |
| 164 | + |
| 165 | +DROP MATERIALIZED VIEW sales_summary; |
| 166 | + |
| 167 | +DROP MATERIALIZED VIEW IF EXISTS monthly_revenue; |
| 168 | + |
| 169 | +-- ============================================================================ |
| 170 | +-- Dialect-Specific Examples |
| 171 | +-- ============================================================================ |
| 172 | + |
| 173 | +-- MySQL with backticks |
| 174 | +CREATE VIEW `user_orders` AS |
| 175 | +SELECT `users`.`id`, `users`.`name`, COUNT(`orders`.`id`) as `order_count` |
| 176 | +FROM `users` |
| 177 | +LEFT JOIN `orders` ON `users`.`id` = `orders`.`user_id` |
| 178 | +GROUP BY `users`.`id`, `users`.`name`; |
| 179 | + |
| 180 | +-- PostgreSQL with double quotes |
| 181 | +CREATE VIEW "user_orders" AS |
| 182 | +SELECT "users"."id", "users"."name", COUNT("orders"."id") as "order_count" |
| 183 | +FROM "users" |
| 184 | +LEFT JOIN "orders" ON "users"."id" = "orders"."user_id" |
| 185 | +GROUP BY "users"."id", "users"."name"; |
| 186 | + |
| 187 | +-- SQL Server with brackets |
| 188 | +CREATE VIEW [user_orders] AS |
| 189 | +SELECT [users].[id], [users].[name], COUNT([orders].[id]) as [order_count] |
| 190 | +FROM [users] |
| 191 | +LEFT JOIN [orders] ON [users].[id] = [orders].[user_id] |
| 192 | +GROUP BY [users].[id], [users].[name]; |
| 193 | + |
| 194 | +-- ============================================================================ |
| 195 | +-- Real-World Examples |
| 196 | +-- ============================================================================ |
| 197 | + |
| 198 | +-- E-commerce: Active product catalog |
| 199 | +CREATE VIEW active_products AS |
| 200 | +SELECT p.id, p.name, p.price, c.name as category, i.quantity |
| 201 | +FROM products p |
| 202 | +JOIN categories c ON p.category_id = c.id |
| 203 | +JOIN inventory i ON p.id = i.product_id |
| 204 | +WHERE p.is_active = 1 AND i.quantity > 0; |
| 205 | + |
| 206 | +-- Analytics: Daily order summary |
| 207 | +CREATE VIEW daily_order_summary AS |
| 208 | +SELECT |
| 209 | + DATE(created_at) as order_date, |
| 210 | + COUNT(*) as total_orders, |
| 211 | + SUM(total) as total_revenue, |
| 212 | + AVG(total) as avg_order_value, |
| 213 | + COUNT(DISTINCT user_id) as unique_customers |
| 214 | +FROM orders |
| 215 | +WHERE status = 'completed' |
| 216 | +GROUP BY DATE(created_at); |
| 217 | + |
| 218 | +-- User management: User permissions view |
| 219 | +CREATE VIEW user_permissions AS |
| 220 | +SELECT |
| 221 | + u.id as user_id, |
| 222 | + u.username, |
| 223 | + u.email, |
| 224 | + r.name as role, |
| 225 | + GROUP_CONCAT(p.name) as permissions |
| 226 | +FROM users u |
| 227 | +JOIN user_roles ur ON u.id = ur.user_id |
| 228 | +JOIN roles r ON ur.role_id = r.id |
| 229 | +JOIN role_permissions rp ON r.id = rp.role_id |
| 230 | +JOIN permissions p ON rp.permission_id = p.id |
| 231 | +GROUP BY u.id, u.username, u.email, r.name; |
0 commit comments