Build dynamic SQL queries, with confidence

A fluent SQL query builder for C#

var query = db.Query("Books").OrderByDesc("PublishingDate");

if(Request.Has("category.name"))
{
    var category = Request.Get("category.name");

    query.Join("Categories", "Categories.Id", "Books.CategoryId")
        .Where("Categories.Name", category);
}

var recentBooks = query.Limit(10).Get();
Expressive

You will be able to write complex queries without hitting the docs

Secure

It uses the parameter binding technique, to prevent SQL injection.
It supports Operator whitelisting.

Multiple Database Vendors

It Supports SqlServer, MySql, PostgreSql, Oracle, SQLite and Firebird.

Flexible and Rich API

Sub queries, nested Where conditions, Common Table Expressions, Complex Join statements and more.

Extensible

Don't wait, add your own methods.
Extend the current compiler to support your favorite database.

Helpers included

Available when you need the little push, Date/Time and String helper methods like WhereDate(), WhereTime(), WhereContains() and many more.

Why developers love SqlKata?

Start and build faster

No long setup required, just write the query and get the data.
With few lines of code, you can start building your app.
It cannot be simpler!

var compiler = new SqlServerCompiler();
var db = new QueryFactory(connection, compiler);

var books = db.Query("Books").Get();
db.Query("Books").Where(q =>
    q.Where("Stock", "<", 50).OrWhere("InHighDemand", 1)
).Union(
    db.Query("Books").Where("Price", "<", 10)
);

You are in Control

Forget about hacky solutions, and write the query the way you want it from the begining.
Unleash your SQL skill and write performant queries from the first minute.

Express natively

A better way to expose your queries.
Share your base queries with your team, and let them build on top of it.
Kind of stored procedure but written in C#.

// define the base queries
class TransactionService
{
    public Query All()
    {
        return db.Query("Transactions").WhereTrue("IsApproved");
    }

    public Query Latest(int top = 10)
    {
        return All().OrderByDesc("Date").Take(top);
    }
}

// then extend them as needed per request
var data = transactionService.Latest(10)
    .Join("Accounts", "Accounts.Id", "AccountId")
    .Get();
SqlKata is compatible with .net core framework SqlKata is compatible with .net framework

Compatible with your Framework

SqlKata is compatible with both .NET Core and .NET Framework.

Works on Windows, Linux and macOS.

Suitable for Complex Dashboards and Heavy Reports

Build advanced dashbaords and reports without sacrificing the performance.

“Developers say that they never had this powerfullness before.”
var visitsTimeline = db.Query("Visits")
    .Join("Users", "Users.Id", "Visits.UserId")
    .WhereBetween("2024-08-20", "2024-11-20")
    .GroupBy("Users.Id", "Visits.Date")
    .Select("Users.Id", "Visits.Date")
    .SelectRaw("count(1) as [Count]")
    .Having("Count", ">", 5)
    .Get();
var activity = db.Query("Activities")
    .Join("Users", "Users.Id", "Visits.UserId")
    .OrderByDesc("Date")
    .Union(new Query("Alerts"))
    .OrderBy("Date")
    .Get();

Web Api Friendly

SqlKata make it easy to build Web API interfaces, you can use it to build REST or GRAPHQL interfaces.

Powered with some useful methods like Include, ForPage and Paginate.

public class UsersController
{
    public async Task<IActionResult> Index()
    {
        // define last purchase for each user
        var lastPurchaseQuery = db.Query("Transactions")
            .Where("Type", "Purchase")
            .GroupBy("Transactions.UserId")
            .Select("Users.Id").SelectRaw("MAX([Transactions.Date])");

        // define users orders
        var ordersQuery = db.Query("Orders");

        // fetch the data
        var users = db.Query("Users")
            .Include("LastVisit", lastPurchaseQuery)
            .IncludeMany("Orders", ordersQuery)
            .ForPage(1, perPage: 10)
            .Get();

        return Json(users);

    }
}

https://app.mysite.com/api/users?include:lastVisit,orders
[{
    "id": 1,

    "lastVisit": {
        "id": "...",
        "date": "..."
    },
    "orders": [{
        "date": "...",
        "amount": "..."
    }]

},
{
    "id": 2,

    "lastVisit": {
        "id": "...",
        "date": "..."
    },
    "orders": [{
        "date": "...",
        "amount": "..."
    }, {
        "date": "...",
        "amount": "..."
    }]
}]

one email per month about tips & tricks, new features, and maybe community feedback