As a Salesforce developer, I was required to display the details of the currently logged-in user or other users in the Salesforce org, such as their name, email, and profile, within Salesforce.
This feature might be used in a dashboard where users can see their account information. To achieve this requirement, I developed an Apex class that retrieves the current user’s details.
Here, I will explain how to get user or logged-in user details using SOQL (Salesforce Object Query Language). Additionally, I will explain other user-related queries so that we can retrieve users’ details.
Get User Info Using SOQL Query
We can use SOQL to retrieve user or current user details in Apex and add conditions to retrieve the specific records we want.
In the steps below, I will explain how to retrieve Salesforce users and current user information using the SOQL query.
Example: Basic SOQL to Retrieve User Details
For example, we want to retrieve all user details from the user object using an SOQL query.
SELECT Id, Name, Email, Username, IsActive FROM UserIn the above SOQL query, we retrieved all user details who are present in the Salesforce org. In the SELECT clause, we need to specify the fields we want to include in the query result, and then in the FROM clause, we need to provide the object name.

Example: Retrieve User Details With WHERE Clause in SOQL
Now, we will understand when we want only those users who are active in our Salesforce org. Then, we need to add conditions to get only active users.
SELECT Id, Name, Email, Username, IsActive FROM User WHERE IsActive = 'True'In the above SOQL query, we added the WHERE clause, which is used to add conditions to the records. Here, we only wanted active users, so using this clause, we retrieved only the active users from the result.

Example: Display All Users By Grouping Their Profiles Using SOQL Query
In a Salesforce org, there are multiple licenses, and when we create any profile using a license, we can add multiple users to that profile.
Now, when we want to display all users with their respective profiles, we need to group users with assigned profiles in the SOQL query.
Let’s see how to create an SOQL query to retrieve all users, grouping their respective profiles, and display the retrieved results.
SELECT COUNT(Id), Profile.Name FROM User GROUP BY Profile.NameIn the above SOQL query, we used the aggregate function to group the user records by profile name. Because SOQL does not support grouping or aggregate functions in the same way as SQL, it retrieves rows directly without applying grouping or aggregate functions.
When we want to get all User records along with their Profile.Name, we need to use the aggregate function along with the GROUP BY clause.
In the image below, you can see the number of users displayed in the ‘Count (Id)’ column and their respective profile names, which are shown in the ‘Name’ column, labeled as ‘Profile’.Name field.

Example: Display Users Created in the Last Three Months Using SOQL Query
Now we will understand how to display user details created within the last 3 months. This means that whenever we create new users and want to retrieve their details, we need to use the CreatedDate field to obtain them.
SELECT Id, Name, CreatedDate FROM User WHERE CreatedDate = LAST_QUARTERIn the above SOQL query, we added the CreatedDate field, and we want users’ details for those created in the last 3 months. For that, we added a condition using the WHERE clause, and then, using the LAST_QUARTER date literals, we provided a time period.

Example: Display Users With Last Login Date Using SOQL Query
When we want to retrieve users’ records to check their last login date and display documents in descending order in the query results, we need to use the ORDER BY clause to sort the results in descending order.
SELECT Id, Name, LastLoginDate FROM User WHERE LastLoginDate != NULL ORDER BY LastLoginDate DESCIn the user object, we have a ‘last login date’ field, which displays the most recent login date for that particular user. In the above SOQL query, we utilized the LastLoginDate field to display users’ recent login dates.
Then, we also added a condition using the WHERE clause not to display null LastLoginDate and to display the query result in descending order; we applied the ORDER BY clause in SOQL.

Example: Display Users With Roles and Profile Using SOQL Query
Using SOQL query, we cannot directly display roles and profiles because they are the lookup with the user object. When we want to display values from parent or child objects, we can use Salesforce Relationship Queries in SOQL to retrieve records from child to parent or parent to child.
When we want to display users with roles and profiles, we need to retrieve records from child to parent, so the SOQL query would be as follows.
SELECT Id, Name, UserRole.Name, Profile.Name FROM UserIn the above SOQL query, we added fields from the user object, and then for the role and profile name, we used dot (.) notation to retrieve a value from the parent object to the child object.

Current or Logged-In User in Salesforce
In Salesforce, the current user is actively logged in, interacts with the Salesforce UI, and performs operations such as creating records, managing data, and automating processes.
When creating a new user in Salesforce, we must fill out several mandatory fields, including name, alias, email, username, profile, time zone, and other relevant details.
Example: Get Details of Current Logged-In User Using SOQL Query
We cannot retrieve the user details for the currently logged-in user directly from the SOQL editor. Instead, we can use the SOQL query in the Apex class, and after executing that Apex class, we can retrieve the currently logged-in user details.
The reason behind this is that to get the currently logged-in user ID, we need to use the UserInfo class in the Apex class.
public class UserLicense {
public void GetUserLicenseAndProfile(){
Id userID = UserInfo.getUserId();
User userObj = [ Select id, Profile.Name, Profile.UserLicense.Name from User where
Id=: userID ];
System.debug ( 'Profile Name: ' +userObj.Profile.Name + ', License: '
+userObj.Profile.UserLicense.Name );
}
}In the above Apex class, we created a SOQL query to retrieve the profile name and license name from the currently logged-in user and stored it in a variable.

In this way, we can get the current user details using SOQL and Apex classes.
Conclusion
I hope you have got an idea about how to get user or logged-in user details Using SOQL (Salesforce Object Query Language). Additionally, I have explained other user-related queries to retrieve the user’s details. We have seen how to use different clauses to filter the user record results that we get from the SOQL query.
You may like to read:
- Get Current User Info Using Apex in Salesforce
- For Clause in SOQL
- HAVING Clause in SOQL (Salesforce Object Query Language)
- SOQL Query in Apex Programming
- OFFSET Clause in SOQL (Salesforce Object Query Language)
I am Bijay Kumar, the founder of SalesforceFAQs.com. Having over 10 years of experience working in salesforce technologies for clients across the world (Canada, Australia, United States, United Kingdom, New Zealand, etc.). I am a certified salesforce administrator and expert with experience in developing salesforce applications and projects. My goal is to make it easy for people to learn and use salesforce technologies by providing simple and easy-to-understand solutions. Check out the complete profile on About us.