# DB Query

> Query MySQL databases, summarize results, and export to CSV/Excel. Use when the user asks to check data, run SQL queries, export tables, or generate data reports from a database.

- Skill: `sizzlecar/db-query` (Agent Skill)
- Install (CLI): `npx skillmds@latest add sizzlecar/db-query`
- Raw SKILL.md: https://api.skillmd.com/api/skills/sizzlecar/db-query/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Data & Analytics
- Author: sizzlecar (https://skillmd.com/u/sizzlecar)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/sizzlecar/db-query

---


# Database Query Skill

## Environment Variables

| Variable | Required | Default | Description |
|----------|----------|---------|-------------|
| `DB_HOST` | Yes | — | MySQL server hostname or IP |
| `DB_USER` | Yes | — | Database username |
| `DB_PASS` | Yes | — | Database password |
| `DB_NAME` | No | — | Default database name |
| `DB_PORT` | No | 3306 | MySQL server port |

## Important

All required environment variables (DB_HOST, DB_USER, DB_PASS, etc.) are pre-configured. Do NOT ask the user for database credentials — just run the query directly.

## Workflow

1. **Verify connectivity** before running queries:
   ```
   mysql -h "$DB_HOST" -P "${DB_PORT:-3306}" -u "$DB_USER" -p"$DB_PASS" -e "SELECT 1" 2>&1
   ```

2. **Run queries** using the mysql CLI with these flags for clean output:
   ```
   mysql -h "$DB_HOST" -P "${DB_PORT:-3306}" -u "$DB_USER" -p"$DB_PASS" "$DB_NAME" -e "YOUR SQL" --batch --skip-column-names
   ```

3. **For tabular output** (human-readable), omit `--skip-column-names`:
   ```
   mysql -h "$DB_HOST" -P "${DB_PORT:-3306}" -u "$DB_USER" -p"$DB_PASS" "$DB_NAME" -e "YOUR SQL" --table
   ```

4. **Export to CSV**:
   ```
   mysql -h "$DB_HOST" -P "${DB_PORT:-3306}" -u "$DB_USER" -p"$DB_PASS" "$DB_NAME" -e "YOUR SQL" --batch | tr '\t' ',' > output.csv
   ```

## Safety Rules

- NEVER run `DROP`, `TRUNCATE`, `DELETE`, or `UPDATE` without explicit user confirmation
- NEVER modify database schema (`ALTER TABLE`, `CREATE TABLE`, `DROP TABLE`) without approval
- Always use `LIMIT` for exploratory queries to avoid fetching millions of rows
- Use `--batch` mode for programmatic output, `--table` for human display
- Mask passwords in any output shown to the user — never echo `$DB_PASS`

## Query Patterns

- **Explore schema**: `SHOW TABLES`, `DESCRIBE table_name`, `SHOW CREATE TABLE table_name`
- **Row counts**: `SELECT COUNT(*) FROM table_name`
- **Sample data**: `SELECT * FROM table_name LIMIT 10`
- **Aggregations**: Use `GROUP BY` with `COUNT`, `SUM`, `AVG` for summaries
- **Date filtering**: `WHERE created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)`

