You are on page 1of 33

DDL statements

I.

Create table

create table <table name>


(<attribute1> data type (size),
<attribute2> data type (size),
<attribute3> data type (size));

Description of table
desc command displays definition of table.
desc <tablename>;

Column constraints
1. not null- this constraint is placed immediately
after data type column.
create table <table name>
(<attribute1> data type (size) not null,
<attribute2> data type (size),

2. unique
create table <table name>
(<attribute1> data type (size) unique,
<attribute2> data type (size) not null unique,
<attribute3> data type (size));

create table <table name>


(<attribute1> data type (size),
<attribute2> data type (size),
<attribute3> data type (size)
unique (<attribute1,attribute2>));

3. check
create table <table name>
(<attribute1> data type (size),
<attribute2> data type (size),
dept num (2) check (dept num in (1, 2, 3,
10, 12, 15))
);

4. Default
create table <table name>
(<attribute1> data type (size) unique,
<attribute2> data type (size) not null unique,
<attribute3> data type (size)) default 1;

II. Alter table


alter table <table name>
add (<attri-name> data type (size))
alter table <table name>
modify (<attri-name> data type (size))
alter table <table name>
drop (<attri-name>)

III. Delete entire table


drop table <table name>

DML statements

I.

Insert into

insert into <table name>


values (<list of values>)
insert into <table name> (<atrri1>, <attri2>)
values (<value1, value2>)

Inserting through parameter


substitution
& operator is used as a substitution operator.
Where SQL * plus prompts for value off a
variable, accepts it and then it substitutes it in
place of variable.
Insert into studinfo values(&srno, &name,
&marks);

II. update
update <table name> set <atrri1>=<value>
Adding where clauseupdate emp
set comm= 100
where deptno= 5

III. Delete
delete from <table name>
where <condition>

Select statement
select statement instructs the database to
retrieve information from table
select * from <table name>
select <attri list> from <tale name>

Where clause
The where is more of a clause than a
command. Hence it is used in sync with the
select command.
It specifies condition based on which select
command will be extracted from a table.

Relational operators

=
>
<
>=
<=
<>
!=

Logical operator
AND
OR
NOT

Special operators

IN
NOT IN
BETWEEN
LIKE

Using IN and NOT IN operator


Checking values in a defined set
select empnme from emp
where empno IN (123, 456, 789);
select empnme from emp
where empno NOT IN (123, 456, 789);

BETWEEN
select empnme from emp
where salary BETWEEN 1000 AND 2000;

Creating table from another table


A table can be created using create table
command with rows and columns driven from
any other table
create table <emp2>
as
select empno, empname, salary
from emp1
where deptno in (10,20)

Display first & last name together


select first_name || || last_name from
table_name;
Giving aliases
select first_name || || last_name as
emp_name from table_name;
select first_name as name from table_name;

Group by function

COUNT
MAX
MIN
AVG
SUM

COUNT
Select count (*) from employee
where dept = electronics;
Output would be 4 rows

Select count (DISTINCT emp_name) from


employee;
Output would be 6 rows

MAX
This function is used to get maximum value
from a column.
To get maximum salary drawn by an employee
query would be,
Select MAX (salary) from employee;

MIN
This function is used to get minimum value
from a column.
To get minimum salary drawn by an employee,
query would be,
Select MIN (salary) from employee;

AVG
This function is used to get average value of a
numeric column.
To get average salary, query would be,
Select AVG (salary) from employee;

SUM
This function is used to get sum of a numeric
column.
To get total salary given out to employee
query would be,
Select SUM (salary) from employee;

HAVING clause
Having clause is used to filter data based on group
functions. This is similar to WHERE condition but
used with group functions.
If you want to select the department that has total
salary paid for its employee is more than 25000, the
SQL query would be,
Select dept, SUM(salary) from employee
group by dept
having SUM(salary) > 25000

TRUNCATE statement
TRUNCATE table table_name;

You might also like