0% found this document useful (0 votes)
6 views

SQL

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

SQL

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

CURD OPERATIONS

#DDL(data definition language)


1)create command: This command is used to create new table.
Syntax:
create table<table name> (<col name> datatype (<size>),
<col name><datatype>(<size>),
……………………);
Eg:
create table employee (id number (5),
ename varchar2(30),
salary number (7,2));
7=total no of digit
2=after no

2) alter command: it is used to altering the existing table. Like adding a new
column, delete the existing column or modify the existing column, etc.
Syntax:
A lter table <table name><add/drop/modify>
(<col name>datatype(<size>), ….. ….. ….. …..);
Eg:
i)add a new column
alter table employee add (dob date,
mobile number(10));
ii)drop a table column
alter table employee drop(mobile);
iii) modify a column
alter table employee modify(salary number (10,2));

3) truncate command: this command is used to delete all rows in a table.


Syntax:
truncate table <table name>;

4) drop command: this is used to delete all rows with table structure.
Syntax:
drop table <table name>;

#DML(data manipulation language)


1)insert command: it is used to insert the data in a table.
Syntax:
a)insert into <table name> (<list of col name>)values(<list of value>);
Eg:
insert into employee(id, salary, dob)
values(101,75000,’Amit’, ’10 October 2001’);

[varchar,varchar2,char=takes in ‘ ‘]
b)insert into <table name> values (<list of values>);
Eg:
insert into employee
values(102, 45000, ’Raj’, ‘2 feb 2001’ );
[order should be same as declared]
 if we use values format then we have to give all the values, if we do not
know then use “null”
NULL in databases
1) In databases or SQL, we have a special value called naull.
2) This value is inserted in every column for which we do not provide any
specific value.

3)eg: insert into employee values (106,'Varun', '23-nov-2001');


in this case assuming that the table has salary col, also, null will be inserted
for Varun’s salary .

4)Another way to write the above command is:


insert into employee values (106,'Varun', '23-nov-2001', null);
5) null is a keyword in SQL and is non case sensitive.

c) insert into <col you want> values <list>;


(when you want to insert to a lesser columns then total)
[alternate to use null multiple times]
Eg:
insert into employee(id,ename,dob) values (107,'Zaid', '15-dec-2003);

c) USED FOR ORACLE IDE ONLY


syntax:
insert into <table name> values (& <col name>, ‘&<col name’> , …..);
Eg:
Sql> insert into employee values (&id, ’&ename’, &salary, ‘&dob’);
enter the id= 105
enter the name= Rajesh
enter the salary= 67000
enter the dob= 20 July 2004

2)update command: to make changes in the / values of the table.


Syntax:
a) update <table_name> set <col_name>=<value>, <col_name>=<value>, . .
(will update all the rows of the table)

b) update <table_name> set <col_name>=<value>, <col_name>=<value>,


where <test_cond>;
(will update only those rows which satisfy the cond given in where
clause)
Eg:
i)update employee set salary=7000;
updates salary of each employee

ii)update employee set salary=7000 where ename=’Deepak’;


updates salary of Deepak only
Q )incrementing salary of employee by 5000
update employee set salary=salary+5000 where id=104;

COMPARAING NULL VALUES


Null cannot be compared with equal to(=). It needs a special operator “is”.
Eg: update employee set salary=50000 where salary is null;

Eg: update employee set salary=null where ename='Zaid';


3)delete command: to remove one or more rows from the table.
Syntax:
a) delete from <table_name>;
for removing all the rows

b) delete from <table_name> where <test_cond>;


for removing selected rows

Eg:
Q) delete the record of the employee ‘Jyoti’.
delete from employee where ename='Jyoti';

Q)delete all the rows


delete from employee;
DELETE V/S TRUNCATE
Delete:
-DML command
-it can be rolled back.
- remove only selected rows.
-slow

Truncate
-DDL command
-it can’t be rolled back.
- removes all the rows.
-fast

Q) why truncate is fast and delete is slow?


This is because when we delete data from a table oracle actually stores the
deleted data in a special i=area in a primary memory called as rolled back
segment. So when delete runs internally 2 commands runs
i) delete
ii)command to save the data in rolled back segment.
However, there is no such concept of rolled back segment in truncate. Hence it
is faster.

#DQL(data query language)


