-- Create a reference to a shared customer tableCREATE TABLE customersUSING deltaSharing LOCATION '/opt/profiles/production.share#sales.crm.customers';-- Now query it like any other tableSELECT * FROM customers LIMIT 10;
-- Select all columnsSELECT * FROM customers;-- Select specific columnsSELECT customer_id, name, email FROM customers;-- Select with aliasesSELECT customer_id AS id, name AS customer_name, email AS contact_emailFROM customers;
-- WHERE clauseSELECT * FROM customersWHERE country = 'USA' AND status = 'active';-- Date filteringSELECT * FROM customersWHERE created_date >= '2024-01-01' AND created_date < '2024-02-01';-- IN clauseSELECT * FROM customersWHERE region IN ('North', 'South', 'East');-- Pattern matchingSELECT * FROM customersWHERE email LIKE '%@example.com';
-- Count recordsSELECT COUNT(*) as total_customers FROM customers;-- Group bySELECT country, COUNT(*) as customer_countFROM customersGROUP BY countryORDER BY customer_count DESC;-- Multiple aggregationsSELECT region, COUNT(*) as total, COUNT(DISTINCT customer_id) as unique_customers, AVG(order_value) as avg_valueFROM customersGROUP BY region;-- Having clauseSELECT country, COUNT(*) as countFROM customersGROUP BY countryHAVING count > 100;
-- Order by single columnSELECT * FROM customersORDER BY created_date DESCLIMIT 100;-- Order by multiple columnsSELECT * FROM customersORDER BY country ASC, name ASC;-- Top N queriesSELECT customer_id, total_purchasesFROM customersORDER BY total_purchases DESCLIMIT 10;
-- Subquery in WHERE clauseSELECT * FROM customersWHERE customer_id IN ( SELECT customer_id FROM orders WHERE total_amount > 1000);-- Subquery in FROM clauseSELECT country, AVG(customer_value) as avg_valueFROM ( SELECT country, customer_id, SUM(total_amount) as customer_value FROM customers c JOIN orders o ON c.customer_id = o.customer_id GROUP BY country, customer_id) customer_totalsGROUP BY country;
-- Single CTEWITH active_customers AS ( SELECT * FROM customers WHERE status = 'active' AND last_order_date >= CURRENT_DATE - INTERVAL 90 DAYS)SELECT country, COUNT(*) as active_countFROM active_customersGROUP BY country;-- Multiple CTEsWITHcustomer_stats AS ( SELECT customer_id, COUNT(*) as order_count, SUM(total_amount) as total_spent FROM orders GROUP BY customer_id),high_value_customers AS ( SELECT customer_id FROM customer_stats WHERE total_spent > 10000)SELECT c.name, c.email, cs.order_count, cs.total_spentFROM customers cJOIN high_value_customers hvc ON c.customer_id = hvc.customer_idJOIN customer_stats cs ON c.customer_id = cs.customer_idORDER BY cs.total_spent DESC;
-- Rank customers by order value within each countrySELECT customer_id, name, country, total_purchases, RANK() OVER (PARTITION BY country ORDER BY total_purchases DESC) as country_rankFROM customersQUALIFY country_rank <= 10;-- Running totalsSELECT order_date, total_amount, SUM(total_amount) OVER (ORDER BY order_date) as running_totalFROM ordersORDER BY order_date;
Create views on top of shared tables for reusable query logic:
-- Create a view for active customersCREATE VIEW active_customers ASSELECT * FROM customersWHERE status = 'active' AND last_order_date >= CURRENT_DATE - INTERVAL 90 DAYS;-- Query the viewSELECT country, COUNT(*) FROM active_customers GROUP BY country;-- Create a materialized resultCREATE TABLE customer_summary ASSELECT country, COUNT(*) as customer_count, AVG(total_purchases) as avg_purchasesFROM customersGROUP BY country;
-- Good: Select specific columnsSELECT customer_id, name, email FROM customers;-- Avoid: SELECT * when you only need a few columnsSELECT * FROM customers;
Selecting only necessary columns reduces data transfer and improves query performance.
Use filters effectively
-- Apply filters as early as possibleSELECT COUNT(*) FROM customersWHERE created_date >= '2024-01-01' AND country = 'USA';
Filtering reduces the amount of data processed in subsequent operations.
Cache frequently used tables
-- Cache a table for multiple queriesCACHE TABLE customers;-- Run multiple queries (will use cached data)SELECT COUNT(*) FROM customers;SELECT country, COUNT(*) FROM customers GROUP BY country;-- Clear cache when doneUNCACHE TABLE customers;
Limit result sets during exploration
-- Use LIMIT when exploring dataSELECT * FROM customers LIMIT 100;-- Sample data for analysisSELECT * FROM customers TABLESAMPLE (10 PERCENT);
Create temporary table references that only exist for your session:
-- Create a temporary table referenceCREATE TEMP VIEW temp_customers USING deltaSharing AS SELECT * FROM deltaSharing.`/opt/profiles/prod.share#sales.crm.customers`;-- Use in queriesSELECT * FROM temp_customers WHERE country = 'USA';-- Temporary table is automatically dropped when session ends