Example5 Study drills


#1

Hi everybody,
Just started to learn the SQL and have a question about Exercise 5.
Have started the study drills section: Write a query to find all people younger than you. Do one that’s older.
I tried the following SELECT (with a little help from google):
SELECT * FROM person WHERE age > (SELECT age FROM person WHERE id = 0);
This works fine as the id is unique.
Problem is when i try this:
SELECT * FROM person WHERE age > (SELECT age FROM person WHERE last_name = “Example”);

Here is my database:
SELECT * From person;
0 Adam Example 33
1 Eva Example 33
2 Beta Example 3
3 Cyril Example 1
4 Damian Example 40
5 Adam Example 70

Not sure if that is a correct SELECT as more values can be matched.
It looks like the 1st match is used, but I want to be sure.

Thanks a lot.

Regards,
Jan


#2

To debug this, first you would see what that sub-select does:

SELECT age FROM person WHERE last_name = “Example”

Since you’re matching on just the last name, and everyone’s last name is “Example” this kind of makes no sense. You’re saying you want each record that the age is greater than all the ages? You would probably want to then add a SUM, or MAX to that subselect so that it returns just 1 value to compare against.