Concatenate the Name and the Profession - Problem

You are given a table Person with information about people and their professions.

Task: Write a SQL query to report each person's name followed by the first letter of their profession enclosed in parentheses.

Requirements:

  • Concatenate the name with the first letter of the profession in parentheses
  • Return results ordered by person_id in descending order
  • The profession is an ENUM with values: 'Doctor', 'Singer', 'Actor', 'Player', 'Engineer', 'Lawyer'

Table Schema

Person
Column Name Type Description
person_id PK int Primary key, unique identifier for each person
name varchar Full name of the person
profession ENUM Profession (Doctor, Singer, Actor, Player, Engineer, Lawyer)
Primary Key: person_id
Note: The profession column contains predefined values representing different career types

Input & Output

Example 1 — Basic Concatenation
Input Table:
person_id name profession
1 Alex Singer
3 Alice Lawyer
2 Bob Doctor
Output:
name_profession
Alice(L)
Bob(D)
Alex(S)
💡 Note:

The query extracts the first letter of each profession: Singer → 'S', Lawyer → 'L', Doctor → 'D'. Then concatenates each name with the first letter in parentheses. Results are ordered by person_id in descending order (3, 2, 1).

Example 2 — Different Professions
Input Table:
person_id name profession
4 John Engineer
5 Mary Actor
6 Tom Player
Output:
name_profession
Tom(P)
Mary(A)
John(E)
💡 Note:

Shows different profession types: Engineer → 'E', Actor → 'A', Player → 'P'. Results ordered by person_id DESC (6, 5, 4), demonstrating the consistent pattern across all profession types.

Constraints

  • 1 ≤ person_id ≤ 1000
  • 1 ≤ name.length ≤ 100
  • profession is one of ['Doctor', 'Singer', 'Actor', 'Player', 'Engineer', 'Lawyer']

Visualization

Tap to expand
Concatenate Name and Profession InitialPerson Tableperson_idnameprofession1AlexSinger2BobDoctorSinger → SDoctor → DCONCAT(name,'(', SUBSTRING(...), ')')ORDER BY person_id DESCResultname_professionBob(D)Alex(S)
Understanding the Visualization
1
Input
Person table with id, name, profession
2
Process
Extract first letter and concatenate
3
Output
Formatted name with profession initial
Key Takeaway
🎯 Key Insight: String functions like CONCAT and SUBSTRING are perfect for formatting output by combining and extracting parts of text data
Asked in
Amazon 12 Microsoft 8 Apple 6
24.1K Views
Medium Frequency
~8 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen