Skip to content

PostgreSQL Tutorial

  • How-To Guides
PostgreSQL Tutorial
Window Functions

PostgreSQL – PERCENT_RANK Function

ByAdmin August 21, 2023August 21, 2023

The PERCENT_RANK() function is used to calculate the relative rank of a row within a result set as a fraction between 0 and 1. It gives the percentage of rows that are ranked lower than the current row. This function is often used to determine the percentile of a particular value within a dataset. The…

Read More PostgreSQL – PERCENT_RANK FunctionContinue

Window Functions

PostgreSQL – CUME_DIST Function

ByAdmin August 13, 2023August 13, 2023

The CUME_DIST() function is used to calculate the cumulative distribution of values within a result set. This function gives you the proportion of values that are less than or equal to the current value in the ordered result set. It’s often used to analyze the relative position of a value within a dataset, especially in…

Read More PostgreSQL – CUME_DIST FunctionContinue

Window Functions

PostgreSQL – NTILE Function

ByAdmin August 13, 2023August 13, 2023

The NTILE() function is used to distribute the rows of a result set into a specified number of roughly equal-sized “tiles” or groups. Each row is assigned a tile number based on the distribution. This function is often used for data segmentation and percentile calculations, especially when you want to divide your data into quantiles…

Read More PostgreSQL – NTILE FunctionContinue

Window Functions

PostgreSQL – NTH_VALUE Function

ByAdmin August 13, 2023August 13, 2023

The NTH_VALUE() function is used to access the value of a column from the nth row within a result set based on a specified order. This function allows you to retrieve values from rows at a specific position relative to the current row. It’s useful for scenarios where you need to access values from rows…

Read More PostgreSQL – NTH_VALUE FunctionContinue

Window Functions

PostgreSQL – LEAD Function

ByAdmin August 13, 2023August 13, 2023

The LEAD() function is used to access the value of a column in a subsequent row within the same result set. This function is often used to compare values between the current row and the next row, or to retrieve values from “leading” rows relative to the current row based on a specified order. It’s…

Read More PostgreSQL – LEAD FunctionContinue

Window Functions

PostgreSQL – LAG Function

ByAdmin August 13, 2023September 22, 2023

The LAG() function is used to access the value of a column in a preceding row within the same result set. This function allows you to compare values between the current row and the previous row or to retrieve values from “lagging” rows relative to the current row based on a specified order. The syntax…

Read More PostgreSQL – LAG FunctionContinue

Window Functions

PostgreSQL – ROW_NUMBER Function

ByAdmin August 13, 2023August 13, 2023

The ROW_NUMBER() function is used to assign a unique integer to each row within a result set. This function is often used to generate consecutive integers based on the specified ordering criteria. It’s useful for scenarios where you need a sequential identifier for each row, such as ranking, pagination, and generating unique row identifiers. The…

Read More PostgreSQL – ROW_NUMBER FunctionContinue

Window Functions

PostgreSQL – LAST_VALUE Function

ByAdmin August 13, 2023August 13, 2023

The LAST_VALUE() function is used to retrieve the last value in an ordered set of rows within a result set. Similar to the FIRST_VALUE() function, the LAST_VALUE() function operates within a window of rows defined by the OVER clause, and it’s often used to get the latest or last occurrence of a certain value within…

Read More PostgreSQL – LAST_VALUE FunctionContinue

Window Functions

PostgreSQL – FIRST_VALUE Function

ByAdmin August 13, 2023August 13, 2023

The FIRST_VALUE() function is used to retrieve the first value in an ordered set of rows within a result set. This function is often used in combination with the OVER clause to perform calculations on a window of rows, and then extract the first value from that window. It’s useful for scenarios where you want…

Read More PostgreSQL – FIRST_VALUE FunctionContinue

Window Functions

PostgreSQL – DENSE_RANK Function

ByAdmin August 13, 2023August 13, 2023

The DENSE_RANK() function is used to assign a unique rank to each row within the result set, similar to the RANK() function. However, unlike the RANK() function, the DENSE_RANK() function doesn’t leave gaps in ranking when there are ties (rows with equal values). Instead, it assigns the same rank to rows with the same values…

Read More PostgreSQL – DENSE_RANK FunctionContinue

Page navigation

