Basics of DML - Data Manipulation Language
Think CRUD: create, read, update, delete
SQL clauses operate in layers: Target → Sources → Filters.
(Some clauses shape data, others filter it.)
- The target is what the statement acts on
- FROM / JOIN define the data sources
- WHERE filters rows
- GROUP BY defines aggregation boundaries
- HAVING filters groups
The target is defined by the main statement keyword
| Statement | Target |
|---|---|
| SELECT | The result set being produced |
| INSERT INTO dbo.Table | The table receiving rows |
| UPDATE dbo.Table | The table being modified |
| DELETE FROM dbo.Table | The table rows being removed |
Statement Written Order:
- SELECT: SELECT → FROM → JOIN → WHERE → GROUP BY → HAVING → ORDER BY
- UPDATE: UPDATE → SET → FROM → WHERE
- INSERT: INSERT INTO → VALUES / SELECT
- DELETE: DELETE → FROM → WHERE
Written order is NOT execution order. SQL is declarative, not procedural. (You describe what you want, not how to get it.)
Basic SELECT Pattern
| |
SELECT with JOIN pattern
| |
SELECT with GROUP BY / HAVING
| |
INSERT with SELECT
Data comes from another query
| |
Note: Each row returned by the SELECT is inserted into dbo.ArchiveTable.
INSERT with values
You already know the data
| |
Helpful Notes
Before writing an UPDATE or DELETE, first write a SELECT using the same WHERE clause. Verify the affected rows, then convert it to UPDATE or DELETE.
We omit the keyword “AS” for UPDATE and DELETE statements. In SQL Server, UPDATE and DELETE don’t treat the table after the keyword as a data source. They treat it as a target object. And targets cannot use AS.
UPDATE with JOIN pattern
| |
DELETE with JOIN pattern
| |