Professional Documents
Culture Documents
Dual is a dummy table which consists of one row and one columns which is used for data manipulations
in sql
Order by clause
Used to sort the rows selected by a select query
By default, sorts by ascending order
Select empno,ename,job,hire_date from emp order by hire_date
Select empno,ename,job,hire_date from emp order by hire_date desc (to sort in descending order)
Character functions
LOWER, UPPER, INITCAP, CONCAT, SUBSTR, INSTR, LPAD, RPAD, TRIM, REPLACE
Select lower(ename) from emp
Select upper(ename) from emp
Select initcap(ename) from emp
Select concat(abc,123) from dual
Select substr(Helloworld,1,5) from dual
Select instr(Helloworld,w) from dual
----- Hello
----- 6
Date functions
SYSDATE will returns the current system date
Select sysdate from dual
Different date functions available are:
MONTHS_BETWEEN, ADD_MONTHS, NEXT_DAY, LAST_DAY,
To convert from one data type to other datatype
TO_CHAR, TO_NUMBER, TO_DATE
CASE statement
Conditional enquiry similar to an if-then-else statement
Select empno,ename,salary,
Case dept_id when 100 then 1.10*salary
When 200 then 0.5*salary
When 300 then 1.25*salary
Else salary END Revised salary
From emp
DECODE function
Select empno,ename from emp where where salary = (select salary from emp where ename = sekhar)
-----for subquery which returns one row
Select empno,ename from emp where salary IN ( select salary from emp where ename like S%)
Types of statements
DCL (Data control language)
DML(Data manipulation language)
TCL (Transaction control language)
Different datatypes
Varchar2(size)
Number(p,s)
Date
Blob
Clob
Boolean
Bfile
DCL
CREATE
Create table test_emp( empno number, ename varchar2(20), salary number(6,2),comm. number)
Create table test_emp as select * from emp
ALTER
Alter table emp add second_comm number ----to add an extra column to an existing table
Alter table emp modify ename varchar2(50) ----to modify the size of an existing column of a table
Alter table emp drop column comm.
----to drop an existing column of a table
DROP
Drop table test_emp
RENAME
Rename test_emp to test_employee
DML
INSERT
Insert into test_emp values(100,sekhar,8500.50,200)
Insert into test_emp(empno,ename,salary) values(100,sekhar,10000)
UPDATE
Update test_emp set ename = ram where ename = sekahr