w3resource

PostgreSQL Basic SELECT Statement: Get the length of the name, including first_name and last_name of all the employees


16. Write a query to get the first name, last name and the length of the name, including first_name and last_name of all the employees from employees table.

Sample Solution:

Code:

-- Selecting the first name, last name, and the sum of the lengths of the first name and last name for all records in the employees table
SELECT first_name, last_name, 
-- Calculating the sum of the lengths of the first name and last name and labeling it as "Length of Names"
LENGTH(first_name) + LENGTH(last_name) AS "Length of Names" 
-- Selecting data from the employees table
FROM employees;

Explanation:

  • This SQL code selects the first name, last name, and calculates the sum of the lengths of the first name and last name for all records in the "employees" table.
  • The LENGTH() function is used to determine the length of each name component.
  • The sum of the lengths of the first name and last name is labeled as "Length of Names" using the AS keyword in the SELECT statement.
  • The result set will contain three columns: first name, last name, and the calculated length of names.

Sample table: employees


Output:

pg_exercises=# SELECT first_name,last_name,
pg_exercises-# LENGTH(first_name)+LENGTH(last_name)  "Length of  Names"
pg_exercises-# FROM employees;
 first_name  |  last_name  | Length of  Names
-------------+-------------+------------------
 Steven      | King        |               10
 Neena       | Kochhar     |               12
 Lex         | De Haan     |               10
 Alexander   | Hunold      |               15
 Bruce       | Ernst       |               10
 David       | Austin      |               11
 Valli       | Pataballa   |               14
 ....
 Alexander   | Khoo        |               13
 Shelli      | Baida       |               11
 Sigal       | Tobias      |               11
 Guy         | Himuro      |                9
 Karen       | Colmenares  |               15
 Matthew     | Weiss       |               12
 ....
 Steven      | Markle      |               12
 Laura       | Bissot      |               11
 Mozhe       | Atkinson    |               13
 James       | Marlow      |               11
 TJ          | Olson       |                7
 ....
 Renske      | Ladwig      |               12
 Stephen     | Stiles      |               13
 John        | Seo         |                7
 Joshua      | Patel       |               11
 Trenna      | Rajs        |               10
...
 Hermann     | Baer        |               11
 Shelley     | Higgins     |               14
 William     | Gietz       |               12
(107 rows)

Practice Online



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

Previous: Write a query to get the first names after removing all the leading and trailing blanks of all the employees from employees table.
Next: Write a query to check whether the first_name column of the employees table containing any number.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/postgresql-exercises/basic/basic-select-statement-exercise-16.php