---
schema_version: "1"
id: 3372332a08668ef4a8184f114a1aa9766b1b9ef5e89895a7767a418f059af41a
title: Using SOQL Queries That Return One Record
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/apexcode/langCon_apex_SOQL_single_row.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_single_row.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: 3ac8a1c4a8cd9f90bc2274520c8f818ce49ccd8cd1514ae9a05e5f75cbcab773
description: SOQL queries can be used to assign a single sObject value when the result list contains only one element.
---
# Using SOQL Queries That Return One Record

SOQL queries can be used to assign a single sObject value when the result list contains only one element.

When the L-value of an expression is a single sObject type, Apex automatically assigns the single sObject record in the query result list to the L-value. A runtime exception results if zero sObjects or more than one sObject is found in the list. For example:

List<Account> accts = \[SELECT Id FROM Account WITH USER\_MODE\];

// These lines of code are only valid if one row is returned from
// the query. Notice that the second line dereferences the field from the
// query without assigning it to an intermediary sObject variable.
Account acct = \[SELECT Id FROM Account WITH USER\_MODE\];
String name = \[SELECT Name FROM Account WITH USER\_MODE\].Name;

This usage is supported with the following Apex types, methods, or operators:

- Database.query method.
- Safe Navigation Operator. See [Safe Navigation Operator](https://developer.salesforce.com/docs/atlas.en-us.264.0.apexcode.meta/apexcode/langCon_apex_SafeNavigationOperator.htm).
- Null Coalescing Operator. See [Null Coalescing Operator](https://developer.salesforce.com/docs/atlas.en-us.264.0.apexcode.meta/apexcode/langCon_apex_NullCoalescingOperator.htm).
- Map.values.
    
    ![Warning](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/images/icon_note_warning.png)
    
    #### Warning
    
    Although currently supported, Salesforce recommends against using this feature with Map.values.

- [← Previous](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_VLSQ.htm "Working with Very Large SOQL Queries")
- [Next →](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_filtering_nulls.htm "Improve Performance by Avoiding Null Values")