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
SQL: Concatenate Name with Profession INPUT TABLE person_id name profession 1 Alice Doctor 2 Bob Engineer 3 Carol Artist 4 David Singer Table: People person_id INT name, profession VARCHAR Order by person_id DESC Concatenate name + (first letter) ALGORITHM STEPS 1 SELECT Columns Choose name and profession 2 Extract First Letter LEFT(profession, 1) 3 CONCAT Function Join name + '(' + letter + ')' 4 ORDER BY DESC Sort by person_id descending SELECT CONCAT(name, '(', LEFT(profession,1), ')') AS result FROM People ORDER BY person_id DESC FINAL RESULT person_id result 4 David(S) 3 Carol(A) 2 Bob(E) 1 Alice(D) OK - 4 rows Sorted by person_id DESC Name + (First letter) of Profession Key Insight: CONCAT() joins multiple strings together. LEFT(string, n) extracts the first n characters. Combine these functions to create formatted output like "Name(X)" where X is the profession's first letter. ORDER BY column DESC sorts results in descending order (highest to lowest values). TutorialsPoint - Concatenate the Name and the Profession | Optimal Solution
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