Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the maximum length of data in a particular field in postgresql?

Tags:

postgresql

I have a table "Users" and one of the column is "USERNAME". I am trying to find out the maximum length of the username by comparing all the usernames. I need this max value to use for another query. I tried

SELECT  max(length(users.username)),username 
FROM users group by username; 

returns all the rows.

Example:

 USERNAME
----------
Tara  
Amyamyse  
Tommy  
John  

I would like to get the result as below:

MAX_Length     USERNAME
--------------------
  8             Amyamyse
like image 531
user2926497 Avatar asked Sep 05 '25 02:09

user2926497


1 Answers

Another option:

select username, length(username) as username_length
from users
where length(username) = (select max(length(username)) from users)

This would return multiple users in case there be a tie for the longest username. If you have specific rules for breaking a tie then update your question.

like image 50
Tim Biegeleisen Avatar answered Sep 07 '25 23:09

Tim Biegeleisen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!