KDB knowledge

SQL command groups: DDL, DML, DCL, TCL, DQL

The five groups of SQL statements, every KDB statement in its group, and the role each group needs.

SQL statements are sorted into five groups by what they do. The group tells you three things at once: what the statement changes, which role a user needs for it, and whether it can go inside BEGIN ... COMMIT.

GroupNameWhat it doesMain statementsRole needed
DDLData Definition Languagemakes, changes and removes objects: tables, views, indexes, functions, procedures, triggersCREATE, ALTER, DROP, RENAMEadmin
DMLData Manipulation Languagechanges the rows inside tablesINSERT, UPDATE, DELETE, CALLwrite
DCLData Control Languagesays who may do whatCREATE USER, GRANT, REVOKEadmin as the user's own ROLE
TCLTransaction Control Languagegroups changes so they are saved together or not at allBEGIN, COMMIT, ROLLBACKread
DQLData Query Languagereads rows and never changes anythingSELECT, EXPLAIN, SHOWread

DDL: Data Definition Language

DDL statements build the shape of a database. They change objects, not rows (except DROP, which removes the rows with the table).

ObjectMakeChangeRemoveHelp
tableCREATE TABLEALTER TABLE, RENAME TABLEDROP TABLE, then UNDROP TABLE to bring it backcreate-table, alter-table, trash
viewCREATE VIEW, CREATE CACHED VIEWdrop and create againDROP VIEWviews
materialized viewCREATE MATERIALIZED VIEWALTER TABLE ... SET TTL; otherwise drop and create againDROP MATERIALIZED VIEWviews
indexCREATE INDEX, CREATE UNIQUE INDEX, CREATE FULLTEXT INDEX-DROP INDEXindexes
saved functionCREATE FUNCTIONCREATE OR REPLACE FUNCTIONDROP FUNCTIONsaved-functions
stored procedureCREATE PROCEDURECREATE OR REPLACE PROCEDUREDROP PROCEDUREprocedures
triggerCREATE TRIGGERALTER TRIGGER ... ENABLE / DISABLEDROP TRIGGERtriggers
sequenceCREATE SEQUENCEALTER SEQUENCEDROP SEQUENCEfunctions-postgres
ENUM typeCREATE TYPE ... AS ENUMALTER TYPE ... ADD VALUEDROP TYPEcolumn-types
trading calendarCREATE CALENDARALTER CALENDAR ... ADD HOLIDAY / ADD ERA / ...DROP CALENDARcalendars
databaseCREATE DATABASEALTER DATABASEDROP DATABASEdatabases

Example: DDL: a table, a column added later, a view and an index (sample data: sample)

CREATE TABLE watchlist (symbol STRING PRIMARY KEY, note STRING);
ALTER TABLE watchlist ADD COLUMN target DOUBLE;
CREATE VIEW watchlist_names AS
SELECT w.symbol, s.name, w.target FROM watchlist w JOIN securities s ON s.symbol = w.symbol;
CREATE INDEX watchlist_note ON watchlist (note);
SHOW COLUMNS FROM watchlist;
SHOW VIEWS;

Result:

