Bird
Raised Fist0
MySQLquery~20 mins

DROP and TRUNCATE behavior in MySQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
DROP and TRUNCATE Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
Difference in row count after DROP vs TRUNCATE

Consider a table employees with 100 rows. What will be the number of rows in the table after executing the following commands?

1. TRUNCATE TABLE employees;
2. DROP TABLE employees;

Choose the correct pair of row counts after each command.

AAfter TRUNCATE: 0 rows, After DROP: 0 rows
BAfter TRUNCATE: 100 rows, After DROP: 0 rows
CAfter TRUNCATE: 0 rows, After DROP: Table does not exist
DAfter TRUNCATE: Table does not exist, After DROP: 0 rows
Attempts:
2 left
💡 Hint

Think about what happens to the table structure after each command.

🧠 Conceptual
intermediate
2:00remaining
Transaction behavior of DROP vs TRUNCATE

Which statement correctly describes the transaction behavior of DROP TABLE and TRUNCATE TABLE in MySQL?

ANeither <code>DROP</code> nor <code>TRUNCATE</code> are transactional and cannot be rolled back.
B<code>TRUNCATE</code> is transactional and can be rolled back; <code>DROP</code> is not transactional and cannot be rolled back.
CBoth <code>DROP</code> and <code>TRUNCATE</code> are transactional and can be rolled back.
D<code>DROP</code> is transactional and can be rolled back; <code>TRUNCATE</code> is not transactional and cannot be rolled back.
Attempts:
2 left
💡 Hint

Consider if these commands can be undone inside a transaction.

📝 Syntax
advanced
2:00remaining
Correct syntax to remove all rows but keep table

Which of the following SQL commands correctly removes all rows from a table orders but keeps the table structure intact?

ADROP TABLE orders;
BTRUNCATE TABLE orders;
CDELETE FROM orders;
DREMOVE ALL FROM orders;
Attempts:
2 left
💡 Hint

Think about which command clears data but does not delete the table.

❓ optimization
advanced
2:00remaining
Performance difference between DELETE and TRUNCATE

You want to remove all rows from a large table logs. Which command is generally faster and why?

A<code>TRUNCATE TABLE logs;</code> because it deallocates data pages without logging individual row deletions.
B<code>DELETE FROM logs;</code> because it logs each row deletion for rollback.
C<code>DROP TABLE logs;</code> because it removes the table and data instantly.
D<code>DELETE * FROM logs;</code> because it deletes all rows with a wildcard.
Attempts:
2 left
💡 Hint

Consider how each command handles logging and data removal.

🔧 Debug
expert
2:00remaining
Error when truncating a table with foreign key constraints

You try to run TRUNCATE TABLE orders; but get an error about foreign key constraints. What is the cause?

ATRUNCATE requires the table to be empty before running.
BTRUNCATE cannot be used on tables with any indexes.
CThe table <code>orders</code> does not exist.
DThe table <code>orders</code> has foreign key constraints referencing it, so TRUNCATE is blocked.
Attempts:
2 left
💡 Hint

Think about how foreign keys affect data removal commands.

Practice

(1/5)
1. What is the main difference between DROP and TRUNCATE commands in MySQL?
easy
A. DROP only deletes data, TRUNCATE deletes the table structure.
B. TRUNCATE deletes the entire table and its data, DROP deletes only the data but keeps the table.
C. DROP deletes the entire table and its data, TRUNCATE deletes only the data but keeps the table.
D. DROP and TRUNCATE do exactly the same thing.

Solution

  1. Step 1: Understand DROP behavior

    DROP removes the entire table including its structure and data permanently.
  2. Step 2: Understand TRUNCATE behavior

    TRUNCATE removes all rows but keeps the table structure for future use.
  3. Final Answer:

    DROP deletes the entire table and its data, TRUNCATE deletes only the data but keeps the table. -> Option C
  4. Quick Check:

    DROP removes table, TRUNCATE empties table [OK]
