Bird
Raised Fist0
MySQLquery~20 mins

String types (VARCHAR, CHAR, TEXT) in MySQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
String Types Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
Output length difference between CHAR and VARCHAR
Consider a MySQL table users with a name column defined as CHAR(10) and another column nickname defined as VARCHAR(10). If you insert the value 'John' into both columns, what will be the result of SELECT LENGTH(name), LENGTH(nickname) FROM users WHERE id = 1;?
MySQL
CREATE TABLE users (id INT PRIMARY KEY, name CHAR(10), nickname VARCHAR(10));
INSERT INTO users VALUES (1, 'John', 'John');
SELECT LENGTH(name), LENGTH(nickname) FROM users WHERE id = 1;
ALENGTH(name) = 10, LENGTH(nickname) = 4
BLENGTH(name) = 4, LENGTH(nickname) = 10
CLENGTH(name) = 4, LENGTH(nickname) = 4
DLENGTH(name) = 10, LENGTH(nickname) = 10
Attempts:
2 left
💡 Hint
Remember that CHAR pads the string with spaces to its fixed length, while VARCHAR stores only the actual string length.
🧠 Conceptual
intermediate
2:00remaining
Choosing between TEXT and VARCHAR for large strings
Which of the following is a key difference between TEXT and VARCHAR types in MySQL when storing large strings?
ATEXT columns are stored inline with the table always, VARCHAR is stored off-page.
BVARCHAR columns can store unlimited data, TEXT columns have a strict 255 character limit.
CTEXT columns can store larger amounts of data but cannot have default values, while VARCHAR has size limits but can have defaults.
DVARCHAR columns cannot be indexed, TEXT columns can be indexed fully.
Attempts:
2 left
💡 Hint
Think about size limits and default value support.
📝 Syntax
advanced
2:00remaining
Valid syntax for defining a VARCHAR column
Which of the following MySQL column definitions for a VARCHAR type is syntactically correct?
Ausername VARCHAR(256) NOT NULL DEFAULT 'guest'
Busername VARCHAR NOT NULL DEFAULT 'guest'
Cusername VARCHAR(0) NOT NULL DEFAULT 'guest'
Dusername VARCHAR(65536) NOT NULL DEFAULT 'guest'
Attempts:
2 left
💡 Hint
Check the required length specification and valid length range for VARCHAR.
❓ optimization
advanced
2:00remaining
Optimizing storage for fixed-length strings
You have a column that stores US state abbreviations, always exactly 2 characters. Which data type choice is most storage-efficient and why?
ATEXT because it can store any length and is flexible.
BCHAR(2) because it stores fixed-length strings without overhead.
CVARCHAR(2) because it saves space by storing only actual length.
DCHAR(10) to allow future longer codes.
Attempts:
2 left
💡 Hint
Consider fixed length and storage overhead.
🔧 Debug
expert
2:00remaining
Why does this TEXT column default cause an error?
Given the table creation statement:
CREATE TABLE articles (id INT PRIMARY KEY, content TEXT NOT NULL DEFAULT '');
Why does this statement cause an error in MySQL?
ANOT NULL is not allowed on TEXT columns.
BPRIMARY KEY must be on a VARCHAR or CHAR column, not INT.
CThe empty string '' is not a valid default for any string type.
DTEXT columns cannot have default values, so DEFAULT '' is invalid.
Attempts:
2 left
💡 Hint
Check MySQL rules about default values for TEXT columns.

Practice

(1/5)
1. Which MySQL string type is best for storing names that can vary in length but are usually short?
easy
A. VARCHAR
B. CHAR
C. TEXT
D. BLOB

Solution

  1. Step 1: Understand string types for short variable-length text

    VARCHAR stores variable-length strings efficiently, saving space for shorter values.
  2. Step 2: Compare with other types

    CHAR is fixed-length and wastes space if data varies; TEXT is for large text; BLOB is for binary data.
  3. Final Answer:

    VARCHAR -> Option A
  4. Quick Check:

    Variable-length short text = VARCHAR [OK]
Hint: Use VARCHAR for short, varying length text [OK]
Common Mistakes:
  • Choosing CHAR wastes space for variable length
  • Using TEXT for small strings is inefficient
  • Confusing BLOB with text types
