You are on page 1of 22

INTRODUCTION TO SQL

SQL stands for structure query language and it is used to communicate

with a database. SQL was introduced by IBM corporation. SQL


statement are used to retrieve data from database , update data on a
database , insert data into a database and delete data from the
database. Some common relational database management system that
use SQL are;

(a) Oracle (b) Sybase (c) Microsoft SQL server (d)MS Access etc...

CHARACTERSTICS OF SQL-

(1)Standard independent language

(2) Cross platform abilities

(3) Speed

(4) Easy to learn

(5)Easy to understand

DATABASE LANGUAGES/TYPE OF SQL COMMANDS

1. DATA DEFINITION LANGUAGE(DDL) –

This language is used for table definition. It updates a special set of


tables called data dictionary. It includes commands for creating,
modifying and dropping the structure of database objects like table,
view .
Some DDL commands are –

a) create table command


create table student
(Roll_no number(2),
Name varchar2(20),
Marks number(2) );
desc student;

b) alter table command


alter table student
add phone_no number(10);

c) drop table command


drop table student;
d)Rename table command

e) truncate table command

2. DATA MANIPULATION LANGUAGE(DML) –

DML commands are used to manipulate the existing database. It


includes commands for storing, retrieving, modifying and deleting the
data.

Some DML commands are –

a) Insert- INSERT to insert rows of data into a table

insert into student1

values(20,'achla',69);
b) select - SELECT to select rows of data from a table
INPUT
select* from student1;

OUTPUT

c) update- UPDATE to change rows of data in a table


INPUT
update table student1
set marks= 69+5;

OUTPUT
d)delete - DELETE to remove rows of data from a table

3. DATA CONTROL LANGUAGE(DCL) –

DCL is used to control a data access from the database and


transaction control. It provides security to database object.

DCL commands are-

a) Revoke - REVOKE to revoke (remove) a privilege from a user

b) Grant - GRANT to grant a privilege to a user

object (database) table

views

indexes

4. TRANSACTION CONTROL LANGUAGE(TCL) –

It is used for managing changes affecting the data. SQL commands


included in this category are-

a) Commit b) Rollback c) Savepoint


INPUT

Create table oodles (Emp_no number(5), NAME VARCHAR2(25),


ADDRESS VARCHAR2(50),salary number(10),mob_no
number(10),dept_no number(5));

INSERT INTO oodles VALUES(01,'ankita','tis


hazari',100000,8853322120,005);

INSERT INTO oodles VALUES(03,'purnima','gandhi


nagar',130000,8834269102,004);

INSERT INTO oodles VALUES(04,'rahul


gupta','saket',120000,8975632014,009);

INSERT INTO oodles VALUES(05,'diksha','rajiv


chowk',150000,7263169542,008);

INSERT INTO oodles VALUES(06,'karan gaur','rohini


east',150000,9246301837,003);

INSERT INTO oodles VALUES(07,'varun dhawan','devli


road',130000,8236201572,002);

INSERT INTO oodles VALUES(08,'harshi','JOR


BAGH',140000,9013875620,007);

INSERT INTO oodles


VALUES(09,'bhavya','rithala',150000,7263014895,001);
INSERT INTO oodles
VALUES(10,'deepali','gurgaon',140000,8563201479,0012);

select * from oodles;

OUTPUT

ADD PRIMARY KEY

INPUT

alter table OODLES

add primary key(EMP_NO);

OUTPUT
TABLE-2

CREATE table AMAZON(Emp_no number(5) REFERENCES


OODLES(Emp_no),DEPT_NO NUMBER(5)PRIMARY KEY, STATE
VARCHAR2(10),PINCODE number(8),mob_no number(10));

INSERT INTO AMAZON VALUES(01,005,'DELHI',110062,8853322120);

INSERT INTO AMAZON


VALUES(03,006,'CHANDIGARH',110038,8834269102);

INSERT INTO AMAZON VALUES(04,004,'JAIPUR',110072,8236201572);


COMBINING TWO TABLE

INPUT

SELECT OODLES.NAME,AMAZON.STATE

FROM OODLES

INNER JOIN AMAZON

ON OODLES.EMP_NO=AMAZON.EMP_NO;

OUTPUT

DML COMMANDS:

1)UPDATE COMMAND

INPUT-

UPDATE OODLES

SET SALARY='1700000'

WHERE NAME='purnima';

SELECT * FROM OODLES;


OUTPUT-

2) RENAME COMMAND

INPUT-

ALTER TABLE OODLES

RENAME TO PWC;

OUTPUT-
3) select command

INPUT-

SELECT * FROM PWC

WHERE EMP_NO>5;

OUTPUT-

INPUT-

SELECT * FROM PWC

WHERE EMP_NO<5;
OUTPUT-

4. DELETE COMMAND

INPUT-

delete from PWC

where salary = 140000;

OUTPUT-
5. ALTER COMMAND

(a) ( FOR ADDING A NEW COLUMN TO AN EXISTING TABLE )

INPUT-

Alter table PWC

Add AGE number(10);

OUTPUT-

(b) (FOR MODIFYING THE EXISTING COLUMN OF AN EXISTING TABLE)

INPUT-

Alter table PWC

Modify age varchar2 (10);

OUTPUT-
DML COMMANDS:

1.ORDER BY-

INPUT-

SELECT *FROM PWC

ORDER BY NAME;

OUTPUT-

DESCRIBE COMMAND-
INPUT-

DESC PWC

OUTPUT-

ORACLE FUNCTIONS:-
CREATE TABLE EMPLOYEE-

INPUT

create table employee001

(employee_no number(3),

department varchar2(20),

basic_salary number(4));

insert into employee001

values(101,'sales',7000);

insert into employee001

values(102,'Finance',8000);

insert into employee001

values(103,'marketing',9000);

select* from employee001;

OUTPUT-

1.AGGREGATE FUNCTINS:
A)SUM()

Input-

SELECT SUM(basic_salary) FROM EMPLOYEE001;

Output-

B)AVG()

Input-

SELECT avg(basic_salary) FROM EMPLOYEE001;

Output-

C)MAX()
Input-

SELECT max(basic_salary) FROM EMPLOYEE001;

Output-

D)MIN()

Input-

SELECT min(basic_salary) FROM EMPLOYEE001;

Output-

E)COUNT()

Input-

SELECT count(basic_salary) FROM EMPLOYEE001;

Output-
2.ARITHEMETIC FUNCTIONS:-

FOR EXAMPLE TABLE AS BELOW:-

INPUT-

CREATE TABLE STUDENT001

(STUDENT_NAME VARCHAR(20),

STUDENT_MARKS NUMBER(3));

INSERT INTO STUDENT001

VALUES('AAKASH',96.99);

INSERT INTO STUDENT001

VALUES('NANCY',97.99);

INSERT INTO STUDENT001

VALUES('RAM',-10);

SELECT *FROM STUDENT001;


OUTPUT-

A)ABS()

INPUT

SELECT abs(-10) FROM STUDENT001;

OUTPUT

B)CEIL()

INPUT

SELECT ceil(96.99) FROM STUDENT001;


OUTPUT

C)FLOOR()

INPUT

select floor(97.99) from student001;

OUTPUT

D)MOD()

INPUT

select mod(4,3) from dual;

OUTPUT
E)POWER()

INPUT

select power(10,2) from dual;

OUTPUT

You might also like