Saturday 24 December 2016

SQL QUERIES WITH ANSWER

SQL QUERIES
***********************************************************

Display the number of employees who are getting salary less than the Blake’s manager.
SELECT COUNT(*)
FROM EMP
WHERE SAL<(SELECT SAL
FROM EMP
WHERE EMPNO IN(SELECT MGR
FROM EMP
WHERE ENAME='BLAKE'));
----------------------------------------------------------------------------------------
List employees who located in Chicago and their commission is zero.
SELECT * FROM EMP
WHERE COMM=0 AND DEPTNO IN(SELECT DEPTNO
FROM DEPT
WHERE LOC='CHICAGO');
----------------------------------------------------------------------------------------
List employees who work for SALES department and their salary greater than average salary of their department.
SELECT * FROM EMP
WHERE DEPTNO IN(SELECT DEPTNO
FROM DEPT
WHERE DNAME='SALES') AND SAL>ANY (SELECT AVG(SAL)
FROM EMP
GROUP BY DEPTNO);
----------------------------------------------------------------------------------------
List employees who are working in RESEARCH department and they are MANAGER(i.e.JOB).
SELECT *
FROM EMP
WHERE JOB='MANAGER' AND DEPTNO IN(SELECT DEPTNO
FROM DEPT
WHERE DNAME='RESEARCH');
--------------------------------------------------------------------------------------------
Display department name of the employees who earn commission.
SELECT DNAME
FROM DEPT
WHERE DEPTNO IN (SELECT DEPTNO
FROM EMP
WHERE COMM IS NOT NULL);
------------------------------------------------------------------------------------------------
Display department name of the employees who earn maximum salary and have no reporting manager.
SELECT DNAME
FROM DEPT
WHERE DEPTNO IN (SELECT DEPTNO
FROM EMP
WHERE SAL=(SELECT MAX(SAL)
FROM EMP
WHERE MGR IS NULL));
----------------------------------------------------------------------------------------------------
Display employee details who are reporting to blake and have commission without using null or not null.
select *
from emp
where comm >=0 and mgr in (select empno
from emp
where ename ='BLAKE' );
-----------------------------------------------------------------------------------------------
List all the department name and loc of all the salesman managers,manager.
select dname,loc from dept
where deptno in (select deptno from emp
where empno in (select mgr from emp
where empno in (select mgr from emp
where job ='SALESMAN')));
----------------------------------------------------------------------
List the employee department name and loc of all the employees who are clerk,reporting to blake and salary is lesser than martin salary.
select dname ,loc            
from dept
where deptno in (select deptno
from emp
where job ='CLERK' and mgr in (select empno 
from emp
where ename ='BLAKE' or sal < (select sal from emp
where ename ='MARTIN')));
---------------------------------------------------------------------------------------------------
List the employees who does not directly report to president, have commission and salary more than max salary of all the clerk without using null or not null.
select *
from emp
where job<> 'PRESIDENT' and comm >=0 and empno in (select empno
from emp
where Sal > (select max(sal)
from emp 
where job='CLERK'));
--------------------------------------------------------------------------------------------
List the employees who joined after 2 years of first employee of the company and more then blake salary.
Select *
From emp
Where hiredate > (select add_months(min(hiredate),2*12)
From emp)
And sal > (select sal
From emp
Where ename='BLAKE')
-------------------------------------------------------------------------------
Display location of all the employees who reporting to BLAKE.
SELECT LOC
FROM DEPT
WHERE DEPTNO IN (SELECT DEPTNO
FROM EMP
WHERE MGR IN (SELECT EMPNO
FROM EMP
WHERE ENAME = 'BLAKE'))
--------------------------------------------------------------------------------------
List all the employees whose job is same as jones and their salary lesser than SCOTT.
SELECT *
FROM EMP
WHERE JOB IN (SELECT JOB
FROM EMP
WHERE ENAME='JONES') AND
SAL < (SELECT SAL
FROM EMP
WHERE ENAME='SCOTT');
----------------------------------------------------------------
Display all the employees of department 30,20 with there annual salary and having atleast 3 employees.
SELECT EMP.*,SAL*12 "ANNUAL SALARY"
FROM EMP
WHERE DEPTNO IN (SELECT DEPTNO
FROM EMP
GROUP BY DEPTNO
HAVING COUNT(*)>=3);
--------------------------------------------------------------------------------
Display all the employees who are earn less than any of the salesman.
SELECT *
FROM EMP
WHERE SAL <ANY(SELECT SAL
FROM EMP
WHERE JOB='SALESMAN');
---------------------------------------------------------------------------------
Display all the employees who are joined before the last person.
select *
from emp
where HIREDATE < (select max(hiredate)
from emp);
----------------------------------------------------------------------
Display last but one employee record.
SELECT *
FROM EMP A
WHERE 1 = (SELECT COUNT(DISTINCT(HIREDATE))
FROM EMP B
WHERE A.HIREDATE < B.HIREDATE)
------------------------------------------------------
Find 3rd minimum salary in the employee table.
select min(sal)
from emp
where Sal > (select min(sal)
from emp
where Sal > (select min(sal)
from emp));
-----------------------------------------------------------
Display all the employees who are earning more than any of the 'MANAGER'.
SELECT *
FROM EMP
WHERE SAL >ANY (SELECT SAL
FROM EMP
WHERE JOB='MANAGER')
         OR
