How do I get the average string length in MySQL?



To get the average string length in MySQL, we will work around a query that gets rows from 1 to 10 and displays the result.

Let us first create a table. The query to create a table is as follows −

mysql> create table AverageString -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Value varchar(20) -> ); Query OK, 0 rows affected (0.66 sec)

Insert some records in the table using insert command. The query is as follows −

mysql> insert into AverageString(Value) values('MySQL Query'); Query OK, 1 row affected (0.18 sec) mysql> insert into AverageString(Value) values('SQL Server Query'); Query OK, 1 row affected (0.17 sec)

Display all records from the table using select statement. The query is as follows −

mysql> select *from AverageString;

Output

+----+------------------+ | Id | Value | +----+------------------+ | 1 | MySQL Query | | 2 | SQL Server Query | +----+------------------+ 2 rows in set (0.00 sec)

The query is as follows to get average string length −

mysql> select avg(length(Value)) from AverageString limit 10;

Output

+--------------------+ | avg(length(Value)) | +--------------------+ | 13.5000 | +--------------------+ 1 row in set (0.00 sec)
Updated on: 2019-07-30T22:30:25+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements