I have tables like these two
test_table
date student test
2012-05-31 Alice Math
2012-05-31 Alice Math
2012-05-31 Bob Math
2012-05-31 Bob Spelling
2012-06-01 Alice Math
2012-06-01 Alice Spelling
score_table
date student test best_daily_score
2012-05-31 Alice Math 90
2012-05-31 Bob Math 50
2012-05-31 Bob Spelling 50
2012-06-01 Alice Math 95
I’d like to detect that Alice’s best_daily_score for the Spelling test on 2012-06-01 hasn’t been recorded yet. I’m looking for the query that returns
2012-06-01 Alice Spelling
which is the only row of
SELECT DISTINCT date, student, test FROM test_table
that is not in
SELECT DISTINCT date, student, test FROM score_table
This doesn’t work:
SELECT DISTINCT date, student, test FROM test_table WHERE date, student, test NOT IN (SELECT DISTINCT date, student, test FROM score_table)
I assume because the left side of NOT IN should not be a list of three things.
Give this a try:
The query tries to match all the records from
test_tabletoscore_tablewhether it exists or not because of theLEFT JOIN. It matches fordate,student, andtest. If the record was not found onscore_table, so basicallybest_daily_scorewould beNULL. That’s why I added a condition to show onlyNULLonbest_daily_score.Click Here for some DEMO (SQLFiddle)