SELECT *
FROM EMP
WHERE SAL > (SELECT MIN(SAL)
FROM EMP
WHERE JOB='MANAGER')
---------------------------------------------------------------------------------------
List employees who joined after 4 years of 1st employee of the company and less than blake salary.
SELECT *
FROM EMP
WHERE HIREDATE > (SELECT ADD_MONTHS(MIN(HIREDATE),4*12)
FROM EMP)
AND SAL < (SELECT SAL
FROM EMP
WHERE ENAME='BLAKE')
------------------------------------------------------------
Display the department information of employee who is working for 'NEW YORK' location.
SELECT DEPTNO,DNAME
FROM DEPT
WHERE LOC='NEW YORK';
-------------------------------------------------------
Display the salary wise employee information for deptno 20 in descending order.
SELECT SAL
FROM EMP
WHERE DEPTNO=20
ORDER BY SAL DESC;
-------------------------------------------------------
Display location of employees, whose name doesn’t start with A and salary between 1000 and 3000.
SELECT LOC
FROM EMP E, DEPT D
WHERE E.DEPTO=D.DEPTNO AND ENAME NOT LIKE 'A%' AND SAL BETWEEN 1000 AND 3000
---------------------------------------------------------------
Display department name of all the employees who are reporting to blake.
SELECT DNAME
FROM DEPT
WHERE DEPTNO IN (SELECT DEPTNO
FROM EMP
WHERE MGR IN (SELECT EMPNO
FROM EMP
WHERE ENAME='BLAKE');
---------------------------------------------
Display martin’s manager’s manager’s department location.
SELECT LOC
FROM DEPT
WHERE DEPTNO IN (SELECT DEPTNO
FROM EMP
WHERE EMPNO IN (SELECT MGR
FROM EMP
WHERE EMPNO IN (SELECT MGR
FROM EMP
WHERE ENAME = 'MARTIN')));
-----------------------------------------------
Display the employee name and their salary who joined 2 years before the last person hired.
SELECT ENAME,SAL
FROM EMP
WHERE HIREDATE > (SELECT ADD_MONTHS(MIN(HIREDATE),2*12)
FROM EMP);
-----------------------------------------------------------------------------
Display the employee name,job and department for those who don’t have commission.
SELECT ENAME,JOB,DEPTNO
FROM EMP
WHERE COMM IS NULL;
---------------------------------------------------------
Display the employee details with their location who earn maximum commission.
SELECT E.*,LOC
FROM EMP E, DEPT D
WHERE E.DEPTNO=D.DEPTNO AND COMM IN (SELECT MAX(COMM)
FROM EMP);
-----------------------------------------------------
Display all the employee whose department is sales and who is earning some commission (i.e commissison is not null or zero) and whose hired before the last person hired.
SELECT E.*,D.*
FROM EMP E, DEPT D
WHERE E.DEPTNO=D.DEPTNO AND
COMM >=0 AND DNAME='SALES' AND HIREDATE < (SELECT MAX(HIREDATE)
FROM EMP);
-------------------------------------------------------------------------------
Display all the department names and manager who is wards manager’s manager.
SELECT DNAME,MGR
FROM EMP E, DEPT D
WHERE E.DEPTNO=D.DEPTNO AND EMPNO IN (SELECT EMPNO
FROM EMP
WHERE EMPNO IN (SELECT MGR
FROM EMP
WHERE EMPNO IN (SELECT MGR
FROM EMP
WHERE ENAME='WARD')));
-----------------------------------------------------------------------------
Display department names and maximum commission of employees whose salary is greater than average salary of all the clerk’s.
SELECT ENAME,EMPNO,COMM,DNAME
FROM EMP E,DEPT D
WHERE E.DEPTNO=D.DEPTNO AND COMM IN (SELECT MAX(COMM)
FROM EMP
WHERE SAL > (SELECT AVG(SAL)
FROM EMP
WHERE JOB='CLERK'));
------------------------------------------------------------------------------------------
Display the last employee record with location.
SELECT E.*,LOC
FROM EMP E,DEPT D
WHERE E.DEPTNO=D.DEPTNO AND HIREDATE IN (SELECT MAX(HIREDATE)
FROM EMP);
-------------------------------------------------------------
Display the department number who working in sales department and they are manager.
SELECT *
FROM EMP
WHERE EMPNO IN (SELECT MGR
FROM EMP
WHERE DEPTNO IN (SELECT DEPTNO
FROM DEPT
WHERE DNAME='SALES'))
-----------------------------------------------------------------------------------------------
Display deprtment name of the employees who earn minimum salary and have reporting manager.
SELECT DNAME
FROM DEPT
WHERE DEPTNO IN (SELECT DEPTNO
FROM EMP
WHERE SAL IN (SELECT MIN(SAL)
FROM EMP
WHERE MGR IS NOT NULL));
------------------------------------------------------------------
Display hiredate and department name of all the employees working for sales.
SELECT HIREDATE,DNAME
FROM EMP E,DEPT D
WHERE E.DEPTNO=D.DEPTNO AND DNAME='SALES';
----------------------------------------------------------------------
Display location and department name of employees who is working as president.
SELECT DNAME,LOC
FROM DEPT
WHERE DEPTNO IN (SELECT DEPTNO
FROM EMP
WHERE JOB='PRESIDENT');
--------------------------------------------------------------------------------
Display the department name of employees whose salary is maximum salary but lesser than 3000.
SELECT DNAME
FROM DEPT
WHERE DEPTNO IN (SELECT DEPTNO
FROM EMP
WHERE SAL IN (SELECT MAX(SAL)
FROM EMP
WHERE SAL < 3000));
-----------------------------------------------------------------------------------
Display the department name who are reporting to ADAMS.
SELECT DNAME
FROM DEPT
WHERE DEPTNO IN (SELECT DEPTNO
FROM EMP
WHERE MGR IN (SELECT EMPNO
FROM EMP
WHERE ENAME ='ADAMS'));
------------------------------------------------------------------------------
Display all the employee whose salary is greater than average salary of department 30.
SELECT *
FROM EMP
WHERE SAL > (SELECT AVG(SAL)
FROM EMP
WHERE DEPTNO=30);
-----------------------------------------------------------------------------
Display the number of employees who work for RESEARCH dept and their sal is lesser than one of the sal in dept 10.
SELECT COUNT(*)
FROM EMP E, DEPT D
WHERE E.DEPTNO=D.DEPTNO AND DNAME='RESEARCH' AND SAL <ANY (SELECT SAL
FROM EMP
WHERE DEPTNO=10);
----------------------------------------------------------------------------------
Display the dname that are having clerk in it.
SELECT DNAME
FROM DEPT
WHERE DEPTNO IN (SELECT DEPTNO
FROM EMP
WHERE JOB='CLERK');
----------------------------------------------------------------------------------
Display the dname that are having atleast one L in it.
SELECT DNAME
FROM DEPT
WHERE DNAME LIKE '%L%';
------------------------------------------------------
Display the employees who joined after BLAKE.
SELECT *
FROM EMP
WHERE HIREDATE > (SELECT HIREDATE
FROM EMP
WHERE ENAME='BLAKE');
---------------------------------------------------
List dname that are having at least 3 employee but not more than 5 employee in it.
SELECT DNAME
FROM DEPT
WHERE DEPTNO IN (SELECT DEPTNO
FROM EMP
GROUP BY DEPTNO
HAVING COUNT(*) BETWEEN 3 AND 5)
--------------------------------------------------------------------------
Display the location of all employee whose reporting manager sal is greater than 2000.
SELECT LOC
FROM DEPT
WHERE DEPTNO IN (SELECT DEPTNO
FROM EMP
WHERE SAL IN (SELECT SAL
FROM EMP
WHERE SAL > 2000 AND MGR IN (SELECT EMPNO
FROM EMP)));
------------------------------------------------------------
Select the emp whose dname is having at least two E in it.
SELECT *
FROM DEPT
WHERE DNAME LIKE '%E%E%';
----------------------------------------------------------------------------------------------
Display ename, sal of employee  who are earning more than any of the analyst.
SELECT ENAME,SAL
FROM EMP
WHERE SAL >ANY (SELECT SAL
FROM EMP
WHERE JOB='ANALYST');
-----------------------------------------------------------------------------------
Select all the emp who are working for CHICAGO.
SELECT E.*
FROM EMP E, DEPT D
WHERE E.DEPTNO=D.DEPTNO AND LOC='CHICAGO';
-------------------------------------------------------------------------------------------------
Query to display employee names  who is having minimum salary in RESEARCH department.
SELECT ENAME
FROM EMP
WHERE SAL IN (SELECT MIN(SAL)
FROM EMP
WHERE DEPTNO IN (SELECT DEPTNO
FROM DEPT
WHERE DNAME='RESEARCH'));
-------------------------------------------------------------------------
List the department names that are having SALESMAN.
SELECT DNAME
FROM DEPT
WHERE DEPTNO IN (SELECT DEPTNO
FROM EMP
WHERE JOB='SALESMAN');
---------------------------------------------------------------------------
List the department names that are having at least 3 employees in it.
SELECT DNAME
FROM DEPT
WHERE DEPTNO IN (SELECT DEPTNO
FROM EMP
GROUP BY DEPTNO
HAVING COUNT(*)>=3);
-------------------------------------------------------------------------
List employees from research and accounting department having at least two reporting.
SELECT *
FROM EMP
WHERE EMPNO IN (SELECT MGR
FROM EMP
GROUP BY MGR
HAVING COUNT(*)>=2) AND
DEPTNO IN (SELECT DEPTNO
FROM DEPT
WHERE DNAME IN ('ACCOUNTING','RESEARCH'))
               OR
SELECT E.*
FROM EMP E, DEPT D
WHERE E.DEPTNO=D.DEPTNO AND DNAME IN ('RESEARCH','ACCOUNTING') AND EMPNO IN (SELECT MGR
FROM EMP
GROUP BY MGR
HAVING COUNT(*)>=2)
------------------------------------------------------------------------------------------------
DISPLAY THE NUMBER OF EMPLOYEES WHO WORK FOR RESEARCH DEPT AND THEIR SALARY IS LESSER THAN ONE OF THE SALARY IN DEPARTMENT IN 10
SELECT COUNT(*)
FROM EMP E, DEPT D
WHERE E.DEPTNO=D.DEPTNO AND DNAME='RESEARCH' AND SAL <ANY (SELECT SAL
FROM EMP
WHERE DEPTNO=10);
------------------------------------------------------------------------------
DISPLAY DNAME AND LOC OF LAST EMPLOYEE RECORD.
SELECT DNAME,LOC
FROM DEPT
WHERE DEPTNO IN (SELECT DEPTNO
FROM EMP
WHERE HIREDATE IN (SELECT MAX(HIREDATE)
FROM EMP));
-----------------------------------------------------------
List the department names that are having no employees at all.
SELECT DNAME
FROM DEPT
WHERE DEPTNO NOT IN (SELECT DEPTNO
FROM EMP);
--------------------------------------------------------------------------
Display all the employees whose job is same as SCOTT.
SELECT *
FROM EMP
WHERE JOB IN (SELECT JOB
FROM EMP
WHERE ENAME='SCOTT');
--------------------------------------------------------------------------------------
Display SCOTT's manager’s manager department name.
SELECT DNAME
FROM DEPT
WHERE DEPTNO IN (SELECT DEPTNO
FROM EMP
WHERE EMPNO IN (SELECT MGR
FROM EMP
WHERE EMPNO IN (SELECT MGR
FROM EMP
WHERE ENAME = 'SCOTT')));
---------------------------------------------------------------------------
List employees whose job is same as SCOTT and their salary greater than SMITH’s salary.
SELECT *
FROM EMP
WHERE JOB IN (SELECT JOB
FROM EMP
WHERE ENAME='SCOTT')
AND
SAL > (SELECT SAL
FROM EMP
WHERE ENAME='SMITH');
--------------------------------------------------------------------------
Display all the employees whose job is same as SCOTT and ALLEN.
SELECT *
FROM EMP
WHERE JOB IN (SELECT JOB
FROM EMP
WHERE ENAME IN ('SCOTT','ALLEN'));
-----------------------------------------------------------------------------
Display all the employees who are actual managers.
SELECT *
FROM EMP
WHERE MGR IN (SELECT EMPNO
FROM EMP);
----------------------------------------------------------------
Display who are all the employees reporting to SCOTT.
SELECT *
FROM EMP
WHERE MGR IN (SELECT EMPNO
FROM EMP
WHERE ENAME='SCOTT');
-----------------------------------------------------------------------
Display the 2nd maximum salary.
SELECT MAX(HIREDATE)
FROM EMP
WHERE HIREDATE < (SELECT MAX(HIREDATE)
FROM EMP);
                    OR
SELECT A.SAL
FROM EMP A
WHERE 1 = (SELECT COUNT(DISTINCT(SAL))
FROM EMP B
WHERE A.SAL < B.SAL);
----------------------------------------------------------------
Display the 3rd maximum salary.
SELECT MAX(HIREDATE)
FROM EMP
WHERE HIREDATE < (SELECT MAX(HIREDATE)
FROM EMP
WHERE HIREDATE < (SELECT MAX(HIREDATE)
FROM EMP));
                       OR
SELECT A.SAL
FROM EMP A
WHERE 2 = (SELECT COUNT(DISTINCT(SAL))
FROM EMP B
WHERE A.SAL < B.SAL);
----------------------------------------------------------------------------
Display all the employees who are earning more than all the MANAGER(JOB).
SELECT *
FROM EMP
WHERE SAL >ALL (SELECT SAL
FROM EMP
WHERE JOB='MANAGER');
                            OR
SELECT *
FROM EMP
WHERE SAL > (SELECT MAX(SAL)
FROM EMP
WHERE JOB='MANAGER');
---------------------------------------------------------------
Display all the employees who are earning more than any of the MANAGER(JOB).
SELECT *
FROM EMP
WHERE SAL >ANY (SELECT SAL
FROM EMP
WHERE JOB='MANAGER');
                            OR
SELECT *
FROM EMP
WHERE SAL > (SELECT MIN(SAL)
FROM EMP
WHERE JOB='MANAGER');
--------------------------------------------------------------------------------------
Select empno, job and salary of all the ANALYST who are earning MORE THAN any of the MANAGER(JOB).
SELECT EMPNO,JOB,SAL
FROM EMP
WHERE JOB='ANALYST' AND SAL >ANY (SELECT SAL
FROM EMP
WHERE JOB='MANAGER');
                          OR
SELECT EMPNO,JOB,SAL
FROM EMP
WHERE JOB='ANALYST' AND SAL > (SELECT MIN(SAL)
FROM EMP
WHERE JOB='MANAGER');
---------------------------------------------------------------------------
Select the department name and location of all the employees working for CLARK.
SELECT DNAME,LOC
FROM DEPT
WHERE DEPTNO IN (SELECT DEPTNO
FROM EMP
WHERE MGR IN (SELECT EMPNO
FROM EMP
WHERE ENAME='CLARK'));
--------------------------------------------------------------------------------
Select all the employees working for DALLAS.
SELECT *
FROM EMP
WHERE DEPTNO IN (SELECT DEPTNO
FROM DEPT
WHERE LOC='DALLAS');
-------------------------------------------------------------------------------------
Display all the employees whose salary is greater than avg sal of department 20.
SELECT SAL
FROM EMP
WHERE SAL > (SELECT AVG(SAL)
FROM EMP
WHERE DEPTNO=20);
--------------------------------------------------------------------------------------------
Display all the employees who gets maximum salary.
SELECT *
FROM EMP
WHERE SAL IN (SELECT MAX(SAL)
FROM EMP);
-------------------------------------------------------------------------------------------------
Display first employee record based on hiredate.
SELECT *
FROM EMP
WHERE HIREDATE IN (SELECT MIN(HIREDATE)
FROM EMP);
-------------------------------------------------------------------------
List the employees who has salary greater than ALLEN.
SELECT *
FROM EMP
WHERE SAL > (SELECT SAL
FROM EMP
WHERE ENAME='ALLEN');
-------------------------------------------------------------------------------------------
List the department names that are having ANALYST.
SELECT DNAME
FROM DEPT
WHERE DEPTNO IN (SELECT DEPTNO
FROM EMP
WHERE JOB='ANALYST');
                       OR
SELECT DNAME
FROM DEPT D
WHERE EXISTS (SELECT JOB
FROM EMP E
WHERE E.DEPTNO=D.DEPTNO AND JOB='ANALYST');
---------------------------------------------------------------------------
List the department names which are having SALESMAN in it.
SELECT DNAME
FROM DEPT
WHERE DEPTNO IN (SELECT DEPTNO
FROM EMP
WHERE JOB='SALESMAN');
----------------------------------------------------------------------------------------
Display the employees whose location is having at least one 'O' in it.
SELECT *
FROM EMP
WHERE DEPTNO IN (SELECT DEPTNO
FROM DEPT
WHERE LOC LIKE '%O%');
--------------------------------------------------------------------------------------------
List the department names that are having at least 1 employee in it.
SELECT DNAME
FROM DEPT
WHERE DEPTNO IN (SELECT DEPTNO
FROM EMP
GROUP BY DEPTNO
HAVING COUNT(*)>=1);
--------------------------------------------------------------------------------------
List the department names that are having at least 4 employees in it
SELECT DNAME
FROM DEPT
WHERE DEPTNO IN (SELECT DEPTNO
FROM EMP
GROUP BY DEPTNO
HAVING COUNT(*)>=4);
------------------------------------------------------------------------------------
Display the department names which are having at least two CLERKS in it.
SELECT DNAME
FROM DEPT
WHERE DEPTNO IN (SELECT DEPTNO
                 FROM EMP
                 WHERE JOB='CLERK'
                 GROUP BY DEPTNO
                 HAVING COUNT(*) >=2)
-----------------------------------------------------------------------------------------------
Display empno ,ename ,deptno and sal, sort the output based on name with in name by deptno and with in deptno by sal.
SELECT EMPNO,ENAME,SAL,DEPTNO
FROM EMP
GROUP BY ENAME,DEPTNO,SAL;
-------------------------------------------------------------------------------------------
Display the name of the employee along with their annual salary.The name of the employee earning highest annual salary should appear first.
SELECT ENAME, SAL*12 "ANNUAL SALARY"
FROM EMP
ORDER BY "ANNUAL SALARY" DESC;
                 OR
SELECT ENAME, SAL*12 "ANNUAL SALARY"
FROM EMP
ORDER BY 2 DESC;
---------------------------------------------------------------------------------
Display deptno and total number of employees working in each department.
SELECT DEPTNO,COUNT(*)
FROM EMP
GROUP BY DEPTNO;
------------------------------------------------------------------------------------
Display the various jobs and total number of employees within each job group.
SELECT JOB,COUNT(*)
FROM EMP
GROUP BY JOB;
-------------------------------------------------------------------------------------
Display the various jobs along with total salary for each of the jobs where total salary is greater than 40000.
SELECT JOB,SUM(SAL)
FROM EMP
GROUP BY JOB
HAVING SUM(SAL) > 40000;
------------------------------------------------------------------------------------------
Display the various jobs along with total number of employees in each job.The output should contain only those jobs with more than three employee.
SELECT JOB,COUNT(*)
FROM EMP
GROUP BY JOB
HAVING COUNT(*)>3;
--------------------------------------------------------------------------------------
Display no. of emp for each managers for 'RESEARCH' AND 'ACCOUNTING'.
SELECT B.ENAME MANAGER,COUNT(*)
FROM EMP A, EMP B
WHERE A.MGR=B.EMPNO AND A.DEPTNO IN (SELECT DEPTNO
                                     FROM DEPT
                                     WHERE DNAME IN ('ACCOUNTING','RESEARCH'))
GROUP BY B.ENAME;
----------------------------------------------------------------------------------
Display the name of the employees who joined on the same date.
SELECT A.ENAME,A.HIREDATE
FROM EMP A, EMP B
WHERE A.HIREDATE=B.HIREDATE AND A.EMPNO<>B.EMPNO;
--------------------------------------------------------------------------
Display the manager name who is having maximum number of employees working under him.
SELECT B.ENAME,COUNT(*)
FROM EMP A, EMP B
WHERE A.MGR=B.EMPNO
GROUP BY B.ENAME
HAVING COUNT(*)=(SELECT MAX(COUNT(MGR))
FROM EMP
GROUP BY MGR);
-----------------------------------------------------------------------------
Display those emp whose manager name is 'JONES' and also with his manager name.
SELECT A.EMPNO,A.ENAME EMPLOYEES, B.EMPNO,B.ENAME MANAGER
FROM EMP A, EMP B
WHERE A.MGR=B.EMPNO AND B.EMPNO IN (SELECT EMPNO
FROM EMP
WHERE ENAME='JONES');
----------------------------------------------------------------------------
Display those emp who havegh same managers.
SELECT DISTINCT A.EMPNO, A.ENAME EMPLOYEE
FROM EMP A, EMP B
WHERE A.MGR=B.MGR AND A.EMPNO<>B.EMPNO;
---------------------------------------------------------------------------------
Display all the employees whose job has string 'MAN'.
SELECT ENAME,JOB
FROM EMP
WHERE INSTR(JOB,'MAN',1) >0;
-----------------------------------------------------------------------------
Display all the emp whose name hass 'L' as third character.
SELECT ENAME
FROM EMP
WHERE SUBSTR(ENAME,3,1)='L';
------------------------------------------------------------------------
Display the result in he following formt, first character in lower case and rest in upper case.
SELECT CONCAT(LOWER(SUBSTR(ENAME,1,1)),UPPER(SUBSTR(ENAME,2)) )
FROM EMP
-------------------------------------------------------------------------
Replace third character with * in the ename column.
SELECT ENAME, CONCAT(REPLACE(ENAME, SUBSTR(ENAME,3), '*'), SUBSTR(ENAME,4) )
FROM EMP;
-----------------------------------------------------------------------------------
Display ename with first three character in upper case and remaining in lower case.
SELECT ENAME,  CONCAT(UPPER(SUBSTR(ENAME,1,3)), LOWER(SUBSTR(ENAME,4)) )
FROM EMP;
-----------------------------------------------------------------------------------------------
Display the number of occurence of 'L' in each name.
SELECT ENAME, LENGTH(ENAME)  -  LENGTH(REPLACE(ENAME,'L'))
FROM EMP;
-------------------------------------------------------------------------------------
Display all the employees which has at least two 'L's in it.
SELECT ENAME
FROM EMP

WHERE INSTR(ENAME,'LL')<>0;

REGULAR EXPRESSION IN UFT



REGULAR EXPRESSION
It is a combination of wild cards & characters used for pattern matching.
A wildcard can be used at any location any number of times.
Wildcards
1) .          (a dot) -> matches with any single character
Ex,
a) a.c     - matches with the following,
abc
a1c
a$c
a c