1) select command: to fetch the data from the table
Syntax:
select <list_of_cols>
from <table_name>
where <test_cond>;
DISPLAYING ALL THE DATA OF THE TABLE
1)select id,ename,dob,salary // Full Table Scan
from employee;

2) select *
from employee;

CHANGING ORDER OF COLUMNS


Select ename,id,salary,dob
from employee;
[internal data of the table remains in the same order]
DISPLAYING ONLY SELECTED COLUMNS
select ename,dob
from employee;

DISPLAYING SELECTED ROWS


Q) Display records of those employees who earn more than 45000.
select *
from employee
where salary>45000;

 COLUMN ALIASING
select id as empid, salary as income , ename
from employee;

#DCL(data control language)


1)grant
2)revoke

#TCL
1)commit command: saves in disk
2)rollback command: undo
3)savepoint
SOME COMMANDS
1) select * from tab; = shows all the existing tables
2) desc <table name> = gives the structure of table(shows col names and
their data types)
3) select * from employee; = shows the table
4) select sysdate from dual; = tells the system date [dd-mon-yy]
5) clear scr
#OPERATORS IN SQL
operators are symbols or words that represent an action or process.Overall SQL
provides us 4 categories of operators which are shown below:
-Arithmetic
-Comparison
-Logical
-Miscellaneous

ARITHMETIC OPERATORS
+(unary), -(unary), /, * , + , -

COMPARSION OPERATORS
=(this is also assignment when used with set) equality
<>, !=, ^= not equality
>
<
>=
<=

LOGICAL OPERATOR
(non-case sensitive)
AND Logical Conjunction operator
OR Logical Disjunction operator
NOT Logical Negation operator

MISCELLANEOUS OPERATORS
|| Concatenation
IN ,NOT IN Comparison for a value in a specified list
BETWEEN , NOT BETWEEN Comparison based on range of values
LIKE , NOT LIKE pattern
IS, IS NOT

Q) WAP to display rec of emp who earn between 40000 to 60000.


select *
from employee
where salary>= and salary<=60000;
OR
select *
from employee
where salary between 40000 and 60000;

Q) Write a query to display records of those employees who doesn’t earn


between 40000 to 60000
select *
from employees
where salary not between 40000 and 6000;

Q) Write a query to display records of those employees who earn 40000 or


50000 or 34000
select *
from employee
where salary in (40000,50000,34000); //order does not matter
Q) Write a query to display records of those employees who do not earn
40000,50000,340000
select *
from employee
where salary != 40000
and salary != 50000
and salary!= 34000;
OR
where salary not in (40000,34000,50000);

Q) Write a query to display records of those employees whose name begins


with “S”.
(PATTERN MATCHING)
select * from employee
where ename like ‘s%’;
[‘S_’ = fetch records of 2 letters only but that second letter can be anything ,
_ is used when length of data is known ]

#GROUP FUNCTIONS
Till above all were Scalar queries.
When we do al the rows collectively, they are called Group Functions.
Done through Vector queries.

Group functions in SQL are those built-in functions which operate on group of
rows.
Thus, rather than operating on one row at a time they operate on multiple
rows but return a single result. Due to this, they are also called Vector or
Aggregate Functions.
Oracle provides 5 group functions and they are:
-SUM
-MAX
-MIN
-AVG
-COUNT //NON-CASE SENSITIVE

Q) wap to display total expense done by the company on the salary of


employees
select sum (salary) as expense
from employee; //title will be expense
OR
select sum (salary) from employee; // title will be sum(salary)

NOTE: We cannot use individual columns in the same select statement as a


group function. Otherwise, query will fail. For example, the query the
following query will not run.
select sum(salary),ename
from employee;

Q) wap to display total expense as well as avg expense done by the company
on the salary of employees
select sum (salary), avg(salary)
from employee;
Q) wap a query to display highest salary
select max(salary)
from employee;

Q) wap a query to display first recruitment done


select min(hirdate)
from employee;

select min(hirdate), ename //Error


from employee;
to do this we need to apply advance sql which is done using sub-queries.
Select ename
From employees
Where hirdate=(select min(hirdate) from employees);

Q)When was the last recruitment done


select max(hirdate)
from employee;

Q) how many enployees are working in the company


select count (ename) //selects non-null values only
from employee;

select count(*)
from employee;
select count (job) =>5
from employee;

select count (distinct job) =>3


from employee;

You might also like