Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a table names 'scoredata' having fields id, lessonid, levelid, studentid, addedate in which score for each day will be entered by the admin. I want to select only those studentid whose score has not been added from last four weeks. How can I do this in a single query.

share|improve this question
up vote 1 down vote accepted
SELECT studentid FROM
    SELECT MAX(addedate) lastdate,studentid
    FROM scoredata GROUP BY studentid
) A
WHERE lastdate < ( DATE(NOW()) - INTERVAL 4 WEEK );

Query Breakdown

  • The subquery gets the last date each student entered a score
  • The WHERE clause outside the subquery checks to all lastdate values older than 4 weeks

Make sure you index studentid,addedate. If you didn't then run

ALTER TABLE scoredata ADD INDEX studentid_addedate_ndx (studentid,addedate);

Give it a Try !!!

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.