sql - How do I list the department name from another table in my results? -


the following employees table:

create table employees     (empid    char(4)         unique not null,      ename    varchar(10),      job      varchar(9),      mgr      char(4),      hiredate date,      salary   decimal(7,2),      comm     decimal(7,2),      deptno   char(2)         not null,          primary key(empid),          foreign key(deptno) references departments(deptno));   insert employees values (7839,'king','president',null,'17-nov-11',5000,null,10); insert employees values (7698,'blake','manager',7839,'01-may-11',2850,null,30); insert employees values (7782,'clark','manager',7839,'02-jun-11',2450,null,10); insert employees values (7566,'jones','manager',7839,'02-apr-11',2975,null,20); insert employees values (7654,'martin','salesman',7698,'28-feb-12',1250,1400,30); insert employees values (7499,'allen','salesman',7698,'20-feb-11',1600,300,30); insert employees values (7844,'turner','salesman',7698,'08-sep-11',1500,0,30); insert employees values (7900,'james','clerk',7698,'22-feb-12',950,null,30); insert employees values (7521,'ward','salesman',7698,'22-feb-12',1250,500,30); insert employees values (7902,'ford','analyst',7566,'03-dec-11',3000,null,20); insert employees values (7369,'smith','clerk',7902,'17-dec-10',800,null,20); insert employees values (7788,'scott','analyst',7566,'09-dec-12',3000,null,20); insert employees values (7876,'adams','clerk',7788,'12-jan-10',1100,null,20); insert employees values (7934,'miller','clerk',7782,'23-jan-12',1300,null,10); 

the following departments table:

create table departments (deptno   char(2)  unique not null, dname    varchar(14), location varchar(13), primary key(deptno));  insert departments values (10,'accounting','new york'); insert departments values (20,'research','dallas'); insert departments values (30,'sales','chicago'); insert departments values (40,'operations','boston'); 

the following query:

select ename, salary employees salary > (select avg(salary) employees); 

sql fiddle demo: http://www.sqlfiddle.com/#!3/61ca4/1

select e.ename,        e.salary,        d.dname   employees e       inner join departments d on e.deptno = d.deptno e.salary > (select avg(salary) employees); 

sql fiddle demo: http://www.sqlfiddle.com/#!3/61ca4/2


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -