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 thelast_namefollowed by a comma, a space, and then thefirst_name. If eitherfirst_nameorlast_nameis 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, iffirst_nameis 'John' andlast_nameis NULL, the result should be 'John'. Iffirst_nameis NULL andlast_nameis '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
-- Write your query here
SELECT
Your query runs on the example tables shown with the problem, then on hidden datasets when you submit.
Write standard SQL. MySQL habits work too: IFNULL, IF, YEAR(), DATE_FORMAT, GROUP_CONCAT, `backticks` and LIMIT 5, 10. It runs on PostgreSQL, so integer division keeps whole numbers (5 / 2 = 2): write 5.0 / 2 for 2.5.