1CREATE TABLE person (id int, first_name varchar(20), age int, gender char(1));
2
3INSERT INTO person VALUES (1, 'Bob', 25, 'M');
4INSERT INTO person VALUES (2, 'Jane', 20, 'F');
5INSERT INTO person VALUES (3, 'Jack', 30, 'M');
6INSERT INTO person VALUES (4, 'Bill', 32, 'M');
7INSERT INTO person VALUES (5, 'Nick', 22, 'M');
8INSERT INTO person VALUES (6, 'Kathy', 18, 'F');
9INSERT INTO person VALUES (7, 'Steve', 36, 'M');
10INSERT INTO person VALUES (8, 'Anne', 25, 'F');
1Bill 32 M 4
2Bob 25 M 2
3Jack 30 M 3
4Nick 22 M 1
5Steve 36 M 5
6Anne 25 F 3
7Jane 20 F 2
8Kathy 18 F 1
1SELECT a.first_name,
2 a.age,
3 a.gender,
4 count(b.age)+1 as rank
5FROM person a left join person b on a.age>b.age and a.gender=b.gender
6group by a.first_name,
7 a.age,
8 a.gender