SQL Keywords and Commands: SELECT, DDL, DML and Transactions

SQL keywords form statements that read data, change rows, define database objects, control transactions and manage permissions.

Lesson 35 of 102Beginner10 min read

SQL keywords form statements that read data, change rows, define database objects, control transactions and manage permissions.

A command reference helps you select the right operation before writing its details. Reading customers, changing one customer, and replacing the customers table are very different tasks, even though all can begin with familiar SQL keywords.

This reference adds detail to the main course. You can read it independently: the small database below supplies every table and row used in the examples. Start with the question each query answers, then compare the returned values with the data rather than memorising a line of syntax.

Set up the practice database

Run this setup once in a new practice database, before the examples. It uses four customers and six sales. Ana has two sales, Ben has one, Cara has two, and Dev has none.

Sale 106 has no customer link; sale 104 has an unknown amount. These deliberate test cases expose missing information and unmatched relationships.

SQL
CREATE TABLE customers (
  id INTEGER PRIMARY KEY,
  name VARCHAR(40) NOT NULL,
  city VARCHAR(40),
  manager_id INTEGER
);
CREATE TABLE sales (
  id INTEGER PRIMARY KEY,
  customer_id INTEGER,
  amount DECIMAL(10,2),
  status VARCHAR(20) NOT NULL,
  created_on DATE NOT NULL
);
INSERT INTO customers (id,name,city,manager_id) VALUES
  (1,'Ana','Leeds',NULL),(2,'Ben','Leeds',1),
  (3,'Cara','Porto',1),(4,'Dev',NULL,2);
INSERT INTO sales (id,customer_id,amount,status,created_on) VALUES
  (101,1,10,'paid','2026-01-01'),
  (102,1,30,'pending','2026-01-02'),
  (103,2,20,'paid','2026-01-02'),
  (104,3,NULL,'pending','2026-02-01'),
  (105,3,20,'paid','2026-02-02'),
  (106,NULL,5,'paid','2026-02-03');

An amount of zero would be a known value. The NULL amount here means the value is unknown. A customer with no matching sale differs from a sale whose customer_id is NULL.

The primary keys identify individual records even when amounts or cities repeat. Dates use year-month-day format. The examples specify sorting whenever order matters; insertion order alone does not guarantee display order.

How SQL keywords and commands works

SELECT reads a result set. FROM supplies the inputs, WHERE filters rows, GROUP BY forms groups, HAVING filters those groups, and ORDER BY determines presentation order. JOIN combines related table inputs, while UNION combines compatible query results. These are parts of statements rather than commands to execute separately. DISTINCT removes duplicate selected rows, and CASE is an expression that chooses a value according to conditions.

INSERT adds rows, UPDATE changes selected rows and DELETE removes selected rows. These are commonly called data manipulation language, or DML. UPDATE and DELETE need special attention to their WHERE conditions: without one they can affect every row. INSERT should name the destination columns explicitly so its values do not silently depend on table column order. SELECT is sometimes classified separately as a query language operation; the classification name is less important than understanding its effect.

Commands that define, change and protect data

CREATE, ALTER and DROP define, modify and remove objects such as tables, views and indexes. This family is called data definition language, or DDL. DROP TABLE removes the table itself; DELETE removes rows while leaving the table available. TRUNCATE is another table-clearing operation in several engines, with permission, trigger and transaction behaviour that differs from DELETE. SQLite does not provide a TRUNCATE command. Do not substitute these operations merely because they can all make a table appear empty.

Transaction commands group changes into a unit. COMMIT makes a transaction’s changes durable; ROLLBACK abandons uncommitted changes. The transaction-start syntax differs between engines. GRANT and REVOKE manage permissions in server databases, while SQLite does not have the same server user-and-role privilege system. Reserved keywords also differ by dialect and version. Prefer descriptive unquoted names such as customer_status rather than naming a column order or group and requiring special quoting throughout the application.

MySQL, PostgreSQL, SQL Server and SQLite

The demonstrated SELECT, GROUP BY and COUNT syntax is shared by all four systems. Database creation, transaction-start commands, table alteration and permission commands have substantial differences. SQLite creates a database by opening a file and does not implement CREATE DATABASE, DROP DATABASE or server-style GRANT.

Check the engine-specific syntax and version requirements in PostgreSQL SQL commands.

Practice task and worked solution

Write a read-only summary showing how many sales have a known amount and how many rows exist in total.

Try the task before reading its worked solution. The supplied rows make the expected answer checkable by hand.

SQL
SELECT COUNT(*) AS all_sales,COUNT(amount) AS known_amounts FROM sales;
Output
all_sales | known_amounts
6 | 5

SELECT performs the read. COUNT(*) includes every sale record, while COUNT(amount) includes only present amounts. The query returns one summary row because no GROUP BY is requested. It does not UPDATE missing amounts or alter the amount column.

