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

SQL: Concatenating Columns

daco2020 2022. 7. 4. 23:00
반응형

Given the table below:

** names table schema **

  • id
  • prefix
  • first
  • last
  • suffix

Your task is to use a select statement to return a single column table containing the full title of the person (concatenate all columns together except id), as follows:

** output table schema **

  • title

Don't forget to add spaces.

 

Solution:

SELECT 
  CONCAT(prefix,' ',first,' ',last,' ',suffix) AS title 
FROM 
  names

 

You can use the 'CONCAT_WS' function to avoid repeating whitespace delimiters.

SELECT 
  CONCAT_WS(' ', prefix, first, last, suffix) AS title 
FROM 
  names

 

Result:

title
Mrs. Dante Kuhic I
Dr. Blanche Corwin Sr.
Miss Clinton Goldner DDS
Ms. Jermaine Walker II
Mrs. Abbie Spinka Jr.

 

반응형