SQL Assignment

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 9

21) LIST THE EMPLOYEES WHOSE SALARY NOT IN THE RANGE OF 1000 TO 2000

AND WORKING IN DEPT 10,20 OR 30 EXCEPT ALL SALESMEN

SELECT*
FROM EMP
WHERE SAL>=1000 AND SAL>=2000;

SELECT*
FROM EMP
WHERE SAL>=1000 AND SAL>=2000 AND JOB != SALESMAN;

EMPNO ENAME JOB MGR HIREDATE SAL COMM


DEPTNO
--------- ---------- --------- ---------- ----------------
---------- ---------- ----------
7566 JONES MANAGER 7839 02-APR-81 2975
20
7698 BLAKE MANAGER 7839 01-MAY-81 2850
30
7782 CLARK MANAGER 7839 09-JUN-81 2450
10
7788 SCOTT ANALYST 7566 19-APR-87 3000
20
7839 KING PRESIDENT 17-NOV-81 5000
10
7902 FORD ANALYST 7566 03-DEC-81 3000
20

22) LIST THE DEPARTMENT NAMES WHICH ARE HAVING LETTER ‘O’ IN THEIR
LOCATIONS AS WELL AS THEIR DEPARTMENT NAMES

SELECT*
FROM DEPT
WHERE LOC LIKE'%O%' AND DNAME LIKE'%O%';

DEPTNO DNAME LOC


------ -------------- -------------
10 ACCOUNTING NEW YORK
40 OPERATIONS BOSTON

23) DISPLAY ALL THE EMPLOYEES WHOSE JOB HAS STRING ‘MAN’ IN IT

SELECT*
FROM EMP
WHERE JOB LIKE '%MAN%';

EMPNO ENAME JOB MGR HIREDATE SAL COMM


DEPTNO
------ ---------- -------- --------- --------------
---------- ---------- ------------
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300
30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500
30
7566 JONES MANAGER 7839 02-APR-81 2975
20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400
30
7698 BLAKE MANAGER 7839 01-MAY-81 2850
30
7782 CLARK MANAGER 7839 09-JUN-81 2450
10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0
30

24)LIST THE EMPLOYEES WHO ARE HIRED AFTER 82 AND BEFORE 87.

SELECT *
FROM EMP
WHERE HIREDATE >= '01-JAN-1983' AND HIREDATE <='31-DEC-1987';

EMPNO ENAME JOB MGR HIREDATE SAL COMM


DEPTNO
----- ---------- --------- -------- ---------------
-------- ---------- ----------
7788 SCOTT ANALYST 7566 19-APR-87 3000
20
7876 ADAMS CLERK 7788 23-MAY-87 1100
20

25)WAQTD ALL THE DETAILS OF EMPLOYEES HIRED IN NOVEMBER AND


DECEMBER.

SELECT *
FROM EMP
WHERE HIREDATE LIKE '%NOV%' OR HIREDATE LIKE '%DEC%';

EMPNO ENAME JOB MGR HIREDATE SAL COMM


DEPTNO
------ ---------- ---------- --------- ----------
---------- ---------- -------------
7369 SMITH CLERK 7902 17-DEC-80 800
20
7839 KING PRESIDENT 17-NOV-81 5000
10
7900 JAMES CLERK 7698 03-DEC-81 950
30
7902 FORD ANALYST 7566 03-DEC-81 3000
20

26)LIST ALL THE EMPLOYEE NAMES AND COMISSION FOR THOSE EMPLOYEES
WHO EARN COMISSION MORE THAN THEIR SALARY

SELECT *
FROM EMP
WHERE COMM > SAL;

EMPNO ENAME JOB MGR HIREDATE SAL COMM


DEPTNO
------ ---------- --------- ---------- -------------
---------- ---------- ----------
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400
30

27)WAQTD NAME AND DESIGNATION FOR ALL THE EMPLOYEES HAVING


REPORTING MANAGERS AND ALSO THRIE NAMES STARTING WITH ‘S’

SELECT *
FROM EMP
WHERE MGR IS NOT NULL AND ENAME LIKE 'S%';

EMPNO ENAME JOB MGR HIREDATE SAL COMM


DEPTNO
----- ---------- --------- ---------- ---------
-------- ---------- -------------
7369 SMITH CLERK 7902 17-DEC-80 800
20
7788 SCOTT ANALYST 7566 19-APR-87 3000
20

28)WAQTD NAME AND SALARY OF ALL THE EMPLOYEES IF THEIR ANNUAL


SALARY ENDS WITH ‘0’

SELECT EMP.*,SAL*12 AS "ANNUAL SALARY"


FROM EMP
WHERE SAL*12 LIKE '%0';

EMPNO ENAME JOB MGR HIREDATE SAL COMM


