0

Im new at SQL and trying to create a basic subquery. I need to find the difference between the avg age in Manager and avg age in Trainee.

Title is one column- Manager or Trainee Age is one column, all within the same table.

Would i use two subqueries to do something like:

Select manager_age - trainee_age
from book1
(select avg(age) as manager_age from book1 where title = "manager")

and another subquery:

(select avg(age) as manager_age from book1 where title = "trainee") 

Im not sure how to do two subqueries in one query or if I should do this another way like a join? Thank you!

1 Answer 1

1
create table book1(title varchar(50), age integer) ;

insert into book1 values('Trainee', 10);
insert into book1 values('Trainee', 20);
insert into book1 values('Manager', 30);
insert into book1 values('Manager', 40);
insert into book1 values('Manager', 50);

select * from book1;

select m.m_age - t.t_age from (
(select title as m_title, avg(age) as m_age
 from book1 where title = 'Manager') m, 
(select title as t_title, avg(age) as t_age
 from book1 where title = 'Trainee') t);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.