> For the complete documentation index, see [llms.txt](https://crystal-garage.gitbook.io/lustra-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://crystal-garage.gitbook.io/lustra-docs/querying/low-level-sql/select-clause.md).

# Select Clause

Start a low-level select query with `Lustra::SQL.select`.

```crystal
query = Lustra::SQL.select(:id, :email)
  .from(:users)
  .where(active: true)
  .order_by(id: :asc)
  .limit(20)

query.to_sql
# => SELECT "id", "email" FROM "users" WHERE "active" = TRUE ORDER BY "id" ASC LIMIT 20
```

When no selected columns are provided, Lustra renders `SELECT *`.

```crystal
Lustra::SQL.select.from(:users).to_sql
# => SELECT * FROM "users"
```

## Selecting Columns

Pass strings, symbols, raw SQL fragments, or aliases.

```crystal
Lustra::SQL.select(:id, :email).from(:users)

Lustra::SQL.select("COUNT(*) AS total").from(:users)

Lustra::SQL.select({uid: "user_id"}).from(:posts)
# => SELECT user_id AS uid FROM "posts"
```

Use `distinct` for `SELECT DISTINCT` or `DISTINCT ON`.

```crystal
Lustra::SQL.select(:role).distinct.from(:users)

Lustra::SQL.select(:id, :email).distinct(%("users"."id")).from(:users)
```

## Filtering

The low-level builder supports the same expression engine used by model collections.

```crystal
Lustra::SQL.select.from(:users).where { users.id > 100 }

Lustra::SQL.select.from(:users).where(email: "admin@example.com")

Lustra::SQL.select.from(:users).where("email LIKE ?", "%@example.com")

Lustra::SQL.select.from(:users).where("email LIKE :pattern", pattern: "%@example.com")
```

Plain string conditions are raw SQL.

```crystal
Lustra::SQL.select.from(:users).where("deleted_at IS NULL")
```

## Fetching Rows

`fetch` yields each row as `Hash(String, Lustra::SQL::Any)`.

```crystal
Lustra::SQL.select(:id, :email).from(:users).fetch do |row|
  puts row["email"]
end
```

Use `to_a` when you need all rows in memory.

```crystal
rows = Lustra::SQL.select(:id).from(:users).to_a
```

Use `fetch_first` or `fetch_first!` for one row.

```crystal
row = Lustra::SQL.select.from(:users).where(id: 1).fetch_first
```

`fetch_first` and `fetch_first!` apply `LIMIT 1` only while fetching. If the builder already has another limit, that limit is restored before the helper returns.

Use `scalar` when the query returns one row and one column.

```crystal
count = Lustra::SQL.select("COUNT(*)").from(:users).scalar(Int64)
```

For large result sets, `fetch_with_cursor` streams rows through a PostgreSQL cursor.

```crystal
Lustra::SQL.select.from(:events).fetch_with_cursor(1_000) do |row|
  puts row["id"]
end
```

## Composing Write Queries

A select query with one table in `FROM` can be converted to an update or delete query. Lustra copies the table and `WHERE` clauses into the write query.

```crystal
inactive_users = Lustra::SQL.select.from(:users).where(active: false)

inactive_users.to_update.set(archived: true).execute
inactive_users.to_delete.execute
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://crystal-garage.gitbook.io/lustra-docs/querying/low-level-sql/select-clause.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