2) {n} -> matches with ‘n’ occurrences of previous character.
Ex,
La{3} -> Laaa
(La){3} -> LaLaLa

.{3} -> matches with any 3 characters.
. . .
Abc
A$3
A12
- - -         -> 3 spaces
3) {n, m} -> matches with minimum ‘n’ occurrences & maximum ‘m’ occurrences of previous characters.
Ex,
(zup){2, 4}
Zup zup
Zup zup zup zup

4) {n, } -> matches with minimum ‘n’ occurrences of previous characters.
Ex,
Yahoo {1, }
Yahoo
Yahoooooooooooo …. (infinity)

5) ? -> matches with 0 or 1 occurrence of previous characters which is equivalent to {0, 1}
 Ex: UFT?--à QT , UFT
6) + -> matches with 1 or more occurrence(infinity) of previous character equivalence to {1, }
Ex – Yahoo+
Matches              Yahoo
                                Yahoooooooooooooooooo …
7) *  matches with zero or more occurrences of previous character which is equivalent to {0, }

8) .* -> matches with any character in any number of lines

9) Set of letters within square brackets -> matches with any single letter from the list
Ex,
[rcb]at
Matches                              rat
                                 cat
                                bat
Not cbat

10) within square brackets , set of digits -> matches with any digit from the list.
Ex ,
[1, 5, 7]
Matches                              1
                                5
                                 7
