You are on page 1of 16

SQL400

CREATE TABLE

Syntax:
Create table <table_name> (col1 datatype1, col2 datatype2 …coln datatypen)

Ex:
CREATE TABLE STUDENT (SNO INT NOT NULL WITH DEFAULT, NAME CHAR (15
) NOT NULL WITH DEFAULT, MARKS INT NOT NULL WITH DEFAULT)

INSERT

This will be used to insert the records into table.


Syntax:

insert into <table_name)(col1, col2, col3 … Coln) values (value1, value2, value3 ….
Valuen)

Ex:
INSERT INTO USER3331/STUDENT (SNO, NAME, MARKS) VALUES (3, 'DILEEP',
200)

UPDATE

This can be used to modify the table data.

Syntax:
Update <table_name> set <col1> = value1, <col2> = value2 where <condition>

Ex:
update student set marks=500 where sno=3

DELETE

This can be used to delete the table data.

Syntax:
Delete <table_name> where <condition>

Ex:
DELETE FROM STUDENT WHERE SNO=1

ALTER

This can be used to add or remove columns and modify the existing column properties.
Syntax:

alter table <table_name> add <col datatype>


Ex:
ALTER TABLE USER3331/STUDENT ADD COLUMN MOBILE INT NOT NULL WITH
DEFAULT

ALTER TABLE USER3331/STUDENT ADD COLUMN ADDR CHAR (20)

DROP

This will be used to drop the database object.

Syntax:
Drop table <table_name>

Ex:
drop table student

RENAME

This will be used to rename the database object

Syntax:
rename <old_table_name> to <new_table_name>

Ex:
RENAME STUDENT TO STUDENT1

SELECTING DATA

Syntax:

Select * from <table_name> -- here * indicates all columns


or
Select col1, col2, … coln from <table_name>

Ex:
SELECT * FROM STUDENT1

SNO NAME MARKS MOBILE ADDR


2 RAJA 100 0 -
3 DILEEP 500 0 -
4 SRAVAN 500 0 VENKATAGIRI

SELECT SNO,NAME FROM STUDENT1

SNO NAME
2 RAJA
3 DILEEP
4 SRAVAN
CONDITIONAL SELECTIONS AND OPERATORS

We have two clauses used in this


➢ Where
➢ Order by

WHERE

Syntax:
select * from <table_name> where <condition>

The following are the different types of operators used in where clause.

Arithmetic operators

➢ Comparison operators
➢ Logical operators
➢ Arithmetic operators -- highest precedence
+, -, *, /

Comparison operators

➢ =, !=, >, <, >=, <=,


➢ between, not between
➢ in, not in
➢ null, not null
➢ like

Logical operators

➢ And
➢ Or -- lowest precedence
➢ not

select * from student1 where sno=3

SNO NAME MARKS MOBILE ADDR


3 DILEEP 500 0 -

select * from student1 where sno>=3

SNO NAME MARKS MOBILE ADDR


3 DILEEP 500 0 -
4 SRAVAN 500 0 VENKATAGIRI

select * from student1 where sno<>3

SNO NAME MARKS MOBILE ADDR


2 RAJA 100 0 -
4 SRAVAN 500 0 VENKATAGIRI
AND

This will gives the output when all the conditions become true.

Syntax:
select * from <table_name> where <condition1> and <condition2> and …<conditionn>

select * from student1 where sno=3 and name='DILEEP'

SNO NAME MARKS MOBILE ADDR


3 DILEEP 500 0 -

select * from student1 where sno=3 and name='SRAVAN'

NO ROWS DISPLAYED

OR

This will gives the output when either of the conditions become true.

Syntax:
select * from <table_name> where <condition1> and <condition2> or .. <conditionn>

select * from student1 where sno=3 OR name='SRAVAN'

SNO NAME MARKS MOBILE ADDR


3 DILEEP 500 0 -
4 SRAVAN 500 0 VENKATAGIRI

BETWEEN

This will gives the output based on the column and its lower bound, upper bound.

Syntax:
select * from <table_name> where <col> between <lower bound> and <upper bound>

SELECT * FROM STUDENT1 WHERE SNO BETWEEN 3 AND 6

SNO NAME MARKS MOBILE ADDR


3 DILEEP 500 0 -
4 SRAVAN 500 0 VENKATAGIRI

NOT BETWEEN

This will gives the output based on the column which values are not in its lower bound,
Upper bound.

