DP-900Chapter 36 of 101Objective 2.3

SQL DML and DDL: SELECT, INSERT, CREATE, ALTER

This chapter covers the two fundamental categories of SQL statements: Data Definition Language (DDL) and Data Manipulation Language (DML), focusing on SELECT, INSERT, CREATE, and ALTER. Mastery of these concepts is critical for the DP-900 exam, as approximately 25-30% of questions relate to querying and manipulating relational data. You will learn the syntax, behavior, and best practices for each command, along with how they interact with Azure SQL Database and SQL Server.

25 min read
Intermediate
Updated May 31, 2026

SQL DML and DDL: Building and Organizing a Library

Imagine you are a librarian tasked with creating and managing a library from scratch. You start by designing the floor plan: you decide how many rooms (databases) you need, and within each room, you plan the shelves (tables) and their dimensions (columns and data types). This design phase is like Data Definition Language (DDL): you use commands like CREATE, ALTER, and DROP to define the structure of your database. For example, you might issue a command to add a new shelf with specific compartments for books (columns) and rules about what can be placed in each compartment (data types and constraints). Once the structure is in place, you begin populating the shelves with books. You add new books (INSERT), retrieve books by browsing the shelves (SELECT), update book locations or details (UPDATE), and remove old books (DELETE). These operations on the data itself are Data Manipulation Language (DML). In this analogy, DDL is like building and modifying the library's physical structure, while DML is like the daily tasks of adding, finding, changing, and removing books. Both are essential: without the structure (DDL), you cannot store data (DML); without data (DML), the structure is empty and useless. In SQL Server, DDL statements are auto-committed, meaning changes to the structure are permanent immediately, just like rearranging shelves has lasting effects on the library's layout. DML statements can be rolled back if they are part of a transaction, analogous to temporarily placing books on a cart and deciding later whether to commit them to the shelves.

How It Actually Works

What Are DDL and DML?

SQL (Structured Query Language) is divided into sublanguages based on the type of operation. Data Definition Language (DDL) deals with the structure of database objects — creating, altering, and dropping tables, views, indexes, and schemas. Data Manipulation Language (DML) deals with the data within those objects — inserting, selecting, updating, and deleting rows. The exam focuses on the four core commands: CREATE (DDL), ALTER (DDL), SELECT (DML), and INSERT (DML). Understanding the difference is crucial because DDL commands are auto-committed (they cannot be rolled back in most contexts), while DML commands are transactional (they can be committed or rolled back).

CREATE: Defining New Objects

The CREATE statement is used to create new database objects such as tables, views, indexes, stored procedures, and databases. The most common use is creating a table:

CREATE TABLE Employees (
    EmployeeID INT PRIMARY KEY,
    FirstName NVARCHAR(50) NOT NULL,
    LastName NVARCHAR(50) NOT NULL,
    HireDate DATE DEFAULT GETDATE()
);

Key components: - Column definitions: Each column has a name, data type, and optional constraints (PRIMARY KEY, NOT NULL, UNIQUE, DEFAULT, CHECK). - Data types: Common types include INT, BIGINT, DECIMAL(p,s), NVARCHAR(n), VARCHAR(n), DATE, DATETIME2, BIT. - Constraints: Enforce data integrity. PRIMARY KEY uniquely identifies each row. FOREIGN KEY links to another table. UNIQUE ensures no duplicate values. CHECK validates data against a condition. DEFAULT provides a value when none is specified. - Table options: You can specify filegroup, compression, and partitioning for performance.

In Azure SQL Database, CREATE TABLE works similarly but with some limitations (e.g., no filegroup specification). The exam may ask about the purpose of constraints or which data type to use for a given scenario.

ALTER: Modifying Existing Objects

ALTER is used to change the structure of an existing object without dropping and recreating it. Common operations include adding, modifying, or dropping columns; adding or dropping constraints; and changing table properties.

Examples:

-- Add a column
ALTER TABLE Employees ADD Email NVARCHAR(100) NULL;

-- Modify a column data type
ALTER TABLE Employees ALTER COLUMN Salary DECIMAL(10,2) NOT NULL;

-- Add a constraint
ALTER TABLE Employees ADD CONSTRAINT CK_Age CHECK (Age >= 18);

