Challenge - 5 Problems
Collation Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Sorting strings with default collation
Given the table fruits with a column
name containing the values: 'apple', 'Banana', 'cherry', 'Apricot', what is the order of names returned by this query?SELECT name FROM fruits ORDER BY name;
PostgreSQL
CREATE TABLE fruits (name TEXT); INSERT INTO fruits (name) VALUES ('apple'), ('Banana'), ('cherry'), ('Apricot'); SELECT name FROM fruits ORDER BY name;
Attempts:
2 left
💡 Hint
PostgreSQL default collation sorts uppercase letters before lowercase letters.
✗ Incorrect
By default, PostgreSQL sorts strings using the database's collation, which places uppercase letters before lowercase letters. So 'Apricot' and 'Banana' come before 'apple' and 'cherry'.
❓ query_result
intermediate2:00remaining
Using COLLATE to change string sort order
What is the order of names returned by this query?
Given the same
SELECT name FROM fruits ORDER BY name COLLATE "C";
Given the same
fruits table as before.PostgreSQL
SELECT name FROM fruits ORDER BY name COLLATE "C";
Attempts:
2 left
💡 Hint
The "C" collation sorts purely by byte value, uppercase letters (65-90) before lowercase (97-122).
✗ Incorrect
The "C" collation sorts strings by ASCII byte values, so uppercase letters (like 'A'=65) come before lowercase letters (like 'a'=97). Same order as default.
📝 Syntax
advanced2:00remaining
Identify the syntax error in COLLATE usage
Which option contains a syntax error when trying to order by a column with a specific collation in PostgreSQL?
PostgreSQL
SELECT name FROM fruits ORDER BY name COLLATE 'en_US';
Attempts:
2 left
💡 Hint
PostgreSQL requires double quotes for collation names, not single quotes.
✗ Incorrect
In PostgreSQL, collation names must be double-quoted identifiers, not string literals. Using single quotes causes a syntax error.
🧠 Conceptual
advanced2:00remaining
Effect of collation on string comparison
Which statement about string comparison with different collations in PostgreSQL is true?
Attempts:
2 left
💡 Hint
Think about how collation defines character order and equivalence.
✗ Incorrect
Collation defines how strings are compared, affecting both sorting and equality checks. Different collations can treat characters as equal or different.
❓ optimization
expert3:00remaining
Optimizing queries with collations for performance
You have a large table with a text column frequently sorted using a non-default collation. Which approach improves query performance the most?
Attempts:
2 left
💡 Hint
Indexes can speed up sorting if they match the collation used.
✗ Incorrect
Creating an index on the column with the specific collation allows PostgreSQL to use the index for sorting, greatly improving performance.