91 lines
2.0 KiB
Plaintext
91 lines
2.0 KiB
Plaintext
---
|
|
title: SELECT | Datasquirel docs
|
|
description: Read data from a table using SELECT queries in Datasquirel
|
|
page_title: SELECT
|
|
page_description: Retrieve rows from your tables using SQL SELECT statements.
|
|
---
|
|
|
|
## Overview
|
|
|
|
`SELECT` retrieves rows from one or more tables. It is the most frequently used SQL statement.
|
|
|
|
## Basic Syntax
|
|
|
|
```sql
|
|
SELECT column1, column2 FROM table_name WHERE condition ORDER BY column1 ASC LIMIT 10;
|
|
```
|
|
|
|
## Examples
|
|
|
|
### Get All Rows
|
|
|
|
```sql
|
|
SELECT * FROM users;
|
|
```
|
|
|
|
### Select Specific Columns
|
|
|
|
```sql
|
|
SELECT id, name, email FROM users;
|
|
```
|
|
|
|
### Filter with WHERE
|
|
|
|
```sql
|
|
SELECT * FROM posts WHERE is_published = 1;
|
|
```
|
|
|
|
### Multiple Conditions
|
|
|
|
```sql
|
|
SELECT * FROM posts WHERE is_published = 1 AND category = 'technology';
|
|
```
|
|
|
|
### Sorting
|
|
|
|
```sql
|
|
SELECT * FROM posts ORDER BY created_at DESC;
|
|
```
|
|
|
|
### Pagination
|
|
|
|
```sql
|
|
SELECT * FROM posts ORDER BY created_at DESC LIMIT 10 OFFSET 20;
|
|
```
|
|
|
|
`LIMIT` caps the number of rows returned. `OFFSET` skips the first N rows — useful for pagination (page 3 with 10 items per page = `LIMIT 10 OFFSET 20`).
|
|
|
|
### JOIN
|
|
|
|
```sql
|
|
SELECT posts.id, posts.title, users.name AS author
|
|
FROM posts
|
|
JOIN users ON posts.user_id = users.id
|
|
WHERE posts.is_published = 1
|
|
ORDER BY posts.created_at DESC;
|
|
```
|
|
|
|
### Aggregation
|
|
|
|
```sql
|
|
SELECT category, COUNT(*) AS total
|
|
FROM posts
|
|
GROUP BY category
|
|
ORDER BY total DESC;
|
|
```
|
|
|
|
## Using SELECT via the API
|
|
|
|
For simple reads, use the [CRUD GET endpoint](/docs/api-reference/crud/get) — it handles filtering, sorting, and pagination through query parameters without writing raw SQL.
|
|
|
|
For complex queries (JOINs, aggregations, subqueries), use the [SQL API](/docs/api-reference/sql/options):
|
|
|
|
```javascript
|
|
const result = await datasquirel.api.sql({
|
|
key: process.env.DATASQUIREL_API_KEY,
|
|
params: {
|
|
query: "SELECT posts.id, posts.title, users.name AS author FROM posts JOIN users ON posts.user_id = users.id WHERE posts.is_published = 1 ORDER BY posts.created_at DESC LIMIT 20",
|
|
},
|
|
});
|
|
```
|