Skip to main content

Query Data in Databend

Databend supports standard SQL with ANSI SQL:2003 analytics extensions. This guide covers essential query techniques from basic to advanced, organized by learning path for optimal understanding.

Learning Pathโ€‹

๐Ÿ“š New to SQL? Start with Basic Queries
๐Ÿ”— Joining data? Go to Combining Data
โšก Need custom logic? Check Advanced Features
๐Ÿš€ Performance issues? Visit Query Optimization


๐Ÿ“š Basic Queriesโ€‹

Master fundamental SQL operations for data selection and aggregation.

Filtering & Selectionโ€‹

-- Select and filter data
SELECT name, salary FROM employees
WHERE department = 'Engineering'
ORDER BY salary DESC;

Aggregating Dataโ€‹

-- Group and summarize data
SELECT department,
COUNT(*) as emp_count,
AVG(salary) as avg_salary
FROM employees
GROUP BY department;

Advanced Groupingโ€‹

Multi-dimensional analysis with CUBE, ROLLUP, and GROUPING SETS


๐Ÿ”— Combining Dataโ€‹

Connect data from multiple sources using JOINs and CTEs.

JOINsโ€‹

-- Combine related tables
SELECT e.name, d.department_name
FROM employees e
JOIN departments d ON e.department_id = d.id;

Common Table Expressions (CTE)โ€‹

-- Structure complex queries
WITH high_earners AS (
SELECT * FROM employees WHERE salary > 75000
)
SELECT department, COUNT(*) as count
FROM high_earners GROUP BY department;

โšก Advanced Featuresโ€‹

Extend capabilities with custom functions and external integrations.

User-Defined Functionsโ€‹

-- Create reusable functions
CREATE FUNCTION calculate_bonus(salary FLOAT, rating FLOAT)
RETURNS FLOAT AS $$ salary * rating * 0.1 $$;

More Advanced Featuresโ€‹


๐Ÿš€ Query Optimizationโ€‹

Analyze and improve query performance with profiling tools.

Query Profileโ€‹

Visual execution plan analysis (Databend Cloud: Monitor โ†’ SQL History)

Performance Analysisโ€‹

-- Analyze query execution
EXPLAIN SELECT * FROM orders o
JOIN customers c ON o.customer_id = c.id
WHERE o.order_date >= '2023-01-01';

Quick Referenceโ€‹

Most Common Patternsโ€‹

-- Top N query
SELECT * FROM employees ORDER BY salary DESC LIMIT 10;

-- Filter and aggregate
SELECT department, AVG(salary)
FROM employees
WHERE hire_date >= '2023-01-01'
GROUP BY department
HAVING AVG(salary) > 70000;

-- Join with CTE
WITH recent_orders AS (
SELECT * FROM orders WHERE order_date >= '2023-01-01'
)
SELECT c.name, COUNT(*) as order_count
FROM customers c
JOIN recent_orders o ON c.id = o.customer_id
GROUP BY c.name;