Hint: DROP removes table, TRUNCATE empties data only [OK]
Common Mistakes:
  • Confusing which command deletes the table structure
  • Thinking TRUNCATE deletes the table itself
  • Assuming DROP only deletes data
2. Which of the following is the correct syntax to remove all rows from a table named employees without deleting the table itself?
easy
A. DROP TABLE employees;
B. TRUNCATE TABLE employees;
C. DELETE TABLE employees;
D. REMOVE TABLE employees;

Solution

  1. Step 1: Identify command to remove all rows but keep table

    TRUNCATE TABLE removes all rows quickly without deleting the table.
  2. Step 2: Check syntax correctness

    TRUNCATE TABLE employees; is the correct syntax. DROP TABLE deletes the table, DELETE TABLE and REMOVE TABLE are invalid.
  3. Final Answer:

    TRUNCATE TABLE employees; -> Option B
  4. Quick Check:

    TRUNCATE syntax correct for emptying table [OK]
Hint: Use TRUNCATE TABLE to empty without deleting [OK]
Common Mistakes:
  • Using DROP instead of TRUNCATE to empty table
  • Using DELETE TABLE which is invalid syntax
  • Confusing REMOVE TABLE as a valid command
3. Given a table products with 1000 rows, what will be the result after running TRUNCATE TABLE products;?
medium
A. All rows are removed but the table structure remains.
B. The table products is deleted completely.
C. Only half of the rows are deleted.
D. Syntax error occurs.

Solution

  1. Step 1: Understand effect of TRUNCATE on data

    TRUNCATE TABLE removes all rows from the table quickly.
  2. Step 2: Understand effect on table structure

    The table structure remains intact and ready for new data.
  3. Final Answer:

    All rows are removed but the table structure remains. -> Option A
  4. Quick Check:

    TRUNCATE empties table, keeps structure [OK]
Hint: TRUNCATE clears data, keeps table ready [OK]
Common Mistakes:
  • Thinking TRUNCATE deletes the table itself
  • Assuming partial row deletion
  • Expecting syntax error
4. You want to remove a table named orders completely, but accidentally run TRUNCATE orders;. What is the problem with this command?
medium
A. It will cause a syntax error because TABLE keyword is missing.
B. It will delete the table and all its data.
C. It will only delete some rows, not all.
D. It will rename the table instead of deleting.

Solution

  1. Step 1: Check syntax of TRUNCATE command

    The correct syntax requires the keyword TABLE: TRUNCATE TABLE orders;.
  2. Step 2: Identify error from missing keyword

    Omitting TABLE causes a syntax error in MySQL.
  3. Final Answer:

    It will cause a syntax error because TABLE keyword is missing. -> Option A
  4. Quick Check:

    TRUNCATE requires TABLE keyword [OK]
Hint: Always include TABLE keyword in TRUNCATE [OK]
Common Mistakes:
  • Omitting TABLE keyword in TRUNCATE
  • Expecting TRUNCATE to delete table structure
  • Confusing TRUNCATE with DROP syntax
5. You have a table customers with foreign key constraints referencing it from other tables. Which command should you use to quickly remove all data from customers without dropping the table and without breaking foreign key constraints?
hard
A. ALTER TABLE customers DROP;
B. TRUNCATE TABLE customers;
C. DROP TABLE customers;
D. DELETE FROM customers;

Solution

  1. Step 1: Understand foreign key constraints effect

    TRUNCATE TABLE cannot be used if foreign keys reference the table; it will cause an error.
  2. Step 2: Choose command that removes data safely

    DELETE FROM customers; removes all rows and respects foreign key constraints.
  3. Step 3: Confirm other options

    DROP TABLE deletes the table, breaking references; ALTER TABLE DROP is invalid syntax.
  4. Final Answer:

    DELETE FROM customers; -> Option D
  5. Quick Check:

    DELETE respects foreign keys, TRUNCATE does not [OK]
Hint: Use DELETE when foreign keys exist, not TRUNCATE [OK]
Common Mistakes:
  • Using TRUNCATE despite foreign key constraints
  • Using DROP which deletes the table
  • Trying invalid ALTER TABLE DROP syntax
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