11) Range of characters within square braces – matches with any single letter from the range.
Ex,
[a – z]1
Matches              a1
                                a1
                                ..
                                ..
                                ..
                                z1
[0 – 9] -> matches with any single digit from the range 0 -9.

12) x | y
x OR y -> matches with either x or y.
Ex,
f|wood -> matches with ‘f’ or wood
(f|w)ood -> matches with food, wood

Wednesday 21 December 2016

SAMPLE TABLES



Sample table: Agent
+------------+----------------------+--------------------+------------+-----------------+---------+
| AGENT_CODE | AGENT_NAME           | WORKING_AREA       | COMMISSION | PHONE_NO        | COUNTRY |
+------------+----------------------+--------------------+------------+-----------------+---------+
| A007       | Ramasundar           | Bangalore          |       0.15 | 077-25814763    |         |
| A003       | Alex                 | London             |       0.13 | 075-12458969    |         |
| A008       | Alford               | New York           |       0.12 | 044-25874365    |         |
| A011       | Ravi Kumar           | Bangalore          |       0.15 | 077-45625874    |         |
| A010       | Santakumar           | Chennai            |       0.14 | 007-22388644    |         |
| A012       | Lucida               | San Jose           |       0.12 | 044-52981425    |         |
| A005       | Anderson             | Brisban            |       0.13 | 045-21447739    |         |
| A001       | Subbarao             | Bangalore          |       0.14 | 077-12346674    |         |
| A002       | Mukesh               | Mumbai             |       0.11 | 029-12358964    |         |
| A006       | McDen                | London             |       0.15 | 078-22255588    |         |
| A004       | Ivan                 | Torento            |       0.15 | 008-22544166    |         |
| A009       | Benjamin             | Hampshair          |       0.11 | 008-22536178    |         |
+------------+----------------------+--------------------+------------+-----------------+---------+