-- Drop a column
ALTER TABLE Employees DROP COLUMN TempColumn;

Important considerations:

Modifying a column to a different data type may fail if existing data cannot be converted (e.g., NVARCHAR to INT with non-numeric values).

Adding a NOT NULL column to an existing table requires a default value or the column must allow NULLs initially, then be updated.

In Azure SQL Database, some ALTER operations (like changing the service tier) are done via ALTER DATABASE, not ALTER TABLE.

SELECT: Retrieving Data

SELECT is the most used DML command. It retrieves rows from one or more tables, optionally filtered, sorted, and aggregated.

Basic syntax:

SELECT column1, column2, ...
FROM table_name
WHERE condition
ORDER BY column ASC|DESC;

Key clauses: - SELECT list: Specifies columns to return. Use * for all columns (avoid in production). - FROM: Specifies the source table(s). Can include JOINs (INNER, LEFT, RIGHT, FULL). - WHERE: Filters rows based on predicates (e.g., =, <>, >, <, LIKE, IN, BETWEEN). - GROUP BY: Groups rows for aggregation (e.g., COUNT, SUM, AVG, MIN, MAX). - HAVING: Filters groups after aggregation (like WHERE for groups). - ORDER BY: Sorts the result set. - TOP/OFFSET-FETCH: Limits rows (e.g., SELECT TOP 10, or OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY).

Execution order (conceptual): FROM -> WHERE -> GROUP BY -> HAVING -> SELECT -> ORDER BY -> TOP/OFFSET-FETCH. Understanding this helps debug unexpected results.

Example:

SELECT DepartmentID, COUNT(*) AS EmployeeCount
FROM Employees
WHERE HireDate >= '2020-01-01'
GROUP BY DepartmentID
HAVING COUNT(*) > 5
ORDER BY EmployeeCount DESC;

INSERT: Adding New Rows

INSERT adds one or more rows to a table.

Syntax variations:

-- Insert a single row with values for all columns
INSERT INTO Employees VALUES (1, 'John', 'Doe', '2023-01-15');

-- Insert with explicit column list (recommended)
INSERT INTO Employees (EmployeeID, FirstName, LastName, HireDate)
VALUES (2, 'Jane', 'Smith', '2023-02-20');

-- Insert multiple rows
INSERT INTO Employees (EmployeeID, FirstName, LastName, HireDate)
VALUES 
    (3, 'Bob', 'Brown', '2023-03-10'),
    (4, 'Alice', 'Jones', '2023-04-05');

-- Insert from another table
INSERT INTO Employees (EmployeeID, FirstName, LastName, HireDate)
SELECT EmployeeID, FirstName, LastName, HireDate
FROM TempEmployees
WHERE Active = 1;

Key points:

If you omit the column list, you must provide values for all columns in the exact order they were defined.

Identity columns (auto-increment) should not be inserted explicitly unless IDENTITY_INSERT is ON.

Default values are used when a column is omitted from the column list and has a DEFAULT constraint.

INSERT can be used with a SELECT statement to copy data between tables.

Transactions and Auto-Commit Behavior

DDL statements (CREATE, ALTER, DROP) are auto-committed in SQL Server and Azure SQL Database, meaning they cannot be rolled back. DML statements (SELECT, INSERT, UPDATE, DELETE) are transactional — they can be wrapped in BEGIN TRANSACTION, COMMIT, and ROLLBACK. This distinction is often tested.

Example of a transaction:

BEGIN TRANSACTION;
INSERT INTO Employees VALUES (5, 'Test', 'User', GETDATE());
UPDATE Employees SET LastName = 'Updated' WHERE EmployeeID = 5;
COMMIT; -- or ROLLBACK;

If a transaction is rolled back, the INSERT and UPDATE are undone. DDL statements executed inside a transaction are still auto-committed, so they persist even if the transaction is rolled back.

Interacting with Azure SQL Database

In Azure SQL Database, the same syntax applies, but there are some differences:

No support for filegroups or FILESTREAM.

ALTER DATABASE is used for scaling service tiers.

Some DDL operations (like adding a column with a default value) are optimized to be online (minimal locking).

