2022-12-06 09:29:41 +00:00
|
|
|
---
|
2024-06-15 11:00:03 +01:00
|
|
|
tags:
|
|
|
|
- SQL
|
|
|
|
- databases
|
2022-12-06 09:29:41 +00:00
|
|
|
---
|
|
|
|
|
|
|
|
# Retrieve data from table with SQL `SELECT` statement
|
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
> Select data from the database. The data returned is stored in a result table
|
|
|
|
> called the **result-set**.
|
2022-12-06 09:29:41 +00:00
|
|
|
|
|
|
|
_A table named `models`:_
|
|
|
|
|
|
|
|
| modelId | name | cores | releaseDate |
|
|
|
|
| ------- | -------------------- | ----- | ----------- |
|
|
|
|
| 2 | Raspberry Pi Model B | 2 | 2022-11-19 |
|
|
|
|
| 3 | Raspberry Pi Model C | 4 | 2022-11-25 |
|
|
|
|
| 4 | MacBook Pro M2 | 9 | 2022-03-18 |
|
|
|
|
|
|
|
|
## Basic usage
|
|
|
|
|
|
|
|
```sql
|
|
|
|
SELECT name FROM models
|
|
|
|
```
|
|
|
|
|
|
|
|
We can pass in a comma-separated **list of fields**:
|
|
|
|
|
|
|
|
```sql
|
|
|
|
SELECT name, releaseDate FROM models
|
|
|
|
```
|
|
|
|
|
|
|
|
## With parameters
|
|
|
|
|
|
|
|
### `WHERE`
|
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
The `WHERE` clause specifies a search criterion. The `WHERE` clause should
|
|
|
|
always be last in the syntax. The clauses are always written in this order:
|
|
|
|
`FROM` followed by `WHERE`
|
2022-12-06 09:29:41 +00:00
|
|
|
|
|
|
|
```sql
|
|
|
|
SELECT name FROM models WHERE cores > 3
|
|
|
|
```
|
|
|
|
|
|
|
|
### Compound statements
|
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
Compound statements allow you to apply more filters to your clauses within an
|
|
|
|
SQL statement. SQL allows you to build complex, combinatorial `WHERE` clauses by
|
|
|
|
using Boolean and mathematical operators (i.e `AND` , `OR` , `>` , `<` , `!=` ,
|
|
|
|
`<=` ...)
|
2022-12-06 09:29:41 +00:00
|
|
|
|
|
|
|
Multiple clauses:
|
|
|
|
|
|
|
|
```sql
|
|
|
|
SELECT name, ram, release_date
|
|
|
|
FROM model
|
|
|
|
WHERE release_date > '2018-01-01' AND ram > 512;
|
|
|
|
```
|
|
|
|
|
|
|
|
More complex logic achieve with parentheses:
|
|
|
|
|
|
|
|
```sql
|
|
|
|
SELECT name, cores, release_date
|
|
|
|
FROM model
|
|
|
|
WHERE (manufacturer_id = 1 OR manufacturer_id = 2) AND cores >= 2;
|
|
|
|
```
|
|
|
|
|
|
|
|
## Transform returned data
|
|
|
|
|
|
|
|
We can apply **ordering** to the return data
|
|
|
|
|
|
|
|
```sql
|
|
|
|
SELECT name FROM models ORDER BY name, cores
|
|
|
|
SELECT name FROM models ORDER BY name, cores DESC
|
|
|
|
```
|
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
> `ORDER BY` always comes last, after the selection and any filtering clauses
|
|
|
|
> but _before_ a `WHERE` clause
|
2022-12-19 08:41:44 +00:00
|
|
|
|
|
|
|
## Paginate
|
|
|
|
|
|
|
|
We can break our returned data into blocks with `LIMIT`
|
|
|
|
|
|
|
|
```sql
|
|
|
|
LIMIT 5 -- Return first five items
|
|
|
|
LIMIT 5,5 -- Return first five items from the sixth row
|
|
|
|
```
|