나는 이렇게 학습한다/Algorithm & SQL

SQL Basics - Position

daco2020 2022. 6. 6. 16:37
반응형

You have access to a table of monsters as follows:

monsters schema

  • id
  • name
  • legs
  • arms
  • characteristics

In each row, the characteristic column has a single comma. Your job is to find it using position(). You must return a table with the format as follows:

output schema

  • id
  • name
  • comma

The comma column will contain the position of the comma within the characteristics string. Order the results by comma.

 

 

Solution:

SELECT 
  id,
  name,
  POSITION(',' in characteristics) AS comma
FROM
  monsters
ORDER BY 
  comma;

 

POSITION is a function that returns an index after checking whether a string is included in the corresponding column.

 

 

 

Result:

id name comma
9 Random 1
7 Random 3
5 Martin 4
1 Cyril 4
6 Random 4
2 Tiny 6
반응형