Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

MySQL String Exercises: Display the first name starts with specific letters

MySQL String: Exercise-14 with Solution

Write a query that displays the first name and the length of the first name for all employees whose name starts with the letters 'A', 'J' or 'M'. Give each column an appropriate label. Sort the results by the employees' first names.

Sample table: employees


Code:

SELECT first_name "Name",
LENGTH(first_name) "Length"
FROM employees
WHERE first_name LIKE 'J%'
OR first_name LIKE 'M%'
OR first_name LIKE 'A%'
ORDER BY first_name ;

Sample Output:

Name			Length
Adam			4
Alana			5
Alberto			7
Alexander		9
Alexander		9
Alexis			6
Allan			5
Alyssa			6
Amit			4
Anthony			7
Jack			4
James			5
James			5
Janette			7
Jason			5
Jean			4
Jennifer		8
Jennifer		8
John			4
John			4
John			4
Jonathon		8
Jose Manuel		11
Joshua			6
Julia			5
Julia			5
Martha			6
Mattea			6
Matthew			7
Michael			7
Michael			7
Mozhe			5

MySQL Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous:Write a query to display the length of first name for employees where last name contain character 'c' after 2nd position.
Next:Write a query to display the first name and salary for all employees. Format the salary to be 10 characters long, left-padded with the $ symbol.

What is the difficulty level of this exercise?