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 8940341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:00:45+00:00 2026-06-15T11:00:45+00:00

I am learning SQL with PostgreSQL, and have run into a problem regarding nested

  • 0

I am learning SQL with PostgreSQL, and have run into a problem regarding nested Aggregate functions.

I am trying to find the details of private owners with the maximum amount of properties, where I have two relations, privateowner and propertyforrent with propertyforrent having a foreign key, ownwerno.

I suspect that my issue is where I am attempting to nest Aggregate Functions, but I cannot see a way around it.

NOTE:- the database I am using has a typo in the attribute ownwerno in propertyforrent, where it should be ownerno.

The code I am attempting to use is shown below~:-

SELECT o.fname, o.lname, telno
FROM privateowner o
WHERE o.ownerno = (SELECT p.ownwerno
           FROM propertyforrent p
           HAVING COUNT(p.ownwerno) = MAX(COUNT(o.ownerno)));

Its accompanying error is as follows:-

ERROR:  column "p.ownwerno" must appear in the GROUP BY clause or be used in a
aggregate function
LINE 3: WHERE o.ownerno = (SELECT p.ownwerno
                                  ^


********** Error **********

ERROR: column "p.ownwerno" must appear in the GROUP BY clause or be used in an
aggregate function
SQL state: 42803
Character: 78

Any insight would be wonderful.

  • 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-15T11:00:46+00:00Added an answer on June 15, 2026 at 11:00 am

    PostgreSQL 9.1 Schema Setup:

    create table privateowner(ownerno integer, fname text);
    insert into privateowner(ownerno, fname) values (1,'Alice'), 
                                                    (2,'Bob'), 
                                                    (3,'Charlie');
    
    create table propertyforrent(ownerno integer);
    insert into propertyforrent(ownerno) values (1), (2), (2), (3), (3);
    

    Query 1:

    with w as ( select ownerno, count(*) as property_count 
                from propertyforrent 
                group by ownerno )
    select * 
    from privateowner
    where ownerno in( select ownerno 
                      from w 
                      where property_count=( select property_count
                                             from w 
                                             order by 1 desc limit 1) )
    

    Results:

    | OWNERNO |   FNAME |
    ---------------------
    |       2 |     Bob |
    |       3 | Charlie |
    

    see this on SQL Fiddle


    inspired by @araqnid’s answer (+1), here is another variant with windowing functions:

    Query:

    select ownerno, fname
    from( select ownerno, fname, rank() over (order by count(*) desc) rnk
          from privateowner join propertyforrent using(ownerno) 
          group by ownerno, fname ) z
    where rnk=1
    

    Results:

    | OWNERNO |   FNAME |
    ---------------------
    |       3 | Charlie |
    |       2 |     Bob |
    

    see this on SQL Fiddle

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

Sidebar

Related Questions

I'm learning some more SQL, and ran into a problem, I have two tabels
I am learning SQL and I have problem with building a query and I
I am learning SQL and am trying to learn JOINs this week. I have
I am trying to run this code below while learning the java sql stuff,
I've been learning SQL for about a day now and I've run into a
I'm in the process of learning Databases and SQL. From what I have read
I have a small test database I'm using for learning SQL. There's a Duel
I am just learning sql and starting with PostgreSQL. Here is what I am
Hello I am just learning postGIS and thus postgresql (9.1) and am trying to
I have been slowly learning SQL the last few weeks. I've picked up all

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.