1

I have a table of data with a Name, a Grade, and a Point. For example, "John" got the grade B, which has a point value of 3.0.

How can I select entries that are less than a grade of "B"?

So I somehow need to do something like this

Value = select Point from MyTable where Grade="B"

and then

select * from MyTable where Point < value

but obviously SQL has to be one statement...

CodeGuy
  • 27,591
  • 71
  • 194
  • 314

2 Answers2

2

You can nest selects and add a subquery:

SELECT realtable.* 
FROM (SELECT Point FROM MyTable WHERE Grade="B" LIMIT 1) subquery, MyTable realtable
WHERE subquery.Point > realtable.Point
Abraham P
  • 14,341
  • 11
  • 53
  • 122
  • this is also very helpful. although I have to give the answer to the other one since it was first and did answer the question. THANKS!! – CodeGuy Oct 24 '12 at 21:40
1

Try it use in subquery as below:

select * 
from MyTable 
where Point < (select Point 
               from MyTable 
               where Grade="B")

but if you subquery returns more than one row try to use aggregate funcion for example min

select * 
from MyTable 
where Point < (select min(Point)
               from MyTable 
               where Grade="B")

or with LIMIT and join:

select * 
from MyTable mt
join (select Point from MyTable 
      where Grade="B"
      order by Point
      LIMIT 1) mt2 on mt.Point < mt2.Point
Robert
  • 24,847
  • 8
  • 64
  • 77