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

SQL Lecture 02

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

WHERE CLAUSE----AND

==========================
SQL> -- FIND THOSE EMPLOYEES WHO WORK AS CLERK IN DEPARTMENT 30
SQL> SELECT * FROM EMP WHERE JOB='CLERK' AND DEPTNO=30;
SQL> SELECT * FROM EMP WHERE JOB='SALESMAN' AND DEPTNO=10;

WHERE CLAUSE----OR
=========================
SQL> SELECT * FROM EMP WHERE JOB='CLERK' OR DEPTNO=30;

WHERE CLAUSE----IN
=========================
SQL> SELECT * FROM EMP WHERE JOB IN ('SALESMAN','CLERK') AND DEPTNO IN (10,20);
SQL> SELECT * FROM EMP WHERE JOB IN ('SALESMAN','CLERK') OR DEPTNO IN (10,20);

WHERE CLAUSE----LIKE
==========================
SQL> SELECT * FROM EMP WHERE HIREDATE NOT LIKE '%87' AND HIREDATE NOT LIKE '%JAN%';

ORDER BY CLAUSE (by default ascending)


====================
SQL> SELECT * FROM EMP ORDER BY EMPNO;
SQL> SELECT * FROM EMP ORDER BY SAL;
SQL> SELECT * FROM EMP ORDER BY COMM DESC;
SQL> SELECT * FROM EMP ORDER BY COMM DESC NULLS LAST;
>>>SQL> SELECT * FROM EMP ORDER BY 6;
>>>SQL> SELECT * FROM EMP ORDER BY DEPTNO,SAL DESC;

Ampersand (&), Double Ampersand (&&)


========================================
They are called "Substitution Variables". The ampersand(&) is used as a prompt for
a variable. It does not store the data physically, instead, it discard changes
right after code is executed.

Double ampersand(&&), on the other hand, will store that value and will not request
user input the next time code is executed. It stores the value in database and
reuses again whenever you reference that variable name.

SQL> SELECT * FROM EMP WHERE DEPTNO=&DEPT_ID;


SQL> /
SQL> /
SQL> SELECT * FROM EMP WHERE SAL BETWEEN &STARTING_SALARY AND &ENDING_SALARY;
SQL> SELECT * FROM EMP WHERE JOB=&DESIGNATION;
SQL> SELECT * FROM EMP WHERE JOB='&DESIGNATION'
SQL> SELECT * FROM EMP WHERE HIREDATE BETWEEN '&START_DATE' AND '&END_DATE';
SQL> SELECT * FROM EMP WHERE JOB=upper('&DESIGNATION');

SQL> SELECT * FROM DEPT;


>>>SQL> INSERT INTO DEPT VALUES (&DEPTNO,'&DNAME','&&LOC')
SQL> SELECT * FROM EMP;
>>>SQL> INSERT INTO EMP VALUES
(&EMPNO,'&ENAME','&JOB',&MGR,'&HOREDATE',&SAL,&COMM,&&DEPTNO);

DEFINE/UNDEFINE
====================
SQL> DEFINE DEPT_ID=20
SQL> SELECT * FROM EMP WHERE DEPTNO=&DEPT_ID;

SQL> UNDEFINE DEPT_ID


SQL> SELECT * FROM EMP WHERE DEPTNO=&DEPT_ID;

>>>SQL> SELECT &COLUMN_LIST FROM &TABLE_NAME;

>>>SQL> SELECT * FROM EMP WHERE &SEARCHING_CONDITION &ORDER_BY_CONDITION;

VERIFY ON/VERIFY OFF


==========================
Setting SET VERIFY ON forces to display the text of a command after it replaces
substitution variables with values.
SQL> SELECT * FROM EMP WHERE DEPTNO=&DEPT_ID;
SQL> SET VERI OFF

LOWER/UPER/INITCAP
========================
SQL> SELECT ENAME, LOWER(ENAME), INITCAP(ENAME) FROM EMP WHERE ENAME='SCOTT';
SQL> SELECT * FROM EMP WHERE LOWER(ENAME)=LOWER('&EMP_NAME');

SUBSTR
===========
The SUBSTRING() extracts a substring with a specified length starting from a
location in an input string.
SQL>SELECT 'KARACHI', SUBSTR('KARACHI',3) FROM DUAL;
SQL>SELECT 'KARACHI', SUBSTR('KARACHI', 6, 2) FROM DUAL;
SQL> SELECT 'KARACHI', SUBSTR('KARACHI', 5, 2) FROM DUAL;
SQL> SELECT 'KARACHI', SUBSTR('KARACHI', 5,3) FROM DUAL;
SQL> SELECT 'KARACHI', SUBSTR('KARACHI', 5,5) FROM DUAL;
SQL>SELECT 'KARACHI', SUBSTR('KARACHI', -3, 3) FROM DUAL;
SQL>SELECT 'KARACHI', SUBSTR('KARACHI', -6, 3) FROM DUAL;
SQL>SELECT 'KARACHI', SUBSTR('KARACHI', -8, 2) FROM DUAL;
SQL>SELECT 'PAKISTAN', SUBSTR('PAKISTAN',1,3), SUBSTR('PAKISTAN',-3,3),
SUBSTR('PAKISTAN',5) FROM DUAL;

INSTR
========
INSTR function returns positional occurrence of a character or group of characters
(substring) within an input string. By default, it returns the first occurrence of
the character(s).

