πŸŽ“ Velnov

πŸ—„οΈ 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.

Key concepts in this topic

Data typesIntegrityINTNumeric typesCHARVARCHARLengthDECIMALFLOATPrecisionPrecision and scaleDATETemporal typesTIMESTAMPTimezonesBOOLEANDialectsTEXTData modellingSortingPrimary keysSequencesUUIDDistributed systems

Jump to: Easy Β· Medium Β· Hard

Easy questions (2)

Q1. easy

Why does every column need a data type?

  1. A It enforces valid values, determines storage size, and defines how comparisons and sorting behave
  2. B It is only documentation
  3. C To make queries shorter
  4. D It is optional

Answer: A β€” It enforces valid values, determines storage size, and defines how comparisons and sorting behave

A DATE column rejects 'hello' and sorts chronologically; a VARCHAR column would accept anything and sort alphabetically. Types are your first line of data quality.

Data typesIntegrity

Q2. easy

Which type stores whole numbers?

  1. A INTEGER (INT)
  2. B VARCHAR
  3. C DECIMAL only
  4. D TEXT

Answer: A β€” INTEGER (INT)

INT typically holds about +/- 2.1 billion. Use BIGINT for larger values and SMALLINT/TINYINT where space matters.

INTNumeric types

Medium questions (17)

Q3. medium

What is the difference between CHAR(10) and VARCHAR(10)?

  1. A CHAR always uses 10 characters (padding with spaces); VARCHAR stores only what you put in
  2. B They are identical
  3. C VARCHAR is always faster
  4. 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?

  1. A Variable-length text with a MAXIMUM of 50 characters
  2. B Exactly 50 characters
  3. C 50 rows
  4. 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?

  1. A Precision 10 (total digits) and scale 2 (digits after the decimal point)
  2. B 10 rows, 2 columns
  3. C A range of 10 to 2
  4. 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?

  1. A DATE
  2. B DATETIME
  3. C TIMESTAMP
  4. 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?

  1. A PostgreSQL has a real BOOLEAN; MySQL stores it as TINYINT(1) where 1 is true and 0 is false
  2. B All databases have BOOLEAN
  3. C Booleans are impossible in SQL
  4. 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?

  1. A Long or unbounded strings - articles, descriptions, comments
  2. B Numbers stored as text
  3. C Exactly 255 characters
  4. 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?

  1. A Automatically assigns the next unique number to a new row - typically for primary keys
  2. B Counts rows
  3. C Sorts the table
  4. 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);
  1. A CAST(value AS type) - standard SQL
  2. B CONVERT_TO(value)
  3. C TYPEOF(value)
  4. 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';
  1. A Storing structured documents in a column, with functions to query inside them
  2. B Only plain text
  3. C Nothing special
  4. 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?

  1. A A column restricted to a fixed list of allowed string values
  2. B A numbering system
  3. C An index type
  4. 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'
  1. A DEFAULT supplies a value when none is given at insert; NULL means no value is stored
  2. B They are the same
  3. C DEFAULT only works for numbers
  4. 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?

  1. A DECIMAL(5,2) - exact and bounded
  2. B INT
  3. C VARCHAR
  4. 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?

  1. A Binary Large Objects - images, PDFs and other binary data
  2. B Long text only
  3. C Boolean lists
  4. 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?

  1. A As VARCHAR - to preserve leading zeros, plus signs and formatting
  2. B As INT
  3. C As DECIMAL
  4. 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?

  1. A ALTER TABLE t ALTER COLUMN c TYPE new_type (syntax varies by database)
  2. B UPDATE TYPE
  3. C CHANGE COLUMN TYPE
  4. 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)?

  1. A A column holding multiple values of the same type in one field
  2. B A table with many columns
  3. C A JSON alias
  4. 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?

  1. A DECIMAL with an explicit scale - or an INTEGER count of the smallest unit (paise/cents)
  2. B FLOAT
  3. C DOUBLE
  4. 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
  1. A FLOAT is binary and cannot represent 0.1 exactly, causing rounding errors in totals
  2. B DECIMAL is faster
  3. C FLOAT cannot store decimals
  4. 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)?

  1. A TIMESTAMP is timezone-aware and converts to UTC; DATETIME stores the literal value unchanged
  2. B They are identical
  3. C DATETIME is smaller
  4. 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?

  1. A Text sorts alphabetically ('10' before '9'), blocks arithmetic, and wastes space
  2. B There is no difference
  3. C VARCHAR is faster for numbers
  4. 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?

  1. A A 128-bit globally unique identifier - useful for distributed systems where ids must be generated without a central counter
  2. B A faster integer
  3. C An encrypted password
  4. 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?

  1. A The database silently converts types to compare them - which can produce surprises and block index use
  2. B It never happens
  3. C It always errors
  4. 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?

  1. A An id column can exhaust its maximum (about 2.1 billion for INT), causing insert failures
  2. B INT never overflows
  3. C It only wastes space
  4. 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?

  1. A Collation defines sort order and case/accent sensitivity for text
  2. B It only affects storage size
  3. C It has no effect on comparisons
  4. 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)?

  1. A Standard behaviour is an error; some configurations truncate with a warning instead
  2. B It always truncates silently
  3. C The column expands
  4. 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?

  1. A Less storage means more rows per page, smaller indexes and faster scans
  2. B It has no effect
  3. C It only saves disk
  4. 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;
  1. A A span of time (e.g. '3 days'), which can be added to or subtracted from dates
  2. B A range of numbers
  3. C An index gap
  4. 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?

  1. A Types enforce correctness at the database level and are costly to change once a table holds millions of rows
  2. B They only matter for documentation
  3. C Types can be changed instantly at any scale
  4. 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

Practice β€œData Types” as a quiz