Professional Documents
Culture Documents
I.
Create table
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));
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;
DML statements
I.
Insert into
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
BETWEEN
select empnme from emp
where salary BETWEEN 1000 AND 2000;
Group by function
COUNT
MAX
MIN
AVG
SUM
COUNT
Select count (*) from employee
where dept = electronics;
Output would be 4 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;