DDL & DML Questions with Answers

8 practice questions on DDL & DML (SQL) with answers and step-by-step solutions. Try 8 samples below, then practise all 8 free in the AptiNest app.

Sample questions

Question 1Easy

What does INSERT INTO archive (id, name) SELECT id, name FROM employees WHERE dept = 'HR'; do?

  1. Copies the HR rows into archive; employees is unchanged
  2. Moves the HR rows from employees into archive
  3. Creates a new table named archive with the HR rows
  4. Fails, because INSERT cannot take a SELECT as its source

Answer: A. Copies the HR rows into archive; employees is unchanged

Explanation

INSERT ... SELECT inserts the result of the query into an existing table. The source rows are not deleted.

Step-by-step solution

  1. INSERT ... SELECT runs the query.
  2. Each result row is inserted into archive.
  3. Nothing is deleted from employees.

Why the other options are wrong

  • B. Moving needs a DELETE as well.
  • C. CREATE TABLE AS creates a table; this requires it to exist.
  • D. INSERT can take a SELECT.

Question 2Easy

Which command removes a table together with its structure?

  1. TRUNCATE TABLE employees;
  2. DROP TABLE employees;
  3. DELETE FROM employees;
  4. REMOVE TABLE employees;

Answer: B. DROP TABLE employees;

Explanation

DROP removes the table definition and its data. DELETE and TRUNCATE remove rows but keep the table.

Step-by-step solution

  1. DROP TABLE removes the table, its data and its structure.
  2. TRUNCATE and DELETE remove rows but keep the table.
  3. So DROP TABLE employees is correct (option B).

Why the other options are wrong

  • A. TRUNCATE empties the table but keeps its structure.
  • C. DELETE removes rows but keeps the table.
  • D. REMOVE TABLE is not a standard SQL command.

Tip: DROP removes the structure; TRUNCATE and DELETE only remove rows.

Question 3Easy

What does UPDATE employees SET salary = 0; do?

  1. Sets salary to 0 in the first row only
  2. Sets salary to 0 in every row
  3. Raises a syntax error
  4. Nothing, because WHERE is missing

Answer: B. Sets salary to 0 in every row

Explanation

Without a WHERE clause, UPDATE applies to all rows of the table.

Step-by-step solution

  1. UPDATE without a WHERE applies to every row in the table.
  2. So every employee's salary becomes 0.
  3. That is option B.

Why the other options are wrong

  • A. No WHERE means every row is updated, not only the first.
  • C. The statement is valid SQL and runs.
  • D. A missing WHERE is not a rule that stops the update; it updates all rows.

Tip: Always check the WHERE clause before running UPDATE or DELETE.

Question 4Easy

Which of these is a DDL (Data Definition Language) command?

  1. UPDATE
  2. SELECT
  3. INSERT
  4. ALTER

Answer: D. ALTER

Explanation

DDL commands define or change structure: CREATE, ALTER, DROP, TRUNCATE. SELECT, INSERT, UPDATE and DELETE work on the data (DML).

Step-by-step solution

  1. DDL defines or changes structure: CREATE, ALTER, DROP, TRUNCATE.
  2. ALTER changes a table's structure.
  3. So ALTER is the DDL command (option D).

Why the other options are wrong

  • A. UPDATE changes row data, so it is DML.
  • B. SELECT reads data, so it is DML or query language, not DDL.
  • C. INSERT adds row data, so it is DML.

Tip: DDL changes structure; DML changes rows.

Question 5Easy

Which statement adds a new column email to employees?

  1. ALTER TABLE employees ADD COLUMN email VARCHAR(100);
  2. UPDATE employees SET COLUMN email VARCHAR(100);
  3. MODIFY TABLE employees ADD email VARCHAR(100);
  4. INSERT COLUMN email VARCHAR(100) INTO employees;

Answer: A. ALTER TABLE employees ADD COLUMN email VARCHAR(100);

Explanation

Changing a table's structure is done with ALTER TABLE ... ADD COLUMN.

Step-by-step solution

  1. Adding a column to a table uses ALTER TABLE ... ADD COLUMN.
  2. The statement names the table and the new column with its type.
  3. So option A is correct.

