DDL, DML & Constraints Questions with Answers

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

Sample questions

Question 1Easy

Which command removes a table and its definition from the database?

  1. DELETE TABLE
  2. TRUNCATE TABLE
  3. REMOVE TABLE
  4. DROP TABLE

Answer: D. DROP TABLE

Explanation

DROP removes the object itself. DELETE and TRUNCATE remove rows but keep the table.

Step-by-step solution

  1. The command must remove the table object and its definition.
  2. DROP TABLE removes the table structure entirely.
  3. So the answer is option D, DROP TABLE.

Why the other options are wrong

  • A. DELETE TABLE is not valid SQL; DELETE removes rows.
  • B. TRUNCATE TABLE removes rows but keeps the table.
  • C. REMOVE TABLE is not a standard SQL command.

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

Question 2Easy

Which of these is a DML command?

  1. TRUNCATE
  2. ALTER
  3. UPDATE
  4. GRANT

Answer: C. UPDATE

Explanation

DML (data manipulation) covers INSERT, UPDATE, DELETE and MERGE. ALTER and TRUNCATE are DDL, GRANT is DCL.

Step-by-step solution

  1. DML is data manipulation: INSERT, UPDATE, DELETE and MERGE.
  2. UPDATE changes row data, so it is DML.
  3. So the answer is option C, UPDATE.

Why the other options are wrong

  • A. TRUNCATE is a DDL command because it empties the table as a structure-level operation.
  • B. ALTER changes table structure, so it is DDL.
  • D. GRANT controls access, so it is DCL.

Tip: DML changes rows (INSERT, UPDATE, DELETE); DDL changes structure; DCL controls access.

Question 3Easy

Which statement inserts two rows in one command?

  1. INSERT INTO flights (code, src) VALUES ('F5', 'GOI'), ('F6', 'PNQ');
  2. INSERT ROWS flights ('F5', 'GOI'), ('F6', 'PNQ');
  3. INSERT INTO flights (code, src) VALUES ('F5', 'GOI') AND ('F6', 'PNQ');
  4. INSERT INTO flights VALUES ('F5', 'GOI'); ('F6', 'PNQ');

Answer: A. INSERT INTO flights (code, src) VALUES ('F5', 'GOI'), ('F6', 'PNQ');

Explanation

A multi-row VALUES list separates row constructors with commas.

Step-by-step solution

  1. Multi-row insert uses VALUES with comma-separated row lists.
  2. Option A follows that pattern.

Why the other options are wrong

  • B. INSERT ROWS is not valid SQL.
  • C. AND does not combine rows.
  • D. The second tuple after a semicolon is invalid.

Tip: Rows separated by commas.

Question 4Easy

Which statement adds a NOT NULL column with a default value to an existing table?

  1. CREATE COLUMN status ON flights DEFAULT 'ON TIME';
  2. ALTER TABLE flights ADD COLUMN status TEXT NOT NULL DEFAULT 'ON TIME';
  3. UPDATE TABLE flights ADD status TEXT DEFAULT 'ON TIME';
  4. MODIFY flights INSERT COLUMN status TEXT;

Answer: B. ALTER TABLE flights ADD COLUMN status TEXT NOT NULL DEFAULT 'ON TIME';

Explanation

Schema changes use ALTER TABLE ... ADD [COLUMN]. The default fills existing rows so NOT NULL is satisfied.

Step-by-step solution

  1. Adding a column uses ALTER TABLE ... ADD COLUMN.
  2. DEFAULT fills existing rows so NOT NULL works.

Why the other options are wrong

  • A. CREATE COLUMN is not SQL.
  • C. UPDATE TABLE is not valid.
  • D. MODIFY ... INSERT COLUMN is not valid.

Tip: ALTER TABLE ADD COLUMN.

Question 5Medium

What does the final SELECT return?

CREATE TABLE u (email TEXT UNIQUE);
INSERT INTO u VALUES ('a@x.in');
INSERT OR IGNORE INTO u VALUES ('a@x.in');
INSERT INTO u VALUES (NULL);
INSERT INTO u VALUES (NULL);
SELECT COUNT(*) FROM u;

