Skip to content

Commit

Permalink
vault backup: 2024-05-16 11:48:03
Browse files Browse the repository at this point in the history
Affected files:
content/.obsidian/plugins/recent-files-obsidian/data.json
content/.obsidian/workspace.json
content/postgresql/PostgreSQL Insert and Import.md
content/postgresql/PostgreSQL Select.md
  • Loading branch information
windsuzu committed May 16, 2024
1 parent 1cde929 commit e55093c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 57 deletions.
20 changes: 10 additions & 10 deletions content/.obsidian/plugins/recent-files-obsidian/data.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
{
"recentFiles": [
{
"basename": "PostgreSQL Select",
"path": "postgresql/PostgreSQL Select.md"
"basename": "Install PostgreSQL on WSL 2 (Ubuntu)",
"path": "postgresql/Install PostgreSQL on WSL 2 (Ubuntu).md"
},
{
"basename": "PostgreSQL",
"path": "postgresql/PostgreSQL.md"
},
{
"basename": "PostgreSQL Select",
"path": "postgresql/PostgreSQL Select.md"
},
{
"basename": "PostgreSQL Insert and Import",
"path": "postgresql/PostgreSQL Insert and Import.md"
},
{
"basename": "PostgreSQL Tables",
"path": "postgresql/PostgreSQL Tables.md"
},
{
"basename": "PostgreSQL Databases",
"path": "postgresql/PostgreSQL Databases.md"
Expand All @@ -20,14 +28,6 @@
"basename": "Connect PostgreSQL on WSL 2 (Ubuntu)",
"path": "postgresql/Connect PostgreSQL on WSL 2 (Ubuntu).md"
},
{
"basename": "Install PostgreSQL on WSL 2 (Ubuntu)",
"path": "postgresql/Install PostgreSQL on WSL 2 (Ubuntu).md"
},
{
"basename": "PostgreSQL Tables",
"path": "postgresql/PostgreSQL Tables.md"
},
{
"basename": "index",
"path": "index.md"
Expand Down
31 changes: 9 additions & 22 deletions content/.obsidian/workspace.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,8 @@
"source": false
}
}
},
{
"id": "5ef31e4588d35e14",
"type": "leaf",
"state": {
"type": "markdown",
"state": {
"file": "postgresql/PostgreSQL Select.md",
"mode": "source",
"source": false
}
}
}
],
"currentTab": 1
]
}
],
"direction": "vertical"
Expand Down Expand Up @@ -115,7 +102,7 @@
"state": {
"type": "localgraph",
"state": {
"file": "postgresql/PostgreSQL Select.md",
"file": "postgresql/PostgreSQL.md",
"options": {
"collapse-filter": true,
"search": "",
Expand Down Expand Up @@ -165,7 +152,7 @@
"state": {
"type": "outline",
"state": {
"file": "postgresql/PostgreSQL Select.md"
"file": "postgresql/PostgreSQL.md"
}
}
},
Expand Down Expand Up @@ -206,7 +193,7 @@
"state": {
"type": "file-properties",
"state": {
"file": "postgresql/PostgreSQL Select.md"
"file": "postgresql/PostgreSQL.md"
}
}
},
Expand All @@ -216,7 +203,7 @@
"state": {
"type": "backlink",
"state": {
"file": "postgresql/PostgreSQL Select.md",
"file": "postgresql/PostgreSQL.md",
"collapseAll": false,
"extraContext": false,
"sortOrder": "alphabetical",
Expand All @@ -233,7 +220,7 @@
"state": {
"type": "outgoing-link",
"state": {
"file": "postgresql/PostgreSQL Select.md",
"file": "postgresql/PostgreSQL.md",
"linksCollapsed": false,
"unlinkedCollapsed": false
}
Expand All @@ -259,15 +246,15 @@
"cmdr:Obsidian Git: Create backup": false
}
},
"active": "5ef31e4588d35e14",
"active": "09dd403f5501ccb2",
"lastOpenFiles": [
"postgresql/PostgreSQL.md",
"postgresql/PostgreSQL Select.md",
"postgresql/PostgreSQL.md",
"postgresql/PostgreSQL Insert and Import.md",
"postgresql/PostgreSQL Tables.md",
"postgresql/PostgreSQL Databases.md",
"postgresql/Connect PostgreSQL on WSL 2 (Ubuntu).md",
"postgresql/Install PostgreSQL on WSL 2 (Ubuntu).md",
"postgresql/PostgreSQL Tables.md",
"index.md",
"postgresql",
"prisma",
Expand Down
4 changes: 1 addition & 3 deletions content/postgresql/PostgreSQL Insert and Import.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
draft: false
date: 2024-05-15 18:06
date: 2024-05-16 00:50
tags:
- postgres
---
Expand Down Expand Up @@ -57,8 +57,6 @@ SELECT * FROM person
# ...
```



> [!info] References
> - [Learn PostgreSQL Tutorial - Full Course for Beginners - YouTube](https://www.youtube.com/watch?v=qw--VYLpxG4)
> - [Mockaroo - Random Data Generator and API Mocking Tool](https://www.mockaroo.com/)
62 changes: 40 additions & 22 deletions content/postgresql/PostgreSQL Select.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
---
draft: false
date: 2024-05-15 19:32
date: 2024-05-16 00:49
tags:
- postgres
---

Use `ORDER BY` to sort the result by one or more columns.
Use `ORDER BY` to sort the result by one or more columns. By default `ORDER BY` uses ascending order (`ASC`), you can change it to descending order by specifying `DESC`.

```sql
# default is ASC
SELECT * FROM person ORDER BY date_of_birth ASC;
SELECT * FROM person ORDER BY date_of_birth DESC;
SELECT * FROM person ORDER BY gender DESC, date_of_birth ASC;
Expand All @@ -17,37 +16,56 @@ SELECT * FROM person ORDER BY gender DESC, date_of_birth ASC;
Use `DISTINCT` to list all distinct values in a column or a combination of columns.

```sql
SELECT DISTINCT gender FROM person
SELECT DISTINCT gender, last_name FROM person
SELECT DISTINCT gender FROM person;
SELECT DISTINCT gender, last_name FROM person;
```

Use `WHERE` and
Use `WHERE` with or without the following keywords or operators to select rows with conditions.

- Use `AND`, `OR` to find union or intersection of conditions
- Use comparison operators like `>`, `<`, `>=`, `<=`, `=`, `<>`
- Use `IN` to quickly select the matching ones in a pool
- Use `BETWEEN AND` to select values between a range
- Use `LIKE` to find values with a certain pattern (Both `%` and `_` denote placeholder)
- Use `ILIKE` to get the effect of `LIKE` but without the case sensitivity

: `AND` `OR` `COMPARISON OPERATORS` `>` `<` `>=` `<=` `=` `<>` `IN` `BETWEEN AND` `LIKE` `ILIKE`


`LIMIT`:


`OFFSET`:


`GROUP BY`:


`GROUP BY HAVING`:
```sql
SELECT * FROM person WHERE gender = 'Agender';
SELECT * FROM person WHERE gender = 'Male' AND date_of_birth > '2024-01-01';
SELECT * FROM person WHERE gender = 'Male' OR gender = 'Female';
SELECT * FROM person WHERE gender IN ('Male', 'Female', 'Agender');
SELECT * FROM person WHERE id BETWEEN 50 AND 55;
SELECT * FROM person WHERE last_name LIKE '%han';
SELECT * FROM person WHERE last_name ILIKE '____lan';
```

Use `LIMIT` to show a certain number of rows. Then use `OFFSET` to skip the first N rows.

AGGREGATE FUNCTIONS
```sql
SELECT * FROM person LIMIT 5 OFFSET 10;
```

Use `GROUP BY` to group rows with the same column value into summary rows. `GROUP BY` is often used with aggregate functions, such as `COUNT()`, `SUM()`, `AVG()`, `MIN()`, `MAX()`. In addition, you can use `GROUP BY HAVING` to add conditions.

`AS`:
```sql
SELECT gender, COUNT(*) FROM person GROUP BY gender;
SELECT gender, COUNT(*) FROM person GROUP BY gender HAVING COUNT(*) > 15;
SELECT gender, AVG(age) FROM person GROUP BY gender;
SELECT gender, MIN(age) FROM person GROUP BY gender;
SELECT gender, MAX(age) FROM person GROUP BY gender;
```

Use `AS` to assign aliases to columns that appear in the result.

`COALESCE`:
```sql
SELECT date_of_birth AS birthday FROM person;
```

Use `COALESCE` as a useful tool for handling NULL values.

```sql
SELECT COALESCE(email, 'Not provided') FROM person;
```

> [!info] References
> - [Learn PostgreSQL Tutorial - Full Course for Beginners - YouTube](https://www.youtube.com/watch?v=qw--VYLpxG4)
Expand Down

0 comments on commit e55093c

Please sign in to comment.