SQL Assignment
SQL Assignment
SQL Assignment
SELECT*
FROM EMP
WHERE SAL>=1000 AND SAL>=2000;
SELECT*
FROM EMP
WHERE SAL>=1000 AND SAL>=2000 AND JOB != SALESMAN;
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%';
23) DISPLAY ALL THE EMPLOYEES WHOSE JOB HAS STRING ‘MAN’ IN IT
SELECT*
FROM EMP
WHERE JOB LIKE '%MAN%';
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';
SELECT *
FROM EMP
WHERE HIREDATE LIKE '%NOV%' OR HIREDATE LIKE '%DEC%';
26)LIST ALL THE EMPLOYEE NAMES AND COMISSION FOR THOSE EMPLOYEES
WHO EARN COMISSION MORE THAN THEIR SALARY
SELECT *
FROM EMP
WHERE COMM > SAL;
SELECT *
FROM EMP
WHERE MGR IS NOT NULL AND ENAME LIKE 'S%';
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
SELECT *
FROM EMP
WHERE ENAME LIKE 'A%' OR ENAME LIKE 'E%' OR ENAME LIKE 'I%' OR ENAME
LIKE 'O%' OR ENAME LIKE 'U%';
SELECT COUNT(SAL)
FROM EMP
WHERE HIREDATE BETWEEN 'FEB';
SELECT COUNT(EMPNO)
FROM EMP
WHERE MGR = 7839;
COUNT(EMPNO)
-----------------------
3
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;
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(DEPTNO)
FROM EMP;
SELECT COUNT(DEPTNO)
FROM EMP
WHERE DEPTNO IN (10,20,30);
COUNT(DEPTNO)
--------------------------
14
DATE : 30 – 07 – 2021
Single-Row Functions
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