2. Which of the following is the correct syntax to declare a column named description that can hold up to 255 characters using VARCHAR in MySQL?
easy
A. description VARCHAR(255)
B. description CHAR(255)
C. description TEXT(255)
D. description VARCHAR

Solution

  1. Step 1: Recall VARCHAR syntax

    VARCHAR requires a length in parentheses, e.g., VARCHAR(255).
  2. Step 2: Check options for correct syntax

    description VARCHAR(255) uses correct syntax; description TEXT(255) uses wrong type; description CHAR(255) uses fixed-length CHAR; description VARCHAR misses length.
  3. Final Answer:

    description VARCHAR(255) -> Option A
  4. Quick Check:

    VARCHAR needs length in parentheses [OK]
Hint: Always specify length in parentheses for VARCHAR [OK]
Common Mistakes:
  • Omitting length for VARCHAR
  • Adding length to TEXT type
  • Confusing CHAR and VARCHAR syntax
3. Given the table users with a username CHAR(10) column, what will be the result of this query?
INSERT INTO users (username) VALUES ('bob');
SELECT username, LENGTH(username) FROM users WHERE username = 'bob';
medium
A. username = 'bob', LENGTH(username) = 3
B. username = 'bob ', LENGTH(username) = 3
C. username = 'bob', LENGTH(username) = 10
D. username = 'bob ', LENGTH(username) = 10

Solution

  1. Step 1: Understand CHAR behavior on insert

    CHAR(10) stores fixed length strings, padding with spaces to length 10.
  2. Step 2: Check LENGTH function on padded string

    LENGTH counts all characters including spaces, so LENGTH('bob ') = 10.
  3. Final Answer:

    username = 'bob ', LENGTH(username) = 10 -> Option D
  4. Quick Check:

    CHAR pads with spaces, LENGTH counts all chars [OK]
Hint: CHAR pads with spaces; LENGTH counts all characters [OK]
Common Mistakes:
  • Assuming LENGTH returns 3 for 'bob'
  • Ignoring space padding in CHAR
  • Confusing CHAR with VARCHAR behavior
4. You have a column defined as notes TEXT. You try to insert a string longer than 65,535 characters and get an error. What is the likely cause and how to fix it?
medium
A. VARCHAR max length is 65,535; use TEXT instead
B. TEXT type has a max length of 65,535 bytes; use MEDIUMTEXT for longer text
C. CHAR cannot store large text; use VARCHAR(65535)
D. TEXT type has no length limit; error is from syntax

Solution

  1. Step 1: Recall TEXT max size limits

    TEXT can store up to 65,535 bytes; longer text causes error.
  2. Step 2: Choose correct larger text type

    MEDIUMTEXT supports up to 16 million bytes, suitable for longer text.
  3. Final Answer:

    TEXT type has a max length of 65,535 bytes; use MEDIUMTEXT for longer text -> Option B
  4. Quick Check:

    TEXT max 65,535 bytes; MEDIUMTEXT for more [OK]
Hint: TEXT max 65k chars; use MEDIUMTEXT for bigger text [OK]
Common Mistakes:
  • Thinking TEXT has unlimited length
  • Using VARCHAR for very large text
  • Confusing CHAR with TEXT size limits
5. You want to store user comments that can be very long but also want to optimize storage for short comments. Which MySQL string type should you choose and why?
hard
A. Use VARCHAR(500) to save space for short comments and allow long ones
B. Use CHAR(500) to fix length and speed up queries
C. Use TEXT because it stores large text without fixed length
D. Use VARCHAR(65535) to cover all comment lengths

Solution

  1. Step 1: Consider storage needs for very long text

    VARCHAR max length is limited (usually 65,535 bytes total row size), so very long comments may exceed it.
  2. Step 2: Evaluate TEXT type for large variable text

    TEXT stores large text efficiently without fixed length and supports very long strings.
  3. Final Answer:

    Use TEXT because it stores large text without fixed length -> Option C
  4. Quick Check:

    Large variable text = TEXT type [OK]
Hint: Use TEXT for very long text, VARCHAR for shorter variable text [OK]
Common Mistakes:
  • Choosing fixed-length CHAR wastes space
  • Using VARCHAR too large causes errors
  • Assuming VARCHAR can store unlimited text
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard