Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

09.02.2024 NVL Coalesce Notepad

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

SELECT * FROM DEPT;

SELECT * FROM EMP;

SELECT * FROM student19;


INSERT INTO student19 VALUES (2,'SHYAM',NULL,'','01014');

SELECT * FROM student19;

NULL:-
UNKNOW VALUE
UNDEFINED VALUE
ITS ABSENCE OF DATA

SELECT * FROM EMP;

SELECT * FROM EMP WHERE MGR = NULL;


SELECT * FROM EMP WHERE MGR IS NULL;

Q:- DISPLAY EMPLOYEE INFORMATION WHERE THERE IS NO REPORTING MANAGER?

SELECT * FROM EMP WHERE MGR IS NULL;

Q:- DISPLAY EMPLOYEE INFORMATION WHERE EMPLOYEES ARE GETTING NO COMMISSION?


SELECT * FROM EMP WHERE COMM IS NULL;

SELECT * FROM EMP;

CREATE TABLE STUDENT_DDL (ROLLNO NUMBER, NAME VARCHAR2(10));


INSERT INTO STUDENT_DDL VALUES (1,'RAM');
INSERT INTO STUDENT_DDL VALUES (2,'LAXMAN');
COMMIT;
SELECT * FROM STUDENT_DDL;

CREATE TABLE "STUDENT_DDL"


( "ROLLNO" NUMBER,
"NAME" VARCHAR2(10 BYTE)
);

SELECT dbms_metadata.get_ddl('TABLE','STUDENT_DDL') FROM DUAL;

SELECT dbms_metadata.get_ddl('TABLE','EMP')
FROM DUAL;

CREATE TABLE "STUDENT_DDL"


( "ROLLNO" NUMBER,
"NAME" VARCHAR2(10 BYTE)
);

Insert into STUDENT_DDL (ROLLNO,NAME) values (1,'RAM');


Insert into STUDENT_DDL (ROLLNO,NAME) values (2,'LAXMAN');

SELECT * FROM STUDENT_DDL;

Q:-DISPLAY EMPNO, ENAME, SAL FOR ALL THE EMPLOYEES?

SELECT EMPNO,ENAME,SAL FROM EMP;


FUNCTION:- FUNCTION IS A DATABASE OBJECT WHICH IS USED TO RETURN(SEND) SOME VALUES

IT IS TWO TYPE
1. SINGLE ROW FUNCTION
2. MULTIROW FUNCTION/AGGREGATE FUNCTION

SINGLE ROW FUNCTION:- THIS FUNCTION WORKS ON EACH ROW OF A TABLE


AND RETURN A SINGLE VALUE

ORACLE HAS PREDEFINED SOME FUNCTIONS


1. NVL FUNCTION

SELECT EMPNO,ENAME,SAL ,COMM, SAL + COMM FROM EMP;

SELECT COMM, NVL(COMM,0) FROM EMP;


SELECT COMM, NVL(COMM,100) FROM EMP;
DESC EMP;

SELECT COMM, NVL(COMM,'RAM') FROM EMP;


SELECT NAME, NVL(NAME,'KRISHNA') FROM STUDENT_DDL;
SELECT NAME, NVL(NAME,1000) FROM STUDENT_DDL;

SELECT EMPNO,ENAME,SAL, COMM, SAL + NVL(COMM,0) FROM EMP;

COLUMN OR EXPRESSION
SELECT NVL(NULL,100) FROM DUAL;
SELECT NVL('SCOTT',100) FROM DUAL;
SELECT NVL(200,100) FROM DUAL;
SELECT NVL(100,100) FROM DUAL;
SELECT NVL(100) FROM DUAL;--ORA-00909: invalid number of arguments

SELECT 100 FROM DUAL;

SELECT 'SCOTT' FROM DUAL;

SELECT 'ENAME' FROM EMP;


SELECT * FROM EMP;

SELECT EMPNO,ENAME,SAL,COMM,COMM , COMM FROM EMP;


SELECT EMPNO,ENAME,SAL,COMM,NVL(COMM,100) FROM EMP;

SELECT EMPNO ,ENAME,SAL ,NVL(COMM,0), SAL + NVL(COMM,0) FROM EMP;

SELECT EMPNO,ENAME,SAL, NVL(COMM,0), SAL +NVL(COMM,0)


FROM EMP WHERE ENAME ='JONES'

NVL2:-

SELECT EMPNO,ENAME,SAL,COMM,NVL2(COMM,200,100) FROM EMP;


SELECT EMPNO,ENAME,SAL,COMM,NVL2(COMM,SAL+200,SAL +100) FROM EMP;

1.NVL2 IS USED TO PROVIDE NULL VALUE TO ALTERNATE VALUE


AND NOT VALUE TO ALTERNATE VALUE
2. IT IS A SINGLE ROW FUNCTION
3. IT ACCEPTS 3 PARAMETER , FIRST IS A COLUMN/EXPRESSION ,
2ND IS ACUAL VALUE FOR NOT NULL, 3RD IS ACTUAL VALUE FOR NULL
4. DATA TYPE SHOULD MATCH BETWEEN THE PARAMETERS
SELECT EMPNO,ENAME,SAL,COMM,NVL2(COMM,200,100) FROM EMP;

SELECT NVL2('HI','HELLO', 'BYE') FROM DUAL;--OP HELLO


SELECT NVL2(NULL,'HELLO', 'BYE') FROM DUAL;--OP BYE

NULLIF:-
1. NULLIF USED TO COMPARE BETWEEN PARAMETERS IF MATECHS THEN DISPLAY NULL
IF DOESNT MATCH THEN DISPLAY FIRST PARAMETER VALUE
2. IT ACCEPTS TWO PARAMETER
3. IT IS A SINGLE ROW FUNCTION
4.DATA TYPE SHOULD MATCH BETWEEN THE PARAMETERS

SELECT NULLIF(100,100) FROM DUAL;--OP NULL


SELECT NULLIF(200,100) FROM DUAL;-- OP 200
SELECT ENAME, SAL, COMM ,NULLIF(SAL,COMM) FROM EMP;

SELECT NULLIF(NULL,NULL) FROM DUAL;--ERROR


--ORA-00932: inconsistent datatypes: expected - got CHAR

COALESCE:-
1.COALESCE FUNCTION IS USED TO PROVIDE FIRST NOT NULL VALUE AMONG THE PARAMETERS
2.WE CAN TAKE N NUMBER OF PARAMETERS
3.DATA TYPE SHOULD MATCH BETWEEN THE PARAMETERS
4.IT IS A SINGLE ROW FUNCTION

SELECT COALESCE(NULL,NULL,100,200,NULL) FROM DUAL;--OP 100


SELECT COALESCE(400,NULL,200,NULL) FROM DUAL; --OP 400
SELECT MGR,COMM,SAL, COALESCE(MGR,COMM,SAL) FROM EMP;

SELECT NVL(NULL,100) FROM DUAL;


SELECT COALESCE(NULL,100) FROM DUAL;

SELECT COALESCE(NULL,'ABC', 100) FROM DUAL;


--ORA-00932: inconsistent datatypes: expected CHAR got NUMBER

INTERVIEW: -DIFF BETWEEN NVL & COALESCE


ANS: -NVL ACCEPT ONLY TWO PARAMTER BUT COALESCE ACCEPT N NUMBER OF PARAMETERS

ALIAS NAME:-

SELECT * FROM EMP;

Q:- DISPLAY EMPID, EMPLOYEENAME, SALARY, ANNUALSALARY

SELECT EMPNO EMPID, ENAME AS EMPLOYEENAME, SAL AS SALARY,


SAL*12 ANNUALSAL
FROM EMP;

SELECT EMPNO "EMPID", ENAME AS "EMPLOYEE NAME", SAL AS SALARY,


SAL*12 "ANNUAL SAL"
FROM EMP;

SELECT EMPNO "EMPID", ENAME AS "EMPLOYEE NAME", SAL AS "salary",


SAL*12 "ANNUAL SAL"
FROM EMP;

ALIAS :-
1. ALIAS IS AN ALTERNATE NAME GIVEN FOR A COLUMN OR TABLE
2. ALIAS RENAME FOR TEMPORARY PURPOSE

ALIAS ARE TWO TYPES


1.COLOUMN ALIAS
2.TABLE ALIAS

1.COLOUMN ALIAS:-1. WHEN WE WRITE A NAME NEXT TO THE COLUMN NAME


IS KNOWN AS COLUMN ALIAS
2.BETWEEN COLUMN AND ALIAS SEPARATED BY AS KEYWORD
WHICH IS OPTIONAL
3.INSTEAD OF AS KEYWORD WE CAN TAKE A SPACE
4.Alias headings appears in uppercase by default.
5.TO APPLY SPACE BETWEEN THE ALIAS NAME OR CHANGE THE
UPPPER CASE TO LOWER CASE WE CAN USE ""(DOUBLE QUOTEATION MARK)
6.An alias can not be used in where condition
but can be used in Order by clause

