> For the complete documentation index, see [llms.txt](https://g4b0.gitbook.io/g4b0-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://g4b0.gitbook.io/g4b0-docs/documentation/hacking-web/sql/query-results.md).

# Query Results

Cómo controlar la salida de los resultados de una query.

**Ordenar resultados — `ORDER BY`**

Ordena por la columna que especifiques. Por defecto es ascendente, pero puedes indicar `ASC` o `DESC`:

```sql
SELECT * FROM logins ORDER BY password;        -- ascendente (default)
SELECT * FROM logins ORDER BY password DESC;   -- descendente
```

También puedes ordenar por **varias columnas**, para tener un orden secundario cuando hay valores duplicados en la primera:

```sql
SELECT * FROM logins ORDER BY password DESC, id ASC;
```

**Limitar resultados — `LIMIT`**

Si la query devuelve muchos registros, limitas la cantidad:

```sql
SELECT * FROM logins LIMIT 2;      -- solo 2 registros
```

Con un **offset** (desplazamiento), indicas desde dónde empezar antes del número de registros:

```sql
SELECT * FROM logins LIMIT 1, 2;   -- empieza en el 2º registro, devuelve 2
```

⚠️ El offset cuenta desde **0**. Por eso `LIMIT 1, 2` empieza a incluir desde el segundo registro.

**Cláusula `WHERE`**

Filtra registros según una condición:

```sql
SELECT * FROM table_name WHERE <condicion>;
```

Ejemplos:

```sql
SELECT * FROM logins WHERE id > 1;              -- id mayor a 1
SELECT * FROM logins WHERE username = 'admin';  -- username exacto
```

⚠️ Los tipos string y date van entre comillas simples (`'`) o dobles (`"`); los números se usan directamente.

**Cláusula `LIKE`**

Selecciona registros que coinciden con un patrón, usando comodines:

* `%` → coincide con **cero o más** caracteres
* `_` → coincide con **exactamente un** caracter

```sql
SELECT * FROM logins WHERE username LIKE 'admin%';  -- empieza con "admin"
SELECT * FROM logins WHERE username LIKE '___';     -- exactamente 3 caracteres (ej. "tom
```


---

# 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://g4b0.gitbook.io/g4b0-docs/documentation/hacking-web/sql/query-results.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.