Identify every keyword in the status-summary example and classify its role: SELECT starts the reading statement, GROUP BY forms groups and ORDER BY controls presentation. Rewrite it with WHERE status='paid' and confirm only the paid summary remains. In a disposable table, compare DELETE and DROP by checking whether a later SELECT can still refer to the table object.

SQL Keywords and Commands: SELECT, DDL, DML and Transactions Syntax

SQL keywords and commands

SELECT status,COUNT(*) AS sale_count
FROM sales
GROUP BY status
ORDER BY status;

SQL Keywords and Commands: SELECT, DDL, DML and Transactions Summary

  • Query keywords such as SELECT, FROM, WHERE, GROUP BY, HAVING and ORDER BY read data without changing it.
  • DML statements, INSERT, UPDATE, DELETE and MERGE, change rows.
  • DDL statements, CREATE, ALTER, DROP and TRUNCATE, define or remove tables, views and indexes.
  • Transaction control, BEGIN, COMMIT and ROLLBACK, groups changes; GRANT and REVOKE manage permissions.
  • DROP TABLE removes a whole table, while DELETE removes chosen rows and keeps the table.

SQL Keywords and Commands: SELECT, DDL, DML and Transactions Examples

Each example shows the code, the output it produces and what each part does.

Example 1

A read-only summary of sales by status

SELECT chooses a label and a count, FROM supplies the sales, GROUP BY collects equal statuses, and ORDER BY alphabetises the two labels. The query reads records and returns a summary; none of its clauses modifies the sales table.

SQL
SELECT status,COUNT(*) AS sale_count
FROM sales
GROUP BY status
ORDER BY status;
Output
status | sale_count
paid | 4
pending | 2

Common SQL Keywords and Commands: SELECT, DDL, DML and Transactions Mistakes

DROP TABLE when DELETE was intended

Using DROP TABLE when the intention is to delete a few obsolete records removes the schema object rather than selected rows. Choose DELETE with an appropriate WHERE for row removal. A command family label cannot replace checking the precise statement effect and its database-specific transaction behaviour.

SQL Keywords and Commands: SELECT, DDL, DML and Transactions in Real Projects

Migrations and data fixes in one script

A maintenance script may contain both a table migration and a data correction. Separate the intended effects clearly and inspect the affected rows before applying changes. Use permissions appropriate to each job so a reporting account can read data without also acquiring schema deletion capability.

Name maintenance files by their concrete effect: a schema migration, a data backfill and a read-only report require different review and permission expectations. A reporting login should normally lack database-dropping capability. Separate a structural table change from a row correction so transaction and rollback assumptions can be checked for the actual operations.

SQL Keywords and Commands: SELECT, DDL, DML and Transactions FAQ

Are DDL and DML separate SQL languages?

No. They are convenient names for families of SQL operations: data definition language for structure and data manipulation language for rows. Both are part of the same SQL, run in the same database and need no separate installation. You'll also see DQL for queries, DCL for permissions and TCL for transactions.

Is a keyword always a command?

No. WHERE, AS, JOIN and DISTINCT are keywords that participate in statements; they are not standalone requests. A command is a complete statement, such as SELECT ... or UPDATE ..., that the database executes.

Can DDL always be rolled back?

No. Transaction behaviour differs, especially in MySQL, where many DDL operations cause implicit commits. PostgreSQL and SQLite can roll back most DDL inside a transaction. Test schema changes on a copy and take a backup before running them in production.

What is the difference between DELETE and TRUNCATE?

DELETE removes rows one by one, can use WHERE, fires row triggers and can always be rolled back inside a transaction. TRUNCATE empties the whole table much faster, usually resets auto-increment counters and skips row-by-row processing; in MySQL it can't be rolled back. SQLite has no TRUNCATE; use DELETE FROM table.

What does MERGE do?

MERGE combines insert, update and sometimes delete in one statement: it compares a source with a target table and decides, row by row, whether to update an existing row or insert a new one. SQL Server, Oracle and PostgreSQL 15+ support it; MySQL and SQLite use INSERT ... ON DUPLICATE KEY UPDATE and ON CONFLICT instead.

SQL Keywords and Commands: SELECT, DDL, DML and Transactions Exercises

Answer each one and check it instantly. If you get stuck, open the hint before the answer.

Multiple choicemedium

What counts appear in the status-summary command example?

SQL Keywords and Commands: SELECT, DDL, DML and Transactions Quiz

2 questions to check what you learned.

  1. Which command changes selected existing rows?

    • INSERT
    • ALTER TABLE
    • UPDATE
  2. What is the main difference between DELETE and DROP TABLE?

    • DELETE removes rows, while DROP TABLE removes the table object
    • DELETE removes the schema object while DROP only clears rows
    • Both remove rows while preserving every table definition