Syntax:
select * from <table_name> where <col> not between <lower bound> and <upper bound>

SELECT * FROM STUDENT1 WHERE SNO NOT BETWEEN 3 AND 6


SNO NAME MARKS MOBILE ADDR
2 RAJA 550 0 -

IN

This will gives the output based on the column and its list of values specified.

Syntax:
select * from <table_name> where <col> in ( value1, value2, value3 … valuen)

SELECT * FROM STUDENT1 WHERE SNO IN (2, 3)

SNO NAME MARKS MOBILE ADDR


2 RAJA 550 0 -
3 DILEEP 500 0 -

NOT IN

This will gives the output based on the column which values are not in the list of
values specified.

Syntax:
select * from <table_name> where <col> not in ( value1, value2, value3 … valuen)

SELECT * FROM STUDENT1 WHERE SNO NOT IN (2, 3)

SNO NAME MARKS MOBILE ADDR


4 SRAVAN 500 0 VENKATAGIRI

NULL

This will gives the output based on the null values in the specified column.

Syntax:
select * from <table_name> where <col> is null

SELECT * FROM STUDENT1 WHERE ADDR IS NULL

SNO NAME MARKS MOBILE ADDR


2 RAJA 550 0 -
3 DILEEP 500 0 -

NOT NULL

This will gives the output based on the not null values in the specified column.

Syntax:
select * from <table_name> where <col> is not null
SELECT * FROM STUDENT1 WHERE ADDR IS NOT NULL

SNO NAME MARKS MOBILE ADDR


4 SRAVAN 500 0 VENKATAGIRI

LIKE

This will be used to search through the rows of database column based on the pattern
you specify.

Syntax:
select * from <table_name> where <col> like <pattern>

SELECT * FROM STUDENT1 WHERE MARKS LIKE 500

SNO NAME MARKS MOBILE ADDR


3 DILEEP 500 0 -
4 SRAVAN 500 0 VENKATAGIRI

select * from student1 where name like 'S%'

SNO NAME MARKS MOBILE ADDR


4 SRAVAN 500 0 VENKATAGIRI

select * from student1 where name like 'S____'

No rows displayed

select * from student1 where name like 'S_____'

No rows displayed

select * from student1 where name like 'S______________'

SNO NAME MARKS MOBILE ADDR


4 SRAVAN 500 0 VENKATAGIRI

% - Refers group of characters.


_ - Refers a single character.

ORDER BY

This will be used to ordering the columns data (ascending or descending).

Syntax:
Select * from <table_name> order by <col> desc

By default oracle will use ascending order.


If you want output in descending order you have to use desc keyword after the column.

SELECT * FROM STUDENT1 ORDER BY NAME DESC

SNO NAME MARKS MOBILE ADDR


4 SRAVAN 500 0 VENKATAGIRI
2 RAJA 550 0 -
3 DILEEP 500 0 -

SELECT * FROM STUDENT1 ORDER BY NAME

SNO NAME MARKS MOBILE ADDR


3 DILEEP 500 0 -
2 RAJA 550 0 -
4 SRAVAN 500 0 VENKATAGIRI

GROUP BY AND HAVING

GROUP BY

Using group by, we can create groups of related information.

Columns used in select must be used with group by, otherwise it was not a group by -
expression.

SELECT * FROM DEPT21

DEPTNO DEPTNAME SAL


10 sales 3.000
10 sales 4.000
10 sales 5.000
20 mktng 6.000
20 mktng 7.000
20 mktng 8.000
30 devp 5.500
30 devp 6.500
30 devp 7.500

SELECT DEPTNO,SUM(SAL) FROM DEPT21 GROUP BY DEPTNO

DEPTNO SUM ( SAL )


10 12.000
20 21.000
30 19.500
HAVING

This will work as where clause which can be used only with group by because of absence
of where clause in group by.

SELECT DEPTNO,SUM(SAL) FROM DEPT21 GROUP BY DEPTNO HAVING


SUM(SAL)>15000

DEPTNO SUM ( SAL )


20 21.000
30 19.500

ORDER OF EXECUTION

➢ Group the rows together based on group by clause.


➢ Calculate the group functions for each group.
➢ Choose and eliminate the groups based on the having clause.
➢ Order the groups based on the specified column.

FUNCTIONS

➢ Numeric functions
➢ String functions
➢ Date functions

Numeric functions

