SQL Query to Find Shortest & Longest String From a Column of a Table

13 Apr, 2021
Here, we are going to see how to find the shortest and longest string from a column of a table in a database with the help of SQL queries. We will first create a database “geeks“, then we create a table “friends” with “firstName“, “lastName“, “age” columns. Then will perform our SQL query on this table to retrieve the shortest and longest string in a column.
For this article, we will be using the MS SQL Server as our database.
Use the below SQL statement to create a database called geeks:
CREATE DATABASE geeks;
USE geeks;
We have the following Employee table in our geeks database :
CREATE TABLE friends(
firstName VARCHAR(30) not NULL,
lastName VARCHAR(30) not NULL,
age INT NOT NULL);
You can use the below statement to query the description of the created table:
EXEC SP_COLUMNS friends;
Use the below statement to add data to the friends table:
INSERT INTO friends
values
('Ajit','Yadav', 20),
('Ajay', 'More', 21),
('Amir', 'Jawadwala', 21),
('Zara', 'Khan', 20),
('Yogesh', 'Vaishnav', 21),
('Ashish', 'Yadav', 21),
('Govind', 'Vaishnav', 22),
('Vishal', 'Vishwakarma', 21);
To verify the contents of the table use the below statement:
SELECT * FROM friends;
Now let’s find the shortest and longest firstName from the table we have just created using char_length(), min(), and max() functions and LIMIT clause.
Use the ow syntax to find the shortest firstName in the friends table:
SYNTAX : 
SELECT  TOP 1 *  FROM<table_name> –Here we want only one row that’s why TOP 1 *
WHERE
len(<string_column>) = 
(SELECT min(len(<string_column>))  FROM<table_name> ) ;
Example : 
SELECT TOP 1 * FROM friends
WHERE
len(firstName) = 
(SELECT min(len(firstName)) FROM friends);
Output :
If there are more than one strings of the same minimum length, and we want to retrieve, lexicographically, the shortest string then can do as following:
SYNTAX :
SELECT TOP 1 *  FROM<table_name>  –Here we want only one row that’s why TOP 1*.
WHERE
len(<string_column>) =
(SELECTmin(len(<string_column>)) FROM <table_name> ) 
ORDER BY <column_name>;   –In this case column_name would be firstName.
Example :
SELECT TOP 1 *  FROM friends
WHERE
len(firstName) = 
(SELECT min(len(firstName)) FROM friends)
ORDER BY firstname;
Output :
SYNTAX :
SELECT TOP 1* FROM<table_name>
WHERE
len(<string_column>) = 
(SELECT max(len(<string_column>)) FROM <table_name> );
Example :
SELECT TOP 1 * FROMfriends
WHERE
len(firstName) = 
(SELECT max(len(firstName)) FROMfriends);
Output :
SYNTAX :
SELECT TOP 1* FROM<table_name>
WHERE
len(<string_column>) =
(SELECT max(len(<string_column>)) from <table_name> )
ORDER BY <string_column>;  –here we would order data by firstName.
Example :
SELECT TOP 1* FROM friends
WHERE
len(firstName) = 
(SELECT max(len(firstName)) FROM friends) 
ORDER BY firstName;
Output :
Picked
SQL-Query
SQL
SQL
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Update Multiple Columns in Single Update Statement in SQL?
Window functions in SQL
SQL | Sub queries in From Clause
What is Temporary Table in SQL?
SQL using Python
SQL Query to Find the Name of a Person Whose Name Starts with Specific Letter
SQL Query to Convert VARCHAR to INT
RANK() Function in SQL Server
SQL Query to Compare Two Dates
How to Write a SQL Query For a Specific Date Range and Date Time?