Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8340081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:04:33+00:00 2026-06-09T05:04:33+00:00

I need make such request: choose list of cities which include letter i and

  • 0

I need make such request: choose list of cities which include letter i and have appropriate number of employees. Also I need take to attention that some city could have 0 employee and that for some employee field DEBT could be null.

Table depart

debt    city
-------------
43      odesa
23      kiev
79      lviv
78      lviv
12      rivne

Table empl

ide     fn      ln      debt
----------------------------
3421    jed     trt     43
354     jed     res     43
43      ged     hjkhg   79
73      ghghg   gfgf    79
456     jkl     gdfg    
532     kkhg    vjv     23
45      ki      vt      
243     ki      vt      78

I wrote this query:

select depart.CITY, count (*) as numb 
from depart 
   inner join empl on  empl.DEBT=depart.DEBT
where depart.CITY like '%i%'
group by depart.CITY;

But I do not know how take care about that some city could have 0 employee (for example this request does not show city rivne which have 0 employee) and that for some employee field DEBT could be null.

I use oracle with toad.

Expected results

city    numb
kiev    1
rivne   0
lviv    3
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T05:04:36+00:00Added an answer on June 9, 2026 at 5:04 am

    To take care of criterion move where before group by. To allow for non-existing employees or null DEBT, use left outer join.

    select depart.CITY, count (empl.DEBT) as numb 
      from depart 
      left join empl 
        on empl.DEBT = depart.DEBT
     where depart.CITY like '%i%'
     group by depart.CITY;
    

    To get all employees grouped by department you would reverse the outer join:

    select depart.CITY, count (empl.ide) as numb 
      from empl  
      left join depart
        on empl.DEBT = depart.DEBT
     -- Note: condition is now part of a join. This is required
     --       as part of outer join because otherwise left hand
     --       table row would be filtered out.
       and depart.CITY like '%i%'
     group by depart.CITY;
    

    (Presumably ide is primary key of employee).
    But this would not return departments without employees, and CITY would be null where department’s city would not match %i% or empl.DEBT would be null from the start.

    To solve the problem, one might extend first query with union all designed to retrieve employees without departments. But there is a question: do we want only departmentless employees, or we consider or employees not working in a city whose name contains i departmentless also. I’ve opted for second possibility.

    select depart.CITY, count (empl.DEBT) as numb 
      from depart 
      left join empl 
        on empl.DEBT = depart.DEBT
     where depart.CITY like '%i%'
     group by depart.CITY;
    select '(unknown or unmatched city)', count (*) as numb
      from empl
      left join depart
        on empl.DEBT = depart.DEBT
       and depart.CITY like '%i%'
     where depart.DEBT is null;
    

    If you need distinction between employees without departments and those who work in a city not containing i, you might use case:

    select case when depart.CITY like '%i%'
                then depart.CITY
                when depart.DEBT is null
                then '(No department)'
                else '(City does not match %i%)'
            end as CITY,
           count (*) as numb
      from empl
      left join depart
        on empl.DEBT = depart.DEBT
     group by 
           case when depart.CITY like '%i%'
                then depart.CITY
                when depart.DEBT is null
                then '(No department)'
                else '(City does not match %i%)'
            end
    

    This query will count employees from matching cities, unmatching cities and those who have no department.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So... I have a URL like www.example.com/stream/ I need to make any request to
I have such a problem. I have class, in which I make a call
I have a problem.. in my project, i need to make an AJAX request
I need to make efficient d-dimensional points searching and also make efficient k-NN queries
I need to make a series (1-20) ajax calls and I need to have
I need to make an HTTP POST request from outside the browser, but the
I need to make an AJAX request from a website to a REST web
I need to make a list of key-value pairs (similar to std::map<std::string, std::string> )
My company is looking to develop an iOS application which would need to make
I have 3 user controls, each one of which will need to pull data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.