Sampel Customer:
+-----------+-------------+-------------+--------------+--------------+-------+-------------+-------------+-------------+---------------+--------------+------------+
|CUST_CODE  | CUST_NAME   | CUST_CITY   | WORKING_AREA | CUST_COUNTRY | GRADE | OPENING_AMT | RECEIVE_AMT | PAYMENT_AMT |OUTSTANDING_AMT| PHONE_NO     | AGENT_CODE |
+-----------+-------------+-------------+--------------+--------------+-------+-------------+-------------+-------------+---------------+--------------+------------+
| C00013    | Holmes      | London      | London       | UK           |     2 |     6000.00 |     5000.00 |     7000.00 |       4000.00 | BBBBBBB      | A003       |
| C00001    | Micheal     | New York    | New York     | USA          |     2 |     3000.00 |     5000.00 |     2000.00 |       6000.00 | CCCCCCC      | A008       |
| C00020    | Albert      | New York    | New York     | USA          |     3 |     5000.00 |     7000.00 |     6000.00 |       6000.00 | BBBBSBB      | A008       |
| C00025    | Ravindran   | Bangalore   | Bangalore    | India        |     2 |     5000.00 |     7000.00 |     4000.00 |       8000.00 | AVAVAVA      | A011       |
| C00024    | Cook        | London      | London       | UK           |     2 |     4000.00 |     9000.00 |     7000.00 |       6000.00 | FSDDSDF      | A006       |
| C00015    | Stuart      | London      | London       | UK           |     1 |     6000.00 |     8000.00 |     3000.00 |      11000.00 | GFSGERS      | A003       |
| C00002    | Bolt        | New York    | New York     | USA          |     3 |     5000.00 |     7000.00 |     9000.00 |       3000.00 | DDNRDRH      | A008       |
| C00018    | Fleming     | Brisban     | Brisban      | Australia    |     2 |     7000.00 |     7000.00 |     9000.00 |       5000.00 | NHBGVFC      | A005       |
| C00021    | Jacks       | Brisban     | Brisban      | Australia    |     1 |     7000.00 |     7000.00 |     7000.00 |       7000.00 | WERTGDF      | A005       |
| C00019    | Yearannaidu | Chennai     | Chennai      | India        |     1 |     8000.00 |     7000.00 |     7000.00 |       8000.00 | ZZZZBFV      | A010       |
| C00005    | Sasikant    | Mumbai      | Mumbai       | India        |     1 |     7000.00 |    11000.00 |     7000.00 |      11000.00 | 147-25896312 | A002       |
| C00007    | Ramanathan  | Chennai     | Chennai      | India        |     1 |     7000.00 |    11000.00 |     9000.00 |       9000.00 | GHRDWSD      | A010       |
| C00022    | Avinash     | Mumbai      | Mumbai       | India        |     2 |     7000.00 |    11000.00 |     9000.00 |       9000.00 | 113-12345678 | A002       |
| C00004    | Winston     | Brisban     | Brisban      | Australia    |     1 |     5000.00 |     8000.00 |     7000.00 |       6000.00 | AAAAAAA      | A005       |
| C00023    | Karl        | London      | London       | UK           |     0 |     4000.00 |     6000.00 |     7000.00 |       3000.00 | AAAABAA      | A006       |
| C00006    | Shilton     | Torento     | Torento      | Canada       |     1 |    10000.00 |     7000.00 |     6000.00 |      11000.00 | DDDDDDD      | A004       |
| C00010    | Charles     | Hampshair   | Hampshair    | UK           |     3 |     6000.00 |     4000.00 |     5000.00 |       5000.00 | MMMMMMM      | A009       |
| C00017    | Srinivas    | Bangalore   | Bangalore    | India        |     2 |     8000.00 |     4000.00 |     3000.00 |       9000.00 | AAAAAAB      | A007       |
| C00012    | Steven      | San Jose    | San Jose     | USA          |     1 |     5000.00 |     7000.00 |     9000.00 |       3000.00 | KRFYGJK      | A012       |
| C00008    | Karolina    | Torento     | Torento      | Canada       |     1 |     7000.00 |     7000.00 |     9000.00 |       5000.00 | HJKORED      | A004       |
| C00003    | Martin      | Torento     | Torento      | Canada       |     2 |     8000.00 |     7000.00 |     7000.00 |       8000.00 | MJYURFD      | A004       |
| C00009    | Ramesh      | Mumbai      | Mumbai       | India        |     3 |     8000.00 |     7000.00 |     3000.00 |      12000.00 | Phone No     | A002       |
| C00014    | Rangarappa  | Bangalore   | Bangalore    | India        |     2 |     8000.00 |    11000.00 |     7000.00 |      12000.00 | AAAATGF      | A001       |
| C00016    | Venkatpati  | Bangalore   | Bangalore    | India        |     2 |     8000.00 |    11000.00 |     7000.00 |      12000.00 | JRTVFDD      | A007       |
| C00011    | Sundariya   | Chennai     | Chennai      | India        |     3 |     7000.00 |    11000.00 |     7000.00 |      11000.00 | PPHGRTS      | A010       |
+-----------+-------------+-------------+--------------+--------------+-------+-------------+-------------+-------------+---------------+--------------+------------+


