{"id":1479,"date":"2026-09-04T15:00:00","date_gmt":"2026-09-04T09:30:00","guid":{"rendered":"https:\/\/swapnilzambare.com\/blog\/soql-basics-first-query-apex\/"},"modified":"2026-09-04T15:00:00","modified_gmt":"2026-09-04T09:30:00","slug":"soql-basics-first-query-apex","status":"publish","type":"post","link":"https:\/\/swapnilzambare.com\/blog\/soql-basics-first-query-apex\/","title":{"rendered":"SOQL Basics: Write Your First Query in Apex"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">SOQL Basics are the first skill you need to read Salesforce data inside Apex code. SOQL stands for Salesforce Object Query Language. It lets your code ask for exactly the records and fields it needs, such as &#8220;all online courses that start this month&#8221;.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this post, you will write your first queries, store the results in lists and maps, and learn the limits every developer must respect. This starts Level 3 of our developer series. It builds on <a href=\"https:\/\/swapnilzambare.com\/blog\/apex-sobjects-generic-vs-specific\/\">Apex sObjects<\/a> and <a href=\"https:\/\/swapnilzambare.com\/blog\/apex-collections-list-set-map\/\">Apex collections: List, Set and Map<\/a>. If you are an admin who already used <a href=\"https:\/\/swapnilzambare.com\/blog\/soql-for-salesforce-admins-workbench-inspector\/\">SOQL in Workbench and Inspector<\/a>, this post shows the same language inside real code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are SOQL Basics?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">SOQL is a query language for reading records from Salesforce objects. A query names the fields to return, the object to read, and optional filters. In Apex, you write a query inside square brackets, and the result comes back as a list of sObjects.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a library request slip. You write &#8220;Title and author of all books on Python, newest first&#8221;. The librarian brings back exactly those books with exactly those details. A SOQL query is that slip, and the Salesforce database is the librarian.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why SOQL Basics Matter<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Almost every piece of Apex code reads data before it does anything else, so SOQL Basics come first. Triggers, classes, batch jobs and Lightning components all need queries. Here is why SOQL Basics matter at Bright Path Tutors.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, a class must find all courses that start next week to send reminders.<\/li>\n\n<li>Also, a trigger must read the parent course before it updates an enrolment.<\/li>\n\n<li>Next, a report page needs the total fee of all online courses.<\/li>\n\n<li>An integration must look up a course by its code before it updates it.<\/li>\n\n<li>Finally, every query counts against limits, so careless queries break code.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">So good queries make code correct, fast and safe within limits.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How SOQL Basics Work in Apex<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">SOQL Basics follow a simple flow inside Apex. First, you write the query in square brackets. Next, Salesforce checks the fields and object at compile time. Then, when the code runs, the database finds the matching records. After that, Salesforce applies sharing and returns the rows. Finally, your code receives a <code>List&lt;Course__c&gt;<\/code>, a single record or a number, depending on how you wrote the query.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-basics-first-query-apex-diagram-1-1024x640.png\" alt=\"SOQL Basics diagram showing write, compile check, run, sharing, return and use steps\" class=\"wp-image-1477\" srcset=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-basics-first-query-apex-diagram-1-1024x640.png 1024w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-basics-first-query-apex-diagram-1-300x188.png 300w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-basics-first-query-apex-diagram-1-768x480.png 768w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-basics-first-query-apex-diagram-1-1536x960.png 1536w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-basics-first-query-apex-diagram-1-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">How an inline SOQL query runs in Apex.<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">SELECT<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">SELECT lists the fields you want. Always use API names, such as <code>Fee__c<\/code>, not labels. The <code>Id<\/code> field comes back automatically, but listing it makes your intent clear.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">FROM<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">FROM names one object, such as <code>Course__c<\/code> or <code>Account<\/code>. A basic query reads from one object at a time. We will join related objects in the post on relationship queries.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">WHERE<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">WHERE filters the records. For example, <code>WHERE Is_Online__c = true<\/code> returns only online courses. We cover filters in depth in the next post.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Inline Queries<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A query inside square brackets is called an inline or static query. The compiler checks it when you save the class. So a typo in a field name is caught at once, not at run time.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Ways to Store Query Results<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">With SOQL Basics, you can store results in several ways. The table below shows the common patterns.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Pattern<\/th><th>Example<\/th><th>Best for<\/th><\/tr><\/thead><tbody><tr><td>List of records<\/td><td>List&lt;Course__c&gt; courses = [SELECT &#8230;];<\/td><td>Most queries<\/td><\/tr><tr><td>Single record<\/td><td>Course__c c = [SELECT &#8230; LIMIT 1];<\/td><td>Exactly one expected row<\/td><\/tr><tr><td>Map by Id<\/td><td>new Map&lt;Id, Course__c&gt;([SELECT &#8230;])<\/td><td>Fast lookups by Id<\/td><\/tr><tr><td>Count only<\/td><td>Integer n = [SELECT COUNT() FROM &#8230;];<\/td><td>Totals without rows<\/td><\/tr><tr><td>For loop<\/td><td>for (Course__c c : [SELECT &#8230;])<\/td><td>Large result sets<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">SOQL vs SQL<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Many developers know SQL from other databases. The table below compares SOQL with standard SQL.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>SOQL<\/th><th>SQL<\/th><\/tr><\/thead><tbody><tr><td>Purpose<\/td><td>Read Salesforce records<\/td><td>Read and change database rows<\/td><\/tr><tr><td>Changes data<\/td><td>No, read only<\/td><td>Yes, with INSERT and UPDATE<\/td><\/tr><tr><td>SELECT *<\/td><td>Not allowed, list fields<\/td><td>Allowed<\/td><\/tr><tr><td>Joins<\/td><td>Relationship queries<\/td><td>JOIN keyword<\/td><\/tr><tr><td>Limits<\/td><td>Governor limits per transaction<\/td><td>Usually none built in<\/td><\/tr><tr><td>Security<\/td><td>Respects sharing and user mode options<\/td><td>Database permissions<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">When to use which is simple. On Salesforce, always use SOQL for reading data. Use DML statements, covered later in this level, to change it.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-basics-first-query-apex-diagram-2-1024x640.png\" alt=\"SOQL Basics diagram comparing SOQL and SQL\" class=\"wp-image-1478\" srcset=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-basics-first-query-apex-diagram-2-1024x640.png 1024w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-basics-first-query-apex-diagram-2-300x188.png 300w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-basics-first-query-apex-diagram-2-768x480.png 768w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-basics-first-query-apex-diagram-2-1536x960.png 1536w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-basics-first-query-apex-diagram-2-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">SOQL vs SQL.<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">A Real SOQL Basics Example in Apex<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s put SOQL Basics to work at Bright Path Tutors. We need a small class that finds online courses and prints their names and fees. Open the Developer Console and create a class called <code>CourseQueryService<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Query a List<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public with sharing class CourseQueryService {\n\n    \/\/ Returns all online courses, cheapest first\n    public static List&lt;Course__c&gt; getOnlineCourses() {\n        return [\n            SELECT Id, Name, Fee__c, Level__c, Start_Date__c\n            FROM Course__c\n            WHERE Is_Online__c = true\n            ORDER BY Fee__c ASC\n        ];\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The class uses <code>with sharing<\/code>, so the query respects the user&#8217;s record access. The query selects five fields from Course__c and keeps only online courses. ORDER BY sorts them by fee, cheapest first.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Run It in Execute Anonymous<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Open Debug &gt; Open Execute Anonymous Window and run this code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>List&lt;Course__c&gt; courses = CourseQueryService.getOnlineCourses();\nSystem.debug('Online courses found: ' + courses.size());\n\nfor (Course__c c : courses) {\n    System.debug(c.Name + ' costs ' + c.Fee__c);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The first line calls the method and stores the list. Then <code>size()<\/code> gives the number of records. Finally, the loop prints each course name and fee. Here is the kind of output you will see in the log.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>USER_DEBUG [2]|DEBUG|Online courses found: 3\nUSER_DEBUG [5]|DEBUG|Apex Basics costs 4500\nUSER_DEBUG [5]|DEBUG|SOQL Basics costs 4800\nUSER_DEBUG [5]|DEBUG|LWC Basics costs 6000<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Build a Map for Fast Lookups<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Map&lt;Id, Course__c&gt; courseById = new Map&lt;Id, Course__c&gt;(\n    [SELECT Id, Name, Fee__c FROM Course__c WHERE Level__c = 'Beginner']\n);\n\nId someId = new List&lt;Id&gt;(courseById.keySet())[0];\nSystem.debug('Lookup by Id: ' + courseById.get(someId).Name);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Passing the query to the Map constructor builds a map keyed by record Id in one line. Later code can find any course by Id without another query. This pattern is very common in triggers.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Query a Single Record Safely<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If you assign a query straight to one record and no row matches, Apex throws a QueryException. So use a list and check it first.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>List&lt;Course__c&gt; found = [\n    SELECT Id, Name FROM Course__c WHERE Name = 'Apex Basics' LIMIT 1\n];\nCourse__c apexCourse = found.isEmpty() ? null : found[0];\nSystem.debug(apexCourse == null ? 'Not found' : 'Found ' + apexCourse.Name);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This code never throws an exception. It returns null when nothing matches, and the caller decides what to do.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">SOQL Basics and Governor Limits<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Salesforce shares servers between many customers, so it limits each transaction. A synchronous transaction can run 100 SOQL queries and retrieve 50,000 records in total. Asynchronous code gets 200 queries. You can check usage with the Limits class.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>System.debug('Queries used: ' + Limits.getQueries() + ' of ' + Limits.getLimitQueries());<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The most common way to break this limit, even for people who know SOQL Basics well, is a query inside a loop. So always query once before the loop and use a map inside it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">SOQL Basics and Security<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Inline SOQL in a <code>with sharing<\/code> class respects record sharing. However, field and object permissions need extra care. You can add <code>WITH USER_MODE<\/code> to a query, so Salesforce enforces the user&#8217;s object and field access too. We will cover this fully in the security level of this series.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes with SOQL Basics<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Writing queries inside for loops, which quickly hits the 100 query limit.<\/li>\n\n<li>Assigning a query to a single record when no row might match.<\/li>\n\n<li>Selecting fields you never use, which slows queries and wastes heap.<\/li>\n\n<li>Using field labels instead of API names.<\/li>\n\n<li>Reading a field that the query did not select, which throws an error.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for SOQL Basics<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Query once, store results in a list or map, then loop.<\/li>\n\n<li>Select only the fields you need.<\/li>\n\n<li>Add filters and LIMIT to keep results small.<\/li>\n\n<li>Use <code>with sharing<\/code> classes and consider <code>WITH USER_MODE<\/code>.<\/li>\n\n<li>Use SOQL for loops when results may be very large.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Task<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Add a method <code>getCoursesStartingSoon()<\/code> that returns courses starting in the next 30 days, sorted by start date. Hint: compare <code>Start_Date__c<\/code> with <code>Date.today().addDays(30)<\/code> using a bind variable, which we explain fully later in this level.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Most Asked Interview Questions on SOQL Basics<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Questions on SOQL Basics appear in almost every Salesforce developer interview. Here are the 20 questions asked most, with short answers you can explain in your own words.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Basic Questions on SOQL Basics<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q1: What is SOQL?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">SOQL is the Salesforce Object Query Language. It reads records from Salesforce objects. It cannot insert, update or delete data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q2: How is SOQL different from SQL?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">SOQL only reads data, does not support SELECT *, uses relationship queries instead of joins and runs within governor limits.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q3: What is an inline SOQL query?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is a query written in square brackets inside Apex. The compiler checks its fields and objects when you save the class.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q4: What does a SOQL query return in Apex?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Usually a list of sObjects. It can also return a single sObject, an Integer for COUNT() or AggregateResult rows for aggregate queries.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">**Q5: Can you use SELECT * in SOQL?**<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No. You must list the fields. In newer API versions, FIELDS(ALL), FIELDS(STANDARD) and FIELDS(CUSTOM) exist, but they have restrictions and are not used in most Apex code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q6: Is the Id field returned if you do not select it?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. Salesforce returns Id automatically for queries that return records. Many developers still list it for clarity.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q7: What happens if you read a field you did not query?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Apex throws a System.SObjectException saying the field was not queried, unless you set the value yourself in code.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Code and Scenario Questions on SOQL Basics<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q8: How many SOQL queries can a transaction run?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">100 in synchronous Apex and 200 in asynchronous Apex.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q9: How many records can SOQL return in one transaction?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A total of 50,000 records across all queries in the transaction.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q10: What happens when you assign a query with no results to a single record?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Apex throws a QueryException with &#8220;List has no rows for assignment to SObject&#8221;. So query into a list and check if it is empty.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q11: How do you build a map of records by Id from a query?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Pass the query to the Map constructor, such as new Map&lt;Id, Course__c&gt;([SELECT Id, Name FROM Course__c]).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q12: Why should you not put SOQL inside a loop?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Each loop pass runs another query, so you quickly hit the 100 query limit. Query once before the loop and use a map.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q13: How do you count records without returning them?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use SELECT COUNT() FROM Course__c, which returns an Integer.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q14: How do you check how many queries your code has used?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use Limits.getQueries() and compare it with Limits.getLimitQueries().<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Advanced Questions on SOQL Basics<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q15: What is a SOQL for loop and when do you use it?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is a for loop that iterates over a query directly. Salesforce fetches records in batches, which helps avoid heap size limits for large results.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q16: Does SOQL respect sharing?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Inline SOQL follows the sharing mode of the class. With sharing classes respect record access, while without sharing classes do not.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q17: How do you enforce field and object permissions in SOQL?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Add WITH USER_MODE to the query, or use WITH SECURITY_ENFORCED in older code. User mode is the newer, recommended option.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q18: Can you query deleted records?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes, with ALL ROWS in Apex. It returns records in the Recycle Bin as well as active records.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q19: What is the difference between a static and a dynamic query?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A static query is written in brackets and checked at compile time. A dynamic query is a string run with Database.query at run time.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q20: How would you speed up a slow SOQL query?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Filter on indexed fields such as Id, Name or external IDs, select fewer fields, add LIMIT and avoid leading wildcards in LIKE filters.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Official Resources<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/developer.salesforce.com\/docs\/atlas.en-us.soql_sosl.meta\/soql_sosl\/sforce_api_calls_soql.htm\" target=\"_blank\" rel=\"noopener noreferrer\">SOQL and SOSL Reference (Salesforce Developers)<\/a><\/li>\n\n<li><a href=\"https:\/\/trailhead.salesforce.com\/content\/learn\/modules\/apex_database\" target=\"_blank\" rel=\"noopener noreferrer\">Trailhead: Apex Basics and Database<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Quick Recap<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>SOQL Basics let Apex read exactly the records and fields it needs.<\/li>\n\n<li>Inline queries use square brackets and are checked at compile time.<\/li>\n\n<li>Results go into lists, single records, maps or counts.<\/li>\n\n<li>Never query inside loops, because of the 100 query limit.<\/li>\n\n<li>Use with sharing and consider WITH USER_MODE for security.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Now you can read records. Next, learn to filter and sort them precisely in our guide to the <a href=\"https:\/\/swapnilzambare.com\/blog\/soql-where-clause-order-by-limit\/\">SOQL WHERE clause, ORDER BY, LIMIT and OFFSET<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SOQL Basics are the first skill you need to read Salesforce data inside Apex code. SOQL stands for Salesforce Object Query Language. It lets&hellip;<\/p>\n","protected":false},"author":1,"featured_media":1476,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_swapz_level":"","_swapz_series_part":0,"_swapz_hide_toc":false,"_swapz_hide_featured":false,"_swapz_description":"","footnotes":""},"categories":[8,96],"tags":[98,26,91,7,97,105],"swapz_series":[],"class_list":["post-1479","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-salesforce","category-salesforce-developer","tag-apex","tag-intermediate","tag-interview-questions","tag-salesforce","tag-salesforce-developer","tag-soql"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>SOQL Basics: Write Your First Query in Apex<\/title>\n<meta name=\"description\" content=\"SOQL Basics for Apex developers: SELECT, FROM, WHERE, inline queries, lists, maps, query limits and 20 most asked interview questions with answers.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/swapnilzambare.com\/blog\/soql-basics-first-query-apex\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SOQL Basics: Write Your First Query in Apex\" \/>\n<meta property=\"og:description\" content=\"SOQL Basics for Apex developers: SELECT, FROM, WHERE, inline queries, lists, maps, query limits and 20 most asked interview questions with answers.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/swapnilzambare.com\/blog\/soql-basics-first-query-apex\/\" \/>\n<meta property=\"og:site_name\" content=\"Swapnil Zambare - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-04T09:30:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-basics-first-query-apex-featured.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2400\" \/>\n\t<meta property=\"og:image:height\" content=\"1260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Swapnil Zambare\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Swapnil Zambare\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/soql-basics-first-query-apex\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/soql-basics-first-query-apex\\\/\"},\"author\":{\"name\":\"Swapnil Zambare\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/person\\\/922ac0131aaa7088c78c2cb1700e0c9a\"},\"headline\":\"SOQL Basics: Write Your First Query in Apex\",\"datePublished\":\"2026-09-04T09:30:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/soql-basics-first-query-apex\\\/\"},\"wordCount\":1908,\"publisher\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/soql-basics-first-query-apex\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/soql-basics-first-query-apex-featured.png\",\"keywords\":[\"Apex\",\"Intermediate\",\"Interview Questions\",\"Salesforce\",\"Salesforce Developer\",\"SOQL\"],\"articleSection\":[\"Salesforce\",\"Salesforce Developer\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/soql-basics-first-query-apex\\\/\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/soql-basics-first-query-apex\\\/\",\"name\":\"SOQL Basics: Write Your First Query in Apex\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/soql-basics-first-query-apex\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/soql-basics-first-query-apex\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/soql-basics-first-query-apex-featured.png\",\"datePublished\":\"2026-09-04T09:30:00+00:00\",\"description\":\"SOQL Basics for Apex developers: SELECT, FROM, WHERE, inline queries, lists, maps, query limits and 20 most asked interview questions with answers.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/soql-basics-first-query-apex\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/soql-basics-first-query-apex\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/soql-basics-first-query-apex\\\/#primaryimage\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/soql-basics-first-query-apex-featured.png\",\"contentUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/soql-basics-first-query-apex-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"SOQL Basics featured image: title card with an Apex code example\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/soql-basics-first-query-apex\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SOQL Basics: Write Your First Query in Apex\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\",\"name\":\"Swapnil Zambare - Blog\",\"description\":\"Salesforce &amp; Tech Guides\",\"publisher\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\",\"name\":\"Swapnil Zambare - Blog\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/site-icon-512.png\",\"contentUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/site-icon-512.png\",\"width\":512,\"height\":512,\"caption\":\"Swapnil Zambare - Blog\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/theSwapnilZambare\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/person\\\/922ac0131aaa7088c78c2cb1700e0c9a\",\"name\":\"Swapnil Zambare\",\"sameAs\":[\"https:\\\/\\\/swapnilzambare.com\\\/blog\"],\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/author\\\/swapnil-zambare\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SOQL Basics: Write Your First Query in Apex","description":"SOQL Basics for Apex developers: SELECT, FROM, WHERE, inline queries, lists, maps, query limits and 20 most asked interview questions with answers.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/swapnilzambare.com\/blog\/soql-basics-first-query-apex\/","og_locale":"en_US","og_type":"article","og_title":"SOQL Basics: Write Your First Query in Apex","og_description":"SOQL Basics for Apex developers: SELECT, FROM, WHERE, inline queries, lists, maps, query limits and 20 most asked interview questions with answers.","og_url":"https:\/\/swapnilzambare.com\/blog\/soql-basics-first-query-apex\/","og_site_name":"Swapnil Zambare - Blog","article_published_time":"2026-09-04T09:30:00+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-basics-first-query-apex-featured.png","type":"image\/png"}],"author":"Swapnil Zambare","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Swapnil Zambare","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/swapnilzambare.com\/blog\/soql-basics-first-query-apex\/#article","isPartOf":{"@id":"https:\/\/swapnilzambare.com\/blog\/soql-basics-first-query-apex\/"},"author":{"name":"Swapnil Zambare","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/person\/922ac0131aaa7088c78c2cb1700e0c9a"},"headline":"SOQL Basics: Write Your First Query in Apex","datePublished":"2026-09-04T09:30:00+00:00","mainEntityOfPage":{"@id":"https:\/\/swapnilzambare.com\/blog\/soql-basics-first-query-apex\/"},"wordCount":1908,"publisher":{"@id":"https:\/\/swapnilzambare.com\/blog\/#organization"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/soql-basics-first-query-apex\/#primaryimage"},"thumbnailUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-basics-first-query-apex-featured.png","keywords":["Apex","Intermediate","Interview Questions","Salesforce","Salesforce Developer","SOQL"],"articleSection":["Salesforce","Salesforce Developer"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/swapnilzambare.com\/blog\/soql-basics-first-query-apex\/","url":"https:\/\/swapnilzambare.com\/blog\/soql-basics-first-query-apex\/","name":"SOQL Basics: Write Your First Query in Apex","isPartOf":{"@id":"https:\/\/swapnilzambare.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/swapnilzambare.com\/blog\/soql-basics-first-query-apex\/#primaryimage"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/soql-basics-first-query-apex\/#primaryimage"},"thumbnailUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-basics-first-query-apex-featured.png","datePublished":"2026-09-04T09:30:00+00:00","description":"SOQL Basics for Apex developers: SELECT, FROM, WHERE, inline queries, lists, maps, query limits and 20 most asked interview questions with answers.","breadcrumb":{"@id":"https:\/\/swapnilzambare.com\/blog\/soql-basics-first-query-apex\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/swapnilzambare.com\/blog\/soql-basics-first-query-apex\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/swapnilzambare.com\/blog\/soql-basics-first-query-apex\/#primaryimage","url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-basics-first-query-apex-featured.png","contentUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-basics-first-query-apex-featured.png","width":2400,"height":1260,"caption":"SOQL Basics featured image: title card with an Apex code example"},{"@type":"BreadcrumbList","@id":"https:\/\/swapnilzambare.com\/blog\/soql-basics-first-query-apex\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/swapnilzambare.com\/blog\/"},{"@type":"ListItem","position":2,"name":"SOQL Basics: Write Your First Query in Apex"}]},{"@type":"WebSite","@id":"https:\/\/swapnilzambare.com\/blog\/#website","url":"https:\/\/swapnilzambare.com\/blog\/","name":"Swapnil Zambare - Blog","description":"Salesforce &amp; Tech Guides","publisher":{"@id":"https:\/\/swapnilzambare.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/swapnilzambare.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/swapnilzambare.com\/blog\/#organization","name":"Swapnil Zambare - Blog","url":"https:\/\/swapnilzambare.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/09\/site-icon-512.png","contentUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/09\/site-icon-512.png","width":512,"height":512,"caption":"Swapnil Zambare - Blog"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.linkedin.com\/in\/theSwapnilZambare"]},{"@type":"Person","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/person\/922ac0131aaa7088c78c2cb1700e0c9a","name":"Swapnil Zambare","sameAs":["https:\/\/swapnilzambare.com\/blog"],"url":"https:\/\/swapnilzambare.com\/blog\/author\/swapnil-zambare\/"}]}},"_links":{"self":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/1479","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/comments?post=1479"}],"version-history":[{"count":0,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/1479\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/media\/1476"}],"wp:attachment":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/media?parent=1479"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/categories?post=1479"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/tags?post=1479"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=1479"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}