Azure SQL Database uses a different recovery model (simple recovery, point-in-time restore).

The exam expects you to know that the core SQL commands are the same across SQL Server and Azure SQL Database, but some advanced features differ.

Common Exam Scenarios

Creating a table with constraints: You may be asked to identify the correct syntax for adding a PRIMARY KEY or FOREIGN KEY.

Altering a table: Questions may test what happens when you try to change a column data type with incompatible data.

SELECT with filtering: Using WHERE with LIKE, IN, BETWEEN; handling NULLs (IS NULL, IS NOT NULL).

INSERT with identity: Knowing you cannot insert into an identity column unless IDENTITY_INSERT is ON.

Difference between DDL and DML: Identifying which commands are DDL vs DML.

Performance Considerations

**SELECT * is discouraged**: It returns all columns, increasing I/O and network traffic. Always specify needed columns.

Indexes: CREATE INDEX (DDL) improves SELECT performance but slows INSERT/UPDATE/DELETE.

Batch inserts: Use multiple rows in one INSERT statement or BULK INSERT for large data loads.

Table variables vs temp tables: Table variables (DECLARE @table TABLE) are often used for small datasets; temporary tables (#temp) support indexing and are better for larger data.

Security and Permissions

To CREATE a table, you need CREATE TABLE permission in the database.

To ALTER a table, you need ALTER permission on that table.

To SELECT, you need SELECT permission on the table or columns.

To INSERT, you need INSERT permission.

Permissions can be granted via GRANT, DENY, or REVOKE (DDL).

Summary of DDL vs DML

| Feature | DDL | DML | |---------|-----|-----| | Commands | CREATE, ALTER, DROP, TRUNCATE | SELECT, INSERT, UPDATE, DELETE | | Affects | Structure of objects | Data within objects | | Transactional | Auto-committed (cannot rollback) | Transactional (can rollback) | | Common exam topics | Syntax for CREATE TABLE, ALTER TABLE, constraints | SELECT clauses, INSERT variations, filtering |

The exam will test your ability to write correct SQL and understand the implications of each command.

Walk-Through

1

Create the Database Table

Use CREATE TABLE to define a new table with columns and constraints. For example: CREATE TABLE Products (ProductID INT PRIMARY KEY, ProductName NVARCHAR(100) NOT NULL, Price DECIMAL(10,2) CHECK (Price > 0), CategoryID INT FOREIGN KEY REFERENCES Categories(CategoryID)). This step establishes the schema. The table is created in the default filegroup unless specified. In Azure SQL Database, the table is created in the primary filegroup. The command is auto-committed, so the table exists immediately.

2

Alter Table Structure

Use ALTER TABLE to modify the table after creation. For example: ALTER TABLE Products ADD Description NVARCHAR(500) NULL; or ALTER TABLE Products ALTER COLUMN Price DECIMAL(12,2) NOT NULL;. Adding a column with a default value may cause a size-of-data operation if the table is large. Changing a column data type may fail if existing data cannot be converted. In Azure SQL Database, some ALTER operations are optimized to be online.

3

Insert Sample Data

Use INSERT INTO to add rows. For example: INSERT INTO Products (ProductID, ProductName, Price, CategoryID) VALUES (1, 'Widget', 9.99, 1);. If the column list is omitted, values must be in the order of column definitions. Identity columns should not be included unless IDENTITY_INSERT is ON. Use multiple rows in one statement for efficiency: INSERT INTO Products VALUES (2, 'Gadget', 14.99, 1), (3, 'Doohickey', 24.99, 2);.

4

Query Data with SELECT

Use SELECT to retrieve data. Example: SELECT ProductName, Price FROM Products WHERE CategoryID = 1 ORDER BY Price DESC;. The FROM clause identifies the table. WHERE filters rows before grouping. ORDER BY sorts the result. Use TOP or OFFSET-FETCH for pagination. Avoid SELECT * in production. Understand execution order: FROM -> WHERE -> GROUP BY -> HAVING -> SELECT -> ORDER BY -> TOP/OFFSET-FETCH.

5

Verify and Test

Run SELECT queries to verify the inserted data and structural changes. Check constraints by attempting invalid inserts (e.g., negative price). Use system views like INFORMATION_SCHEMA.TABLES and sys.columns to verify schema. In Azure SQL Database, use sys.dm_db_partition_stats for space usage. This step ensures the DDL and DML operations worked as expected.

What This Looks Like on the Job

Enterprise Scenario 1: E-Commerce Product Catalog

A large e-commerce company needs to manage millions of products across multiple categories. They use DDL to create tables like Products, Categories, and Inventory. The Products table has columns: ProductID (INT IDENTITY), ProductName (NVARCHAR(200)), Price (DECIMAL(10,2)), CategoryID (INT FK), and CreatedDate (DATETIME2 DEFAULT GETDATE()). They use ALTER TABLE to add columns like DiscountPercent and IsActive as business requirements evolve. Data is inserted via ETL processes using BULK INSERT or INSERT INTO...SELECT from staging tables. SELECT queries are heavily optimized with indexes (CREATE INDEX) and use WHERE clauses to filter by category, price range, or search terms. Performance considerations: indexing strategy is critical; too many indexes slow down INSERTs. They use partitioning (CREATE PARTITION FUNCTION) to manage large tables. Common misconfigurations: forgetting to add NOT NULL constraints leads to NULL values in critical columns; adding a column with a default value to a large table causes long-running operations and potential blocking.

Enterprise Scenario 2: Healthcare Patient Records

A hospital database stores patient information with strict compliance requirements (HIPAA). They use DDL to create tables with constraints: PatientID (INT PK), SSN (NVARCHAR(11) UNIQUE), FirstName, LastName, DateOfBirth (DATE), and DiagnosisCode (NVARCHAR(10) FK to DiagnosisCodes). ALTER TABLE is used to add columns for new data points (e.g., COVID-19 vaccination status). INSERT operations are performed by front-end applications, often within transactions to ensure data consistency. SELECT queries are used for reporting and analytics, often with JOINs across multiple tables. Security is paramount: permissions are granted using GRANT SELECT, INSERT, etc. Common issues: accidentally dropping a column (DDL) without understanding dependencies; inserting duplicate SSNs due to missing UNIQUE constraint; SELECT queries that return too many rows causing performance degradation.

Enterprise Scenario 3: Financial Transactions

A bank processes millions of transactions daily. They use DDL to create tables with precise data types: TransactionID (BIGINT IDENTITY), AccountID (INT FK), Amount (DECIMAL(18,2)), TransactionDate (DATETIME2), and Status (TINYINT). ALTER TABLE is used to add indexes for faster querying. INSERT operations are done within transactions that include multiple tables (e.g., debit from one account, credit to another). SELECT queries are used for balance inquiries and fraud detection, often with aggregation and filtering by date range. Performance considerations: use of appropriate data types (e.g., DECIMAL for money, not FLOAT); indexing on AccountID and TransactionDate. Common pitfalls: inserting values that violate CHECK constraints (e.g., negative amount); altering a column data type from INT to BIGINT without checking for overflow; SELECT queries that cause table scans due to missing indexes.

How DP-900 Actually Tests This

What DP-900 Tests on This Topic (Objective 2.3)

The exam objective 2.3 states: 'Identify basic DDL and DML statements.' Specifically, you need to be able to:

Identify DDL statements (CREATE, ALTER, DROP, TRUNCATE)

Identify DML statements (SELECT, INSERT, UPDATE, DELETE)

Write basic SELECT statements with WHERE, ORDER BY, and column selection

Write basic INSERT statements

Understand the purpose of constraints (PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK, DEFAULT)

Know the difference between DDL and DML in terms of transaction behavior

Common Wrong Answers and Why Candidates Choose Them

1.

Confusing TRUNCATE with DELETE: TRUNCATE is DDL (removes all rows, cannot be rolled back), DELETE is DML (can be rolled back). Candidates often think both are DML.

2.

**Thinking SELECT * is best practice**: Many candidates choose SELECT * because it's easy, but the exam expects you to know it's inefficient and should be avoided in production.

3.

Misidentifying ALTER as DML: ALTER modifies structure, not data, so it's DDL. Candidates may think it manipulates data.

4.

Forgetting that DDL is auto-committed: Questions may ask which statements can be rolled back. DDL cannot, DML can (within a transaction).

Specific Numbers, Values, and Terms

Data types: INT, BIGINT, DECIMAL(p,s), NVARCHAR(n), VARCHAR(n), DATE, DATETIME2, BIT. Know the difference between VARCHAR and NVARCHAR (NVARCHAR stores Unicode).

Constraints: PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK, DEFAULT. Know that a table can have only one PRIMARY KEY but multiple UNIQUE constraints.

Default values: DEFAULT GETDATE() for date columns, DEFAULT 0 for numeric.

Identity: IDENTITY(1,1) starts at 1 and increments by 1.

ORDER BY: Default is ASC. DESC for descending.

LIKE: Wildcards % (any characters) and _ (single character).

IN: Used for multiple values, e.g., WHERE Color IN ('Red', 'Blue').

BETWEEN: Inclusive, e.g., WHERE Price BETWEEN 10 AND 20.

Edge Cases and Exceptions

NULL handling: NULL is not equal to anything, even NULL. Use IS NULL or IS NOT NULL. In WHERE clauses, NULL comparisons evaluate to unknown, so rows with NULL are excluded unless explicitly handled.

IDENTITY_INSERT: Must be ON to insert explicit values into an identity column. This is a common trick question.

ALTER TABLE with NOT NULL: Adding a NOT NULL column to a table with existing rows requires a default value or the column must be added as NULL, then updated, then altered to NOT NULL.

Azure SQL Database differences: No filegroups, no FILESTREAM, no full-text indexing in basic tiers. ALTER DATABASE is used for scaling.

How to Eliminate Wrong Answers Using the Underlying Mechanism

If a question asks which statement is DDL, look for commands that change structure (CREATE, ALTER, DROP, TRUNCATE). If it changes data, it's DML.

For transaction questions, remember DDL is auto-committed. If the answer includes rolling back a CREATE TABLE, it's wrong.

For SELECT questions, eliminate answers that use * unless the question specifically asks for all columns. Prefer explicit column lists.

For INSERT, ensure the column list matches the value list in number and order. If the column list is omitted, values must match the table definition order.

Key Takeaways

DDL commands (CREATE, ALTER, DROP, TRUNCATE) modify database structure and are auto-committed.

DML commands (SELECT, INSERT, UPDATE, DELETE) modify data and are transactional.

Use explicit column lists in INSERT and SELECT to avoid errors and improve performance.

Constraints (PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK, DEFAULT) enforce data integrity.

NULL is not equal to NULL; use IS NULL or IS NOT NULL in WHERE clauses.

Identity columns require IDENTITY_INSERT ON to insert explicit values.

ALTER TABLE can add, modify, or drop columns; be cautious with NOT NULL columns on existing data.

SELECT execution order: FROM -> WHERE -> GROUP BY -> HAVING -> SELECT -> ORDER BY -> TOP/OFFSET-FETCH.

In Azure SQL Database, DDL syntax is similar to SQL Server but with some limitations (no filegroups).

TRUNCATE resets identity columns and logs only page deallocations; DELETE logs each row removal.

Easy to Mix Up

These come up on the exam all the time. Here's how to tell them apart.

DDL (Data Definition Language)

Commands: CREATE, ALTER, DROP, TRUNCATE

Affects database object structure (tables, indexes, etc.)

Auto-committed; cannot be rolled back

Does not require explicit transaction; single statements are atomic

Requires higher permissions (e.g., CREATE TABLE, ALTER)

DML (Data Manipulation Language)

Commands: SELECT, INSERT, UPDATE, DELETE

Affects data within objects (rows)

Transactional; can be rolled back if wrapped in a transaction

Often used within explicit transactions for consistency

Requires data-level permissions (e.g., SELECT, INSERT)

Watch Out for These

Mistake

TRUNCATE is a DML statement because it removes data.

Correct

TRUNCATE is DDL because it deallocates data pages and cannot be rolled back. DELETE is DML and can be rolled back. TRUNCATE also resets identity columns.

Mistake

SELECT * is always acceptable and efficient.

Correct

SELECT * returns all columns, increasing I/O and network traffic. It also breaks if the table schema changes. Always specify needed columns in production.

Mistake

You can roll back a CREATE TABLE statement if you wrap it in a transaction.

Correct

DDL statements (CREATE, ALTER, DROP) are auto-committed even inside a transaction. They cannot be rolled back. Only DML statements are transactional.

Mistake

NULL is equal to NULL in a WHERE clause.

Correct

NULL is not equal to anything, including NULL. Use IS NULL or IS NOT NULL to check for NULL values. Comparisons like column = NULL always return false.

Mistake

You can insert values into an identity column without any special handling.

Correct

By default, you cannot insert explicit values into an identity column. You must first enable IDENTITY_INSERT ON for the table, then insert, then turn it off.

Do You Actually Know This?

Reveal each answer, then mark whether you got it right. Score 60%+ to unlock the next chapter.

Frequently Asked Questions

What is the difference between DDL and DML in SQL?

DDL (Data Definition Language) includes commands like CREATE, ALTER, DROP, and TRUNCATE that define or modify the structure of database objects. DML (Data Manipulation Language) includes commands like SELECT, INSERT, UPDATE, and DELETE that manipulate the data within those objects. The key difference is that DDL statements are auto-committed and cannot be rolled back, while DML statements can be part of a transaction and rolled back. For the DP-900 exam, you need to identify which category each command belongs to.

Can I roll back a CREATE TABLE statement?

No, you cannot roll back a CREATE TABLE statement. DDL statements are auto-committed in SQL Server and Azure SQL Database, meaning they are permanently applied as soon as they execute. Even if you wrap a CREATE TABLE inside a BEGIN TRANSACTION...ROLLBACK, the table will still be created. Only DML statements (INSERT, UPDATE, DELETE, SELECT) can be rolled back within a transaction. This is a common exam trap.

How do I insert multiple rows in one INSERT statement?

You can insert multiple rows in a single INSERT statement using the VALUES clause with multiple row constructors. Example: INSERT INTO Employees (ID, Name) VALUES (1, 'Alice'), (2, 'Bob'), (3, 'Charlie');. This is more efficient than multiple single-row inserts because it reduces round trips to the database. You can also use INSERT INTO...SELECT to insert rows from another table or query.

What is the purpose of a PRIMARY KEY constraint?

A PRIMARY KEY constraint uniquely identifies each row in a table. It enforces uniqueness and prevents NULL values. A table can have only one PRIMARY KEY, which can be a single column or a composite of multiple columns. The database automatically creates a unique index on the primary key columns to enforce uniqueness and speed up queries. In the exam, you may be asked to identify the correct syntax for defining a PRIMARY KEY.

What happens if I try to ALTER a column to a different data type with incompatible data?

If you try to change a column's data type and existing data cannot be converted (e.g., NVARCHAR to INT with non-numeric values), the ALTER statement will fail with an error. For example, ALTER TABLE Products ALTER COLUMN Price INT will fail if Price contains values like '9.99' because it cannot be converted to an integer. You must ensure data compatibility before altering. In Azure SQL Database, some conversions may succeed with rounding or truncation, but it's risky.

How do I handle NULL values in a WHERE clause?

NULL represents missing or unknown data. In a WHERE clause, you cannot use = NULL or <> NULL because comparisons with NULL evaluate to unknown (not true or false). Instead, use IS NULL or IS NOT NULL. For example: SELECT * FROM Employees WHERE ManagerID IS NULL;. Also be aware that aggregate functions like COUNT(*) count all rows, while COUNT(column) excludes NULLs. This is a common exam point.

What is the difference between TRUNCATE and DELETE?

TRUNCATE is a DDL command that removes all rows from a table by deallocating the data pages. It is faster than DELETE, logs only page deallocations, resets identity columns, and cannot be rolled back. DELETE is a DML command that removes rows one by one, logs each row deletion, does not reset identity columns, and can be rolled back if part of a transaction. TRUNCATE requires higher permissions (ALTER table). On the exam, know that TRUNCATE is DDL and DELETE is DML.

Terms Worth Knowing

Ready to put this to the test?

You've just covered SQL DML and DDL: SELECT, INSERT, CREATE, ALTER — now see how well it sticks with free DP-900 practice questions. Full explanations included, no account needed.

Done with this chapter?