Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to aggregate names into a single string separated by commas.
PostgreSQL
SELECT STRING_AGG(name, [1]) AS all_names FROM users; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a semicolon ';' which is not a common separator in lists.
Forgetting to put the separator inside quotes.
Using a period '.' which is uncommon for separating names.
✗ Incorrect
The STRING_AGG function concatenates strings using the specified separator. Here, a comma ',' is used to separate names.
2fill in blank
mediumComplete the code to aggregate emails separated by a semicolon.
PostgreSQL
SELECT STRING_AGG(email, [1]) AS all_emails FROM contacts; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a comma ',' instead of a semicolon.
Not quoting the separator character.
Using a colon ':' which is not the requested separator.
✗ Incorrect
Using ';' as the separator aggregates emails separated by semicolons.
3fill in blank
hardFix the error in the code to aggregate product names separated by a space.
PostgreSQL
SELECT STRING_AGG(product_name [1] ' ') AS products FROM inventory;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the comma between arguments.
Using a semicolon instead of a comma.
Using parentheses incorrectly.
✗ Incorrect
The STRING_AGG function requires a comma between the column and the separator string.
4fill in blank
hardFill both blanks to aggregate city names separated by a dash and order them alphabetically.
PostgreSQL
SELECT STRING_AGG(city, [1] ORDER BY [2]) AS cities FROM locations;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using an underscore '_' instead of a dash.
Ordering by a non-existent column like 'name'.
Forgetting to quote the separator.
✗ Incorrect
The separator is a dash '-' and the ORDER BY uses the city column to sort names alphabetically.
5fill in blank
hardFill all three blanks to aggregate usernames in uppercase, separated by a colon, ordered by user ID.
PostgreSQL
SELECT STRING_AGG(UPPER([1]), [2] ORDER BY [3]) AS user_list FROM users;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'name' instead of 'username' or 'user_id'.
Forgetting to quote the separator.
Not using the correct column for ordering.
✗ Incorrect
We convert 'username' to uppercase, separate by colon ':', and order by 'user_id'.