1 2 Next PageNext

  • Querying Data
    • SELECT
    • WHERE
    • DISTINCT
    • DISTINCT ON
    • ORDER BY
    • GROUP BY
    • HAVING
    • LIMIT
    • OFFSET
    • INNER JOIN
    • LEFT JOIN
    • RIGHT JOIN
    • SELF JOIN
    • CROSS JOIN
    • NATURAL JOIN
    • FULL OUTER JOIN
    • UNION
    • UNION ALL
    • INTERSECT
    • IN
    • BETWEEN
    • LIKE
    • EXISTS
    • ANY
    • ALL
    • CUBE
    • ROLLUP
    • GROUPING SETS
  • CONSTRAINTS
    • Primary Key Constraint
    • Foreign Key Constraint
    • Check Constraint
    • Not Null Constraint
    • Unique Constraint
    • Exclusion Constraint
  • Conditional Expressions
    • CASE
    • COALESCE
    • LEAST
    • GREATEST
    • NULLIF
  • Comparison Operators
    • <=
    • >=
    • IS NULL
    • Not Equal (!=)
    • Equal(=)
    • Less Than (<)
    • Greater Than (>)
  • Logical Operators
    • AND
    • OR
    • NOT
  • Aggregate Functions
    • COUNT
    • SUM
    • MAX
    • MIN
    • AVG
    • STRING_AGG
    • ARRAY_AGG
  • String Functions
    • LENGTH
    • CONCAT
    • TRIM
    • LEFT
    • RIGHT
    • POSITION
    • SUBSTRING
    • REPLACE
    • TRANSLATE
    • SPLIT_PART
    • CHR
    • LPAD
    • FORMAT
    • MD5
    • ASCII
    • REGEXP_MATCHES
    • REGEX_REPLACE
  • Date Functions
    • NOW
    • CURRENT_DATE
    • CURRENT_TIME
    • CURRENT_TIMESTAMP
    • LOCALTIME
    • TO_DATE
    • TO_TIMESTAMP
    • DATE_TRUNC
    • DATE_PART
    • EXTRACT
    • AGE
  • Math Functions
    • ROUND
    • FLOOR
    • CEIL
    • TRUNC
    • ABS
    • MOD
  • Conversion Functions
    • TO_CHAR
    • TO_DATE
    • TO_NUMBER
    • TO_TIMESTAMP
  • Window Functions
    • ROW_NUMBER
    • RANK
    • DENSE_RANK
    • FIRST_VALUE
    • LAST_VALUE
    • LEAD
    • LAG
    • NTH_VALUE
    • NTILE
    • CUME_DIST
  • Data Manipulation
    • Insert
    • Update
    • Delete
    • Truncate
  • Data Types
    • DATA TYPES
    • INTEGER
    • BIGINT
    • NUMERIC
    • CHAR
    • VARCHAR
    • TEXT
    • DATE
    • TIME
    • TIMESTAMP
    • INTERVAL
    • BOOLEAN
    • ENUM
    • SERIAL
    • UUID
    • JSON
    • JSONB
    • Array
    • HSTORE
    • USER DEFINED
  • Database Objects
    • TABLES
    • VIEWS
    • INDEXES
    • SEQUENCES
    • MATERIALIZED VIEWS
    • FUNCTIONS
    • TRIGGERS
    • TYPES
  • Database Security
    • Roles
    • Privileges
    • GRANT
    • REVOKE
  • Table Management
    • Create Table
    • Rename Table
    • DROP TABLE
    • Add Column to a Table
    • Rename Column
    • Modify Datatype
    • DROP COLUMN
  • ARRAY FUNCTIONS
    • ARRAY_FILL
    • CARDINALITY
    • STRING_TO_ARRAY
    • ARRAY_TO_STRING
    • ARRAY_PREPEND
    • ARRAY_CAT
    • ARRAY_APPEND
    • ARRAY_REPLACE
    • ARRAY_REMOVE
    • ARRAY_POSITIONS
    • ARRAY_POSITION
    • ARRAY_LENGTH
    • UNNEST
  • JSON Functions
    • JSON_AGG
    • JSON_POPULATE_RECORDSET
    • JSON_EXTRACT_PATH_TEXT
    • JSON_POPULATE_RECORD
    • JSON_ARRAY_ELEMENTS
    • JSON_EACH
    • JSON_ARRAY_LENGTH
    • JSON_OBJECT
    • JSON_BUILD_OBJECT
    • JSON_BUILD_ARRAY
    • row_to_json
    • array_to_json
    • to_json
  • XML Functions
    • XMLCONCAT
    • XMLAGG
    • XMLELEMENT
    • XPATH
  • Transaction Control
    • COMMIT
    • ROLLBACK
    • SAVEPOINT
    • ROLLBACK PREPARED
  • Schema Management
    • Drop Schema
    • Change Schema Owner
    • Rename Schema
    • Create Schema
    • CURRENT_SCHEMA
  • PL/PgSQL
    • Programming in PostgreSQL Database
    • Variables
    • Constants
    • Row Type Variables
    • Functions
    • Procedures
    • Triggers
    • Cursors
    • IF Statement
    • CASE Statement
    • LOOP Statement
    • FOR LOOP
    • WHILE Statement

© 2025 PostgreSQL-Tutorial.com

  • How-To Guides