1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-17 05:58:01 +00:00

Compare commits

...

4 Commits

Author SHA1 Message Date
Bob Conan
ecf78bd0d6 Merge 5615da920f into 9afe6503ec 2024-12-03 09:56:28 -05:00
jbranchaud
9afe6503ec Add Fetch Specific Number Of Results as a Postgres TIL 2024-12-03 08:52:38 -06:00
Bob Conan
5615da920f Update README.md, fix typos 2024-11-15 16:16:31 -06:00
BobConanDev
c60c63f554 Updated README.md, fix typo(s) 2024-11-15 16:42:57 -05:00
2 changed files with 46 additions and 3 deletions

View File

@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
_1524 TILs and counting..._
_1525 TILs and counting..._
---
@@ -191,7 +191,7 @@ _1524 TILs and counting..._
- [Aliasing An Ansible Host](devops/aliasing-an-ansible-host.md)
- [Allow Cross-Origin Requests To Include Cookies](devops/allow-cross-origin-requests-to-include-cookies.md)
- [Allow HTTPS Through Your UFW Firewall](devops/allow-https-through-your-ufw-firewall.md)
- [Check For Cached Site Assocation File For iOS](devops/check-for-cached-site-association-file-for-ios.md)
- [Check For Cached Site Association File For iOS](devops/check-for-cached-site-association-file-for-ios.md)
- [Check The Status of All Services](devops/check-the-status-of-all-services.md)
- [Check The Syntax Of nginx Files](devops/check-the-syntax-of-nginx-files.md)
- [Connect To An RDS PostgreSQL Database](devops/connect-to-an-rds-postgresql-database.md)
@@ -733,7 +733,7 @@ _1524 TILs and counting..._
- [Check If Clusters Are Upgrade Compatible](postgres/check-if-clusters-are-upgrade-compatible.md)
- [Check If The Local Server Is Running](postgres/check-if-the-local-server-is-running.md)
- [Check If User Role Exists For Database](postgres/check-if-user-role-exists-for-database.md)
- [Check Table For Any Oprhaned Records](postgres/check-table-for-any-orphaned-records.md)
- [Check Table For Any Orphaned Records](postgres/check-table-for-any-orphaned-records.md)
- [Checking Inequality](postgres/checking-inequality.md)
- [Checking The Type Of A Value](postgres/checking-the-type-of-a-value.md)
- [Clear The Screen In psql](postgres/clear-the-screen-in-psql.md)
@@ -777,6 +777,7 @@ _1524 TILs and counting..._
- [Escaping String Literals With Dollar Quoting](postgres/escaping-string-literals-with-dollar-quoting.md)
- [Export Query Results To A CSV](postgres/export-query-results-to-a-csv.md)
- [Extracting Nested JSON Data](postgres/extracting-nested-json-data.md)
- [Fetch Specific Number Of Results](postgres/fetch-specific-number-of-results.md)
- [Find Duplicate Records In Table Without Unique Id](postgres/find-duplicate-records-in-table-without-unique-id.md)
- [Find Records That Contain Duplicate Values](postgres/find-records-that-contain-duplicate-values.md)
- [Find Records That Have Multiple Associated Records](postgres/find-records-that-have-multiple-associated-records.md)

View File

@@ -0,0 +1,42 @@
# Fetch Specific Number Of Results
If you pull up just about any intro to PostgreSQL (or even SQL), one of the
first things they are going to teach you is the `limit` clause. This is taught
as _the_ way for limiting the result set to a specific number of rows.
```sql
> select title from books limit 4;
+-----------------------+
| title |
|-----------------------|
| The Secret History |
| A Gentleman in Moscow |
| Exhalation: Stores |
| Annihilation |
+-----------------------+
SELECT 4
```
You might be as surprised as I was to learn that `limit` is not part of the SQL
standard. It is extremely common for this use case, but the SQL standard
defines `fetch first N rows only` as the way to fetch a specific number of
rows. As we can see, [it works identically to `limit
N`](https://www.postgresql.org/docs/current/sql-select.html#SQL-LIMIT).
```sql
> select title from books fetch first 4 rows only;
+-----------------------+
| title |
|-----------------------|
| The Secret History |
| A Gentleman in Moscow |
| Exhalation: Stores |
| Annihilation |
+-----------------------+
SELECT 4
```
The `rows` and `row` keywords are interchangeable which makes statements more
readable if, for instance, you're doing `... fetch first 1 row only`.
[source](https://www.cybertec-postgresql.com/en/postgresql-limit-vs-fetch-first-rows-with-ties/)