Wednesday, March 23, 2011

Question on oracle and sql




How to display ename, dname even if there no employees working in a
particular department(use outer join). (Oracle DB) (SQL)
Answer
Select ename, dname from emp e, dept d where e.deptno (+)=
d.deptno;

Question How to display employee name and his manager name. (Oracle DB) (SQL)

Answer Select e.ename, m.ename from emp e, emp m where e.mgr=m.empno;

Question

How to display the department name along with total salary in each
department. (Oracle DB) (SQL)
Answer Select deptno, sum(sal) from emp group by deptno;

Question How to display the department name and total number of employees in

each department. (Oracle DB) (SQL)
Answer select deptno,count(*) from emp group by deptno;

Question How to display the current date and time (Oracle DB) (SQL)

Answer select to_char(sysdate,'month mon dd yy yyyy hh:mi:ss') from dual;


Question How to delete the rows of employees who are working in the company for

more than 2 years. (Oracle DB) (SQL)
Answer delete from emp where floor(sysdate-hiredate)>2*365;

Question

How to provide a commission to employees who are not earning any
commission. (Oracle DB) (SQL)
Answer update emp set comm=300 where comm is null;

Question Write a query, If any employee has commission his commission should be

incremented by 10% of his salary. (Oracle DB) (SQL)
Answer update emp set comm=comm*10/100 where comm is not null;

Question

How to display employee name and department name for each employee.
(Oracle DB) (SQL)
Answer select ename,dname from emp e, dept d where e.deptno=d.deptno;

Question How to display employee number, name and location of the department

in which he is working. (Oracle DB) (SQL)
Answer Select empno, ename, loc from emp e, dept d where e.deptno=d.deptno;


Question
I want to give a validation saying that sal cannot be greater 10,000(note
give a name to this column). How to do this? (Oracle DB) (SQL)
Answer alter table emp add constraint emp_sal_check check (sal<10000);

Question

How to add column called as mgr to your emp table. This column should
be related to empno. Give a
command to add this constraint (Oracle DB) (SQL)
Answer
Alter table emp add mgr number (5);
Alter table emp add constraint emp_mgr foreign key (empno);

Question

How to add dept no column to your emp table. This dept no column
should be related to deptno column of
dept table (Oracle DB) (SQL)
Answer
Alter table emp add deptno number (3);
Alter table emp1 add constraint emp1_deptno foreign key(deptno)
references dept(deptno);

Question

How to create table called as new emp. Using single command create this
table as well as to get data
into this table (use create table as) (Oracle DB) (SQL)
Answer create table newemp as select *from emp;

Question How to create table called as newemp. This table should contain only

empno,ename, dname (Oracle DB) (SQL)
Answer create table newemp as select empno,ename,dname from emp e , dept d
where e.deptno=d.deptno;


Question How to create table emp with only one column empno (Oracle DB) (SQL)
Answer Create table emp (empno number(5));

Question How to add this column to emp table ename Varchar(20). (Oracle

DB) (SQL)
Answer alter table emp add ename varchar2(20) not null;

Question

How to add primary key constraint after the table has been created?
(Oracle DB) (SQL)
Answer alter table emp add constraint emp_empno primary key (empno);

Question How to increase the length of ename column to 30 characters. (Oracle

DB) (SQL)
Answer alter table emp modify ename varchar2(30);

Question How to add salary column to emp table. (Oracle DB) (SQL)

Answer alter table emp add sal number(7,2);

1 comment:

  1. As claimed by Stanford Medical, It's really the one and ONLY reason women in this country get to live 10 years more and weigh on average 42 pounds less than us.

    (Just so you know, it is not related to genetics or some secret exercise and EVERYTHING around "HOW" they eat.)

    P.S, What I said is "HOW", and not "WHAT"...

    Click this link to find out if this brief questionnaire can help you unlock your true weight loss potential

    ReplyDelete