---
title: "Get list of one column SOQL"
url: "https://blog.beyondthecloud.dev/tips/get-list-of-one-column-soql"
category: "SOQL"
tags: "apex"
published: "2024-01-02"
---

# Get list of one column SOQL

The simplest way to retrieve record IDs without a for loop is by using a Map and the keySet() method.

### Get field values in SOQL

How to Obtain Unique Field Values Without Using a For Loop?

The simplest way to retrieve record IDs without a for loop is by using a Map and the keySet() method.

```apex
Set<Id> accountIds = new Map<Id, Account>(
    [SELECT Id FROM Account]
).keySet();
```

But how can you obtain values for other fields?

You can accomplish this using field aliasing.

As shown on previous slide, a Map will automatically use the Id as a Map key.

```apex
Set<String> industries = new Set<String>();

for (Account acc : [SELECT Industry FROM Account]) {
    industries.add(acc.Industry);
}
```

```apex
Set<String> industries = new Map<String, SObject>([
    SELECT Industry Id
    FROM Account
    WHERE Industry != NULL
    GROUP BY Industry
]).keySet();
```

To avoid Row with null Id at index: 0 always add null check (!= NULL)
