As a database developer working with PostgreSQL, I recently received a requirement to work with PostgreSQL order by length of string. In this comprehensive guide, I’ll walk you through various methods to sort data by string length in PostgreSQL, with real-world examples.
PostgreSQL Order By Length Of String
Let’s explore how to implement this functionality in PostgreSQL.
Method 1: Using the LENGTH() Function
The most straightforward approach is to use PostgreSQL’s built-in LENGTH() function, which returns the number of characters in a string.
Syntax
SELECT column_name
FROM table_name
ORDER BY LENGTH(column_name) ASC;
Example
I have a table of customer feedback; I want to sort it by the length of the feedback comment using the query below.
SELECT customer_name, feedback
FROM customer_feedback
ORDER BY LENGTH(feedback) ASC;
After executing the above query, I got the expected output; it displayed the shortest comments first, as shown in the screenshot below.

Method 2: Using CHAR_LENGTH() for Unicode Support
We can also use the CHAR_LENGTH() for this purpose, especially when you’re working with international text that includes Unicode characters.
Syntax
SELECT column_name
FROM table_name
ORDER BY CHAR_LENGTH(column_name) DESC;
Example
We can execute the below query to list the longest essays first.
SELECT student_name, essay_title, essay_content
FROM student_essays
ORDER BY CHAR_LENGTH(essay_content) DESC;
After executing the above query, I got the expected output, as shown in the below screenshot.

Method 3: Combining Length Sorting with Other Criteria
In real-world applications, I often need to sort by multiple criteria.
Example
The below query will sort first by length and then alphabetically. Products are sorted by their description length, and products with the same description length are sorted alphabetically by name.
SELECT product_name, product_description
FROM products
ORDER BY LENGTH(product_description) ASC, product_name ASC;
After executing the above query, I got the expected output shown in the screenshot below.

Example
For a task management system, we can execute the below query to sort the prioritized tasks based on multiple factors.
SELECT task_id, task_name, task_description, priority
FROM Tasks
WHERE assignee = 'John Smith'
ORDER BY priority DESC, LENGTH(task_description) ASC;
After executing the above query, I got the expected output.

Method 4: Using Window Functions with Length Sorting
You can also combine length sorting with window functions using the below query. This query not only sorts by feedback length but also assigns a rank based on that length.
SELECT
customer_name,
feedback,
LENGTH(feedback) AS feedback_length,
RANK() OVER (ORDER BY LENGTH(feedback) DESC) AS length_rank
FROM customer_feedback;
After executing the above query, I got the expected output, which is shown in the screenshot below.

Conclusion
Sorting by string length in PostgreSQL is so cool that it can enhance data organization and user experience across various applications. Whether using the simple LENGTH() function or combining it with more complex sorting criteria.
You may also like the articles below.
- PostgreSQL Order By Enum
- PostgreSQL ORDER BY COUNT
- PostgreSQL Order By Union
- PostgreSQL Order By Array
- PostgreSQL Order By Group By
I am Bijay having more than 15 years of experience in the Software Industry. During this time, I have worked on MariaDB and used it in a lot of projects. Most of our readers are from the United States, Canada, United Kingdom, Australia, New Zealand, etc.
Want to learn MariaDB? Check out all the articles and tutorials that I wrote on MariaDB. Also, I am a Microsoft MVP.