# `cols`, `xcol`, `xcols`

_Table columns_

## `cols`

_Column names of a table_
```syntax
cols x    cols[x]
```

Where `x` is a

- table
- the name of a table as a symbol atom
- a filesymbol for a splayed table

returns its column names as a symbol vector. For a keyed table, the key columns are included.
```q
q)trade:([]time:09:30:00.000 09:31:00.000;sym:`IBM`MSFT;price:101.5 52.25;size:100 200)
q)cols trade            /value
`time`sym`price`size
q)cols`trade            /reference
`time`sym`price`size
q)`:db/trade/ set .Q.en[`:db] trade
`:db/trade/
q)cols `:db/trade       /splayed table on disk
`time`sym`price`size
q)cols ([k:1 2]v:3 4)   /keyed table
`k`v
```

## `xcol`

_Rename table columns or dictionary keys_
```syntax
x xcol y    xcol[x;y]
```

Where `y` is a table or dictionary passed by value, and `x` is

- a **symbol vector** of length no greater than `count cols y` returns `y` with its first `count x` columns (or keys) renamed
- a **dictionary** formed from two symbol vectors, returns `y` with the columns (or keys) in `key x` renamed as `value x`
```q
q)t:([]a:3 4 5; b:6 7 8; c:`z`u`i)
q)`d`e xcol t                               / rename first two columns
d e c
-----
3 6 z
4 7 u
5 8 i
q)([a:`A;c:`C]) xcol t                        / rename selected columns
A b C
-----
3 6 z
4 7 u
5 8 i
q)([q:`r]) xcol t              / nonexistent column names in key x signal a length error
'length
  [0]  ([q:`r]) xcol t              / nonexistent column names in key x signal a length error
                ^
```

> **Info — Dictionary as right argument**
>
>
> From KDB-X 2026.10.23, `y` can also be a dictionary, in which case its keys are renamed.
>
```q
q)([a:`c;b:`d]) xcol ([a:10;b:20])
c| 10
d| 20
q)`x`y xcol ([a:10;b:20])
x| 10
y| 20
```

_Q for Mortals_
[§9.8.1 `xcol`](../learn/q4m/9_Queries_q-sql.md#981-xcol)

## `xcols`

_Reorder table columns or dictionary keys_
```syntax
x xcols y    xcols[x;y]
```

Where

- `y` is a simple table or a dictionary passed by value
- `x` is a symbol vector of some or all of `y`’s column names or keys (can also be an atom)

returns `y` with `x` as its first columns (or keys).
```q
q)t:([]a:3 4 5; b:6 7 8; c:`z`u`i)
q)`b xcols t
b a c
-----
6 3 z
7 4 u
8 5 i
q)t:xcols[reverse cols t;t]              / reverse cols and reassign
q)cols t
`c`b`a
```

> **Info — Dictionary as right argument**
>
>
> From KDB-X 2026.10.23, `y` can also be a dictionary, in which case its keys are reordered.
>
```q
q)`c xcols ([a:1;b:2;c:3])
c| 3
a| 1
b| 2
```

_Q for Mortals_
[§9.8.2 `xcols`](../learn/q4m/9_Queries_q-sql.md#982-xcols)

----

[Dictionaries](dictsandtables.md),
[Metadata](metadata.md)


[Tables](table.md)
