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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:39:13+00:00 2026-05-18T00:39:13+00:00

I have a system that’s similar to the Q&A feature of stackoverflow. The main

  • 0

I have a system that’s similar to the Q&A feature of stackoverflow. The main difference is hat each question has an expiration datetime:

CREATE TABLE questions (
     id INT NOT NULL AUTO_INCREMENT,
     title CHAR(100) NOT NULL,
     details TEXT
     PRIMARY KEY (id)
) 

CREATE TABLE answers (
     id INT NOT NULL AUTO_INCREMENT,
     question_id INT NOT NULL
     details TEXT
     expiration_datetime DATETIME
     score INT
     PRIMARY KEY (id)
) 

I want to display a question together with the top N non-expired answers ordered by score. I’m thinking something like this:

SELECT * FROM questions, answers 
WHERE questions.id=1 AND questions.id=answers.question_id AND answers.expiration_datetime > NOW() 
ORDER BY answers.score DESC LIMIT 10

A few questions:

1) Is the query above the most efficient way to do what I want? How is it different from explicitly using JOIN such as:

SELECT * FROM questions JOIN answers ON questions.id=answers.question_id
WHERE questions.id=1 AND answers.expiration_datetime > NOW() 
ORDER BY answers.score DESC LIMIT 10

2) How to make my query use index? I’m thinking adding this index to TABLE answers:

INDEX (question_id, expiration_datetime, score)

Would the above index work for my query? It doesn’t seems right because expiration_datetime is ascending while I need score to be descending. What can I do here?

  • 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-18T00:39:13+00:00Added an answer on May 18, 2026 at 12:39 am

    Joining tables with INNER JOIN or in the FROM clause gives the same execution plan. However, the former is much more easy to read (and that’s what standard suggest since 1992).

    Your RDBMS will use an index whenever it’s possible. It’s usually a good idea to print the execution plan (EXPLAIN SELECT * FROM -- ...). However, note that creating multiple columns index can be tricky. In your case, you can use the index on question_id but not on expiration_datetime or score because they aren’t the first columns of the index. You would have to make three different index.

    P.S. SELECT * isn’t recommanded. Always type the columns you need (easy to read and re-read) and only mention the columns you will use. No point to select, for instance, initial_date if you never use this column in your script!

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

Sidebar

Related Questions

I have a system that has many components interacting with each other. It occured
I have a system that tracks what documents users view. Each document has its
I have a system that runs like this: main.exe runs sub.exe runs sub2.exe and
We have a system that has some Bash scripts running besides Java code. Since
We have a system that has a Mysql database with about 2Gigs of data.
I have a system that has N servers on the Amazon AWS cloud. They
I have a system that uses tags to categorize content similar to the way
We have a system that was originally developed decades ago and has increased in
I have a system that receives input from the public each day. Each morning
I have a system that generates large quantities of PostScript files that each contain

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.