w3resource

PostgreSQL String() Function: Display the first name and length of first name which starts with the letters 'A', 'J' or 'M'


13. Write a query that displays the first name and the character 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 Solution:

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 table: employees


Output:

pg_exercises=# SELECT first_name "Name",
pg_exercises-# LENGTH(first_name) "Length"
pg_exercises-# FROM employees
pg_exercises-# WHERE first_name LIKE 'J%'
pg_exercises-# OR first_name LIKE 'M%'
pg_exercises-# OR first_name LIKE 'A%'
pg_exercises-# ORDER BY first_name ;
    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
(32 rows)

Practice Online


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

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming