Tuesday, March 27, 2012

Easy SQL Problem

I have a simple table that stores
name score
-- --
jim 343
bob 322
jane 122
I need a SQL query that will return the name and score of the person who has
the lowest score:
eg. jane 122
SELECT name, min(score) as score FROM tester group by name;
What is the best query for the job?
http://www.plus2net.com/sql_tutorial/sql_min.php
this tutorial seems to be wrong. It doesn't work when i run it.Mark Thomson wrote:
> I have a simple table that stores
> name score
> -- --
> jim 343
> bob 322
> jane 122
> I need a SQL query that will return the name and score of the person who h
as
> the lowest score:
> eg. jane 122
I would write something like this:
SELECT name, score FROM tester WHERE score = (SELECT MIN(score) FROM tester)|||Try,
select *
from tester
where score = (select min(score) from tester)
-- or
select top 1 with ties *
from tester
order by score desc
AMB
"Mark Thomson" wrote:

> I have a simple table that stores
> name score
> -- --
> jim 343
> bob 322
> jane 122
>
> I need a SQL query that will return the name and score of the person who h
as
> the lowest score:
> eg. jane 122
>
> SELECT name, min(score) as score FROM tester group by name;
> What is the best query for the job?
> http://www.plus2net.com/sql_tutorial/sql_min.php
> this tutorial seems to be wrong. It doesn't work when i run it.
>
>|||Many Thanks,
I've never heard of the ties keyword before.
Cheers.
"Alejandro Mesa" <AlejandroMesa@.discussions.microsoft.com> wrote in message
news:BC89C4FC-E232-4DCA-85B8-FCA33AC303CC@.microsoft.com...
> Try,
> select *
> from tester
> where score = (select min(score) from tester)
> -- or
> select top 1 with ties *
> from tester
> order by score desc
>
> AMB
> "Mark Thomson" wrote:
>|||select top 1 name, score from tableName order by score
If course, if 2 people have the same score, this will return only one of
them.
"Mark Thomson" <@.@.@.> wrote in message
news:OacoT6sjFHA.476@.TK2MSFTNGP14.phx.gbl...
>I have a simple table that stores
> name score
> -- --
> jim 343
> bob 322
> jane 122
>
> I need a SQL query that will return the name and score of the person who
> has the lowest score:
> eg. jane 122
>
> SELECT name, min(score) as score FROM tester group by name;
> What is the best query for the job?
> http://www.plus2net.com/sql_tutorial/sql_min.php
> this tutorial seems to be wrong. It doesn't work when i run it.
>|||...and for that you to work as you want, remove 'desc'.
"Mark Thomson" <@.@.@.> wrote in message
news:OUd1QFtjFHA.1204@.TK2MSFTNGP12.phx.gbl...
> Many Thanks,
> I've never heard of the ties keyword before.
> Cheers.
> "Alejandro Mesa" <AlejandroMesa@.discussions.microsoft.com> wrote in
> message news:BC89C4FC-E232-4DCA-85B8-FCA33AC303CC@.microsoft.com...
>|||You are right.
AMB
"Raymond D'Anjou" wrote:

> ...and for that you to work as you want, remove 'desc'.
> "Mark Thomson" <@.@.@.> wrote in message
> news:OUd1QFtjFHA.1204@.TK2MSFTNGP12.phx.gbl...
>
>

No comments:

Post a Comment