➢ MIN
➢ MAX
➢ AVG
➢ SUM
➢ SQRT
➢ MOD
➢ NVL

MIN

SELECT MIN(SAL) FROM DEPT21

MIN ( SAL )
3.000

MAX

SELECT MAX(SAL) FROM DEPT21

MAX ( SAL )
8.000
AVG

SELECT AVG(SAL) FROM DEPT21

AVG ( SAL )
5.833

SUM

SELECT SUM(SAL) FROM DEPT21

SUM ( SAL )
52.500

MOD

SELECT MOD(SAL, 10) FROM DEPT21

MOD ( SAL , 10 )
0
0
0
0
0
0
0
0
0

NVL

SELECT DEPTNO,NVL(SAL, 10000) FROM DEPT21

DEPTNO NVL ( SAL , 10000 )


10 3.000
10 4.000
10 5.000
20 6.000
20 7.000
20 8.000
30 5.500
30 6.500
30 7.500
40 10.000
40 10.000
40 10.000
String Functions

➢ LOWER
➢ UPPER
➢ CONCAT
➢ LENGTH
➢ SUBSTR
➢ INSTR

LOWER

SELECT LOWER(NAME) FROM STUDENT1

LOWER ( NAME )
raja
dileep
sravan

UPPER

SELECT UPPER(LOC) FROM DEPT

UPPER ( LOC )
HYD
BLORE
CHENNAI

LENGTH

SELECT LENGTH(LOC) FROM DEPT

LENGTH ( LOC )
15
15
15

CONCAT

SELECT CONCAT(SNO,NAME) FROM STUDENT1

CONCAT
2RAJA
3DILEEP
4SRAVAN

SUBSTR

This will be used to extract substrings.


SELECT SUBSTR(NAME, 2) FROM STUDENT1

SUBSTR ( NAME , 2 )
AJA
ILEEP
RAVAN

INSTR

This will allows you for searching through a string for set of characters.

Syntax:
instr (string, search_str [, start_chr_count [, occurrence] ])

SELECT INSTR(NAME, 'A', 3, 1) FROM STUDENT1

INSTR
4
0
3

SELECT INSTR(NAME, 'A', 5, 1) FROM STUDENT1

INSTR
0
0
5

Date Functions

CURRENT_DATE

This will returns the current date in the session’s time zone.

SELECT CURRENT_DATE FROM STUDENT1

CURRENT DATE
02/02/22
02/02/22
02/02/22

CURRENT_TIME

This will returns the current time in the session’s time zone.

SELECT CURRENT_TIME FROM STUDENT1

CURRENT TIME
04:56:13
04:56:13
04:56:13
CURRENT_TIMESTAMP

This will returns the current time stamp in the session’s time zone.

SELECT CURRENT_TIMESTAMP FROM STUDENT1

CURRENT TIMESTAMP
2022-02-02-04.58.30.726748
2022-02-02-04.58.30.726748
2022-02-02-04.58.30.726748

LOCALTIMESTAMP

This will returns local timestamp in the active time zone.

SELECT LOCALTIMESTAMP FROM STUDENT1

CURRENT TIMESTAMP
2022-02-02-05.01.52.688447
2022-02-02-05.01.52.688447
2022-02-02-05.01.52.688447

SUBQUERIES

➢ Nesting of queries, one within the other is termed as a subquery.


➢ A statement containing a subquery is called a parent query.
➢ Subqueries are used to retrieve data from tables that depend on the values in the table
itself.

TYPES

➢ Single row subqueries


➢ Multi row subqueries
➢ Multiple subqueries

Single row subqueries

In single row subquery, it will return one value.

select * from student1 where sno=(select sno from student1 where name like 'D%')

SNO NAME MARKS MOBILE ADDR


3 DILEEP 500 0 -

Multi row subqueries

In multi row subquery, it will return more than one value. In such cases we should
include operators like any, all, in or not in between the comparison operator and the
subquery.
SELECT * FROM STUDENT1 WHERE SNO >= ANY (SELECT SNO FROM STUDENT1
WHERE MARKS BETWEEN 500 AND 600)

SNO NAME MARKS MOBILE ADDR


2 RAJA 550 0 -
3 DILEEP 500 0 -
4 SRAVAN 500 0 VENKATAGIRI

Multiple subqueries

There is no limit on the number of subqueries included in a where clause. It allows nesting of
a query within a subquery.

