Format Customer NamesEasy

Format Customer Names

EasySQLPractice library · +10 XPstring functionssqlformatting

UptoSkills Arena needs to display customer names consistently across all platforms. Given a table of customer information, you need to generate a properly formatted full name.

Tables:

  • customers: customer_id (integer), first_name (varchar(50)), last_name (varchar(50))

Return:

  • full_name: The formatted full name, which should be the last_name followed by a comma, a space, and then the first_name. If either first_name or last_name is NULL, the corresponding part in the full name should be omitted, but the comma and space should still appear if both parts are present. For example, if first_name is 'John' and last_name is NULL, the result should be 'John'. If first_name is NULL and last_name is 'Doe', the result should be 'Doe'. If both are NULL, the result should be an empty string. If both are present, it should be 'LastName, FirstName'.

The results should be ordered by customer_id.

Tables

CREATE TABLE customers (
  customer_id integer PRIMARY KEY,
  first_name varchar(50),
  last_name varchar(50)
);

Example data

INSERT INTO customers (customer_id, first_name, last_name) VALUES
(1, 'Aarav', 'Sharma'),
(2, 'Diya', NULL),
(3, NULL, 'Patel'),
(4, 'Vihaan', 'Gupta'),
(5, 'Anaya', 'Singh'),
(6, NULL, NULL),
(7, 'Aditya', 'Kumar');

Your query is also checked on hidden datasets with different data.

Hints