Giffyguy Giffyguy - 2 months ago 6
SQL Question

How do I Write a SQL Query With a Condition Involving a Second Table?

Table1

...

LogEntryID

*PrimaryKey*


Value

ThresholdID - - - Link to the appropriate threshold being applied to this log entry.

...

Table2

...

ThresholdID
*PrimaryKey*


Threshold

...

All fields are integers.

The "..." thingies are there to show that these tables hold a lot more imformation than just this. They are set up this way for a reason, and I can't change it at this point.

I need write a SQL statement to select every record from Table1 where the Value field in that particular log record is less than the Threshold field in the linked record of Table2.

I'm newish to SQL, so I know this is a basic question.

If anyone can show me how this SQL statement would be structured, it would be greatly appreciated.

Answer
SELECT T1.*
  FROM Table1 T1
  JOIN Table2 T2 ON T2.ThresholdID = T1.ThresholdID
 WHERE T2.Threshold > T1.Value