0
0
PostgreSQLquery~20 mins

String collation and sort order in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Collation Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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;
A["Banana", "Apricot", "apple", "cherry"]
B["apple", "Apricot", "Banana", "cherry"]
C["Apricot", "Banana", "apple", "cherry"]
D["apple", "Banana", "Apricot", "cherry"]
Attempts:
2 left
💡 Hint
PostgreSQL default collation sorts uppercase letters before lowercase letters.
query_result
intermediate
2:00remaining
Using COLLATE to change string sort order
What is the order of names returned by this query?
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";
A["Apricot", "Banana", "apple", "cherry"]
B["Banana", "Apricot", "apple", "cherry"]
C["apple", "Banana", "Apricot", "cherry"]
D["apple", "Apricot", "Banana", "cherry"]
Attempts:
2 left
💡 Hint
The "C" collation sorts purely by byte value, uppercase letters (65-90) before lowercase (97-122).
📝 Syntax
advanced
2: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';
ASELECT name FROM fruits ORDER BY name COLLATE "en_US";
BSELECT name FROM fruits ORDER BY name COLLATE 'en_US';
CSELECT name FROM fruits ORDER BY name COLLATE en_US;
DSELECT name FROM fruits ORDER BY name COLLATE 'en-US';
Attempts:
2 left
💡 Hint
PostgreSQL requires double quotes for collation names, not single quotes.
🧠 Conceptual
advanced
2:00remaining
Effect of collation on string comparison
Which statement about string comparison with different collations in PostgreSQL is true?
AComparing strings with different collations always returns an error.
BCollation has no effect on string operations in PostgreSQL.
CCollation affects only sorting order, not equality comparison.
DCollation affects both sorting order and equality comparison results.
Attempts:
2 left
💡 Hint
Think about how collation defines character order and equivalence.
optimization
expert
3: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?
AAdd an index on the text column using the non-default collation.
BUse ORDER BY with COLLATE in every query without indexes.
CConvert the column to lowercase before sorting without indexes.
DCreate a materialized view sorted by the column without indexes.
Attempts:
2 left
💡 Hint
Indexes can speed up sorting if they match the collation used.