PROKA Posted April 2, 2005 Posted April 2, 2005 Ok so I have this table named "Users" with the fields 'username' and 'age' I'm interested to know who's the youngest username. How do I formulate the SELECT statement ? Quote Development & Research Department @ Elven Soft
michael_hk Posted April 2, 2005 Posted April 2, 2005 If you are using SQL Server... Select username from Users where age = (Select min(age) from Users) Quote There is no spoon. <<The Matrix>>
kejpa Posted April 4, 2005 Posted April 4, 2005 Select username from Users where age = (Select min(age) from Users) Subselect, not too good. Use an aggregation instead: SELECT username, Min(age) AS YoungestMember FROM Users GROUP BY Users.age; /Kejpa Quote
cpopham Posted April 4, 2005 Posted April 4, 2005 Quick question, I understand that the subselect statement my add a small amount of overhead time to the server, but when you are talking about a Select statement this small how much time is it really wasting? I try to use the most efficient ways of doing things, but if you did not know another way of doing it, then what is wrong with the nested select statement? :) Just trying to learn some things here. Chester Quote ____________________________________________ http://www.pophamcafe.com I am starting a developers section, more tutorials than anything.
*Experts* Nerseus Posted April 4, 2005 *Experts* Posted April 4, 2005 Here's another way to get the youngest person - this is SQL Server syntax, other languages use different syntax: SELECT TOP 1 username, Age AS YoungestMember FROM Users ORDER BY Age ASC kejpa's method works if you want all people, by age - not just the youngest. The subselect method works fine though you can't control which user you get back other than one - more or less randomly - from the users that match the youngest age. If 32 people are all 14 years old then you'll get one of the 14, but you can't control which. The "TOP 1" with ORDER BY will let you have a little more control, if it's needed. Performance of each? Kinda depends on the amount of data you have and disbersement of values. The ORDER BY is going to want to try to use an index, as will the MIN() on the subselect. Those types of operations will vary depending on the data being looked at - in this case Age. The SQL engine has an Optimizer that evaluates all the SQL and creates a plan for how it thinks it can get to what you want the fastest. If you've got on the order of a few thousand or less users you'll probably be fine. -ner Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
michael_hk Posted April 6, 2005 Posted April 6, 2005 kejpa's method works if you want all people, by age - not just the youngest. The subselect method works fine though you can't control which user you get back other than one - more or less randomly - from the users that match the youngest age. If 32 people are all 14 years old then you'll get one of the 14, but you can't control which. The "TOP 1" with ORDER BY will let you have a little more control, if it's needed. -ner The subselect method Select username from Users where age = (Select min(age) from Users) will return all (32) users who are 14. Quote There is no spoon. <<The Matrix>>
*Experts* Nerseus Posted April 8, 2005 *Experts* Posted April 8, 2005 You're right, my goof - I think I was thinking he still wanted just one username and that even with a sub-select he'd still want a TOP 1 or similar. -ner Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.