0
0
PostgreSQLquery~20 mins

Character types (char, varchar, text) in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Character Types Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output length of stored values in different character types

Consider a table messages with three columns: fixed_char of type char(5), var_char of type varchar(5), and text_col of type text. The row inserted is ('hi', 'hi', 'hi'). What will be the result of the query that selects the length of each column's value?

PostgreSQL
CREATE TABLE messages (fixed_char char(5), var_char varchar(5), text_col text);
INSERT INTO messages VALUES ('hi', 'hi', 'hi');
SELECT length(fixed_char) AS len_fixed, length(var_char) AS len_var, length(text_col) AS len_text FROM messages;
A{"len_fixed": 2, "len_var": 2, "len_text": 2}
B{"len_fixed": 5, "len_var": 5, "len_text": 5}
C{"len_fixed": 5, "len_var": 2, "len_text": 2}
D{"len_fixed": 2, "len_var": 5, "len_text": 2}
Attempts:
2 left
💡 Hint

Remember that char(n) pads the string with spaces to the fixed length n.

🧠 Conceptual
intermediate
2:00remaining
Difference in storage behavior between char and varchar

Which statement correctly describes the difference between char(n) and varchar(n) in PostgreSQL?

A<code>char(n)</code> and <code>varchar(n)</code> both store variable-length strings without padding.
B<code>char(n)</code> stores variable-length strings; <code>varchar(n)</code> stores fixed-length strings padded with spaces.
C<code>char(n)</code> and <code>varchar(n)</code> both store fixed-length strings padded with spaces.
D<code>char(n)</code> always stores fixed-length strings padded with spaces; <code>varchar(n)</code> stores variable-length strings without padding.
Attempts:
2 left
💡 Hint

Think about how the database treats the length of stored strings for each type.

📝 Syntax
advanced
2:00remaining
Which query will cause an error due to character type length limit?

Given a table users with a column username varchar(3), which INSERT statement will cause an error?

PostgreSQL
CREATE TABLE users (username varchar(3));
AINSERT INTO users VALUES ('abcd');
BINSERT INTO users VALUES ('abc');
CINSERT INTO users VALUES ('ab');
DINSERT INTO users VALUES ('a');
Attempts:
2 left
💡 Hint

Check the length of the string compared to the column's maximum length.

optimization
advanced
2:00remaining
Choosing the best character type for large text data

You need to store user comments that can be very long, sometimes thousands of characters. Which character type is best to use in PostgreSQL for this column?

A<code>char(1000)</code>
B<code>text</code>
C<code>varchar(1000)</code>
D<code>varchar(255)</code>
Attempts:
2 left
💡 Hint

Consider flexibility and performance for very long strings.

🔧 Debug
expert
2:00remaining
Why does this query return unexpected trailing spaces?

Given a table products with a column code char(4), the query SELECT code FROM products WHERE code = 'AB' returns no rows, but SELECT code FROM products WHERE code LIKE 'AB%' returns rows. Why?

ABecause <code>char(4)</code> pads 'AB' with spaces, so equality fails but LIKE matches the prefix.
BBecause <code>char(4)</code> trims trailing spaces automatically, causing LIKE to work but = to fail.
CBecause <code>char(4)</code> stores only the first two characters, so 'AB' is incomplete.
DBecause <code>char(4)</code> converts all values to uppercase, affecting equality.
Attempts:
2 left
💡 Hint

Think about how fixed-length char stores strings and how comparisons work.