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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:14:41+00:00 2026-05-27T08:14:41+00:00

I have these tables: CREATE TABLE User( Username varchar(15) NOT NULL, Name varchar(20) DEFAULT

  • 0

I have these tables:

CREATE TABLE User( 
Username varchar(15) NOT NULL, 
Name varchar(20) DEFAULT '', 
Surname varchar(20) DEFAULT '', 
Email varchar(30) DEFAULT '', 
City varchar(20) DEFAULT '', 
Description varchar(1000) DEFAULT '', 
userID INTEGER NOT NULL, 
Primary key(Username, userID), 
Foreign key(userID) references Followers(userID) 
);"

CREATE TABLE Followers( 
userID INTEGER NOT NULL, 
usernameFollower varchar(15), 
Primary key(userID), 
Foreign key(usernameFollower) references User(Username) 
);"

CREATE TABLE Post( 
Username varchar(15) NOT NULL, 
userID INTEGER NOT NULL, 
Date DATETIME NOT NULL, 
Message VARCHAR(500) NOT NULL, 
Primary key(Username, Date), 
Foreign key(Username) references User(Username) 
);"

I need to get with a query a list with all the users in User, and for each user the date of the latest post they made and the number of followers they have.

For example, the output have to be like this:

USERNAME        LATEST POST                 FOLLOWERS
user1           2011/11/11 18:30:11         5
user2           2011/10/05 17:30:00         1
user3           2010/05/11 18:30:11         90
user4           2011/11/11 18:30:11         5

I can’t figure how to make it..I presume for the count of the followers I need to use COUNT(), but how can I link it to the latest post of the user? Is possible in just one query? Or do I need to use views?

Thanks in advance, best regards.

  • 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-05-27T08:14:41+00:00Added an answer on May 27, 2026 at 8:14 am

    You need to join across all 3 tables via the userID. You can then select the latest post date by grouping by user id. Originally, I also joined onto the Followers table, but this has an unintended side-effect, so…

    Something like:

    SELECT U.Name AS UserName,
           MAX(P.Date) AS LatestPost,
           (SELECT COUNT(F.userNameFollower) FROM Followers WHERE userID = U.userID) AS FollowerCount
    FROM User U
    INNER JOIN Post P ON (U.userID = P.userID)
    
    GROUP BY U.UserID
    

    Should work.

    Please note that my query is assuming that a user has posted. If they haven’t then change the INNER JOIN to a LEFT JOIN.

    The reason the original query doesn’t work is that it matches a row in Post for each user, and a row in Followers for each user – thus multiplying the number of results. Instead, we can use a subquery to select the count of followers with the matching userid in the User table.

    HOWEVER… this only counts the total number of followers a user has, not the number of followers for a post. The reason for this is because you have only linked a follower to a user. You’d also need to have a relationship between Follower and Post.

    You can do this in a number of ways; you can create a PostFollower table consisting of PostId and UserId (so you can see which user has followed each post) or you can add a PostId to the Follower table, and store the follower userId and the PostId, allowing you to store which user followed which posts.

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

Sidebar

Related Questions

So I have three tables: CREATE TABLE tblUser ( [pkUserID] [int] IDENTITY(1,1) NOT NULL,
I have these tables: customer -------- customer_id int name varchar(255) order ----- order_id int
Suppose you have these tables: Table Name: Salesman Fields: S_ID(Primary Key), Name Table Name:
If I have these two tables: <table> <tr> <td>Small Length Content</td> </tr> </table> <table>
Here is my USER table CREATE TABLE IF NOT EXISTS `users` ( `id` int(11)
I have table user which have fields username , password , and type .
Say I have a table like this: create table users ( user_id int not
I have a User table defined like this: CREATE TABLE Users( UserId int IDENTITY(1,1)
Suppose that I have 2 tables: [User] - UserID - Username [Task] - TaskID
Hi there I have an SQL table that looks like this: CREATE TABLE IF

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.