KDB knowledge

Sample data

The small made-up stock market dataset that most help examples use, with a few rows of each.

Most examples in this help use a small made-up stock market dataset: companies, brokers, price candles, trades and live prices. The names and prices only look real. It is just one example of what KDB stores; the same SQL works for sensor readings, logs and metrics, accounting, users and logins, or any app's data. The examples call it the fixture sample. Every example starts with a fresh copy, so an example that changes rows does not change the next one.

TableKindRowsWhat it holds
securitiesrelational5companies: CITY, COAST, VOLT, TELCO, ACME
brokersrelational5broker numbers and names
ochl_daytime-series, PARTITION BY YEAR57daily candles of CITY, COAST, VOLT: 4 days of 2025-12 and 15 days of 2026-08 and 2026-09
ochl_minutestime-series, PARTITION BY MONTH956one-minute candles of CITY and COAST on 2026-09-09 and 2026-09-10
trade_logtime-series, PARTITION BY DAY32trades on 2026-09-09 and 2026-09-10
live_datatime-series, PARTITION BY DAY12live prices of CITY, COAST, VOLT, TELCO at three times on 2026-09-10

To load the same data into your own database, see quick start. The files are docs/tutorial/examples/setup/schema.sql and sample-data.sql.

Example: All tables and their row counts (sample data: sample)

SHOW TABLES;

Result:

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

(6 rows)

securities

Syntax:

CREATE TABLE securities (
  symbol SYMBOL PRIMARY KEY,
  name   STRING NOT NULL,
  sector STRING
);

One row per listed company. symbol is the key.

ColumnTypeMeaning
symbolSYMBOLstock symbol, like CITY
nameSTRINGcompany name
sectorSTRINGbusiness sector, like Banking

Example: All companies (sample data: sample)

SELECT symbol, name, sector FROM securities ORDER BY symbol;

Result:

symbol (SYMBOL)name (STRING)sector (STRING)
ACMEAcme Investment CompanyInvestment
CITYCity Bank LimitedBanking
COASTCoastal Bank LimitedBanking
TELCONorthwind Telecom LimitedTelecom
VOLTVolt River Hydropower LimitedEnergy

(5 rows)

Example: Last daily close with the company name (a join) (sample data: sample)

SELECT s.symbol, s.name, d.close
FROM ochl_day d JOIN securities s ON s.symbol = d.symbol
WHERE d.time IN '2026-09-10'
ORDER BY s.symbol;

Result:

symbol (SYMBOL)name (STRING)close (DOUBLE)
CITYCity Bank Limited504.9
COASTCoastal Bank Limited376.1
VOLTVolt River Hydropower Limited191.2

(3 rows)

brokers

Syntax:

CREATE TABLE brokers (
  id   INT PRIMARY KEY,
  name STRING NOT NULL
);

One row per broker. trade_log.buyer and trade_log.seller point to brokers(id) with REFERENCES.

ColumnTypeMeaning
idINTbroker number
nameSTRINGbroker name

Example: All brokers (sample data: sample)

SELECT id, name FROM brokers ORDER BY id;

Result:

id (INT)name (STRING)
1Keystone Securities
34Vision Securities
45Imperial Securities
49Online Securities
58Northgate Securities

(5 rows)

ochl_day

Syntax:

CREATE TABLE ochl_day (
  time   TIMESTAMP ROUND 1d,
  symbol SYMBOL,
  open   DOUBLE,
  high   DOUBLE,
  low    DOUBLE,
  close  DOUBLE,
  volume DOUBLE
) TIMESERIES(time) PARTITION BY YEAR DEDUP KEY (symbol, time);

Daily candles (open, high, low, close and volume). time is the trading date at 00:00 UTC. ROUND 1d cuts every time down to the day, and DEDUP KEY (symbol, time) allows one row per symbol per day.

Example: Daily candles of one day (sample data: sample)

SELECT time, symbol, open, high, low, close, volume FROM ochl_day WHERE time IN '2026-09-10' ORDER BY symbol;

Result:

time (TIMESTAMP)symbol (SYMBOL)open (DOUBLE)high (DOUBLE)low (DOUBLE)close (DOUBLE)volume (DOUBLE)
2026-09-10T00:00:00.000ZCITY504.6507500.7504.9183040
2026-09-10T00:00:00.000ZCOAST380.1382373.2376.1186200
2026-09-10T00:00:00.000ZVOLT185.6192.1184.2191.241470

(3 rows)

Example: How many days per symbol (sample data: sample)

SELECT symbol, count(*) AS days, min(time) AS first_day, max(time) AS last_day FROM ochl_day GROUP BY symbol ORDER BY symbol;

Result:

symbol (SYMBOL)days (LONG)first_day (TIMESTAMP)last_day (TIMESTAMP)
CITY192025-12-28T00:00:00.000Z2026-09-10T00:00:00.000Z
COAST192025-12-28T00:00:00.000Z2026-09-10T00:00:00.000Z
VOLT192025-12-28T00:00:00.000Z2026-09-10T00:00:00.000Z

(3 rows)

ochl_minutes

Syntax:

CREATE TABLE ochl_minutes (
  time   TIMESTAMP ROUND 1m,
  symbol SYMBOL,
  open   DOUBLE,
  high   DOUBLE,
  low    DOUBLE,
  close  DOUBLE,
  volume DOUBLE
) TIMESERIES(time) PARTITION BY MONTH DEDUP KEY (symbol, time);

One-minute candles. The market is open 11:00 to 15:00 local time (+05:45, Nepal time), which is 05:15 to 09:15 UTC, so a full day has 240 minutes.

