Professional Documents
Culture Documents
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
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
Syntax:
Update <table_name> set <col1> = value1, <col2> = value2 where <condition>
Ex:
update student set marks=500 where sno=3
DELETE
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:
DROP
Syntax:
Drop table <table_name>
Ex:
drop table student
RENAME
Syntax:
rename <old_table_name> to <new_table_name>
Ex:
RENAME STUDENT TO STUDENT1
SELECTING DATA
Syntax:
Ex:
SELECT * FROM STUDENT1
SNO NAME
2 RAJA
3 DILEEP
4 SRAVAN
CONDITIONAL SELECTIONS AND OPERATORS
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
Logical operators
➢ And
➢ Or -- lowest precedence
➢ not
This will gives the output when all the conditions become true.
Syntax:
select * from <table_name> where <condition1> and <condition2> and …<conditionn>
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>
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>
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>
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)
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)
NULL
This will gives the output based on the null values in the specified column.
Syntax:
select * from <table_name> where <col> is null
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
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>
No rows displayed
No rows displayed
ORDER BY
Syntax:
Select * from <table_name> order by <col> desc
GROUP BY
Columns used in select must be used with group by, otherwise it was not a group by -
expression.
This will work as where clause which can be used only with group by because of absence
of where clause in group by.
ORDER OF EXECUTION
FUNCTIONS
➢ Numeric functions
➢ String functions
➢ Date functions
Numeric functions
➢ MIN
➢ MAX
➢ AVG
➢ SUM
➢ SQRT
➢ MOD
➢ NVL
MIN
MIN ( SAL )
3.000
MAX
MAX ( SAL )
8.000
AVG
AVG ( SAL )
5.833
SUM
SUM ( SAL )
52.500
MOD
MOD ( SAL , 10 )
0
0
0
0
0
0
0
0
0
NVL
➢ LOWER
➢ UPPER
➢ CONCAT
➢ LENGTH
➢ SUBSTR
➢ INSTR
LOWER
LOWER ( NAME )
raja
dileep
sravan
UPPER
UPPER ( LOC )
HYD
BLORE
CHENNAI
LENGTH
LENGTH ( LOC )
15
15
15
CONCAT
CONCAT
2RAJA
3DILEEP
4SRAVAN
SUBSTR
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] ])
INSTR
4
0
3
INSTR
0
0
5
Date Functions
CURRENT_DATE
This will returns the current date in the session’s time zone.
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.
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.
CURRENT TIMESTAMP
2022-02-02-04.58.30.726748
2022-02-02-04.58.30.726748
2022-02-02-04.58.30.726748
LOCALTIMESTAMP
CURRENT TIMESTAMP
2022-02-02-05.01.52.688447
2022-02-02-05.01.52.688447
2022-02-02-05.01.52.688447
SUBQUERIES
TYPES
select * from student1 where sno=(select sno from student1 where name like 'D%')
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)
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.
JOINS
Types:
➢ Equi join
➢ Cross join
➢ Outer join
o Left outer
o Right outer
o Full outer
Equi join
An equi join is a type of join that combines tables based on matching values in specified
columns.
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.
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.
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.
This will display the all matching records and the non-matching records from both tables.
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:
MARKS MARKS_RANK
500 1
500 1
550 3