SQL Basics: Trimming the field

Valentyn
Oct 4, 2021

You have access to a table of monsters as follows:

monsters schema

  • id
  • name
  • legs
  • arms
  • characteristics

The monsters in the provided table have too many characteristics, they really only need one each. Your job is to trim the characteristics down so that each monster only has one. If there is only one already, provide that. If there are multiple, provide only the first one (don’t leave any commas in there).

You must return a table with the format as follows:

output schema

  • id
  • name
  • characteristic

Order by id.

Solution

There we learn simple function split_part. It splits substring on a specified delimiter and returns the n-th substring. Don’t forget to order the result by id.

SELECT
id,
name,
split_part(characteristics, ',', 1) as characteristic
FROM
monsters
ORDER BY
id;

--

--