DDL and DML
The statements that define and change data: CREATE, ALTER and DROP TABLE with types and constraints, identity generation, INSERT/UPDATE/DELETE/TRUNCATE/MERGE, and BEGIN, COMMIT, ROLLBACK and savepoints. Interviewers ask because constraints are where correctness is actually enforced, and DELETE versus TRUNCATE inside a transaction is a standard check.
part ofSQLoverview, primer and where to startread it →on this pageshowhide
explore
- Schema Definition (DDL)27 questions
- CREATE TABLE5 questions
- ALTER and DROP TABLE6 questions
- ANSI Data Types and Choosing Them6 questions
- Identity, Sequences and Generated Columns5 questions
- Declaring Constraints5 questions
- Data Modification (DML)30 questions
- INSERT5 questions
- UPDATE and Correlated Updates5 questions
- DELETE5 questions
- TRUNCATE vs DELETE5 questions
- MERGE and Upsert5 questions
- RETURNING-Style Output5 questions
- Transaction Statements9 questions
- BEGIN, COMMIT and ROLLBACK5 questions
- SAVEPOINT and Partial Rollback4 questions
- AI & Data Scientistrole
- AI Engineerrole
- BI Analystrole
- Backend Developerrole
- Cyber Security Expertrole
- Data Analystrole
- Data Engineerrole
- Full Stack Developerrole
- Java Backend Developerrole
- Java SDETrole
- Kotlin Backend Developerrole
- MLOps Engineerrole
- Machine Learning Engineerrole
- PostgreSQL DBArole
- QA Engineerrole
- SQLskill
questions
66 · 3 sectionsWhat value do existing rows get when you run ALTER TABLE ... ADD COLUMN on a populated table?
basics
~20 sExisting rows get NULL unless the new column declares a DEFAULT, in which case every existing row is populated with that default value. Adding a NOT NULL column with no DEFAULT to a table that already holds rows is rejected.
In CREATE TABLE, when must a constraint be written at table level instead of inline on a column?
basics
~20 sAny rule covering more than one column — a composite PRIMARY KEY, UNIQUE or FOREIGN KEY, or a CHECK comparing two columns — must be written as a table constraint after the column list. Single-column rules may be written either way.
What does DEFAULT CURRENT_TIMESTAMP in a CREATE TABLE column definition mean, and when is it evaluated?
basics
~20 sDEFAULT names the value the engine stores when an INSERT supplies none for that column. The expression is evaluated at insert time, so each row records its own insertion moment rather than one value frozen when the table was created.
Why is FLOAT the wrong type for a money column, and what should you use instead?
basics
~20 sFLOAT and REAL store binary approximations, so a value such as 0.10 is never held exactly and the error accumulates across sums and multiplications. Money needs an exact type: DECIMAL/NUMERIC with a declared precision and scale.
How do you declare an auto-numbered surrogate key column in standard SQL?
basics
~20 sStandard SQL uses an identity column: id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY. The engine supplies each value from an attached sequence generator, whose START WITH, INCREMENT BY, CYCLE and MINVALUE/MAXVALUE options you set in parentheses.
What does DELETE FROM orders WHERE status = 'DRAFT' remove, and what happens if you omit WHERE?
basics
~10 sIt removes every whole row of orders whose status is 'DRAFT'; the table and all other rows stay. With no WHERE clause, DELETE FROM orders removes every row in the table.
Why should an INSERT statement name its target columns instead of relying on column order?
basics
~20 sAn INSERT without a column list binds values to columns by position, so adding, dropping or reordering a column silently shifts every value. Naming the columns pins each value to its column and lets omitted columns take their declared defaults.
How does MERGE INTO ... USING ... ON perform an upsert in a single statement?
basics
~20 sMERGE joins a target table to a source rowset through an ON condition, then applies one action per source row: WHEN MATCHED THEN UPDATE for keys that already exist in the target, WHEN NOT MATCHED THEN INSERT for the rest.
What does adding RETURNING order_id to an INSERT statement produce?
basics
~10 sRETURNING makes the INSERT also yield a result set: one row per inserted row, containing the listed columns with their final stored values — generated identity keys, applied defaults and computed columns included.
What is the difference between TRUNCATE TABLE and DELETE FROM with no WHERE clause?
basics
~20 sTRUNCATE TABLE empties a table in one bulk operation: no WHERE clause, no per-row DELETE triggers, usually an identity reset, and far less logging. DELETE removes rows one at a time as ordinary DML and can be filtered.
What do START TRANSACTION, COMMIT and ROLLBACK do, and what happens without them?
basics
~20 sSTART TRANSACTION (or BEGIN) opens an explicit transaction; COMMIT ends it and makes all its work permanent; ROLLBACK ends it and discards all its work. Without them, autocommit wraps each statement in its own transaction that commits immediately.
Does ROLLBACK TO SAVEPOINT end the transaction, and what happens to earlier work?
basics
~10 sROLLBACK TO SAVEPOINT undoes only the statements executed after that savepoint. The transaction stays open, everything done before the savepoint is still pending, and you must still issue COMMIT or ROLLBACK to finish.
Does CREATE TABLE inside an open transaction commit the work already done by that transaction?
basics
~20 sIt depends on the engine. MySQL and Oracle commit implicitly before and after DDL, so a later ROLLBACK undoes nothing that came earlier. PostgreSQL, SQL Server and SQLite treat DDL as transactional and roll it back with everything else.
What does SET TRANSACTION configure, and when must you issue it?
basics
~20 sSET TRANSACTION sets a transaction's characteristics — its isolation level and its access mode, READ ONLY or READ WRITE — not any data. It must be issued before the transaction executes its first query or data-modifying statement, or the engine rejects it.
After ROLLBACK TO SAVEPOINT s1, which savepoints in the transaction are still usable?
basics
~10 ss1 itself stays established and can be rolled back to again. Every savepoint created after s1 is destroyed by the rewind, and naming one afterwards raises an invalid-savepoint error rather than doing nothing.