(SQLite.)

  1. 3
  2. 1
  3. 2
  4. 4

Answer: A. 3

Explanation

INSERT OR IGNORE skips the duplicate email silently. UNIQUE allows several NULLs in SQLite (NULLs are not equal), so both NULL rows are kept: 1 + 2 = 3.

Step-by-step solution

  1. The first insert adds 'a@x.in'; the OR IGNORE insert is skipped because it duplicates it.
  2. SQLite allows several NULLs in a UNIQUE column, so both NULL rows are kept.
  3. That gives 1 + 2 = 3 rows, option A.

Why the other options are wrong

  • B. The two NULL rows are both kept, so the count is more than 1.
  • C. The two NULL rows are both kept, so the count is 3, not 2.
  • D. The duplicate email is skipped, so there are not 4 rows.

Tip: In SQLite, UNIQUE allows several NULLs because NULL values are not equal to each other.

Question 6Medium

What does the final SELECT return?

CREATE TABLE t (id INTEGER PRIMARY KEY, qty INTEGER DEFAULT 5, note TEXT);
INSERT INTO t (id, note) VALUES (1, 'a');
INSERT INTO t VALUES (2, NULL, 'b');
SELECT SUM(qty), COUNT(qty) FROM t;
  1. 5, 2
  2. 5, 1
  3. 10, 2
  4. NULL, 0

Answer: B. 5, 1

Explanation

The default applies only when the column is omitted. Row 2 explicitly inserts NULL, so it does not get 5.

Step-by-step solution

  1. Row 1 is inserted without qty, so it gets the default 5.
  2. Row 2 explicitly inserts NULL for qty, so it stays NULL and does not get 5.
  3. SUM(qty) is 5 and COUNT(qty) is 1, so the answer is option B.

Why the other options are wrong

  • A. COUNT(qty) skips NULL, so it is 1, not 2.
  • C. Row 2's qty is NULL, so the sum is 5, not 10.
  • D. Row 1 has qty 5, so the sum is not NULL.

Tip: A DEFAULT applies only when the column is left out, not when NULL is given.

Question 7Medium

What does the final SELECT return?

flights
code | src | dst | seats
F1   | BLR | DEL | 180
F2   | DEL | BOM | NULL
F3   | BOM | BLR | 150
F4   | BLR | HYD | 70
UPDATE flights SET seats = seats + 20 WHERE seats < 160;
SELECT code, seats FROM flights ORDER BY code;
  1. F1 200, F2 NULL, F3 170, F4 90
  2. F1 180, F2 NULL, F3 150, F4 90
  3. F1 180, F2 NULL, F3 170, F4 90
  4. F1 180, F2 20, F3 170, F4 90

Answer: C. F1 180, F2 NULL, F3 170, F4 90

Explanation

Only rows where seats < 160 is TRUE change (F3, F4). F2 has NULL seats, so the condition is UNKNOWN and it stays NULL.

Step-by-step solution

  1. UPDATE ... WHERE seats < 160 applies only where the condition is TRUE.
  2. F3 (150) becomes 170, and F4 (70) becomes 90; F1 (180) is unchanged.
  3. F2 has NULL seats, so the condition is UNKNOWN and it stays NULL; the answer is option C.

Why the other options are wrong

  • A. F1 (180) is not below 160, so it stays 180, not 200.
  • B. F3 (150) is below 160, so it changes to 170.
  • D. F2 stays NULL; it is not set to 20.

Tip: A NULL in a WHERE comparison is UNKNOWN, so that row is not updated.

Question 8Medium

How does TRUNCATE TABLE differ from DELETE FROM table without a WHERE clause (in MySQL/Oracle)?

  1. TRUNCATE is DDL that deallocates all rows quickly without firing row-level DELETE triggers, and usually cannot be rolled back
  2. TRUNCATE removes the table structure as well
  3. TRUNCATE can take a WHERE clause
  4. DELETE cannot remove every row

