Query to find customers that have deposit in all branches located in a specific city





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{
margin-bottom:0;
}








3















I have 3 tables as follow:



Customer (CustomerName , CustomerCity , CustomerStreet)
Branch (BranchName , BranchCity)
Deposit (CustomerName , BranchName , AccountNumber , Balance)


Here I show you sample data of the above tables :



Customer ('John','City1','Street1')
('Lili','City2','street2')

Branch ('Branch1','Kaj')
('Branch2','Bahar')
('Branch3','Bahar')
('Branch4','Bahar')

Deposit ('John','Branch1',12,1000)
('Lili','Branch1',123,1222)
('Lili','Branch2',124,45233)
('Lili','Branch3',345,6000)
('Lili','Branch4',567,5600)


So based on the information above, I need a query to give me CustomerName
that have a deposit in all branches that are in Bahar.



I should see 'Lili' in the output because it has deposits for all branches that are in Bahar but I should not see John cause it has a deposit only in one branch of Bahar.



I did my best to write the query but I could not. Could you help me with this please?










share|improve this question

































    3















    I have 3 tables as follow:



    Customer (CustomerName , CustomerCity , CustomerStreet)
    Branch (BranchName , BranchCity)
    Deposit (CustomerName , BranchName , AccountNumber , Balance)


    Here I show you sample data of the above tables :



    Customer ('John','City1','Street1')
    ('Lili','City2','street2')

    Branch ('Branch1','Kaj')
    ('Branch2','Bahar')
    ('Branch3','Bahar')
    ('Branch4','Bahar')

    Deposit ('John','Branch1',12,1000)
    ('Lili','Branch1',123,1222)
    ('Lili','Branch2',124,45233)
    ('Lili','Branch3',345,6000)
    ('Lili','Branch4',567,5600)


    So based on the information above, I need a query to give me CustomerName
    that have a deposit in all branches that are in Bahar.



    I should see 'Lili' in the output because it has deposits for all branches that are in Bahar but I should not see John cause it has a deposit only in one branch of Bahar.



    I did my best to write the query but I could not. Could you help me with this please?










    share|improve this question





























      3












      3








      3


      1






      I have 3 tables as follow:



      Customer (CustomerName , CustomerCity , CustomerStreet)
      Branch (BranchName , BranchCity)
      Deposit (CustomerName , BranchName , AccountNumber , Balance)


      Here I show you sample data of the above tables :



      Customer ('John','City1','Street1')
      ('Lili','City2','street2')

      Branch ('Branch1','Kaj')
      ('Branch2','Bahar')
      ('Branch3','Bahar')
      ('Branch4','Bahar')

      Deposit ('John','Branch1',12,1000)
      ('Lili','Branch1',123,1222)
      ('Lili','Branch2',124,45233)
      ('Lili','Branch3',345,6000)
      ('Lili','Branch4',567,5600)


      So based on the information above, I need a query to give me CustomerName
      that have a deposit in all branches that are in Bahar.



      I should see 'Lili' in the output because it has deposits for all branches that are in Bahar but I should not see John cause it has a deposit only in one branch of Bahar.



      I did my best to write the query but I could not. Could you help me with this please?










      share|improve this question
















      I have 3 tables as follow:



      Customer (CustomerName , CustomerCity , CustomerStreet)
      Branch (BranchName , BranchCity)
      Deposit (CustomerName , BranchName , AccountNumber , Balance)


      Here I show you sample data of the above tables :



      Customer ('John','City1','Street1')
      ('Lili','City2','street2')

      Branch ('Branch1','Kaj')
      ('Branch2','Bahar')
      ('Branch3','Bahar')
      ('Branch4','Bahar')

      Deposit ('John','Branch1',12,1000)
      ('Lili','Branch1',123,1222)
      ('Lili','Branch2',124,45233)
      ('Lili','Branch3',345,6000)
      ('Lili','Branch4',567,5600)


      So based on the information above, I need a query to give me CustomerName
      that have a deposit in all branches that are in Bahar.



      I should see 'Lili' in the output because it has deposits for all branches that are in Bahar but I should not see John cause it has a deposit only in one branch of Bahar.



      I did my best to write the query but I could not. Could you help me with this please?







      sql-server t-sql query






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 28 at 10:40









      Michael Green

      16.3k8 gold badges36 silver badges73 bronze badges




      16.3k8 gold badges36 silver badges73 bronze badges










      asked May 27 at 5:56









      PTTTPTTT

      1601 silver badge10 bronze badges




      1601 silver badge10 bronze badges

























          4 Answers
          4






          active

          oldest

          votes


















          6
















          I have two solutions for your question :
          solution 1 , I use Not Exists , Except



          Select distinct C.CustomerName
          From customers C
          where NOT EXISTS ((Select BranchName
          From Branch
          Where BranchCity='Bahar')

          EXCEPT

          (Select BranchName
          From Deposit a
          where A.CustomerName = C.CustomerName))


          In solution 2 I use Not Exists ,



          Select Distinct C.CustomerName
          From Customers C
          Where NOT EXISTS (Select BranchName
          From Branch b
          Where BranchCity='Bahar'
          and NOT EXISTS (select BranchName
          from Deposit a
          where a.CustomerName=C.CustomerName
          and a.BranchName=b.branchName))


          Each one of the query above will answer your query and give you the desired result.






          share|improve this answer



































            0
















            select CustomerName
            from Deposit
            group by CustomerName
            having count(distinct BranchName) = (select count(*) from Branch)


            Note that distinct is only used if there are multiple deposits by the same customer and branch.
            However, it's possible to use join with Customer table or nested lookup queries for more information.






            share|improve this answer



































              0
















              ;with cte as(
              select c.Name as Cname,b.Name as bName,
              ROW_NUMBER() over(partition by c.Name order by b.Name,c.Name) RowID
              from Customer c
              inner join Deposit d on c.Name=d.CustomerName
              inner join Branch b on b.Name=d.Branch
              where b.City='Bahar'
              )
              select Cname from cte
              group by cName
              having max(RowID)=(select count(*) from Branch where City='Bahar')





              share|improve this answer

































                -1
















                SELECT c.CustomerName
                FROM Customer c
                CROSS JOIN Branch b
                LEFT JOIN Deposit d
                ON d.CustomerName = c.CustomerName
                AND d.BranchName = b.BranchName
                WHERE b.BranchCity = 'Bahar'
                GROUP BY c.CustomerName
                HAVING SUM(CASE WHEN d.AccountNumber IS NULL THEN 1 ELSE 0 END) = 0





                share|improve this answer



























                  Your Answer








                  StackExchange.ready(function() {
                  var channelOptions = {
                  tags: "".split(" "),
                  id: "182"
                  };
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function() {
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled) {
                  StackExchange.using("snippets", function() {
                  createEditor();
                  });
                  }
                  else {
                  createEditor();
                  }
                  });

                  function createEditor() {
                  StackExchange.prepareEditor({
                  heartbeatType: 'answer',
                  autoActivateHeartbeat: false,
                  convertImagesToLinks: false,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: null,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader: {
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  },
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  });


                  }
                  });















                  draft saved

                  draft discarded
















                  StackExchange.ready(
                  function () {
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f239115%2fquery-to-find-customers-that-have-deposit-in-all-branches-located-in-a-specific%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  4 Answers
                  4






                  active

                  oldest

                  votes








                  4 Answers
                  4






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  6
















                  I have two solutions for your question :
                  solution 1 , I use Not Exists , Except



                  Select distinct C.CustomerName
                  From customers C
                  where NOT EXISTS ((Select BranchName
                  From Branch
                  Where BranchCity='Bahar')

                  EXCEPT

                  (Select BranchName
                  From Deposit a
                  where A.CustomerName = C.CustomerName))


                  In solution 2 I use Not Exists ,



                  Select Distinct C.CustomerName
                  From Customers C
                  Where NOT EXISTS (Select BranchName
                  From Branch b
                  Where BranchCity='Bahar'
                  and NOT EXISTS (select BranchName
                  from Deposit a
                  where a.CustomerName=C.CustomerName
                  and a.BranchName=b.branchName))


                  Each one of the query above will answer your query and give you the desired result.






                  share|improve this answer
































                    6
















                    I have two solutions for your question :
                    solution 1 , I use Not Exists , Except



                    Select distinct C.CustomerName
                    From customers C
                    where NOT EXISTS ((Select BranchName
                    From Branch
                    Where BranchCity='Bahar')

                    EXCEPT

                    (Select BranchName
                    From Deposit a
                    where A.CustomerName = C.CustomerName))


                    In solution 2 I use Not Exists ,



                    Select Distinct C.CustomerName
                    From Customers C
                    Where NOT EXISTS (Select BranchName
                    From Branch b
                    Where BranchCity='Bahar'
                    and NOT EXISTS (select BranchName
                    from Deposit a
                    where a.CustomerName=C.CustomerName
                    and a.BranchName=b.branchName))


                    Each one of the query above will answer your query and give you the desired result.






                    share|improve this answer






























                      6














                      6










                      6









                      I have two solutions for your question :
                      solution 1 , I use Not Exists , Except



                      Select distinct C.CustomerName
                      From customers C
                      where NOT EXISTS ((Select BranchName
                      From Branch
                      Where BranchCity='Bahar')

                      EXCEPT

                      (Select BranchName
                      From Deposit a
                      where A.CustomerName = C.CustomerName))


                      In solution 2 I use Not Exists ,



                      Select Distinct C.CustomerName
                      From Customers C
                      Where NOT EXISTS (Select BranchName
                      From Branch b
                      Where BranchCity='Bahar'
                      and NOT EXISTS (select BranchName
                      from Deposit a
                      where a.CustomerName=C.CustomerName
                      and a.BranchName=b.branchName))


                      Each one of the query above will answer your query and give you the desired result.






                      share|improve this answer















                      I have two solutions for your question :
                      solution 1 , I use Not Exists , Except



                      Select distinct C.CustomerName
                      From customers C
                      where NOT EXISTS ((Select BranchName
                      From Branch
                      Where BranchCity='Bahar')

                      EXCEPT

                      (Select BranchName
                      From Deposit a
                      where A.CustomerName = C.CustomerName))


                      In solution 2 I use Not Exists ,



                      Select Distinct C.CustomerName
                      From Customers C
                      Where NOT EXISTS (Select BranchName
                      From Branch b
                      Where BranchCity='Bahar'
                      and NOT EXISTS (select BranchName
                      from Deposit a
                      where a.CustomerName=C.CustomerName
                      and a.BranchName=b.branchName))


                      Each one of the query above will answer your query and give you the desired result.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited May 28 at 11:40

























                      answered May 27 at 6:08









                      Pantea TourangPantea Tourang

                      56716 bronze badges




                      56716 bronze badges




























                          0
















                          select CustomerName
                          from Deposit
                          group by CustomerName
                          having count(distinct BranchName) = (select count(*) from Branch)


                          Note that distinct is only used if there are multiple deposits by the same customer and branch.
                          However, it's possible to use join with Customer table or nested lookup queries for more information.






                          share|improve this answer
































                            0
















                            select CustomerName
                            from Deposit
                            group by CustomerName
                            having count(distinct BranchName) = (select count(*) from Branch)


                            Note that distinct is only used if there are multiple deposits by the same customer and branch.
                            However, it's possible to use join with Customer table or nested lookup queries for more information.






                            share|improve this answer






























                              0














                              0










                              0









                              select CustomerName
                              from Deposit
                              group by CustomerName
                              having count(distinct BranchName) = (select count(*) from Branch)


                              Note that distinct is only used if there are multiple deposits by the same customer and branch.
                              However, it's possible to use join with Customer table or nested lookup queries for more information.






                              share|improve this answer















                              select CustomerName
                              from Deposit
                              group by CustomerName
                              having count(distinct BranchName) = (select count(*) from Branch)


                              Note that distinct is only used if there are multiple deposits by the same customer and branch.
                              However, it's possible to use join with Customer table or nested lookup queries for more information.







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited May 27 at 9:46

























                              answered May 27 at 8:08









                              Amir BahramiAmir Bahrami

                              12 bronze badges




                              12 bronze badges


























                                  0
















                                  ;with cte as(
                                  select c.Name as Cname,b.Name as bName,
                                  ROW_NUMBER() over(partition by c.Name order by b.Name,c.Name) RowID
                                  from Customer c
                                  inner join Deposit d on c.Name=d.CustomerName
                                  inner join Branch b on b.Name=d.Branch
                                  where b.City='Bahar'
                                  )
                                  select Cname from cte
                                  group by cName
                                  having max(RowID)=(select count(*) from Branch where City='Bahar')





                                  share|improve this answer






























                                    0
















                                    ;with cte as(
                                    select c.Name as Cname,b.Name as bName,
                                    ROW_NUMBER() over(partition by c.Name order by b.Name,c.Name) RowID
                                    from Customer c
                                    inner join Deposit d on c.Name=d.CustomerName
                                    inner join Branch b on b.Name=d.Branch
                                    where b.City='Bahar'
                                    )
                                    select Cname from cte
                                    group by cName
                                    having max(RowID)=(select count(*) from Branch where City='Bahar')





                                    share|improve this answer




























                                      0














                                      0










                                      0









                                      ;with cte as(
                                      select c.Name as Cname,b.Name as bName,
                                      ROW_NUMBER() over(partition by c.Name order by b.Name,c.Name) RowID
                                      from Customer c
                                      inner join Deposit d on c.Name=d.CustomerName
                                      inner join Branch b on b.Name=d.Branch
                                      where b.City='Bahar'
                                      )
                                      select Cname from cte
                                      group by cName
                                      having max(RowID)=(select count(*) from Branch where City='Bahar')





                                      share|improve this answer













                                      ;with cte as(
                                      select c.Name as Cname,b.Name as bName,
                                      ROW_NUMBER() over(partition by c.Name order by b.Name,c.Name) RowID
                                      from Customer c
                                      inner join Deposit d on c.Name=d.CustomerName
                                      inner join Branch b on b.Name=d.Branch
                                      where b.City='Bahar'
                                      )
                                      select Cname from cte
                                      group by cName
                                      having max(RowID)=(select count(*) from Branch where City='Bahar')






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered May 28 at 12:24









                                      Asrar Ahmad EhsanAsrar Ahmad Ehsan

                                      991 silver badge9 bronze badges




                                      991 silver badge9 bronze badges


























                                          -1
















                                          SELECT c.CustomerName
                                          FROM Customer c
                                          CROSS JOIN Branch b
                                          LEFT JOIN Deposit d
                                          ON d.CustomerName = c.CustomerName
                                          AND d.BranchName = b.BranchName
                                          WHERE b.BranchCity = 'Bahar'
                                          GROUP BY c.CustomerName
                                          HAVING SUM(CASE WHEN d.AccountNumber IS NULL THEN 1 ELSE 0 END) = 0





                                          share|improve this answer






























                                            -1
















                                            SELECT c.CustomerName
                                            FROM Customer c
                                            CROSS JOIN Branch b
                                            LEFT JOIN Deposit d
                                            ON d.CustomerName = c.CustomerName
                                            AND d.BranchName = b.BranchName
                                            WHERE b.BranchCity = 'Bahar'
                                            GROUP BY c.CustomerName
                                            HAVING SUM(CASE WHEN d.AccountNumber IS NULL THEN 1 ELSE 0 END) = 0





                                            share|improve this answer




























                                              -1














                                              -1










                                              -1









                                              SELECT c.CustomerName
                                              FROM Customer c
                                              CROSS JOIN Branch b
                                              LEFT JOIN Deposit d
                                              ON d.CustomerName = c.CustomerName
                                              AND d.BranchName = b.BranchName
                                              WHERE b.BranchCity = 'Bahar'
                                              GROUP BY c.CustomerName
                                              HAVING SUM(CASE WHEN d.AccountNumber IS NULL THEN 1 ELSE 0 END) = 0





                                              share|improve this answer













                                              SELECT c.CustomerName
                                              FROM Customer c
                                              CROSS JOIN Branch b
                                              LEFT JOIN Deposit d
                                              ON d.CustomerName = c.CustomerName
                                              AND d.BranchName = b.BranchName
                                              WHERE b.BranchCity = 'Bahar'
                                              GROUP BY c.CustomerName
                                              HAVING SUM(CASE WHEN d.AccountNumber IS NULL THEN 1 ELSE 0 END) = 0






                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered May 28 at 9:35









                                              VisakhVisakh

                                              11 bronze badge




                                              11 bronze badge


































                                                  draft saved

                                                  draft discarded



















































                                                  Thanks for contributing an answer to Database Administrators Stack Exchange!


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid



                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.


                                                  To learn more, see our tips on writing great answers.




                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function () {
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f239115%2fquery-to-find-customers-that-have-deposit-in-all-branches-located-in-a-specific%23new-answer', 'question_page');
                                                  }
                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown







                                                  Popular posts from this blog

                                                  He _____ here since 1970 . Answer needed [closed]What does “since he was so high” mean?Meaning of “catch birds for”?How do I ensure “since” takes the meaning I want?“Who cares here” meaningWhat does “right round toward” mean?the time tense (had now been detected)What does the phrase “ring around the roses” mean here?Correct usage of “visited upon”Meaning of “foiled rail sabotage bid”It was the third time I had gone to Rome or It is the third time I had been to Rome

                                                  Bunad

                                                  Færeyskur hestur Heimild | Tengill | Tilvísanir | LeiðsagnarvalRossið - síða um færeyska hrossið á færeyskuGott ár hjá færeyska hestinum