SELECT * FROM STUDENT1 WHERE NAME = (SELECT NAME FROM STUDENT1


WHERE MARKS > (SELECT MARKS FROM STUDENT1 WHERE ADDR IS NOT
NULL))

SNO NAME MARKS MOBILE ADDR


2 RAJA 550 0 -

JOINS

➢ The purpose of a join is to combine the data across tables.


➢ A join is actually performed by the where clause which combines the specified rows
of tables.
➢ If a join involves in more than two tables then oracle joins first two tables based on
the joins condition and then compares the result with the next table and so on.

Types:

➢ Equi join
➢ Cross join
➢ Outer join
o Left outer
o Right outer
o Full outer

SELECT * FROM NEMP

EMPNO ENAME JOB MGR DEPTNO


111 DILEEP DEVP 444 10
222 ASHOK DEVP 444 20
333 RAJA DEVP 444 30
444 SRAVAN MGR 555 40
SELECT * FROM DEPT

DEPTNO DNAME LOC


10 sales hyd
20 hr blore
30 tech chennai

Equi join

An equi join is a type of join that combines tables based on matching values in specified
columns.

Please remember that:

➢ The column names do not need to be the same.


➢ The resultant table contains repeated columns.
➢ It is possible to perform an equi join on more than two tables.

SELECT EMPNO, ENAME, JOB, LOC FROM NEMP E, DEPT D WHERE


E.DEPTNO=D.DEPTNO

EMPNO ENAME JOB LOC


111 DILEEP DEVP hyd
222 ASHOK DEVP blore
333 RAJA DEVP Chennai

Cross join

A cross join, also known as a Cartesian Product join, returns a result table where each row
from the first table is combined with each row from the second table.

SELECT EMPNO,ENAME,JOB,LOC FROM NEMP ,DEPT

EMPNO ENAME JOB LOC

111 DILEEP DEVP hyd

111 DILEEP DEVP blore

111 DILEEP DEVP chennai

222 ASHOK DEVP hyd

222 ASHOK DEVP blore

222 ASHOK DEVP chennai

333 RAJA DEVP hyd

333 RAJA DEVP blore


333 RAJA DEVP chennai

444 SRAVAN MGR hyd

444 SRAVAN MGR blore

444 SRAVAN MGR chennai

LEFT OUTER JOIN

This will display the all matching records and the records which are in left hand side table
those that are not in right hand side table.

SELECT EMPNO,ENAME,JOB,D.DEPTNO,LOC FROM NEMP E LEFT OUTER JOIN


DEPT D ON(E.DEPTNO=D.DEPTNO)

EMPNO ENAME JOB DEPTNO LOC


111 DILEEP DEVP 10 hyd
222 ASHOK DEVP 20 blore
333 RAJA DEVP 30 chennai
444 SRAVAN MGR - -

RIGHT OUTER JOIN

This will display the all matching records and the records which are in right hand side
table those that are not in left hand side table.

SELECT EMPNO,ENAME,JOB,D.DEPTNO,LOC FROM NEMP E RIGHT OUTER JOIN


DEPT D ON(E.DEPTNO=D.DEPTNO)

EMPNO ENAME JOB DEPTNO LOC


111 DILEEP DEVP 10 hyd
222 ASHOK DEVP 20 blore
333 RAJA DEVP 30 chennai

FULL OUTER JOIN

This will display the all matching records and the non-matching records from both tables.

SELECT EMPNO,ENAME,JOB,D.DEPTNO,LOC FROM NEMP E FULL OUTER JOIN


DEPT D ON(E.DEPTNO=D.DEPTNO)

EMPNO ENAME JOB DEPTNO LOC


111 DILEEP DEVP 10 hyd
222 ASHOK DEVP 20 blore
333 RAJA DEVP 30 chennai
444 SRAVAN MGR - -
RANK()

The RANK () is a window function that calculates the rank of a row in a set of rows.
The RANK() returns the same rank for the rows with the same value.

Because RANK() adds the number of tied rows to the tied rank to calculate the next rank, the
ranks may not be sequential. In other words, there may have gaps in the sequential rank
numbering.

Syntax:

RANK() OVER ([partition_clause] order_by_clause)

SELECT marks,RANK() OVER(ORDER BY MARKS) MARKS_RANK FROM


STUDENT1

MARKS MARKS_RANK
500 1
500 1
550 3

You might also like