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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:44:17+00:00 2026-05-24T17:44:17+00:00

I have one sql table that looks like this called posts: id | user

  • 0

I have one sql table that looks like this called “posts”:

id | user
--------------------------------
0  | tim
1  | tim
2  | bob

And another called “votes” that stores either upvotes or downvotes on the posts in the “posts” table:

id | postID | type
--------------------------------
0  | 0      | 0
1  | 2      | 1
2  | 0      | 1
3  | 0      | 1
4  | 3      | 0

In this table, the ‘type’ is either a 0 for downvote or 1 for upvote.

How would I go about ordering posts by “tim” by the number of (upvotes – downvotes) the post has?

  • 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-24T17:44:18+00:00Added an answer on May 24, 2026 at 5:44 pm
    SELECT
      p.id,
      p.user,
      SUM(v.type * 2 - 1) AS votecount
    FROM posts p
      LEFT JOIN votes v ON p.id = v.postID
    WHERE p.user = 'tim'
    GROUP BY p.id, p.user
    ORDER BY votes DESC
    

    UPDATE – p and v explained.

    In this query, p and v are aliases of, respectively, posts and votes. An alias is essentially an alternative name and it is defined only within the scope of the statement that declares it (in this case, the SELECT statement). Not only a table can have an alias, but a column too. In this query, votecount is an alias of the column represented by the SUM(v.type * 2 - 1) expression. But presently we are talking only about tables.

    Before I go on with explanation about table aliases, I’ll briefly explain why you may need to prefix column names with table names, like posts.id as opposed to just id. Basically, when a query references more than one table, like in this case, you may find it quite useful always to prefix column names with the respective table names. That way, when you are revisiting an old script, you can always tell which column belongs to which table without having to look up the structures of the tables referenced. Also it is mandatory to include the table reference when omitting it creates ambiguity as to which table the column belongs to. (In this case, referencing the id column without referencing the posts table does create ambiguous situation, because each table has got their own id.)

    Now, a large and complex query may be difficult to read when you write out complete table names before column names. This is where (short) aliases come in handy: they make a query easier to read and understand, although I’ve already learnt that not all people share that opinion, and so you should judge for yourself: this question contains two versions of the same query, one with long-named table references and the other with short-aliased ones, as well as an opinion (in a comment to one of the answers) why aliases are not suitable.

    Anyway, using short table aliases in this particular query may not be as beneficial as in some more complex statements. It’s just that I’m used to aliasing tables whenever the query references more than one.

    This MySQL documentation article contains the official syntax for aliasing tables in MySQL (which is actually the same as in standard SQL).

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

Sidebar

Related Questions

I have an SQL table called "posts" that looks like this: id | category
I have an SQL table called trainings that looks like this: +-----+-----------+--------+------------+------------+------------+-------+ | Id
Let's just say I have a table called TABLE_NAME that looks like this: id
I have 2 tables. The first one, called 'users' looks like this: id |
Right now, I have a SQL Query like this one: SELECT X, Y FROM
At the moment I have a SQL Server 2005 table that looks a bit
I have a report that looks something like this: CompanyA Workflow27 June5 CompanyA Workflow27
I have a SQL Mobile database with one table. It has several columns with
I have a similar question to this one SQL products/productsales I want to do
I have a sql query that runs super fast, around one second, when not

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.