SQL QUERRIES Date:27/08/07 Q:1 Write a query to display name, job, hiredate and employee number for each employee with employee number appearing first. SQL> SELECT empno,ename,job,hiredate 2 FROM emp; EMPNO ENAME JOB HIREDATE ---------- ---------- --------- --------- 7369 SMITH CLERK 17-DEC-80 7499 ALLEN SALESMAN 20-FEB-81 7521 WARD SALESMAN 22-FEB-81 7566 JONES MANAGER 02-APR-81 7654 MARTIN SALESMAN 28-SEP-81 7698 BLAKE MANAGER 01-MAY-81 7782 CLARK MANAGER 09-JUN-81 7788 SCOTT ANALYST 19-APR-87 7839 KING PRESIDENT 17-NOV-81 7844 TURNER SALESMAN 08-SEP-81 7876 ADAMS CLERK 23-MAY-87 7900 JAMES CLERK 03-DEC-81 7902 FORD ANALYST 03-DEC-81 7934 MILLER CLERK 23-JAN-82
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
SQL QUERRIESDate:27/08/07
Q:1 Write a query to display name, job, hiredate and employee
number for each employee with employee number appearing
first.
SQL> SELECT empno,ename,job,hiredate 2 FROM emp;
EMPNO ENAME JOB HIREDATE---------- ---------- --------- --------- 7369 SMITH CLERK 17-DEC-80 7499 ALLEN SALESMAN 20-FEB-81 7521 WARD SALESMAN 22-FEB-81 7566 JONES MANAGER 02-APR-81 7654 MARTIN SALESMAN 28-SEP-81 7698 BLAKE MANAGER 01-MAY-81 7782 CLARK MANAGER 09-JUN-81 7788 SCOTT ANALYST 19-APR-87 7839 KING PRESIDENT 17-NOV-81 7844 TURNER SALESMAN 08-SEP-81 7876 ADAMS CLERK 23-MAY-87 7900 JAMES CLERK 03-DEC-81 7902 FORD ANALYST 03-DEC-81 7934 MILLER CLERK 23-JAN-82
14 rows selected.
Q2: Write a query to display unique jobs from the employee table.
SQL> SELECT DISTINCT(job) 2 FROM emp;
JOB---------CLERKSALESMANPRESIDENTMANAGERANALYST
Q3: Write a query to display the name concatenated by job seprated by comma.
Q14: Write a query to display the name of all employees where the third letter of their name is A.
SQL> SELECT ename 2 FROM emp 3 WHERE ename LIKE '__A%';
ENAME----------BLAKECLARKADAMS
Q15 : Write a query to display the names of all employees that has 2 R’s or A’s in their name and are in department number 30 or their manger is 7788.
SQL> SELECT ename 2 FROM emp 3 WHERE ename LIKE '%R%R%' OR ename LIKE '%A%A%' AND (deptno=30 OR mgr=7788);
ENAME----------TURNERADAMS
Q16: Write a query to display the name ,job and salary of all employees where job is Clerk or Analyst and their salary are not equal to 1000,3000 or 5000.
SQL> SELECT ename,job,sal 2 FROM emp 3 WHERE job IN('CLERK','ANALYST') AND sal NOT IN(1000,3000,5000);
Q23: Write a query to display the following for each employee :- <ename> earns <salary> monthly but wants <3 times salary> label the column as Dream Salary.
SQL> SELECT ename||' earns '||sal||' monthly but wants '||3*sal Dream_Salary 2 FROM emp;
DREAM_SALARY--------------------------------------------------------------------------------SMITH earns 800 monthly but wants 2400ALLEN earns 1600 monthly but wants 4800WARD earns 1250 monthly but wants 3750JONES earns 2975 monthly but wants 8925MARTIN earns 1250 monthly but wants 3750BLAKE earns 2850 monthly but wants 8550CLARK earns 2450 monthly but wants 7350SCOTT earns 3000 monthly but wants 9000KING earns 5000 monthly but wants 15000TURNER earns 1500 monthly but wants 4500ADAMS earns 1100 monthly but wants 3300JAMES earns 950 monthly but wants 2850FORD earns 3000 monthly but wants 9000MILLER earns 1300 monthly but wants 3900
14 rows selected.
Q24: Write a query to display the name and salary for all employees. Format the salary to be 15 character long, left padded with $ sign.
SQL> SELECT INITCAP(ename) ename,LENGTH(ename) length 2 FROM emp 3 WHERE ename LIKE 'J%' OR ename LIKE 'A%' OR ename LIKE 'M%';
Q25: Write a query to display the employees name with the first letter capitalized and all other letters lower case and length of their name for all employees whose name start with J,A and M.
SQL> SELECT INITCAP(ename) ename,LENGTH(ename) length 2 FROM emp 3 WHERE ename LIKE 'J%' OR ename LIKE 'A%' OR ename LIKE 'M%';
Q27: Write a query to display the employee name and commission amount . If the employee doesnot earn commission put ‘No Commission’.
SQL>SELECT ename,DECODE(comm,0,'No Commission',NULL,'No Commission',comm) "COMM" 2 FROM emp;
ENAME COMM---------- ------------------------SMITH No Commission
ALLEN 300WARD 500JONES No CommissionMARTIN 1400BLAKE No CommissionCLARK No CommissionSCOTT No CommissionKING No CommissionTURNER No CommissionADAMS No Commission
ENAME COMM---------- -------------------------JAMES No CommissionFORD No CommissionMILLER No Commission
14 rows selected.
Q28: Write a query to display the name, department name and department number of all employees.
SQL> SELECT ename,e.deptno,dname 2 FROM emp e,dept d 3 WHERE e.deptno=d.deptno;
Q33: Write a query to display the employee name and employee number along with their manager’s name and manager’s number.
SQL> SELECT e.ename,e.empno,m.ename mname,m.empno mgrno 2 FROM emp e,emp m 3 WHERE e.mgr=m.empno;
ENAME EMPNO MNAME MGRNO---------- ---------- ---------- ----------SMITH 7369 FORD 7902ALLEN 7499 BLAKE 7698WARD 7521 BLAKE 7698JONES 7566 KING 7839MARTIN 7654 BLAKE 7698BLAKE 7698 KING 7839CLARK 7782 KING 7839SCOTT 7788 JONES 7566TURNER 7844 BLAKE 7698ADAMS 7876 SCOTT 7788JAMES 7900 BLAKE 7698FORD 7902 JONES 7566MILLER 7934 CLARK 7782
13 rows selected.
Q.34 Write a query to display the employee name& employee number along with their manager's name,manager's numberalong with the employees who donot have a manager?
SQL> SELECT E.ENAME,E.EMPNO,M.ENAME,E.MGR 2 FROM EMP E LEFT OUTER JOIN EMP M 3 ON(E.MGR=M.EMPNO);
FORD 7902 JONES 7566SCOTT 7788 JONES 7566JAMES 7900 BLAKE 7698TURNER 7844 BLAKE 7698MARTIN 7654 BLAKE 7698WARD 7521 BLAKE 7698ALLEN 7499 BLAKE 7698MILLER 7934 CLARK 7782ADAMS 7876 SCOTT 7788CLARK 7782 KING 7839BLAKE 7698 KING 7839
ENAME EMPNO ENAME MGR---------- ---------- ---------- ----------JONES 7566 KING 7839SMITH 7369 FORD 7902KING 7839
14 rows selected.
Q35: Write a querry to display the employee name , department number and all the employee that work in the same department as the given employee.
SQL> SELECT e1.ename,e1.deptno,e2.ename 2 FROM emp e1,emp e2 3 WHERE e1.deptno=e2.deptno 4 AND e1.empno>e2.empno;
Q36: Write a query to display the name , job , department name, salary and grade for all employees.SQL>SELECT ename,job,dname,sal,grade 2 FROM emp e,dept d,salgrade s 3 WHERE e.deptno=d.deptno AND( sal BETWEEN losal AND hisal);
Q38SQL>SELECT e.ename e_name,e.hiredate e_hiredate,m.ename m_name,m.hiredate m_hiredate 2 FROM emp e,emp m 3 WHERE m.empno=e.mgr AND MONTHS_BETWEEN(e.hiredate,m.hiredate)<0;
WARD 22-FEB-81 BLAKE 01-MAY-81JONES 02-APR-81 KING 17-NOV-81BLAKE 01-MAY-81 KING 17-NOV-81CLARK 09-JUN-81 KING 17-NOV-81
6 rows selected.
Q.39 Write a query to display the employees' name& the amount of the salaries of the employees are indicated throughasterisks.Each asterisk signifies a hundred dollars?
SQL>SELECT EMPNO||' '||RPAD('*',(ROUND(SAL/100)),'*') 2 "EMPLOYEE AND THEIR SAL" FROM EMP 3 ORDER BY SAL DESC;
EMPLOYEE AND THEIR SAL--------------------------------------------------------------------------------7839 **************************************************7788 ******************************7902 ******************************7566 ******************************7698 *****************************7782 *************************7499 ****************7844 ***************7934 *************7521 *************7654 *************
EMPLOYEE AND THEIR SAL--------------------------------------------------------------------------------7876 ***********7900 **********7369 ********
14 rows selected.
Q40SQL>SELECT MAX(sal) high_sal,MIN(sal) low_sal,SUM(sal) sum_sal,AVG(sal) avg_sal 2 FROM emp;
Q.46 Write a query to display the department name,location name,number of employees and the average salary for all employees in that department?
SQL> SELECT dname,loc,COUNT(*) "Count", AVG(sal) "Avg_sal" 2 FROM DEPT d,EMP e 3 WHERE e.deptno=d.deptno 4 GROUP BY dname,loc;
DNAME LOC Count Avg_sal-------------- ------------- ---------- ----------ACCOUNTING NEW YORK 3 2916.66667RESEARCH DALLAS 5 2175SALES CHICAGO 6 1566.66667
Q47SQL> SELECT ename,hiredate 2 FROM emp 3 WHERE deptno = 4 (SELECT deptno 5 FROM emp 6 WHERE ename='BLAKE');
Q51SQL> SELECT e.deptno,ename,job 2 FROM emp e,dept d 3 WHERE e.deptno=d.deptno AND dname='SALES';
DEPTNO ENAME JOB---------- ---------- --------- 30 ALLEN SALESMAN 30 WARD SALESMAN 30 MARTIN SALESMAN 30 BLAKE MANAGER 30 TURNER SALESMAN 30 JAMES CLERK
6 rows selected.
Q52SQL>SELECT empno,ename,sal 2 FROM emp 3 WHERE sal >
4 (SELECT AVG(sal) 5 FROM emp) 6 AND deptno = ANY 7 (SELECT deptno 8 FROM emp 9 WHERE ename LIKE '%T%');
EMPNO ENAME SAL---------- ---------- ---------- 7902 FORD 3000 7788 SCOTT 3000 7566 JONES 2975 7698 BLAKE 2850
Q53SQL>SELECT ename,deptno,sal 2 FROM emp 3 WHERE deptno = ANY 4 (SELECT deptno 5 FROM emp 6 WHERE comm <>0) 7 AND 8 sal = ANY 9 (SELECT sal 10 FROM emp 11 WHERE comm <>0);