Example: The first 5 minutes of CITY on 2026-09-10 (11:00 Nepal time) (sample data: sample)

SELECT time, symbol, open, high, low, close, volume
FROM ochl_minutes
WHERE symbol = 'CITY' AND time >= '2026-09-10T11:00:00+05:45'
ORDER BY time
LIMIT 5;

Result:

time (TIMESTAMP)symbol (SYMBOL)open (DOUBLE)high (DOUBLE)low (DOUBLE)close (DOUBLE)volume (DOUBLE)
2026-09-10T05:15:00.000ZCITY504.6504.6504.4504.4960
2026-09-10T05:16:00.000ZCITY504.4504.7504.3504.6700
2026-09-10T05:17:00.000ZCITY504.6504.9504504.2150
2026-09-10T05:18:00.000ZCITY504.2504.6504.1504.41050
2026-09-10T05:19:00.000ZCITY504.4504.8504.1504.5450

(5 rows)

Example: Minute candles turned into hourly candles (sample data: sample)

SELECT time, first(open) AS open, max(high) AS high, min(low) AS low, last(close) AS close, sum(volume) AS volume
FROM ochl_minutes
WHERE symbol = 'COAST' AND time IN '2026-09-09'
SAMPLE BY 1h;

Result:

time (TIMESTAMP)open (DOUBLE)high (DOUBLE)low (DOUBLE)close (DOUBLE)volume (DOUBLE)
2026-09-09T05:00:00.000Z398398.8392.2392.933000
2026-09-09T06:00:00.000Z392.9393.6387.4387.743470
2026-09-09T07:00:00.000Z387.7389.2381.6382.349820
2026-09-09T08:00:00.000Z382.3384.2379.238049500
2026-09-09T09:00:00.000Z380380.9379.4380.110540

(5 rows)

trade_log

Syntax:

CREATE TABLE trade_log (
  date           TIMESTAMP ROUND 1d,
  transaction_id LONG,
  symbol         SYMBOL,
  buyer          INT REFERENCES brokers(id),
  seller         INT REFERENCES brokers(id),
  quantity       LONG,
  rate           DOUBLE,
  amount         DOUBLE AS (quantity * rate)
) TIMESERIES(date) PARTITION BY DAY DEDUP KEY (date, transaction_id);

Every trade of the day: who bought, who sold, how many shares and at what rate. amount is a computed column: it is not stored, KDB works it out from quantity * rate when you read it.

Example: The first trades of 2026-09-10 with broker names (sample data: sample)

SELECT f.transaction_id, f.symbol, b.name AS buyer, s.name AS seller, f.quantity, f.rate, f.amount
FROM trade_log f
JOIN brokers b ON b.id = f.buyer
JOIN brokers s ON s.id = f.seller
WHERE f.date IN '2026-09-10'
ORDER BY f.transaction_id
LIMIT 5;

Result:

transaction_id (LONG)symbol (SYMBOL)buyer (STRING)seller (STRING)quantity (LONG)rate (DOUBLE)amount (DOUBLE)
100009COASTVision SecuritiesImperial Securities5040020000
100010CITYKeystone SecuritiesImperial Securities5051725850
100011VOLTKeystone SecuritiesNorthgate Securities501849200
100012VOLTNorthgate SecuritiesImperial Securities20018436800
100013COASTNorthgate SecuritiesKeystone Securities5039819900

(5 rows)

Example: Turnover per symbol (sample data: sample)

SELECT symbol, count(*) AS trades, sum(quantity) AS shares, sum(amount) AS turnover
FROM trade_log
GROUP BY symbol
ORDER BY turnover DESC;

Result:

symbol (SYMBOL)trades (LONG)shares (LONG)turnover (DOUBLE)
TELCO627702438260
CITY1124201247230
COAST1130701227510
VOLT440073950

(4 rows)

live_data

Syntax:

CREATE TABLE live_data (
  timestamp        TIMESTAMP,
  symbol           SYMBOL,
  ltp              DOUBLE,
  ltv              LONG,
  point_change     DOUBLE,
  pct_change       DOUBLE,
  open             DOUBLE,
  high             DOUBLE,
  low              DOUBLE,
  avg_traded_price DOUBLE,
  prev_close       DOUBLE,
  volume           LONG
) TIMESERIES(timestamp) PARTITION BY DAY DEDUP KEY (timestamp, symbol);

Snapshots of live market prices: last traded price (ltp), last traded volume (ltv), change from the previous close, and the day's open, high, low and volume so far.

Example: The latest price of every symbol (sample data: sample)

SELECT timestamp, symbol, ltp, pct_change, volume
FROM live_data
LATEST ON timestamp PARTITION BY symbol
ORDER BY symbol;

Result:

timestamp (TIMESTAMP)symbol (SYMBOL)ltp (DOUBLE)pct_change (DOUBLE)volume (LONG)
2026-09-10T09:14:00.000ZCITY527.12.7175490
2026-09-10T09:14:00.000ZCOAST401.90.735210
2026-09-10T09:14:00.000ZTELCO897.71.969340
2026-09-10T09:14:00.000ZVOLT184-0.8655940

(4 rows)

Example: All snapshots of TELCO (sample data: sample)

SELECT timestamp, ltp, point_change, pct_change FROM live_data WHERE symbol = 'TELCO' ORDER BY timestamp;

Result:

timestamp (TIMESTAMP)ltp (DOUBLE)point_change (DOUBLE)pct_change (DOUBLE)
2026-09-10T05:20:00.000Z860.5-20.5-2.33
2026-09-10T07:15:00.000Z869-12-1.36
2026-09-10T09:14:00.000Z897.716.71.9

(3 rows)