ποΈ SQL Β· Topic 8 of 20
Data Types β SQL Questions with Answers
30 practice questions on Data Types, each with the correct answer and a clear explanation of why it is right. Read through them as revision notes, or take this topic as an interactive quiz to test yourself.
2 easy Β· 17 medium Β· 11 hard Β· Part of SQL: Databases from basics to window functions - 20 topics.
Medium questions (17)
Q3. medium
What is the difference between CHAR(10) and VARCHAR(10)?
- A CHAR always uses 10 characters (padding with spaces); VARCHAR stores only what you put in
- B They are identical
- C VARCHAR is always faster
- D CHAR holds more
Answer: A β CHAR always uses 10 characters (padding with spaces); VARCHAR stores only what you put in
CHAR suits fixed-length codes like country codes; VARCHAR suits names and emails. CHAR padding can cause surprising trailing spaces in comparisons.
CHARVARCHAR
Q4. medium
What does VARCHAR(50) mean?
- A Variable-length text with a MAXIMUM of 50 characters
- B Exactly 50 characters
- C 50 rows
- D 50 bytes only
Answer: A β Variable-length text with a MAXIMUM of 50 characters
Inserting a longer string either errors or truncates depending on settings. Choose limits that reflect real data - overly tight limits cause production failures.
VARCHARLength
Q6. medium
What does DECIMAL(10, 2) specify?
- A Precision 10 (total digits) and scale 2 (digits after the decimal point)
- B 10 rows, 2 columns
- C A range of 10 to 2
- D 10 bytes with 2 decimals
Answer: A β Precision 10 (total digits) and scale 2 (digits after the decimal point)
So the largest value is 99999999.99. Exceeding the scale rounds; exceeding the precision raises an error.
DECIMALPrecision and scale
Q7. medium
Which type stores a date WITHOUT a time?
- A DATE
- B DATETIME
- C TIMESTAMP
- D TIME
Answer: A β DATE
DATE holds year-month-day. DATETIME/TIMESTAMP add the time of day, and TIME stores only the clock portion.
DATETemporal types
Q9. medium
How do booleans work across databases?
- A PostgreSQL has a real BOOLEAN; MySQL stores it as TINYINT(1) where 1 is true and 0 is false
- B All databases have BOOLEAN
- C Booleans are impossible in SQL
- D Only strings can hold true/false
Answer: A β PostgreSQL has a real BOOLEAN; MySQL stores it as TINYINT(1) where 1 is true and 0 is false
SQL Server uses BIT. This is why some codebases compare with = 1 and others with = TRUE - both are correct for their platform.
BOOLEANDialects
Q10. medium
What is TEXT used for?
- A Long or unbounded strings - articles, descriptions, comments
- B Numbers stored as text
- C Exactly 255 characters
- D Binary files
Answer: A β Long or unbounded strings - articles, descriptions, comments
TEXT has no practical length limit but often cannot be fully indexed and may be stored separately. Use VARCHAR when you have a sensible maximum.
TEXTVARCHAR
Q12. medium
What does AUTO_INCREMENT / SERIAL / IDENTITY do?
- A Automatically assigns the next unique number to a new row - typically for primary keys
- B Counts rows
- C Sorts the table
- D Encrypts the id
Answer: A β Automatically assigns the next unique number to a new row - typically for primary keys
AUTO_INCREMENT in MySQL, SERIAL/GENERATED AS IDENTITY in PostgreSQL, IDENTITY in SQL Server. Gaps in the sequence are normal after failed inserts - do not treat ids as a count.
Primary keysSequences
Q14. medium
How do you convert between types?
SELECT CAST('2026-07-04' AS DATE);
- A CAST(value AS type) - standard SQL
- B CONVERT_TO(value)
- C TYPEOF(value)
- D It happens automatically always
Answer: A β CAST(value AS type) - standard SQL
CAST is portable; CONVERT() exists in SQL Server and MySQL with extra formatting options. PostgreSQL also allows the shorthand value::type.
CASTConversion
Q16. medium
What does the JSON data type allow?
SELECT data->>'email' FROM users WHERE data->>'plan' = 'pro';
- A Storing structured documents in a column, with functions to query inside them
- B Only plain text
- C Nothing special
- D Binary images
Answer: A β Storing structured documents in a column, with functions to query inside them
PostgreSQL's JSONB and MySQL's JSON support indexing and path queries - useful for flexible attributes. Overusing it, though, sacrifices the integrity relational design gives you.
JSONSemi-structured data
Q17. medium
What is the ENUM type?
- A A column restricted to a fixed list of allowed string values
- B A numbering system
- C An index type
- D A join type
Answer: A β A column restricted to a fixed list of allowed string values
ENUM('small','medium','large') enforces valid values. Changing the list later requires altering the table, so many teams prefer a lookup table plus a foreign key.
ENUMConstraints
Q19. medium
What is the difference between NULL and a DEFAULT value?
status VARCHAR(20) NOT NULL DEFAULT 'pending'
- A DEFAULT supplies a value when none is given at insert; NULL means no value is stored
- B They are the same
- C DEFAULT only works for numbers
- D NULL is a kind of default
Answer: A β DEFAULT supplies a value when none is given at insert; NULL means no value is stored
Combining NOT NULL with a DEFAULT guarantees a usable value in every row - often better than allowing NULLs you must handle in every query.
DEFAULTNOT NULL
Q20. medium
Which type would you choose for a percentage like 87.5?
- A DECIMAL(5,2) - exact and bounded
- B INT
- C VARCHAR
- D BOOLEAN
Answer: A β DECIMAL(5,2) - exact and bounded
INT would lose the fraction and text would prevent arithmetic. DECIMAL keeps the value exact and lets you add a CHECK constraint for 0-100.
DECIMALModelling
Q22. medium
What is a BLOB used for?
- A Binary Large Objects - images, PDFs and other binary data
- B Long text only
- C Boolean lists
- D Backups
Answer: A β Binary Large Objects - images, PDFs and other binary data
Storing files in the database is possible but often discouraged: it bloats backups and slows queries. Storing a file PATH or URL is usually better.
BLOBFile storage
Q24. medium
How should you store a phone number?
- A As VARCHAR - to preserve leading zeros, plus signs and formatting
- B As INT
- C As DECIMAL
- D As BOOLEAN
Answer: A β As VARCHAR - to preserve leading zeros, plus signs and formatting
Numeric storage destroys leading zeros and cannot hold '+91' or extensions. The rule of thumb: if you never do arithmetic on it, it is not a number.
ModellingVARCHAR
Q26. medium
How do you change a column's type?
- A ALTER TABLE t ALTER COLUMN c TYPE new_type (syntax varies by database)
- B UPDATE TYPE
- C CHANGE COLUMN TYPE
- D Types cannot be changed
Answer: A β ALTER TABLE t ALTER COLUMN c TYPE new_type (syntax varies by database)
MySQL uses MODIFY COLUMN; PostgreSQL uses ALTER COLUMN ... TYPE. On huge tables this can lock and rewrite the table, so plan such migrations carefully.
ALTER TABLEMigrations
Q28. medium
What is an ARRAY column (PostgreSQL)?
- A A column holding multiple values of the same type in one field
- B A table with many columns
- C A JSON alias
- D Not supported anywhere
Answer: A β A column holding multiple values of the same type in one field
Convenient for simple lists like tags, but it breaks first normal form. A junction table remains the better choice when you need joins, constraints or per-item data.
ArraysNormalization
Q30. medium
Which is the safest way to store money?
- A DECIMAL with an explicit scale - or an INTEGER count of the smallest unit (paise/cents)
- B FLOAT
- C DOUBLE
- D VARCHAR
Answer: A β DECIMAL with an explicit scale - or an INTEGER count of the smallest unit (paise/cents)
Both approaches avoid binary rounding. Storing integer cents is common in payment systems because every arithmetic operation stays exact.
DECIMALMoney
Hard questions (11)
Q5. hard
Why should you use DECIMAL/NUMERIC for money instead of FLOAT?
price DECIMAL(10, 2) -- 8 digits before the point, 2 after
- A FLOAT is binary and cannot represent 0.1 exactly, causing rounding errors in totals
- B DECIMAL is faster
- C FLOAT cannot store decimals
- D There is no difference
Answer: A β FLOAT is binary and cannot represent 0.1 exactly, causing rounding errors in totals
DECIMAL(10,2) stores exact values. With FLOAT, summing thousands of prices drifts by fractions of a cent - unacceptable for financial data.
- A standard interview question about numeric precision.
DECIMALFLOATPrecision
Q8. hard
What is the key difference between TIMESTAMP and DATETIME (MySQL)?
- A TIMESTAMP is timezone-aware and converts to UTC; DATETIME stores the literal value unchanged
- B They are identical
- C DATETIME is smaller
- D TIMESTAMP cannot store dates
Answer: A β TIMESTAMP is timezone-aware and converts to UTC; DATETIME stores the literal value unchanged
TIMESTAMP also has a narrower range (roughly 1970-2038). For global applications, storing UTC timestamps and converting for display is the usual approach.
TIMESTAMPTimezones
Q11. hard
Why store numbers in numeric columns rather than VARCHAR?
- A Text sorts alphabetically ('10' before '9'), blocks arithmetic, and wastes space
- B There is no difference
- C VARCHAR is faster for numbers
- D Numeric types cannot be indexed
Answer: A β Text sorts alphabetically ('10' before '9'), blocks arithmetic, and wastes space
The classic symptom is a report where 100 appears before 20. Note that identifiers like phone numbers and postcodes ARE text - leading zeros matter and you never do arithmetic on them.
Data modellingSorting
Q13. hard
What is a UUID/GUID key, and when is it preferred?
- A A 128-bit globally unique identifier - useful for distributed systems where ids must be generated without a central counter
- B A faster integer
- C An encrypted password
- D A type of index
Answer: A β A 128-bit globally unique identifier - useful for distributed systems where ids must be generated without a central counter
UUIDs avoid collisions across servers and hide row counts, at the cost of more storage and (for random UUIDs) worse index locality than sequential integers.
UUIDDistributed systems
Q15. hard
What is implicit type conversion, and why be careful?
- A The database silently converts types to compare them - which can produce surprises and block index use
- B It never happens
- C It always errors
- D It is always faster
Answer: A β The database silently converts types to compare them - which can produce surprises and block index use
Comparing a VARCHAR column to a number may force converting EVERY row, ignoring the index. Match types explicitly in your queries and schema.
Implicit conversionPerformance
Q18. hard
Why can INT overflow be a real problem?
- A An id column can exhaust its maximum (about 2.1 billion for INT), causing insert failures
- B INT never overflows
- C It only wastes space
- D It corrupts other tables
Answer: A β An id column can exhaust its maximum (about 2.1 billion for INT), causing insert failures
Several large platforms have hit this in production. For high-volume tables (events, logs, messages) choose BIGINT from the start - migrating later is painful.
BIGINTCapacity planning
Q21. hard
Why do string comparisons depend on COLLATION?
- A Collation defines sort order and case/accent sensitivity for text
- B It only affects storage size
- C It has no effect on comparisons
- D It is a type of index
Answer: A β Collation defines sort order and case/accent sensitivity for text
The same query can return different results on two servers with different collations - a subtle portability issue. It also determines whether 'a' = 'A'.
CollationCharacter sets
Q23. hard
What happens when you INSERT a 60-character string into VARCHAR(50)?
- A Standard behaviour is an error; some configurations truncate with a warning instead
- B It always truncates silently
- C The column expands
- D It becomes NULL
Answer: A β Standard behaviour is an error; some configurations truncate with a warning instead
MySQL in strict mode errors, but older/lax modes silently truncated - which quietly corrupted data. Validate input length in the application too.
VARCHARStrict mode
Q25. hard
What is the advantage of choosing the SMALLEST adequate type?
- A Less storage means more rows per page, smaller indexes and faster scans
- B It has no effect
- C It only saves disk
- D It prevents NULLs
Answer: A β Less storage means more rows per page, smaller indexes and faster scans
On large tables the savings compound: narrower rows mean fewer disk reads and better cache use. Balance this against future growth needs.
PerformanceStorage
Q27. hard
What does the INTERVAL type represent?
SELECT order_date + INTERVAL '7 days' AS due_date FROM orders;
- A A span of time (e.g. '3 days'), which can be added to or subtracted from dates
- B A range of numbers
- C An index gap
- D A transaction period
Answer: A β A span of time (e.g. '3 days'), which can be added to or subtracted from dates
PostgreSQL and Oracle support INTERVAL directly; MySQL uses it in expressions like DATE_ADD(d, INTERVAL 3 DAY).
INTERVALDate arithmetic
Q29. hard
Why is choosing types an important DESIGN decision, not a detail?
- A Types enforce correctness at the database level and are costly to change once a table holds millions of rows
- B They only matter for documentation
- C Types can be changed instantly at any scale
- D Applications handle all validation anyway
Answer: A β Types enforce correctness at the database level and are costly to change once a table holds millions of rows
The database is the last line of defence: applications get rewritten, but a well-typed schema keeps rejecting invalid data for years.
DesignData integrity