Why the other options are wrong

  • B. UPDATE changes row values; it does not add a column.
  • C. MODIFY TABLE is not standard SQL for adding a column.
  • D. INSERT adds rows, not columns.

Tip: ALTER TABLE changes structure; UPDATE and INSERT change data.

Question 6Medium

What does the final SELECT return?

employees
id | name  | dept  | salary | manager_id
1  | Asha  | Sales | 60000  | NULL
2  | Ravi  | Sales | 45000  | 1
3  | Meera | HR    | 50000  | 1
4  | John  | IT    | 70000  | NULL
5  | Kiran | IT    | 45000  | 4
BEGIN;
DELETE FROM employees;
ROLLBACK;
SELECT COUNT(*) FROM employees;
  1. 0
  2. NULL
  3. 5
  4. An error

Answer: C. 5

Explanation

ROLLBACK undoes every change made since BEGIN, so all five rows are back.

Step-by-step solution

  1. BEGIN starts a transaction; DELETE removes all five rows inside it.
  2. ROLLBACK undoes everything since BEGIN, so the rows come back.
  3. COUNT(*) is 5 (option C).

Why the other options are wrong

  • A. ROLLBACK restores the rows, so the count is not 0.
  • B. COUNT(*) never returns NULL; it returns a number.
  • D. The statements are valid, so no error occurs.

Tip: Use ROLLBACK to undo a DELETE inside an open transaction.

Question 7Medium

What does the final SELECT return?

employees
id | name  | dept  | salary | manager_id
1  | Asha  | Sales | 60000  | NULL
2  | Ravi  | Sales | 45000  | 1
3  | Meera | HR    | 50000  | 1
4  | John  | IT    | 70000  | NULL
5  | Kiran | IT    | 45000  | 4
UPDATE employees SET salary = salary + 5000 WHERE dept = 'IT';
SELECT SUM(salary) FROM employees;
  1. 280000
  2. 270000
  3. 290000
  4. 275000

Answer: A. 280000

Explanation

The original total is 270000. Two IT employees each get 5000 more, so the total becomes 280000.

Step-by-step solution

  1. The original total is 60000 + 45000 + 50000 + 70000 + 45000 = 270000.
  2. The IT rows are John (70000 + 5000 = 75000) and Kiran (45000 + 5000 = 50000), an increase of 10000.
  3. The new total is 270000 + 10000 = 280000 (option A).

Why the other options are wrong

  • B. 270000 is the total before the update; IT salaries did rise.
  • C. 290000 adds 20000, but only 10000 was added in total.
  • D. 275000 adds only 5000, but each of the two IT rows gets 5000.

Tip: A WHERE-limited UPDATE changes only the matching rows; the total changes by the number of rows times the increase.

Question 8Hard

What does the final SELECT return?

employees
id | name  | dept  | salary | manager_id
1  | Asha  | Sales | 60000  | NULL
2  | Ravi  | Sales | 45000  | 1
3  | Meera | HR    | 50000  | 1
4  | John  | IT    | 70000  | NULL
5  | Kiran | IT    | 45000  | 4
DELETE FROM employees
WHERE salary < (SELECT AVG(salary) FROM employees);
SELECT COUNT(*) FROM employees;
  1. 2
  2. 4
  3. 5
  4. 3

Answer: A. 2

Explanation

The average (54000) is computed over the table before any deletions. Ravi, Meera and Kiran earn less and are deleted, leaving Asha and John.

Step-by-step solution

  1. The average salary is 54000, computed over the full table.
  2. Rows with salary below 54000: Ravi 45000, Meera 50000, Kiran 45000 are deleted.
  3. Asha and John remain, so the count is 2 (option A).

Why the other options are wrong

  • B. 4 would mean only one employee was deleted, but three earn less than 54000.
  • C. 5 would mean nothing was deleted, but three earn less than the average.
  • D. 3 is the number of rows deleted, not the number left.

Tip: A subquery in DELETE's WHERE sees the table as it was before the delete.

Practise all 8 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all SQL topics · All topics