Skip to content

Instantly share code, notes, and snippets.

@cjmamo
Last active June 27, 2017 06:05
Show Gist options
  • Save cjmamo/0ba4ad21df38dacee9d64258c0166da4 to your computer and use it in GitHub Desktop.
Save cjmamo/0ba4ad21df38dacee9d64258c0166da4 to your computer and use it in GitHub Desktop.
SELECT * FROM
(SELECT *, DENSE_RANK() OVER (ORDER BY name, id) count FROM
(SELECT *, DENSE_RANK() OVER (ORDER BY name, id) offset_ FROM
(SELECT a.id, a.name, b.title
FROM AUTHOR a, BOOK b
WHERE a.id = b.authorId) result) result_offset
WHERE offset_ > 0) result_offset_count
WHERE count <= 2
SELECT * FROM
(SELECT *, DENSE_RANK() OVER (ORDER BY name, id) count FROM
(SELECT a.id, a.name, b.title, DENSE_RANK() OVER (ORDER BY a.name, a.id) offset_
FROM AUTHOR a, BOOK b
WHERE a.id = b.authorId) result_offset
WHERE offset_ > 0) result_offset_count
WHERE count <= 2
SELECT * FROM
(SELECT *, DENSE_RANK() OVER (ORDER BY name, id) offset_ FROM
(SELECT a.id, a.name, b.title
FROM AUTHOR a, BOOK b
WHERE a.id = b.authorId) result) result_offset
WHERE offset_ > 0 AND offset_ <= (0 + 2)
SELECT *, DENSE_RANK() OVER (ORDER BY name, id) count FROM
(SELECT a.id, a.name, b.title, DENSE_RANK() OVER (ORDER BY a.name, a.id) offset_
FROM AUTHOR a, BOOK b
WHERE a.id = b.authorId) result_offset
WHERE offset_ > 1
SELECT * FROM
(SELECT a.id, a.name, b.title, DENSE_RANK() OVER (ORDER BY a.name, a.id) offset_
FROM AUTHOR a, BOOK b
WHERE a.id = b.authorId) result_offset
WHERE offset_ > 1
SELECT a.id, a.name, b.title, DENSE_RANK() OVER (ORDER BY a.name, a.id) offset_
FROM AUTHOR a, BOOK b
WHERE a.id = b.authorId
SELECT a.id, a.name, b.title
FROM AUTHOR a, BOOK b
WHERE a.id = b.authorId
ORDER BY a.name LIMIT 2 OFFSET 0
ID | Name | Title
21 | Hintjens Peter | Confessions of a Necromancer
21 | Hintjens Peter | Social Architecture
21 | Hintjens Peter | ZeroMQ, Messaging for Many Applications
89 | Chomsky Noam | How the World Works
89 | Chomsky Noam | The Architecture of Language
89 | Chomsky Noam | What Kind of Creatures are We?
144 | Antonopoulos Andreas | The Internet of Money
144 | Antonopoulos Andreas | Mastering Bitcoin
144 | Antonopoulos Andreas | Mastering Ethereum
ID | Name | Title
144 | Antonopoulos Andreas | The Internet of Money
144 | Antonopoulos Andreas | Mastering Bitcoin
ID | Name | Title | offset_
144 | Antonopoulos Andreas | The Internet of Money | 1
144 | Antonopoulos Andreas | Mastering Bitcoin | 1
144 | Antonopoulos Andreas | Mastering Ethereum | 1
89 | Chomsky Noam | How the World Works | 2
89 | Chomsky Noam | The Architecture of Language | 2
89 | Chomsky Noam | What Kind of Creatures are We? | 2
22 | Hintjens Peter | Confessions of a Necromancer | 3
22 | Hintjens Peter | Social Architecture | 3
22 | Hintjens Peter | ZeroMQ, Messaging for Many Applications | 3
ID | Name | Title | offset_
89 | Chomsky Noam | How the World Works | 2
89 | Chomsky Noam | The Architecture of Language | 2
89 | Chomsky Noam | What Kind of Creatures are We? | 2
22 | Hintjens Peter | Confessions of a Necromancer | 3
22 | Hintjens Peter | Social Architecture | 3
22 | Hintjens Peter | ZeroMQ, Messaging for Many Applications | 3
ID | Name | Title | offset_ | count
89 | Chomsky Noam | How the World Works | 2 | 1
89 | Chomsky Noam | The Architecture of Language | 2 | 1
89 | Chomsky Noam | What Kind of Creatures are We? | 2 | 1
22 | Hintjens Peter | Confessions of a Necromancer | 3 | 2
22 | Hintjens Peter | Social Architecture | 3 | 2
22 | Hintjens Peter | ZeroMQ, Messaging for Many Applications | 3 | 2
ID | Name | Title | offset_ | count
144 | Antonopoulos Andreas | The Internet of Money | 1 | 1
144 | Antonopoulos Andreas | Mastering Bitcoin | 1 | 1
144 | Antonopoulos Andreas | Mastering Ethereum | 1 | 1
89 | Chomsky Noam | How the World Works | 2 | 2
89 | Chomsky Noam | The Architecture of Language | 2 | 2
89 | Chomsky Noam | What Kind of Creatures are We? | 2 | 2
CREATE TABLE AUTHOR
(
id INTEGER PRIMARY KEY,
name VARCHAR(255)
);
CREATE TABLE BOOK
(
id INTEGER PRIMARY KEY,
authorId INTEGER REFERENCES Author (id),
title VARCHAR(255)
);
SELECT a.id, a.name, b.title
FROM (SELECT * FROM AUTHOR ORDER BY name LIMIT 2 OFFSET 0) a, BOOK b
WHERE a.id = b.authorId
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment