# `lj`, `ljf`

_Left join_
```syntax
x lj  y     lj [x;y]
x ljf y     ljf[x;y]
```

Where

- `x` is a table, or the name of a table as a symbol, in which case `lj` updates the table in place
- `y` is
    - a keyed table whose key columns are columns of `x`, returns `x` and `y` joined on the key columns of `y`
    - or (for `lj` only) the general empty list `()`, returns `x`

For each record in `x`, the result has one record with the columns of `y` joined to the columns of `x`:

- If there is a matching record in `y`, it is joined to the `x` record, and common columns take their values from `y`.
- If there is no matching record in `y`, common columns are unchanged and new columns are null.

If `y` has duplicate keys, `lj` uses the first matching record.
```q
q)show x:([]a:1 2 3;b:`I`J`K;c:10 20 30)
a b c
------
1 I 10
2 J 20
3 K 30

q)show y:([a:1 3;b:`I`K]c:1 2;d:10 20)
a b| c d
---| ----
1 I| 1 10
3 K| 2 20

q)x lj y
a b c  d
---------
1 I 1  10
2 J 20
3 K 2  20
```

This expression returns the `y` columns that `lj` joins to `x`:
```q
q)y[select a,b from x]
c d
----
1 10

2 20
```

`lj` signals a `type` error if `y` is not a keyed table.
```q
q)x lj 0!y
'type
  [0]  x lj 0!y
         ^
```

`lj` is a cover for `,\:` ([Join Each Left](join.md)) that also accepts a keyed table as `x`.

`lj` is a [multithreaded primitive](mt-primitives.md).

## `ljf`

With `lj`, a null in a matching record of `y` overwrites the value in `x`.
`ljf` instead fills from `x` where the value in `y` is null.
```q
q)show x:([]a:1 2;b:`x`y;c:10 20)
a b c
------
1 x 10
2 y 20
q)show y:([a:1 2]b:``z;c:1 0N)
a| b c
-| ---
1|   1
2| z
q)x lj y
a b c
-----
1   1
2 z
q)x ljf y
a b c
------
1 x 1
2 z 20
```

----

[Joins](joins.md)


_Q for Mortals_
[§9.9.2 Ad Hoc Left Join](../learn/q4m/9_Queries_q-sql.md#992-ad-hoc-left-join-lj)