Sample table:orders
   ORD_NUM ORD_AMOUNT ADVANCE_AMOUNT ORD_DATE  CUST_CODE       AGENT_CODE      ORD_DESCRIPTION
---------- ---------- -------------- --------- --------------- --------------- -----------------
    200114       3500           2000 15-AUG-08 C00002          A008
    200122       2500            400 16-SEP-08 C00003          A004
    200118        500            100 20-JUL-08 C00023          A006
    200119       4000            700 16-SEP-08 C00007          A010
    200121       1500            600 23-SEP-08 C00008          A004
    200130       2500            400 30-JUL-08 C00025          A011
    200134       4200           1800 25-SEP-08 C00004          A005
    200108       4000            600 15-FEB-08 C00008          A004
    200103       1500            700 15-MAY-08 C00021          A005
    200105       2500            500 18-JUL-08 C00025          A011
    200109       3500            800 30-JUL-08 C00011          A010
    200101       3000           1000 15-JUL-08 C00001          A008
    200111       1000            300 10-JUL-08 C00020          A008
    200104       1500            500 13-MAR-08 C00006          A004
    200106       2500            700 20-APR-08 C00005          A002
    200125       2000            600 10-OCT-08 C00018          A005
    200117        800            200 20-OCT-08 C00014          A001
    200123        500            100 16-SEP-08 C00022          A002
    200120        500            100 20-JUL-08 C00009          A002
    200116        500            100 13-JUL-08 C00010          A009
    200124        500            100 20-JUN-08 C00017          A007
    200126        500            100 24-JUN-08 C00022          A002
    200129       2500            500 20-JUL-08 C00024          A006
    200127       2500            400 20-JUL-08 C00015          A003
    200128       3500           1500 20-JUL-08 C00009          A002
    200135       2000            800 16-SEP-08 C00007          A010
    200131        900            150 26-AUG-08 C00012          A012
    200133       1200            400 29-JUN-08 C00009          A002
    200100       1000            600 08-JAN-08 C00015          A003
    200110       3000            500 15-APR-08 C00019          A010
    200107       4500            900 30-AUG-08 C00007          A010
    200112       2000            400 30-MAY-08 C00016          A007
    200113       4000            600 10-JUN-08 C00022          A002
    200102       2000            300 25-MAY-08 C00012          A012


