TRUNCATE()() — SQL Technical Interview Questions & Answers
TRUNCATE()()
TRUNCATE()()
- TRUNCATE() is a MySQL math function: TRUNCATE(x, d) cuts decimals without rounding — not TRUNCATE TABLE. Use it in SELECT, e.g. SELECT TRUNCATE(marks) FROM students;
- These functions do not change stored marks unless you UPDATE. NULL in, NULL out for most of them.
- Trap: mixing TRUNCATE with TRUNCATE TABLE (different word). Trap: maths on VARCHAR without CAST.
- In a viva, one numeric example beats listing twenty functions.
- On the example next to this theory: TRUNCATE()() — dELETE removes matching rows. TRUNCATE clears a whole table faster (MySQL).
Technical Interview Tip: Explain TRUNCATE with one number. TRUNCATE(x, d) cuts decimals without rounding — not TRUNCATE TABLE.
Code Examples for TRUNCATE()()
TRUNCATE()() — sample query
CREATE TABLE tags (id INTEGER PRIMARY KEY, label TEXT);
INSERT INTO tags VALUES (1, 'old'), (2, 'keep');
DELETE FROM tags WHERE label = 'old';
SELECT * FROM tags;TRUNCATE()() — dELETE removes matching rows. TRUNCATE clears a whole table faster (MySQL).
How to Explain TRUNCATE()() to an Interviewer
What is TRUNCATE?
TRUNCATE(x, d) cuts decimals without rounding — not TRUNCATE TABLE Used in SELECT