MySQL | COMPRESS( ) Function

The MySQL COMPRESS() function is used for the compression of a string. The value returned by the COMPRESS() function is a binary string.

The COMPRESS() function stores non-empty strings as a four-byte length of the uncompressed string, which is then followed by the compressed string. If the string ends with space, a “.” character is added to the string. Also, it should be noted that empty strings are stored as empty strings. The COMPRESS() function accepts one parameter which is the string to be compressed.



Parameters Used:

  • string_to_compress – It is used to specify the plain text string that is to be compressed.

Return Value:
The COMPRESS function in MySQL returns a compressed string.

Supported Versions of MySQL:

  • MySQL 5.7
  • MySQL 5.6
  • MySQL 5.5
  • MySQL 5.1
  • MySQL 5.0
  • MySQL 4.1

Example-1: Implementing COMPRESS function on a string.


Example-2: Implementing COMPRESS function on a string which has a combination of characters and integers.




Example-3: Implementing COMPRESS function on a string and returning the length of the string after compression.

COMPRESS('geeksforgeeks'), LENGTH(COMPRESS('geeksforgeeks')); 


\0\0\0x?KOM?.N?/J?\0%?f    22 

Example-4: Implementing COMPRESS function on a NULL string and returning the length of the string after compression.




My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.