You are on page 1of 5

To select all columns from a table:

Select * from table_name


To select required columns from a table:
Select column1,column2,column3 from table_name
To select distinct rows from a table(i.e avoid duplicate rows)
Select distinct column1 from table_name
To describe the structure of a table in the database:
Desc table_name or describe table_name
To restrict the rows returned by a select query by using where condition
Select * from table_name where column1 = <condition>
Arithmetic operators which can be used in a select query
+,_,*,/
Comparision conditions which can be used in a select query
> , < , <= , >= , = , <>
Select * from emp where salary >= 3000
Other comparision conditions which can used for comparing character and date data types are:
Between and, IN, LIKE,
Select * from emp where join_date between <date1> and <date2>
Select * from emp where ename like Ram%
Select * from emp where empno in (100,200,300)
To check for null values in a select query
Select * from emp where comm. Is null
Logical operators used in a select query are:
AND, OR, NOT
Select * from emp where empno in (100,200,300) AND ename like S%
Select * from emp where ename like RA% OR job like Cl%
Select * from emp where ename NOT IN (sekhar,ram,srinivas)
Select * from dual

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

Provides conditional enquiry same as of case or if-then-else statements:


Select empno,ename,decode(dept_id,100,1.10*salary,200,0.5*salary,300,1.25*salary,salary) from emp
JOINS
To display data from more than one table we use joins
Different types of joins are:
Equi join, Non-equi join, outer join, self join
Equi join:
Select a.empno,a.ename,a.salary,b.deptname from emp a, dept b where a.dept_id = b.dept_id
Nonequi join
Select a.empno, a.ename, a.salary, b.deptname from emp a,dept b where a.salary between a.low_salary
and a.high_salary
Outer join:
Left outer join : returns the common rows of both tables and tbe uncommon rows of the first table
Right outer join: returns the common rows of both tables and the uncommon rows of the second table.
Select * from emp a, dept b where a.dept_id(+) = b.dept_id
Select * from emp a, dept b where a.dept_id = b.dept_id(+)
Self join:
To join a table to itself
Select a.empno,a.ename,b.ename from emp a, emp b where a.manager_id = b.emp_id
Group functions
Group functions operate on set of rows to give one result per group
Types of group functions are:
AVG, COUNT, MIN, MAX, SUM
Select count(*) from emp where dept_id = 100
To get the average salary in employees table for each department:
Select dept_id, avg(salary) from dept group by dept_id
To restrict the rows retrieved in a select query group by clause can be done by HAVING clause
It is similar to where condition in a select statement
Select dept_id, max(salary) from emp group by dept_id having max(salary) > 3000
Subquery
The subquery executes once before the main query executes

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)

-----create, alter, drop, rename


------insert, update, delete
------commit, rollback

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

You might also like