# Backend Database Query Builder

> CodeIgniter query builder, joins, complex queries.

- Skill: `valec3/backend-database-query-builder` (Agent Skill)
- Install (CLI): `npx skillmds add valec3/backend-database-query-builder`
- Raw SKILL.md: https://api.skillmd.com/api/skills/valec3/backend-database-query-builder/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: AI & ML
- Author: valec3 (https://skillmd.com/u/valec3)
- Updated: 2026-09-10
- Page: https://skillmd.com/skills/valec3/backend-database-query-builder

---


# Backend Database Query Builder

## When to use this skill
- Complex database queries
- Dynamic query construction
- JOIN operations
- Query optimization

## Workflow
- [ ] Use query builder for safety
- [ ] Avoid raw SQL when possible
- [ ] Use prepared statements
- [ ] Optimize with indexes
- [ ] Use get() vs first()

## Instructions

### Basic Queries
```php
<?php

$users = $this->db->table('users')
    ->where('status', 'active')
    ->orderBy('created_at', 'DESC')
    ->get()
    ->getResult();
```

### Joins
```php
<?php

$orders = $this->db->table('orders o')
    ->join('users u', 'u.id = o.user_id')
    ->join('products p', 'p.id = o.product_id')
    ->select('o.*, u.name as user_name, p.name as product_name')
    ->where('o.status', 'completed')
    ->get()
    ->getResult();
```

### Complex Conditions
```php
<?php

$this->db->table('users')
    ->groupStart()
        ->where('role', 'admin')
        ->orWhere('role', 'moderator')
    ->groupEnd()
    ->where('status', 'active')
    ->get();
```

## Resources
- Query builder prevents SQL injection
- Use whereIn() for multiple values
- Use groupBy() and having() for aggregates

