PHPackages                             alephtools/sqlbuilder - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. alephtools/sqlbuilder

ActiveLibrary[Database &amp; ORM](/categories/database)

alephtools/sqlbuilder
=====================

The library to build plain SQL.

v2.2.1(2y ago)17.0k↓58.8%3[1 PRs](https://github.com/AlephTav/sqlbuilder/pulls)MITPHPPHP &gt;=8.1

Since Jul 1Pushed 1y ago2 watchersCompare

[ Source](https://github.com/AlephTav/sqlbuilder)[ Packagist](https://packagist.org/packages/alephtools/sqlbuilder)[ RSS](/packages/alephtools-sqlbuilder/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (4)Versions (11)Used By (0)

SQL Builder
===========

[](#sql-builder)

The library bases on two ideas:

- an SQL statement expressed with this library should be similar to the statement itself;
- flexible combination of statement components.

Thus, using this library you get simple but, at the same time, powerful tool to build SQL statements in flexible and intuitive way.

```
use AlephTools\SqlBuilder\PostgreSql\SelectStatement;
use AlephTools\SqlBuilder\Sql\Expression\ConditionalExpression;

$st = (new SelectStatement())
    ->select([
        'u.id',
        'u.name',
        'company' => 'c.name',
        'unreadMessageCount' => (new SelectStatement())
            ->select('COUNT(*)')
            ->from('user_messages', 'm')
            ->where('m.user_id = u.id')
            ->andWhere('m.read_at IS NULL')
    ])
    ->from('users u')
    ->innerJoin('companies c', 'c.id = u.company_id')
    ->where('u.deleted_at IS NULL')
    ->andWhere((new ConditionalExpression())
        ->where('u.roles', 'IN', ['ADMIN', 'RESELLER'])
        ->orWhere(
            (new SelectStatement())
                ->select('COUNT(*)')
                ->from('user_contacts uc')
                ->where('uc.user_id = u.id'),
            '>',
            5
        )
    );

// Outputs:
// SELECT
//     u.id, u.name, c.name company,
//     (SELECT COUNT(*) FROM user_messages m WHERE m.user_id = u.id AND m.read_at IS NULL) unreadMessageCount
// FROM users u
// INNER JOIN companies c ON c.id = u.company_id
// WHERE
//     u.deleted_at IS NULL AND (
//         u.roles IN (:p1, :p2) OR
//         (SELECT COUNT(*) FROM user_contacts uc WHERE uc.user_id = u.id) > :p3
//     )
echo $st->toSql();

// Outputs:
// ['p1' => 'ADMIN', 'p2' => 'RESELLER', 'p3' => 5]
print_r($st->getParams());

// Executes statement if StatementExecutor is defined, otherwise an exception is thrown
$rows = $st->rows();
```

Installation
------------

[](#installation)

```
composer require alephtools/sqlbuilder

```

###  Health Score

38

—

LowBetter than 83% of packages

Maintenance33

Infrequent updates — may be unmaintained

Popularity25

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 56.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~153 days

Recently: every ~205 days

Total

8

Last Release

754d ago

Major Versions

v1.0.2 → v2.0.02022-03-11

PHP version history (2 changes)v1.0.0PHP &gt;=7.4

v2.0.0PHP &gt;=8.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1460449?v=4)[Aleph Tav](/maintainers/AlephTav)[@AlephTav](https://github.com/AlephTav)

---

Top Contributors

[![AlephTav](https://avatars.githubusercontent.com/u/1460449?v=4)](https://github.com/AlephTav "AlephTav (38 commits)")[![ElisDN](https://avatars.githubusercontent.com/u/1673552?v=4)](https://github.com/ElisDN "ElisDN (21 commits)")[![ArcanumOvO](https://avatars.githubusercontent.com/u/49122229?v=4)](https://github.com/ArcanumOvO "ArcanumOvO (7 commits)")[![tyliooo](https://avatars.githubusercontent.com/u/62645431?v=4)](https://github.com/tyliooo "tyliooo (1 commits)")

---

Tags

sqlAlephSQL Builder

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/alephtools-sqlbuilder/health.svg)

```
[![Health](https://phpackages.com/badges/alephtools-sqlbuilder/health.svg)](https://phpackages.com/packages/alephtools-sqlbuilder)
```

###  Alternatives

[doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database schema introspection and management.

9.7k605.0M6.8k](/packages/doctrine-dbal)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k117.2M118](/packages/jdorn-sql-formatter)[doctrine/sql-formatter

a PHP SQL highlighting library

1.9k181.8M112](/packages/doctrine-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k54.9M11.6k](/packages/illuminate-database)[phpmyadmin/sql-parser

A validating SQL lexer and parser with a focus on MySQL dialect.

48252.2M78](/packages/phpmyadmin-sql-parser)[ifsnop/mysqldump-php

PHP version of mysqldump cli that comes with MySQL

1.3k6.0M77](/packages/ifsnop-mysqldump-php)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