DEPTNO ANNUAL SALARY
------ ---------- --------- ----------
------------------- ---------- ---------- -------------
--------------------------
7369 SMITH CLERK 7902 17-DEC-80 800
20 9600
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300
30 19200
7521 WARD SALESMAN 7698 22-FEB-81 1250 500
30 15000
7566 JONES MANAGER 7839 02-APR-81 2975
20 35700
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
15000
7698 BLAKE MANAGER 7839 01-MAY-81 2850
30 34200
7782 CLARK MANAGER 7839 09-JUN-81 2450
10 29400
7788 SCOTT ANALYST 7566 19-APR-87 3000
20 36000
7839 KING PRESIDENT 17-NOV-81 5000
10 60000
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0
30 18000
7876 ADAMS CLERK 7788 23-MAY-87 1100
20 13200
7900 JAMES CLERK 7698 03-DEC-81 950
30 11400
7902 FORD ANALYST 7566 03-DEC-81 3000
20 36000
7934 MILLER CLERK 7782 23-JAN-82 1300
10 15600

29)WAQTD NAME OF THE EMPLOYEE HAVING ATLEAST 2L’s IN HIS NAME .

SELECT *
FROM EMP
WHERE ENAME LIKE '%L%L%';
EMPNO ENAME JOB MGR HIREDATE SAL COMM
DEPTNO
------ ---------- -------------- ---------- ---------------
---------- ---------- ----------
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300
30
7934 MILLER CLERK 7782 23-JAN-82 1300
10

30)WAQTD NAME OF THE EMPLOYEES WHOS NAME STARTS WITH A ‘VOWEL’

SELECT *
FROM EMP
WHERE ENAME LIKE 'A%' OR ENAME LIKE 'E%' OR ENAME LIKE 'I%' OR ENAME
LIKE 'O%' OR ENAME LIKE 'U%';

EMPNO ENAME JOB MGR HIREDATE SAL COMM


DEPTNO
---------- ---------- --------- ---------- --------------
---------- ---------- ----------
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300
30
7876 ADAMS CLERK 7788 23-MAY-87 1100
20

ASSIGNMENT ON DATE : 27 – 07 – 2021

6.WAQTD TOTAL SALARY NEEDED TO PAY EMPLOYEES HIRED IN FEB

SELECT COUNT(SAL)
FROM EMP
WHERE HIREDATE BETWEEN 'FEB';

7.WAQTD NUMBER OF EMPLOYEES REPORTING TO 7839 (MGR)

SELECT COUNT(EMPNO)
FROM EMP
WHERE MGR = 7839;

COUNT(EMPNO)
-----------------------
3

8.WAQTD NUMBER OF EMPLOYEES GETTING COMISSION IN DEPTNO 30

SELECT COUNT(EMPNO)
FROM EMP
WHERE COMM IS NOT NULL AND DEPTNO = 30;

COUNT(EMPNO)
------------------------
4

9.WAQTD AVG SAL , TOTAL SAL , NUMBER OF EMPS AND MAXIMUM SALARY GIVEN
TO EMPLOYEES WORKING AS PRESIDENT
SELECT AVG(SAL),SUM(SAL),COUNT(EMPNO),MAX(SAL) AS "MAX SALARY TO PRESIDENT"
FROM EMP;

AVG(SAL) SUM(SAL) COUNT(EMPNO) MAX SALARY TO PRESIDENT


-------------- -------------- ------------------------
------------------------------------------
2073.21429 29025 14 5000

10.WAQTD NUMBER OF EMPLOYEES HAVING 'A' IN THEIR NAMES

SELECT COUNT(ENAME)
FROM EMP
WHERE ENAME LIKE '%A%';

COUNT(ENAME)
------------------------
7

11.WAQTD NUMBER OF EMPS AND TOTAL SALary needed to pay THE EMPLOYEES WHO HAVE 2
CONSICUTIVE L's IN THEIR NAMES

SELECT COUNT(ENAME) , SUM(SAL) || ' RS ' AS "TOTAL SALARY"


FROM EMP
WHERE ENAME LIKE '%LL%';

COUNT(ENAME) TOTAL SALARY


------------------------- ------------------------
2 2900 RS

12.WAQTD NUMBER OF DEPARTMENTS PRESENT IN EMPLOYEE TABLE

SELECT COUNT(DEPTNO)
FROM EMP;

SELECT COUNT(DEPTNO)
FROM EMP
WHERE DEPTNO IN (10,20,30);

COUNT(DEPTNO)
--------------------------
14

DATE : 30 – 07 – 2021

Single-Row Functions

1)UPPER - Function converts a string to upper case.


