Database: network_2

Question: How many friends does Dan have?
SQL: SELECT count(T2.friend) FROM Person AS T1 JOIN PersonFriend AS T2 ON T1.name  =  T2.name WHERE T1.name  =  'Dan'

Question: Who is the oldest person?
SQL: SELECT name FROM Person WHERE age  =  (SELECT max(age) FROM person)

Question: Who is the oldest person whose job is student?
SQL: SELECT name FROM Person WHERE job  =  'student' AND age  =  (SELECT max(age) FROM person WHERE job  =  'student' )

Question: Who is the youngest male?
SQL: SELECT name FROM Person WHERE gender  =  'male' AND age  =  (SELECT min(age) FROM person WHERE gender  =  'male' )

Question: How old is the doctor named Zach?
SQL: SELECT age FROM Person WHERE job  =  'doctor' AND name  =  'Zach'

Question: How many people whose age is greater 30 and job is engineer?
SQL: SELECT count(*) FROM Person WHERE age  >  30 AND job  =  'engineer'

Question: What is average age of male for different job title?
SQL: SELECT avg(age) ,  job FROM Person WHERE gender  =  'male' GROUP BY job

Question: Find the number of people who is under 40 for each gender.
SQL: SELECT count(*) ,  gender FROM Person WHERE age  <  40 GROUP BY gender

Question: Find the name of people whose age is greater than any engineer sorted by their age.
SQL: SELECT name FROM Person WHERE age  >  (SELECT min(age) FROM person WHERE job  =  'engineer') ORDER BY age

Question: Find the number of people whose age is greater than all engineers.
SQL: SELECT count(*) FROM Person WHERE age  >  (SELECT max(age) FROM person WHERE job  =  'engineer')

Question: Find the name and age of all males in order of their age.
SQL: SELECT name, age FROM Person WHERE gender  =  'male' ORDER BY age

Question: Find the name and age of the person who is a friend of both Dan and Alice.
SQL: SELECT T1.name ,  T1.age FROM Person AS T1 JOIN PersonFriend AS T2 ON T1.name  =  T2.name WHERE T2.friend  =  'Dan' INTERSECT SELECT T1.name ,   T1.age FROM Person AS T1 JOIN PersonFriend AS T2 ON T1.name  =  T2.name WHERE T2.friend  =  'Alice'

Question: Find the name and age of the person who is a friend of Dan or Alice.
SQL: SELECT DISTINCT T1.name ,  T1.age FROM Person AS T1 JOIN PersonFriend AS T2 ON T1.name  =  T2.name WHERE T2.friend  =  'Dan' OR T2.friend  =  'Alice'

Question: Find the name of the person who has friends with age above 40 and under age 30?
SQL: SELECT T1.name FROM Person AS T1 JOIN PersonFriend AS T2 ON T1.name  =  T2.name WHERE T2.friend IN (SELECT name FROM Person WHERE age  >  40) INTERSECT SELECT T1.name FROM Person AS T1 JOIN PersonFriend AS T2 ON T1.name  =  T2.name WHERE T2.friend IN (SELECT name FROM Person WHERE age  <  30)

Question: Find the name of the person who has friends with age above 40 but not under age 30?
SQL: SELECT T1.name FROM Person AS T1 JOIN PersonFriend AS T2 ON T1.name  =  T2.name WHERE T2.friend IN (SELECT name FROM Person WHERE age  >  40) EXCEPT SELECT T1.name FROM Person AS T1 JOIN PersonFriend AS T2 ON T1.name  =  T2.name WHERE T2.friend IN (SELECT name FROM Person WHERE age  <  30)