The list of all the queries

Derived tables with ranking

Query goal: Find views and materialized views that use rank and dense_rank window functions.
Notes about the query: In the returned subquery of view/materialized view the query adds line break (br) tag at the beginning of each clause for the better readability in case the query result is displayed in a web browser. LEFT JOIN and RIGHT JOIN keywords are marked with red because quite often outer join is used unnecessarily because INNER JOIN operation would be enough. The query uses (b) tags to mark some of the main clauses in the subquery of the derived table.
Query type: General (Overview of some aspect of the database.)
Query license: MIT License
Data source: INFORMATION_SCHEMA+system catalog
SQL query: Click on query to copy it

WITH user_defined_derived_tables AS (SELECT
table_schema,
table_name,
regexp_replace(
regexp_replace(
regexp_replace(
regexp_replace(
regexp_replace(
regexp_replace(
regexp_replace(
regexp_replace(
regexp_replace(
regexp_replace(
regexp_replace(
view_definition,'FROM','<br><b>FROM</b>','g'),
'ORDER BY','<br><b>ORDER BY</b>','g'), 
'WHERE','<br><b>WHERE</b>','g'), 
'GROUP BY','<br><b>GROUP BY</b>','g'), 
'SELECT','<b>SELECT</b>','g'), 
'LEFT JOIN','<font color=red>LEFT JOIN</font>','g'), 
'RIGHT JOIN','<font color=red>RIGHT JOIN</font>','g'),  
'HAVING','<br><b>HAVING</b>','g'),
'UNION','<br><b>UNION</b>','g'),
'EXCEPT','<br><b>EXCEPT</b>','g'),
'INTERSECT','<br><b>INTERSECT</b>','g') AS def,
table_type
FROM (SELECT 
views.table_schema, 
views.table_name, 
views.view_definition,
'VIEW' AS table_type
FROM 
information_schema.views
WHERE table_schema NOT IN (SELECT schema_name
FROM INFORMATION_SCHEMA.schemata
WHERE schema_name<>'public' AND
schema_owner='postgres' AND schema_name IS NOT NULL)
UNION SELECT schemaname, matviewname, definition, 'MATERIALIZED VIEW' AS table_type
FROM pg_catalog.pg_matviews
ORDER BY table_name) AS foo)
SELECT table_schema, table_name, def, table_type
FROM user_defined_derived_tables
WHERE def~*'rank[(][)][[:space:]]+over' AND def~*'dense_rank[(][)][[:space:]]+over'
ORDER BY table_schema, table_name;

Categories where the query belongs to

Category nameCategory description
Derived tablesQueries of this category provide information about the derived tables (views, materialized views), which are used to implement virtual data layer.
System-defined functionsQueries of this category provide information about the use of system-defined functions.

Reference materials for further reading

Reference
http://www.postgresqltutorial.com/postgresql-rank-function/
http://www.postgresqltutorial.com/postgresql-dense_rank-function/

The list of all the queries