postgres-queries
PostgreSQL query patterns and best practices
What this skill does
# PostgreSQL Queries Skill
Patterns for writing effective PostgreSQL queries.
## Basic Queries
### SELECT Patterns
```sql
-- Select with conditions
SELECT id, name, email
FROM users
WHERE status = 'active'
AND created_at > NOW() - INTERVAL '30 days'
ORDER BY created_at DESC
LIMIT 10;
-- Select with joins
SELECT
u.id,
u.name,
COUNT(o.id) as order_count,
SUM(o.total) as total_spent
FROM users u
LEFT JOIN orders o ON o.user_id = u.id
WHERE u.status = 'active'
GROUP BY u.id, u.name
HAVING COUNT(o.id) > 0
ORDER BY total_spent DESC;
```
### INSERT Patterns
```sql
-- Single insert with returning
INSERT INTO users (name, email, status)
VALUES ('John Doe', '[email protected]', 'active')
RETURNING id, created_at;
-- Multi-row insert
INSERT INTO users (name, email, status)
VALUES
('User 1', '[email protected]', 'active'),
('User 2', '[email protected]', 'pending'),
('User 3', '[email protected]', 'active')
RETURNING *;
-- Insert with conflict handling (upsert)
INSERT INTO users (email, name, status)
VALUES ('[email protected]', 'John Doe', 'active')
ON CONFLICT (email)
DO UPDATE SET
name = EXCLUDED.name,
updated_at = NOW()
RETURNING *;
```
### UPDATE Patterns
```sql
-- Update with conditions
UPDATE users
SET
status = 'inactive',
updated_at = NOW()
WHERE last_login_at < NOW() - INTERVAL '1 year'
RETURNING id, email;
-- Update from another table
UPDATE orders o
SET status = 'shipped'
FROM shipments s
WHERE s.order_id = o.id
AND s.shipped_at IS NOT NULL;
-- Conditional update
UPDATE products
SET
price = CASE
WHEN category = 'electronics' THEN price * 0.9
WHEN category = 'clothing' THEN price * 0.8
ELSE price
END,
updated_at = NOW()
WHERE on_sale = true;
```
### DELETE Patterns
```sql
-- Delete with returning
DELETE FROM sessions
WHERE expires_at < NOW()
RETURNING id, user_id;
-- Delete using subquery
DELETE FROM users
WHERE id IN (
SELECT user_id
FROM inactive_users
WHERE days_inactive > 365
);
```
## Advanced Queries
### Common Table Expressions (CTE)
```sql
-- Simple CTE
WITH active_users AS (
SELECT * FROM users WHERE status = 'active'
)
SELECT u.*, COUNT(o.id) as order_count
FROM active_users u
LEFT JOIN orders o ON o.user_id = u.id
GROUP BY u.id;
-- Recursive CTE for hierarchical data
WITH RECURSIVE category_tree AS (
-- Base case
SELECT id, name, parent_id, 0 as level
FROM categories
WHERE parent_id IS NULL
UNION ALL
-- Recursive case
SELECT c.id, c.name, c.parent_id, ct.level + 1
FROM categories c
INNER JOIN category_tree ct ON c.parent_id = ct.id
)
SELECT * FROM category_tree ORDER BY level, name;
```
### Window Functions
```sql
-- Row number
SELECT
id,
name,
department,
salary,
ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) as rank
FROM employees;
-- Running totals
SELECT
date,
amount,
SUM(amount) OVER (ORDER BY date) as running_total
FROM transactions;
-- Moving average
SELECT
date,
value,
AVG(value) OVER (ORDER BY date ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) as weekly_avg
FROM metrics;
-- Lead/Lag
SELECT
id,
sale_date,
amount,
amount - LAG(amount) OVER (ORDER BY sale_date) as change_from_previous
FROM sales;
```
### JSONB Operations
```sql
-- Query JSONB field
SELECT * FROM products
WHERE metadata->>'color' = 'red';
-- Query nested JSONB
SELECT * FROM products
WHERE metadata->'dimensions'->>'height' > '10';
-- JSONB contains
SELECT * FROM products
WHERE metadata @> '{"featured": true}';
-- JSONB array contains
SELECT * FROM products
WHERE metadata->'tags' ? 'sale';
-- Update JSONB
UPDATE products
SET metadata = jsonb_set(metadata, '{stock}', '100')
WHERE id = 1;
-- Aggregate to JSONB
SELECT
user_id,
jsonb_agg(
jsonb_build_object(
'id', id,
'title', title,
'date', created_at
)
) as posts
FROM posts
GROUP BY user_id;
```
### Full-Text Search
```sql
-- Create tsvector column
ALTER TABLE articles ADD COLUMN search_vector tsvector;
UPDATE articles SET search_vector =
to_tsvector('english', coalesce(title, '') || ' ' || coalesce(body, ''));
-- Create GIN index
CREATE INDEX idx_articles_search ON articles USING GIN(search_vector);
-- Search query
SELECT
id,
title,
ts_rank(search_vector, query) as rank
FROM articles, to_tsquery('english', 'database & performance') query
WHERE search_vector @@ query
ORDER BY rank DESC;
-- Highlight results
SELECT
id,
ts_headline('english', body, to_tsquery('english', 'postgresql'),
'StartSel=<mark>, StopSel=</mark>') as highlighted
FROM articles
WHERE search_vector @@ to_tsquery('english', 'postgresql');
```
## Array Operations
```sql
-- Array contains
SELECT * FROM products
WHERE 'electronics' = ANY(categories);
-- Array overlap
SELECT * FROM products
WHERE categories && ARRAY['electronics', 'computers'];
-- Unnest array
SELECT
id,
unnest(tags) as tag
FROM articles;
-- Aggregate to array
SELECT
user_id,
array_agg(DISTINCT category ORDER BY category) as categories
FROM purchases
GROUP BY user_id;
```
## Date/Time Operations
```sql
-- Date truncation
SELECT
date_trunc('day', created_at) as day,
COUNT(*) as count
FROM orders
GROUP BY date_trunc('day', created_at)
ORDER BY day;
-- Date ranges
SELECT * FROM events
WHERE event_date BETWEEN '2024-01-01' AND '2024-12-31';
-- Age calculation
SELECT
name,
birth_date,
EXTRACT(YEAR FROM age(birth_date)) as age
FROM users;
-- Generate series
SELECT
date::date,
COALESCE(COUNT(o.id), 0) as order_count
FROM generate_series(
'2024-01-01'::date,
'2024-01-31'::date,
'1 day'::interval
) as date
LEFT JOIN orders o ON date_trunc('day', o.created_at) = date
GROUP BY date
ORDER BY date;
```
## Integration
Used by:
- `database-developer` agent
- `fullstack-developer` agent
Related in Backend & APIs
jfrog
IncludedInteract with the JFrog Platform via the JFrog CLI and REST/GraphQL APIs. Use this skill when the user wants to manage Artifactory repositories, upload or download artifacts, manage builds, configure permissions, manage users and groups, work with access tokens, configure JFrog CLI servers, search artifacts, manage properties, set up replication, manage JFrog Projects, run security audits or scans, look up CVE details, query exposures scan results from JFrog Advanced Security, manage release bundles and lifecycle operations, aggregate or export platform data, or perform any JFrog Platform administration task. Also use when the user mentions jf, jfrog, artifactory, xray, distribution, evidence, apptrust, onemodel, graphql, workers, mission control, curation, advanced security, exposures, or any JFrog product name.
cupynumeric-migration-readiness
IncludedPre-migration readiness assessor for porting NumPy to cuPyNumeric. Use BEFORE substantial porting work begins when the user asks whether code will scale on GPU, whether they should migrate to cuPyNumeric, which NumPy patterns transfer cleanly, what must be refactored before porting, or mentions pre-port assessment, scaling analysis, or refactor planning. Inspect the user's source code, look up NumPy usage, cross-reference the cuPyNumeric API support manifest, and distinguish distributed-scaling-friendly patterns from blockers such as unsupported APIs, scalar synchronization, host round-trips, Python/object-heavy control flow, shape/data-dependent branching, and in-place mutation hazards. Produce a verdict of READY, LIGHT REFACTOR, SIGNIFICANT REFACTOR, or NOT RECOMMENDED, with concrete refactor pointers.
alibabacloud-data-agent-skill
IncludedInvoke Alibaba Cloud Apsara Data Agent for Analytics via CLI to perform natural language-driven data analysis on enterprise databases. Data Agent for Analytics is an intelligent data analysis agent developed by Alibaba Cloud Database team for enterprise users. It automatically completes requirement analysis, data understanding, analysis insights, and report generation based on natural language descriptions. This tool supports: discovering data resources (instances/databases/tables) managed in DMS, initiating query or deep analysis sessions, real-time progress tracking, and retrieving analysis conclusions and generated reports. Use this Skill when users need to query databases, analyze data trends, generate data reports, ask questions in natural language, or mention "Data Agent", "data analysis", "database query", "SQL analysis", "data insights".
token-optimizer
IncludedReduce OpenClaw token usage and API costs through smart model routing, heartbeat optimization, budget tracking, and native 2026.2.15 features (session pruning, bootstrap size limits, cache TTL alignment). Use when token costs are high, API rate limits are being hit, or hosting multiple agents at scale. The 4 executable scripts (context_optimizer, model_router, heartbeat_optimizer, token_tracker) are local-only — no network requests, no subprocess calls, no system modifications. Reference files (PROVIDERS.md, config-patches.json) document optional multi-provider strategies that require external API keys and network access if you choose to use them. See SECURITY.md for full breakdown.
resend-cli
IncludedUse this skill when the task is specifically about operating Resend from an AI agent, terminal session, or CI job via the official resend CLI: installing/authenticating the CLI, sending/listing/updating/cancelling emails, batch sends, domains and DNS, webhooks and local listeners, inbound receiving, contacts, topics, segments, broadcasts, templates, API keys, profiles, or debugging Resend CLI/API failures. Trigger on mentions of Resend CLI, `resend`, `resend doctor`, `resend emails send`, `resend domains`, `resend webhooks listen`, `resend emails receiving`, or agent-friendly terminal automation.
alibabacloud-odps-maxframe-coding
IncludedUse this skill for MaxFrame SDK development and documentation navigation on Alibaba Cloud MaxCompute (ODPS). Helps answer MaxFrame API, concept, official example, and supported pandas API questions; create data processing programs; read/write MaxCompute tables; debug jobs (remote or local); and build custom DPE runtime images. Trigger when users mention MaxFrame, MaxCompute with MaxFrame, ODPS table processing, DPE runtime, MaxFrame docs/examples, DataFrame/Tensor operations, or GPU runtime setup. Works for both English and Chinese queries about Alibaba Cloud data processing with MaxFrame.