SQL> SELECT 'PAKISTAN', INSTR('PAKISTAN','A') FROM DUAL;


SQL> SELECT 'PAKISTAN', INSTR('PAKISTAN','A',1,1),INSTR('PAKISTAN','A',1,2) FROM
DUAL;
SQL> SELECT 'PAKISTAN', INSTR('PAKISTAN','A',-1,1), INSTR('PAKISTAN','A',-1,2) FROM
DUAL;

LPAD/RPAD
============
SQL> SELECT ENAME, LPAD(ENAME,10,'*'), RPAD(ENAME,10,'*') FROM EMP;
SQL> SELECT ENAME, LPAD(ENAME,10,'*'), RPAD(ENAME,10,'*'),
INSTR(RPAD(ENAME,10,'*'),'*',1) FROM EMP;
LENGTH
==========
SQL> SELECT * FROM EMP WHERE LENGTH(ENAME)=5;

REPLACE
===========

SQL> SELECT ENAME, REPLACE (ENAME,'L','%') FROM EMP;


TRIM
======
Oracle TRIM() function removes spaces or specified characters from the begin, end
or both ends of a string.

SQL> SELECT 'AMERICA', TRIM('A' FROM 'AMERICA'),'AUSTRIA', TRIM('A' FROM


'AUSTRIA'),'INDIA', TRIM('A' FROM 'INDIA'),
'AFGHANISTAN', TRIM('A' FROM 'AFGHANISTAN'),'PAKISTAN',TRIM('A' FROM
'PAKISTAN')FROM DUAL;

MOD
======
The Oracle MOD function returns the REMAINDER of x divided by y.
SQL> SELECT MOD(5000,1000), MOD(2200,1000) FROM DUAL;

ROUND
========
SQL> SELECT ROUND(98.946,2), ROUND(98.954,2) FROM DUAL;
SQL> SELECT ROUND(98.946,0), ROUND(98.454,0) FROM DUAL;

TRUNCATE
===========
SQL> SELECT TRUNC (98.946,2), TRUNC(98.954,2) FROM DUAL;
SQL> SELECT TRUNC(98.946,0), TRUNC(98.454,0) FROM DUAL;
SQL> SELECT TRUNC (98.9469,3), TRUNC(98.9548,4) FROM DUAL;
SQL> SELECT ENAME, HIREDATE, SYSDATE-HIREDATE NO_OF_DAYS FROM EMP;
SQL> SELECT ENAME, HIREDATE, TRUNC(SYSDATE-HIREDATE) NO_OF_DAYS FROM EMP;
SQL> SELECT ENAME, HIREDATE, TRUNC(SYSDATE-HIREDATE,1) NO_OF_DAYS FROM EMP;

SQL> conn / as sysdba


SQL> select value from nls_session_parameters where parameter='NLS_DATE_FORMAT';
SQL> SELECT ENAME,HIREDATE, TRUNC(MONTHS_BETWEEN(SYSDATE,HIREDATE)) NO_OF_MONTHS
FROM EMP;
SQL> SELECT SYSDATE, NEXT_DAY(SYSDATE,'WEDNESDAY') FROM DUAL;
SQL> INSERT INTO EMP VALUES(9001,USER,'CLERK',7788,SYSDATE,3000,NULL,10);
SQL> ALTER SESSION SET NLS_DATE_FORMAT='DD-MON-RR HH12:MI.SS AM';
SQL> SELECT SYSDATE, NEXT_DAY(SYSDATE,'WEDNESDAY') FROM DUAL;
SQL> ALTER SESSION SET NLS_DATE_FORMAT='DD-MON-RR';

TO_DATE , TO_NUMBER& TO_CHAR


=====================================
The Oracle TO_CHAR function converts a number or date to a string.
The Oracle TO_DATE function converts a string to a date
The Oracle TO_NUMBER function converts a string to a number.

SQL> SELECT TO_DATE('2015/05/15 8:30:25', 'YYYY/MM/DD HH:MI:SS') FROM DUAL;


SQL> SELECT TO_DATE('2003/07/09', 'yyyy/mm/dd') FROM DUAL;
SQL> SELECT TO_DATE('070903', 'MMDDYY') FROM DUAL;
SQL> SELECT TO_DATE('20020315', 'yyyymmdd') FROM DUAL;
SQL> SELECT TO_NUMBER('1,5000','9,9999'), TO_DATE('15-12-87','DD-MM-RR') FROM DUAL;
SQL> SELECT HIREDATE, TO_CHAR(HIREDATE,'DAY') DAY FROM EMP;
SQL> SELECT HIREDATE, TO_CHAR(HIREDATE,INITCAP('DAY'))DAYS FROM EMP;
SQL> SELECT HIREDATE, TO_CHAR(HIREDATE,'DDTH.MONTH YEAR DAY') NEW_DATE FROM EMP;
SQL> SELECT SAL, TO_CHAR(SAL,'99,999.99'), TO_CHAR(SAL,'09,999.99') FROM EMP;
SQL> SELECT SAL, TO_CHAR(SAL,'$99,999.99') FROM EMP;
SQL> ALTER SESSION SET NLS_CURRENCY='RS.';
SQL> SELECT SAL, TO_CHAR(SAL,'L99,999.99') FROM EMP;

You might also like