EXAMPLE :-
SELECT UPPER(ENAME) FROM EMP;
UPPER(ENAME)
--------------------
SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK
SCOTT
KING
TURNER
ADAMS
JAMES
FORD
2)LOWER - Function converts a string to lower case.
EXAMPLE :-
SELECT LOWER(ENAME) FROM EMP;
LOWER(ENAME)
---------------------
smith
allen
ward
jones
martin
blake
clark
scott
king
turner
adams
james
ford
miller

3)INITCAP - Function converts only the initial alphabets of a string to upper case.

EXAMPLE :-
SELECT INITCAP(ENAME) FROM EMP;

INITCAP(ENAME)
----------------------
Smith
Allen
Ward
Jones
Martin
Blake
Clark
Scott
King
Turner
Adams
James
Ford
Miller
4)CONCAT - Function concatenates two string values.
EXAMPLE :-
SELECT CONCAT(EMPNO,ENAME) FROM EMP;
CONCAT(EMPNO,ENAME)
-----------------------------------
7369SMITH
7499ALLEN
7521WARD
7566JONES
7654MARTIN
7698BLAKE
7782CLARK
7788SCOTT
7839KING
7844TURNER
7876ADAMS
7900JAMES
7902FORD
7934MILLER
5)LENGTH - Function returns the length of the input string.
EXAMPLE :-
SELECT LENGTH('Structured Query Language')AS LENGTHOFSTRING FROM DUAL;
LENGTHOFSTRING
------------------------
25
6)SUBSTR - Function returns a portion of a string from a given start point to an
end point.
EXAMPLE :-
SELECT SUBSTR(ENAME,2,4) FROM EMP;
SUBSTR
----------
MITH
LLEN
ARD
ONES
ARTI
LAKE
LARK
COTT
ING
URNE
DAMS
AMES
ORD
ILLE
7)INSTR - Function returns numeric position of a character or a string in a given
string.
EXAMPLE :-
SELECT INSTR (ENAME,'A') FROM EMP;
INSTR(ENAME,'A')
------------------------
0
1
2
0
2
3
3
0
0
0
1
2
0
0
8)LPAD and RPAD - Functions pad the given string upto a specific length with a
given character.
EXAMPLE :-
SELECT RPAD(ENAME,8,'**') ||LPAD(EMPNO,6,'**') FROM EMP;
RPAD(ENAME)||LPAD(EMPNO)
------------------------------------------
SMITH*****7369
ALLEN*****7499
WARD******7521
JONES*****7566
MARTIN****7654
BLAKE*****7698
CLARK*****7782
SCOTT*****7788
KING******7839
TURNER****7844
ADAMS*****7876
JAMES*****7900
FORD******7902
MILLER****7934
9)TRIM - Function trims the string input from the start or end.
EXAMPLE :-
SELECT TRIM(' structured query language ') AS TrimmedString FROM DUAL;
TRIMMEDSTRING
---------------------------------
structured query language
10)REPLACE - Function replaces characters from the input string with a given
character.
EXAMPLE :-
SELECT REPLACE('SQL FUNCTIONS', 'SQL', 'SCHOOL')FROM DUAL;
REPLACE('SQL FUNCTIONS’)
-------------------------------------
SCHOOL FUNCTIONS
11)MONTHS_BETWEEN - Function returns the count of months between the two dates.

EXAMPLE :-
SELECT MONTHS_BETWEEN ('31-MAR-1995', '28-FEB-1994') FROM DUAL;

MONTHS_BETWEEN('31-MAR-1995','28-FEB-1994')
--------------------------------------------------------------------
13
12)ADD_MONTHS - Function add 'n' number of months to an input date.
EXAMPLE :-
SELECT ADD_MONTHS ('16-Sep-81', 3)FROM DUAL;
ADD_MONTH
------------------
16-DEC-81
13)LAST_DAY - Function returns last day of the month of the input date.
EXAMPLE :-
SELECT LAST_DAY ('01-JAN-1980')FROM DUAL;
LAST_DAY('01-JAN-1980')
---------------------------------
31-JAN-80
14)NEXT_DAY - Function returns the next day of the date specified.
EXAMPLE :-
SELECT NEXT_DAY ('01-Jun-91', 'Wednesday')FROM DUAL;
NEXT_DAY('01-Jun-08', 'Wednesday')
--------------------------------------------------
04-JUN-91
15)ROUND and TRUNC - Functions are used to round and truncate the number value.
EXAMPLE :-
SELECT ROUND(3.456) FROM DUAL;
ROUND(3.456)
------------------
3
SELECT TRUNC(1456.967) FROM DUAL;
TRUNC(1456.967)
-----------------------
1456
16)MOD - Functions used to return the remainder of the division operation between
two numbers.
EXAMPLE :-
SELECT MOD( 90,7) FROM DUAL
MOD(90,7)
----------=
6

You might also like