SQL Lecture 02
SQL Lecture 02
SQL Lecture 02
==========================
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%';
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.
DEFINE/UNDEFINE
====================
SQL> DEFINE DEPT_ID=20
SQL> SELECT * FROM EMP WHERE DEPTNO=&DEPT_ID;
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).
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
===========
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;