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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:23:36+00:00 2026-06-09T20:23:36+00:00

I have a table categories and table posts . I want to return categories

  • 0

I have a table categories and table posts . I want to return categories that have more than 3 posts.

My query

SELECT `categories`.`category_title`, COUNT(posts.post_id) as total_posts 
FROM (`categories`) 
JOIN `posts` ON `posts`.`category_id` = `categories`.`category_id` 
HAVING `total_posts` > 3 
ORDER BY `categories`.`date_created` desc

it returns just 1 row.. What is the correct way to do this type of query without using 2 queries?

  • 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-09T20:23:38+00:00Added an answer on June 9, 2026 at 8:23 pm

    Your query is making use of a MySQL feature called “hidden columns” and you might not even know it. This is because your query is referencing elements, such as date_created, which should be aggregated but are not (“should” here means according to the SQL standard and most other databases).

    The problem with your query is that it is missing the group by. An alternative way of writing this is with the aggregation in a subquery, before joining to category:

    SELECT `categories`.`category_title`, total_posts 
    FROM `categories` JOIN
         (select categoryid, COUNT(posts.post_id) as total_posts
          from `posts`
          group by categoryid
          having count(*) > 3
         ) pc
         ON `pc`.`category_id` = `categories`.`category_id`
    ORDER BY `categories`.`date_created` desc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two table posts & categories post_id | post_title | post_content | post_cat
I have a table for posts and a table for categories, what i want
I have categories table: cat_id cat_name Than categories - posts relationship table cat_rel :
If I have a table of categories with a featured row that is default
I have a Categories table in which each category has a ParentId that can
I have a database table named Categories . I want to create a form
Greetings, fellow coders! I have this table that contains categories and subcategories (actually I
I'm making a sitemap. I have a table, which connects posts with categories (2
i need help in sql query i have table named -- threads contain post_id
e.g. If I have a table like below:- create table Categories(CategoryId int primary key,

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.