SELECT EMPNO "EMPID", ENAME AS "EMPLOYEE NAME", SAL AS "salary",


SAL*12 "ANNUAL SAL"
FROM EMP;

SELECT EMPNO ,ENAME,SAL , DEPTNO SAL


FROM EMP;

SELECT EMPNO,ENAME,SAL SALARY FROM EMP WHERE SAL=5000;


SELECT EMPNO,ENAME,SAL SALARY FROM EMP WHERE SALARY=5000;--ERROR

TABLE ALIAS:-

SELECT * FROM EMP E;

SELECT E.ENAME, E.SAL , E.DEPTNO FROM EMP E;--TABLE ALIAS

SELECT EMP.ENAME, EMP.SAL , EMP.DEPTNO FROM EMP ;

SELECT E.ENAME, E.SAL , E.DEPTNO FROM EMP E;

SELECT E.*,E.SAL*12 FROM EMP E;

SELECT EMPNO,ENAME,JOB,MGR,HIREDATE,SAL, COMM, DEPTNO, SAL*12 FROM EMP;

SELECT E.*,SAL*12 FROM EMP E;

Q:- SEND A CSV REPORT OF EMPNO ,EMPLOYEE_NAME,MONTHLY_SALARY,


QUARTERLY_SALARY ,HALF_YEARLY_SALARY, ANNUAL_SALARY

SELECT EMPNO ,ENAME AS EMPLOYEE_NAME, SAL AS MONTHLY_SALARY,


SAL *3 AS QUARTERLY_SALARY, SAL*6 AS HALFYEARLY_SAL,
SAL *12 AS ANNUAL_SAL FROM EMP;

SELECT EMPNO ,ENAME AS EMPLOYEE_NAME,


SAL + NVL(COMM,0) AS MONTHLY_TOTAL_SALARY,
(SAL+NVL(COMM,0)) *3 AS QUARTERLY_TOTAL_SALARY,
(SAL+ NVL(COMM,0)) *6 AS HALFYEARLY_TOTAL_SAL,
SAL *12 AS ANNUAL_SAL FROM EMP;
QUARTER:- FINANCIAL QUARTER :- APR- JUN, JULY-SEP, OCT-DEC,
JAN -MARCH

CONCATENATION OPERATOR : -COMBINE

1. USING CONCATENATION OPERATOR WE CAN COMBINE COLUMNS OR EXPRESSIONS


IN A SINGLE COLOUMN
2. IT IS REPRESENTED BY DOUBLE VERTICAL BARS (||)
3. AFTER COMBINE THE RESULTANT COLUMN BEHAVES LIKE A CHARACTER DATA TYPE.

SELECT EMPNO ,ENAME , JOB, ENAME || JOB FROM EMP;


SELECT EMPNO ,ENAME , JOB, ENAME||' ' || JOB FROM EMP;

SELECT EMPNO ,ENAME , JOB, ENAME || ' --> ' || JOB AS EMPLOYEEJOB FROM EMP;

SELECT 'HI' FROM DUAL;


SELECT 'HI' FROM EMP;

SELECT 100||200 + 500 FROM DUAL;

Q:- DISPLAY "KING IS EARNING MONTHLY SALARY AS 5000"


"ALLEN IS EARNING MONTHLY SALARY AS 1600"
"SMITH IS EARNING MONTHLY SALARY AS 2850"

SELECT ENAME ||' IS EARNING MONTHLY SALARY :- ' || SAL FROM EMP;

SELECT * FROM EMP;

ENAME, SALARY

Q:- KING IS WORKING AS A PRESIDENT


SELECT ENAME || ' IS WORKING AS A ' || JOB FROM EMP;

KING'S COMM IS 0
ALLEN'S COMM IS 300

SELECT ENAME || '''S COMM IS ' || NVL(COMM,0) FROM EMP;


--WITH IN EXPRESSION IF WE WANT TO DISPLAY SINGLE QUOTE THEN TAKE TWICE

SELECT ENAME || '''''S COMM IS ' || NVL(COMM,0) FROM EMP;

SELECT ENAME || '''S COMM IS ' || NVL(COMM,0) FROM EMP WHERE ENAME ='KING';

SELECT 'HI' FROM EMP;

TASK :- DISPAY EMPID, EMPLOYEENAME, MONTHLYSALARY,


PF(12 % OF SAL), DA (10 % OF SAL) , HRA (15%OF SAL),
MONTHLY_GROSS (MONTHLYSALARY + PF + DA + HRA),
ANNUAL_CTC(MONTHLYSALARY + PF + DA + HRA)*12

You might also like