CREATE TABLE watchlist (

table 'watchlist' created

ALTER TABLE watchlist ADD COLUMN target DOUBLE

table 'watchlist' altered

CREATE VIEW watchlist_names AS SELECT w.symbol, s.name, w.target FROM watchlist AS w JOIN securities

view 'watchlist_names' created

CREATE INDEX watchlist_note ON watchlist (note)

index 'watchlist_note' created on table 'watchlist' (0 rows)

SHOW COLUMNS FROM watchlist

name (STRING)type (STRING)primary_key (BOOL)not_null (BOOL)computed (BOOL)references (STRING)
symbolSTRINGtruefalsefalseNULL
noteSTRINGfalsefalsefalseNULL
targetDOUBLEfalsefalsefalseNULL

(3 rows)

SHOW VIEWS

name (STRING)query (STRING)kind (STRING)refresh (STRING)
watchlist_namesSELECT w.symbol, s.name, w.target FROM watchlist AS w JOIN securities AS s ON s.symbol = w.symbolcomplexNULL

(1 row)

DML: Data Manipulation Language

DML statements change the rows of tables. The table itself stays as it is.

StatementWhat it doesHelp
INSERT INTO ... VALUESadds rowsinsert
INSERT OR REPLACE, INSERT OR IGNORE, ON CONFLICTadds rows, or replaces / keeps the row with the same keyinsert
INSERT INTO ... SELECTadds the rows a query givesinsert
UPDATE ... SET ... WHEREchanges rowsupdate-delete
DELETE FROM ... WHEREremoves rowsupdate-delete
... RETURNINGshows the rows that were added, changed or removedupdate-delete
CALL name(...)runs a stored procedure, which changes rowsprocedures

Example: DML: add, change and remove rows (sample data: sample)

Setup:

CREATE TABLE watchlist (symbol STRING PRIMARY KEY, note STRING, target DOUBLE);
INSERT INTO watchlist VALUES ('CITY', 'bank', 520), ('VOLT', 'hydro', 250);
UPDATE watchlist SET target = 540 WHERE symbol = 'CITY';
DELETE FROM watchlist WHERE symbol = 'VOLT' RETURNING symbol, note;
SELECT * FROM watchlist;

Result:

INSERT INTO watchlist VALUES ('CITY', 'bank', 520), ('VOLT', 'hydro', 250)

2 row(s) inserted

UPDATE watchlist SET target = 540 WHERE symbol = 'CITY'

1 row(s) updated

DELETE FROM watchlist WHERE symbol = 'VOLT' RETURNING symbol, note

symbol (STRING)note (STRING)
VOLThydro

(1 row)

1 row(s) deleted

SELECT * FROM watchlist

symbol (STRING)note (STRING)target (DOUBLE)
CITYbank540

(1 row)

DCL: Data Control Language

DCL statements decide who may connect and what each user may do.

StatementWhat it doesHelp
CREATE USER name PASSWORD '...' ROLE ...adds a user with a role for every databaseusers-and-roles
ALTER USERchanges a password or a roleusers-and-roles
DROP USERremoves a userusers-and-roles
GRANT role ON DATABASE db TO namegives a role on one databaseusers-and-roles
GRANT ... ON TABLEgives rights on one tabletable-rights
REVOKE ... FROM nametakes a grant awayusers-and-roles
SHOW USERSlists users, roles and grantsusers-and-roles

The roles, from least to most: none < read < write < admin. Each role may do everything of the roles before it.

Example: DCL: an admin, a read-only report user, and an app that may write one database (sample data: sample)

Setup:

CREATE USER admin WITH PASSWORD 'Long-Admin-Passw0rd' ROLE admin;
CREATE USER report WITH PASSWORD 'Reports-only-secret-7' ROLE read;
CREATE USER shop_app WITH PASSWORD 'Another-long-secret-42' ROLE none;
GRANT write ON DATABASE demo TO shop_app;
SHOW USERS;

Result:

CREATE USER report WITH PASSWORD '***' ROLE read

user 'report' created with role read

CREATE USER shop_app WITH PASSWORD '***' ROLE none

user 'shop_app' created with role none

GRANT write ON DATABASE demo TO shop_app

user 'shop_app' has role write on database 'demo'

SHOW USERS

name (STRING)role (STRING)grants (STRING)created (TIMESTAMP)changed (TIMESTAMP)table_grants (STRING)two_factor (BOOL)statement_timeout (STRING)priority (STRING)
adminadminNULL2026-10-22T05:44:28.977Z2026-10-22T05:44:28.977ZNULLfalseNULLNULL
reportreadNULL2026-10-22T05:44:29.197Z2026-10-22T05:44:29.197ZNULLfalseNULLNULL
shop_appnonedemo=write2026-10-22T05:44:29.265Z2026-10-22T05:44:29.268ZNULLfalseNULLNULL

(3 rows)

TCL: Transaction Control Language

TCL statements group DML into one transaction: KDB saves all of its changes together, or none of them.

StatementWhat it doesHelp
BEGIN (also BEGIN TRANSACTION, BEGIN WORK)starts a transactiontransactions
COMMITsaves every change since BEGIN, in one writetransactions
ROLLBACKthrows away every change since BEGINtransactions
SELECT ... FOR UPDATEreads rows that no other session may change before you COMMITtransactions

Example: TCL: a transfer that is saved, and a mistake that is undone (sample data: sample)

Setup:

CREATE TABLE accounts (id INT PRIMARY KEY, owner STRING NOT NULL, balance DECIMAL(2) NOT NULL);
INSERT INTO accounts VALUES (1, 'Ram', 1000), (2, 'Sita', 0);
BEGIN;
UPDATE accounts SET balance = balance - 300 WHERE id = 1;
UPDATE accounts SET balance = balance + 300 WHERE id = 2;
COMMIT;

BEGIN;
DELETE FROM accounts;
ROLLBACK;

SELECT * FROM accounts ORDER BY id;

Result:

BEGIN

transaction started

UPDATE accounts SET balance = balance - 300 WHERE id = 1

1 row(s) updated

UPDATE accounts SET balance = balance + 300 WHERE id = 2

1 row(s) updated

COMMIT

transaction committed: 2 change(s) saved

BEGIN

transaction started

DELETE FROM accounts

2 row(s) deleted

ROLLBACK

transaction rolled back: 1 change(s) undone

SELECT * FROM accounts ORDER BY id

id (INT)owner (STRING)balance (DECIMAL(2))
1Ram700.00
2Sita300.00

(2 rows)

DQL: Data Query Language

DQL statements read. They never change a row or an object, so they are safe to run at any time.

StatementWhat it doesHelp
SELECTreads rows: WHERE, JOIN, GROUP BY, ORDER BY, LIMITselect
SELECT ... SAMPLE BY, LATEST ONtime-series candles and the newest row per keysample-by, latest-on
WITH ... SELECTa query with named partsunion-subqueries
EXPLAIN SELECT ...shows how KDB will run a query, without running itexplain
SHOW TABLES, SHOW VIEWS, SHOW INDEXES, ...lists the objects of a databaseshow-tables
DECLARE, FETCH, CLOSEreads a big result a part at a timecursors

Example: DQL: a report, a query plan and a list of objects (sample data: sample)

SELECT symbol, count(*) AS trades, sum(quantity) AS shares
FROM trade_log GROUP BY symbol ORDER BY trades DESC, symbol LIMIT 3;
EXPLAIN SELECT * FROM ochl_day WHERE symbol = 'CITY';
SHOW TABLES;

Result:

SELECT symbol, count(*) AS trades, sum(quantity) AS shares FROM trade_log GROUP BY symbol ORDER BY t

symbol (SYMBOL)trades (LONG)shares (LONG)
CITY112420
COAST113070
TELCO62770

(3 rows)

EXPLAIN SELECT * FROM ochl_day WHERE symbol = 'CITY'

plan (STRING)
scan table ochl_day (2 of 2 partitions, about 57 rows)
read only 1 key range(s) of each partition
filter while scanning: 1 condition(s)
engine: column

(4 rows)

SHOW TABLES

name (STRING)kind (STRING)partition (STRING)columns (INT)rows (LONG)
brokersrelationalNONE25
live_datatimeseriesDAY1212
ochl_daytimeseriesYEAR757
ochl_minutestimeseriesMONTH7956
securitiesrelationalNONE35
trade_logtimeseriesDAY832

(6 rows)

Which group is my statement?

Starts withGroup
CREATE, ALTER, DROP, RENAME, UNDROP (tables, views, indexes, functions, procedures, triggers, sequences, databases)DDL
INSERT, UPDATE, DELETE, CALLDML
CREATE USER, ALTER USER, DROP USER, GRANT, REVOKEDCL
BEGIN, COMMIT, ROLLBACKTCL
SELECT, WITH, EXPLAIN, SHOW, DECLARE, FETCH, CLOSEDQL

Other statements belong to looking after the database, not to one of the five groups: BACKUP DATABASE, CHECK TABLE, CHECKPOINT (see backup and safety) and USE (see databases).