'*********************************************************************************
SAMPLE TABLE: REGIONS

REGION_ID REGION_NAME
--------- ------------------------
        1 Europe
        2 Americas
        3 Asia
        4 Middle East and Africa


SAMPEL TABLE:COUNTRIES

CO_ID COUNTRY_NAME                              REGION_ID
-- ---------------------------------------- ----------
AR Argentina                                         2
AU Australia                                         3
BE Belgium                                           1
BR Brazil                                            2
CA Canada                                            2
CH Switzerland                                       1
CN China                                             3
DE Germany                                           1
DK Denmark                                           1
EG Egypt                                             4
FR France                                            1
HK HongKong                                          3
IL Israel                                            4
IN India                                             3
IT Italy                                             1
JP Japan                                             3
KW Kuwait                                            4

SAMPLE TABLE:LOCATIONS
LOCATION_ID STREET_ADDRESS                           POSTAL_CODE  CITY                           STATE_PROVINCE            CO_ID
----------- ---------------------------------------- ------------ ------------------------------ ---
       1000 1297 Via Cola di Rie                     00989        Roma                                                     IT
       1100 93091 Calle della Testa                  10934        Venice                                                   IT
       1200 2017 Shinjuku-ku                         1689         Tokyo                          Tokyo Prefecture          JP
       1300 9450 Kamiya-cho                          6823         Hiroshima                                                JP
       1400 2014 Jabberwocky Rd                      26192        Southlake                      Texas                     US
       1500 2011 Interiors Blvd                      99236        South San Francisco            California                US
       1600 2007 Zagora St                           50090        South Brunswick                New Jersey                US
       1700 2004 Charade Rd                          98199        Seattle                        Washington                US
       1800 147 Spadina Ave                          M5V 2L7      Toronto                        Ontario                   CA
       1900 6092 Boxwood St                          YSW 9T2      Whitehorse                     Yukon                     CA
       2000 40-5-12 Laogianggen                      190518       Beijing                                                  CN
       2100 1298 Vileparle (E)                       490231       Bombay                         Maharashtra               IN


SAMPLE TABLE:DEPARTMENTS:

DEPARTMENT_ID DEPARTMENT_NAME                MANAGER_ID LOCATION_ID
------------- ------------------------------ ---------- -----------
           10 Administration                        200        1700
           20 Marketing                             201        1800
           30 Purchasing                            114        1700
           40 Human Resources                       203        2400
           50 Shipping                              121        1500
           60 IT                                    103        1400
           70 Public Relations                      204        2700
           80 Sales                                 145        2500
           90 Executive                             100        1700
          100 Finance                               108        1700