Toggle Side Panel

  • Blogs
  • Consultants
    • Salesforce Product Expertise
      • Top Salesforce ConsultantsTop Salesforce Consultants
      • Marketing Cloud ConsultantsMarketing Cloud Consultants
      • Service Cloud ConsultantsService Cloud Consultants
      • Experience Cloud ConsultantsExperience Cloud Consultants
      • Analytics Cloud ConsultantsAnalytics Cloud Consultants
    • Salesforce Industry Expertise
      • Non-Profit Cloud ConsultantsNon-Profit Cloud Consultants
      • Financial Service Cloud ConsultantsFinancial Service Cloud Consultants
      • Health Cloud ConsultantsHealth Cloud Consultants
      • Commerce Cloud ConsultantsCommerce Cloud Consultants
      • Manufacturing Cloud ConsultantsManufacturing Cloud Consultants
    • Salesforce Experts by Location
      • USATop Salesforce Consultants in USA
      • IndiaTop Salesforce Consultants in India
      • AustraliaTop Salesforce Consultants in Australia
      • United KingdomTop Salesforce Consultants in UK
      • CanadaTop Salesforce Consultants in Canada
  • Webinars
  • Contact Us
  • Discussions
More options
    Sign in Sign up
    • Blogs
    • Consultants
      • Salesforce Product Expertise
        • Top Salesforce ConsultantsTop Salesforce Consultants
        • Marketing Cloud ConsultantsMarketing Cloud Consultants
        • Service Cloud ConsultantsService Cloud Consultants
        • Experience Cloud ConsultantsExperience Cloud Consultants
        • Analytics Cloud ConsultantsAnalytics Cloud Consultants
      • Salesforce Industry Expertise
        • Non-Profit Cloud ConsultantsNon-Profit Cloud Consultants
        • Financial Service Cloud ConsultantsFinancial Service Cloud Consultants
        • Health Cloud ConsultantsHealth Cloud Consultants
        • Commerce Cloud ConsultantsCommerce Cloud Consultants
        • Manufacturing Cloud ConsultantsManufacturing Cloud Consultants
      • Salesforce Experts by Location
        • USATop Salesforce Consultants in USA
        • IndiaTop Salesforce Consultants in India
        • AustraliaTop Salesforce Consultants in Australia
        • United KingdomTop Salesforce Consultants in UK
        • CanadaTop Salesforce Consultants in Canada
    • Webinars
    • Contact Us
    • Discussions
    Close search

    Activity › Forums › Salesforce® Discussions › SOQL Query ( Accounts and Contacts ) in Salesforce

    Tagged: Account, Contact Record, Developer Console, Salesforce Apex, Salesforce SOQL, SOQL, SOQL Query

    • Salesforce® Discussions

      SOQL Query ( Accounts and Contacts ) in Salesforce

      Posted by Rahul Kr on August 10, 2017 at 4:12 AM

      Dear Sir / Mam,
      I have one question :-

      Write an SOQL Query in Developer Console where I want to Display Accounts and their Corresponding Contacts ( First Name, Last Name etc ) BUT only those Accounts who have more than ONE Contact.
      Thanks
      Regards

      Rahul

      # 9958495609

      Aman replied 9 years, 1 month ago 3 Members · 2 Replies
      • Account
      • Contact Record
      • Developer Console
      • Salesforce Apex
      • Salesforce SOQL
      • SOQL
      • SOQL Query
    • 2 Replies
    • Shaharyar

      Member
      August 10, 2017 at 5:12 AM

      Hello Rahul,

      #Apex Class

      public class AccountAndContacts {

      public static void displayContacts(){
      List<Account> acc=[Select Id,Name from Account];
      for(Account a:acc){
      List<Contact> con=[Select LastName from contact where AccountId=:a.id];
      for(Contact c:con){
      if(con.size()>1){
      System.debug('AccountName '+''+a.Name+'RelatedContacts '+''+c.LastName);
      }}}}}

       

       

      #Anonymous Window

      AccountAndContacts.displayContacts();

       

      #Check the output in debugger log...

      Hope it will help you.

      Note:I have written it in simple understandable ,way you can optimize code by using nested query.

    • [adinserter block='9']
    • Aman

      Member
      August 28, 2017 at 10:54 AM

      Hello Rahul ,

      you can try this :

      public class SearchCont {
      public static List<Account> acct;
      public static List<AggregateResult> aggRes;
      public static void contSearch(){
      List<String> ide=new List<String>();
      aggRes =[select accountId,count(id) from contact where account.name !=null group by accountId having count(id) >1];
      for (AggregateResult ar : aggRes) {
      ide.add((String)ar.get('accountId'));
      }
      acct=[select name,(select LastName,FirstName from contacts) from account where id in :ide ];
      for(account acc: acct){
      system.debug('###'+acc);
      for(contact con: acc.contacts){
      system.debug('###'+con.LastName);
      system.debug('###'+con.FirstName);

      }
      }
      }
      }

    Log In to reply.

    • Public
    • All Members
    • My Connections
    • Only Me
    • Public
    • All Members
    • My Connections
    • Only Me
    • Public
    • All Members
    • My Connections
    • Only Me

    Popular Salesforce Blogs

    platform events

    Platform Events in Salesforce - An Introduction

    Blog in Salesforce, Salesforce Tutorial

    Platform Event A platform event is much the same as another custom item yet this would just be alluded by outside systems to speak with…

    API Clients, API Name Postfix, Business Cycle, Business Procedure, Client Activities
    Ayush Sep 25, 2020
    8,594  Views
    Web to Lead

    Web to Lead Guide: All you Need to Know | Salesforce Guide

    Blog in Salesforce

    Are you still updating the new leads manually on Salesforce or your CRM? Is your Sales team occupied with tasks that are not that fruitful? Stop…

    Business, Create a Web to Lead, CRM, HTML Code, Landing Pages
    SP Apr 14, 2022
    2,304  Views
    Your 1st Step to Become a Salesforce Certified Platform Developer 1 - Exam Notes

    Your 1st Step to become a Salesforce Certified Platform Developer 1 [Exam Notes]

    Blog in Salesforce Certifications

    Are you one among the many who endeavour to become a proficient Salesforce Developer? Then, this would be your first step after the completion of…

    App Builder Certification, Certification Trailblazer, Certified Platform Developer 1, Newbie Salesforce Developer, Salesforce Certifications
    Guha Feb 6, 2018
    8,421  Views

    Popular Salesforce Videos

    Security in Salesforce - What Developers Must Know

    Security in Salesforce - What Developers Must Know

    Video in Salesforce Security

    Security in Salesforce 0:00​ Introduction 3:00​ Agenda 4:05​ Data security 5:18​ User Mode & System Mode 9:13​ CRUD & FLS Data Model 10:45​ Enforcing CRUD…

    Salesforce Training, Apex Class, Salesforce Lightning, Salesforce Developers, Salesforce Video
    Prafull May 4, 2021
    2,036  Views
    Types of Generative AI Models | How Does Generative AI Work?

    Types of Generative AI Models | How Does Generative AI Work?

    Video in Others

    Have you ever wondered how AI can be so creative? How do they generate art, music, and even text all on their own? Look no…

    salesforce, Salesforce Community, Salesforce Webinar, Webinar, Live Webinar
    Cyntexa Sep 5, 2025
    325  Views
    See the Future of APIs at Salesforce

    See the Future of APIs at Salesforce

    Video in Salesforce Apex, Salesforce Training

    Salesforce APIs are a way for other applications (or code in other applications) to programmatically access data within your Salesforce org, in a simple and…

    Salesforce Training, salesforce, Salesforce Applications, Salesforce Org, Salesforce Video
    Prafull Apr 7, 2021
    2,555  Views
    Footer Forcetalks logo

    support@forcetalks.com

    • twitterx

    Quick Links

    Advertise with Us

    Salesforce® Articles

    Dreamforce 2023

    Top Salesforce® Bloggers 2023

    Top Salesforce Consultants

    Get Listed

    Company

    Contact Us

    About Us

    Privacy Policy

    Terms & Conditions

    InsightHub

    Salesforce Blogs

    Salesforce Videos

    Salesforce Groups

    Salesforce Jobs

    © 2026 - Forcetalks ● All Rights Reserved

    Salesforce® is a trademark of Salesforce® Inc. No claim is made to the exclusive right to use “Salesforce”. Any services offered within the Forcetalks website/app are not sponsored or endorsed by Salesforce®.

    We use cookies to enhance your browsing experience. Please see our privacy policy if you'd like more information on our use of cookies.