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

  • Home
  • SEARCH
  • 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 8540537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:37:14+00:00 2026-06-11T11:37:14+00:00

I have an objects table and votes table. I am currently grouping them and

  • 0

I have an objects table and votes table. I am currently grouping them and sorting by most recent vote:

SELECT objects.id, objects.name, MAX(votes.created_at) AS mv 
       FROM "objects"
       INNER JOIN "votes" ON "votes"."object_id" = "objects"."id" 
       GROUP BY objects.id, objects.name 
       ORDER BY mv DESC

However, this only returns the objects that have votes. I searched on SO and found answers regarding how to include results that don’t exist, but I’m looking to include both those that exist and those that do not.

Is there a way to tack onto the end of the above all of the objects that don’t have votes as well?

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

    With a LEFT JOIN, NULL values are filled in where no matching rows are found – as you found out yourself:

    super frustrating because it works perfectly, except all of the
    entries without votes are at the beginning!

    Now, NULL is normally sorted last. But reversed with DESC it comes first. In Postgres you can use NULLS LAST to explicitly sort NULL to the end (since version 8.3):

    
    SELECT o.id, o.name, max(v.created_at) AS lastest_vote
    FROM   objects o
    LEFT   JOIN votes v ON v.object_id = o.id 
    GROUP  BY o.id, o.name
    ORDER  BY lastest_vote DESC NULLS LAST, o.name; -- name only as tiebreaker

    If objects.id is the primary key you can simplify the GROUP BY (in Postgres 9.1 or later):

    GROUP  BY o.id -- primary key covers all columns of this table
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say, I have a table objects . It has fields id , name
Have this objects and table types: CREATE TYPE Person_typ AS OBJECT ( name CHAR(20),
I have an objects, tags, and tag_relationships table. Objects and tags each have an
I have a table that describes a variety of objects in my system (ie.
So, I have two objects: A table with a number of rows and columns.
I have a MySQL table for persistence of some Java objects and use Hibernate
I have an UDF that selects top 6 objects from a table (with a
I am using LINQ to Entites where I have two table objects ( ReferenceCodes
data.table objects now have a := operator. What makes this operator different from all
Suppose I have the following objects (one table per object) with this relations: A

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.