7
 Queries of Joins Presented By : Monika Segal Asst. Professor Dept. CSE

Queries

Embed Size (px)

DESCRIPTION

study material dbms

Citation preview

  • Queries of Joins

    Presented By :Monika Segal

    Asst. ProfessorDept. CSE

  • create table dept_m ( id int primary key, name varchar(20) );

    insert into dept_m values (101,'CSE');

    create table emp_m ( id int primary key, name varchar(20), salary number(8), dept_id int references dept_m(id) ); insert into emp_m values (1,'A',36000, 101 ); insert into emp_m (id, name, salary) values (6,'F',54000);

  • select * from dept_m; select * from emp_m;

    select * from dept_m natural join emp_m;

    select * from dept_m inner join emp_m on dept_m.id = emp_m.dept_id;

    select * from dept_m, emp_m where dept_m.id = emp_m.dept_id;

    select * from dept_m, emp_m where dept_m.id= emp_m.dept_idand emp_m.salary >40000;

    select * from dept_m, emp_m where emp_m.salary >40000;

  • select * from dept_m cross join emp_m;

    create table emp_m1

    (

    e_id int primary key,

    name varchar(20),

    salary number(8),

    id int

    references dept_m(id)

    );

    alter table emp_m1 rename column name to e_name;

    select * from dept_m natural join emp_m;

    select * from dept_m natural join emp_m1;

  • select * from dept_m full outer join emp_m on dept_m.id = emp_m.dept_id;

    select * from dept_m, emp_m where dept_m.id(+) = emp_m.dept_id;

    select * from dept_m right outer join emp_m on dept_m.id = emp_m.dept_id;

    select * from dept_m, emp_m where dept_m.id = emp_m.dept_id(+);

    select * from dept_m left outer join emp_m on dept_m.id = emp_m.dept_id;

    select * from dept_m inner join emp_m on dept_m.id = emp_m.dept_id;

    select * from dept_m, emp_m where dept_m.id = emp_m.dept_id;

  • select a.e_id,a.e_name,b.e_id, b.e_name from emp_m1 a , emp_m1 b where a.mgr_id=b.e_id;

    select * from dept_m , emp_m;

    select * from dept_m where id in ( select dept_id from emp_m where salary > 40000);

    select * from dept_m where id in( select dept_id from emp_m group by dept_id having count(id)>1);

  • select sum(salary), avg(salary), min (salary), max(salary), count(*), count(dept_id) from emp_m;

    select sum(salary) from emp_m group by dept_id;

    select sum(salary) from emp_m group by dept_id having sum(salary)> 90000;