GROUP_CONCAT in SQL
Level 4135 keystrokes
GROUP_CONCAT concatenates values from multiple rows into a single string.
SQL code
-- Choose the columns to return1SELECT -- Department of each group2 department, -- GROUP_CONCAT joins values from many rows into one string3 GROUP_CONCAT( -- The value to join: each employee's name4 name -- Sort names alphabetically inside each group5 ORDER BY name -- Put a comma and a space between names6 SEPARATOR ', ' -- Call the joined column members7 ) AS members-- Read rows from the employees table8FROM9 employees-- One group, and one result row, per department10GROUP BY11 department-- Sort result rows by department name12ORDER BY13 department;Output
+------------+---------------------+ | department | members | +------------+---------------------+ | HR | Alice, Bob | | IT | Carol, Dan, Eve | | Sales | Frank | +------------+---------------------+ 3 rows in set
Practice typing this code. Results are not recorded in the rankings.
Symbols in this code
The symbols you type in this SQL code and how often they appear.
,× 2'× 2_× 1;× 1(× 1)× 1