Answer: A. TRUNCATE is DDL that deallocates all rows quickly without firing row-level DELETE triggers, and usually cannot be rolled back

Explanation

TRUNCATE empties the table as a DDL operation (it auto-commits in MySQL and Oracle); DELETE removes rows one by one, can be filtered and rolled back. DROP removes the structure.

Step-by-step solution

  1. TRUNCATE is a DDL operation that empties the table quickly and does not fire row-level DELETE triggers.
  2. In MySQL and Oracle it auto-commits, so it usually cannot be rolled back.
  3. So option A describes the difference correctly.

Why the other options are wrong

  • B. TRUNCATE empties the table; it keeps the table structure, and DROP is what removes it.
  • C. TRUNCATE takes no WHERE clause, so it cannot filter rows.
  • D. DELETE without WHERE can remove every row; it just removes them one by one.

Tip: TRUNCATE empties a whole table fast; DELETE can filter rows and can be rolled back.

Question 9Hard

What does the final SELECT return?

CREATE TABLE stock (item TEXT PRIMARY KEY, qty INTEGER);
INSERT INTO stock VALUES ('pen', 10), ('ink', 4);
INSERT INTO stock VALUES ('pen', 3), ('cap', 2)
  ON CONFLICT(item) DO UPDATE SET qty = qty + excluded.qty;
SELECT item, qty FROM stock ORDER BY item;

(SQLite / PostgreSQL upsert.)

  1. cap 2, ink 4, pen 13
  2. cap 2, ink 4, pen 3
  3. ink 4, pen 10 (the insert fails)
  4. cap 2, ink 4, pen 10

Answer: A. cap 2, ink 4, pen 13

Explanation

ON CONFLICT DO UPDATE turns the duplicate 'pen' insert into an update: excluded.qty is the proposed value 3, so 10 + 3 = 13. 'cap' is inserted normally.

Step-by-step solution

  1. The pen row exists, so the upsert turns the insert into an update: 10 + excluded.qty.
  2. excluded.qty is the proposed value 3, so pen becomes 10 + 3 = 13.
  3. Cap is new and is inserted with 2, and ink stays at 4, so the answer is option A.

Why the other options are wrong

  • B. Pen is updated to 13, not kept at the new value 3.
  • C. The insert does not fail; ON CONFLICT turns it into an update.
  • D. Pen is 13, not 10, because the proposed 3 is added.

Tip: ON CONFLICT DO UPDATE adds excluded values; excluded refers to the row you tried to insert.

Question 10Hard

What happens when the DELETE runs?

CREATE TABLE dept (id INTEGER PRIMARY KEY, name TEXT);
CREATE TABLE emp (id INTEGER PRIMARY KEY, dept_id INTEGER REFERENCES dept(id));
INSERT INTO dept VALUES (10, 'Ops'), (20, 'HR');
INSERT INTO emp VALUES (1, 10);
PRAGMA foreign_keys = ON;
DELETE FROM dept WHERE id = 10;

(SQLite with foreign keys enabled.)

  1. Department 10 is deleted and emp.dept_id becomes NULL
  2. Department 10 and employee 1 are both deleted
  3. Department 10 is deleted and emp keeps the dangling value 10
  4. It fails, because a child row still references department 10

Answer: D. It fails, because a child row still references department 10

Explanation

With no ON DELETE action, the default (NO ACTION/RESTRICT) blocks deleting a parent row that is still referenced.

Step-by-step solution

  1. PRAGMA foreign_keys = ON enforces foreign keys.
  2. Employee 1 still references department 10, and there is no ON DELETE action.
  3. The default NO ACTION blocks the delete, so it fails, option D.

Why the other options are wrong

  • A. Without an ON DELETE SET NULL action, the child value is not changed automatically.
  • B. Employee 1 is not deleted; the parent delete is blocked.
  • C. The dangling value is not kept; the delete is blocked before that.

Tip: With foreign keys on and no ON DELETE action, a referenced parent row cannot be deleted.

Practise all 23 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