DMS Unit 3
DMS Unit 3
DM 22319 unit 1
Vidya Lunge 1
DM 22319 unit 1
● You can find the given the word from the sentence; even you can
substring the character up to the given point in the string.
● You can find a word from the given point and of the given length
using the MID function.
● You can also find the nth position of the given the word in a string.
Vidya Lunge 2
DM 22319 unit 1
Result: educba_to_learn
FORMAT(): It changes the format of the text from a string to any other
format.
Example:
FORMAT("0.254", "Percent");
Result: ‘25.40%’
INSERT(): It helps you insert text, integer, float, or double into your
database.
Example:
LCASE("eduCBA.com To Learn");
Vidya Lunge 3
DM 22319 unit 1
INITCAP()
LOWER()
RIGHT()
LEFT(): It is used to get the substring from the left of the string to the
given index position.
Example:
Result: edu
LENGTH('educba.com');
Result: 10
SELECT LOWER('EDUCBA.COM');
Result: educba.com
Vidya Lunge 4
DM 22319 unit 1
LPAD(): It adds left padding with the given symbol to make the string of
a given size.
Example:
LPAD('yahoo', 7, '@');
Result:@@yahoo
LTRIM(): It trims the given character from the left of the string.
Example:
LTRIM('345yahoo', '345');
RPAD()
RTRIM()
Result: yahoo
MID(): It gives you the substring from a given position to the number of
characters in the string.
Example:
MID("educba.com", 3, 2);
Result: cba
Example:
Vidya Lunge 5
DM 22319 unit 1
Result: 3
Result: educbaeducba
REPLACE('456yahoo456', '456');
Result: yahoo
SELECT REVERSE('educba.com');
Result: moc.abcuda
RIGHT(): It is used to get the substring from the right of the string to the
given index.
Example:
Result: ‘.com’
RPAD(): It adds the right padding with the given symbol to make the
string of the given size.
Vidya Lunge 6
DM 22319 unit 1
Example:
RPAD('educba', 7, '@');
Result: ‘educba@@’
RTRIM(): It trims the given character from the right of the string.
Example:
RTRIM('educbapou', 'pou');
Result: ‘educba’
Example:
SELECT SPACE(6);
Result: ‘ ‘
Example:
Vidya Lunge 7
DM 22319 unit 1
Result: 0
Example:
SUBSTR('educba.com', 1, 5);
Result: ‘educba’
Example:
Result: ‘C’
Example:
Result: ‘https:’
Vidya Lunge 8
DM 22319 unit 1
Example:
Result: 567
Example:
UCASE("EduCbA");
Result: EDUCBA
+++++++++++++++++++++++++++++++++++++++++++++++++++
+++++
ABS(X)
This function returns the absolute value of X. For example −
Vidya Lunge 9
DM 22319 unit 1
MOD(X,Y)
The variable X is divided by Y and their remainder is returned. For example −
SIGN(X)
This method returns 1 if X is positive, -1 if it is negative and 0 if the value of X is
0. For example −
FLOOR(X)
This returns the largest integer value that is either less than X or equal to it. For
example −
Select floor(5.7) from dual;
This returns 5.
CEIL(X)
This returns the smallest integer value that is either more than X or equal to it. For
example −
Select ceil(5.7);
This returns 6.
ROUND(X)
This function returns the value of X rounded off to the whole integer that is nearest
to it. For example −
Select round(5.7);
This returns 6.
Select round(5.3);
Vidya Lunge 10
DM 22319 unit 1
POWER(X,Y)
This function returns the value of x raised to the power of Y For example −
Select power(2,5);
This returns 32.
SQRT(X)
This function returns the square root of X. For example −
Select sqrt(9);
This returns 3.
ASIN(X)
This function accepts a Sin value as the input and returns the angle in radians. For
example −
Select asin(0);
This returns 0.
ACOS(X)
This function accepts a Cos value as the input and returns the angle in radians. For
example −
Select acos(1);
This returns 0.
ATAN(X)
This function accepts a Tan value as the input and returns the angle in radians. For
example −
Select atan(0);
This returns 0.
SIN(X)
This function accepts an angle in radians as its parameter and returns its Sine
value. For example −
Vidya Lunge 11
DM 22319 unit 1
Select sin(0);
This returns 0.
COS(X)
This function accepts an angle in radians as its parameter and returns its Cosine
value. For example −
Select cos(0);
This returns 1.
TAN(X)
This function accepts an angle in radians as its parameter and returns its Tan value.
For example −
Select tan(0);
This returns 0
Function Description
s
Vidya Lunge 12
DM 22319 unit 1
SQRT() The SQL SQRT() returns the square root of given value
in the argument.
Vidya Lunge 13
DM 22319 unit 1
Vidya Lunge 14
DM 22319 unit 1
Vidya Lunge 15
DM 22319 unit 1
Vidya Lunge 16
DM 22319 unit 1
Vidya Lunge 17
DM 22319 unit 1
+---------------------------------------------------------+
| TIME('2018-08-01 11:33:25') |
+---------------------------------------------------------+
| 11:33:25 |
+---------------------------------------------------------+
1 row in set (0.00 sec)
This displays the time part of '2018-08-01 11:33:25' in the form of a
string.
Dual table
What is DUAL table in SQL/oracle?
● DUAL is a table automatically created by Oracle Database along with
the data dictionary.
● DUAL is in the schema of the user SYS but is accessible by the name
DUAL to all users.
● It has one column, DUMMY , defined to be VARCHAR2(1) , and contains
one row with a value X .
● It is suitable for use in selecting a pseudo column such as SYSDATE or
USER.
Vidya Lunge 18
DM 22319 unit 1
1. COUNT FUNCTION
● COUNT function uses the COUNT(*) that returns the count of all the rows
in a specified table.
Syntax
Sample table:
PRODUCT_MAST
Vidya Lunge 19
DM 22319 unit 1
Item1 Com1 2 10 20
Item2 Com2 3 25 75
Item3 Com1 2 30 60
Item4 Com3 5 10 50
Item5 Com2 2 20 40
Item6 Com1 3 25 75
Item8 Com1 3 10 30
Item9 Com2 2 25 50
Example: COUNT()
Vidya Lunge 20
DM 22319 unit 1
1. SELECT COUNT(*)
2. FROM PRODUCT_MAST;
Output:
10
1. SELECT COUNT(*)
2. FROM PRODUCT_MAST;
3. WHERE RATE>=20;
Output:
Output:
Vidya Lunge 21
DM 22319 unit 1
Output:
Com1 5
Com2 3
Com3 2
Output:
Com1 5
Com2 3
2. SUM Function
Sum function is used to calculate the sum of all selected columns. It works on
numeric fields only.
Syntax
Example: SUM()
1. SELECT SUM(COST)
2. FROM PRODUCT_MAST;
Vidya Lunge 22
DM 22319 unit 1
Output:
670
1. SELECT SUM(COST)
2. FROM PRODUCT_MAST
3. WHERE QTY>3;
Output:
320
1. SELECT SUM(COST)
2. FROM PRODUCT_MAST
3. WHERE QTY>3
4. GROUP BY COMPANY;
Output:
Com1 150
Com2 170
Vidya Lunge 23
DM 22319 unit 1
4. HAVING SUM(COST)>=170;
Output:
Com1 335
Com3 170
3. AVG function
The AVG function is used to calculate the average value of the numeric type.
AVG function returns the average of all non-Null values.
Syntax
1. AVG()
2. or
3. AVG( [ALL|DISTINCT] expression )
Example:
1. SELECT AVG(COST)
2. FROM PRODUCT_MAST;
Output:
67.00
4. MAX Function
MAX function is used to find the maximum value of a certain column. This
function determines the largest value of all selected values of a column.
Syntax
Vidya Lunge 24
DM 22319 unit 1
1. MAX()
2. or
3. MAX( [ALL|DISTINCT] expression )
Example:
1. SELECT MAX(RATE)
2. FROM PRODUCT_MAST;
30
5. MIN Function
MIN function is used to find the minimum value of a certain column. This
function determines the smallest value of all selected values of a column.
Syntax
1. MIN()
2. or
3. MIN( [ALL|DISTINCT] expression )
Example:
1. SELECT MIN(RATE)
2. FROM PRODUCT_MAST;
Output:
10
Vidya Lunge 25
DM 22319 unit 1
SQL Clauses
The following are the various SQL clauses:
1. GROUP BY
Syntax
1. SELECT column
2. FROM table_name
3. WHERE conditions
4. GROUP BY column
5. ORDER BY column
Vidya Lunge 26
DM 22319 unit 1
Sample table:
PRODUCT_MAST
Item1 Com1 2 10 20
Item2 Com2 3 25 75
Item3 Com1 2 30 60
Item4 Com3 5 10 50
Item5 Com2 2 20 40
Item6 Cpm1 3 25 75
Item8 Com1 3 10 30
Item9 Com2 2 25 50
Example:
Vidya Lunge 27
DM 22319 unit 1
Output:
Com1 5
Com2 3
Com3 2
2. HAVING
● If you are not using GROUP BY clause then you can use HAVING
function like a WHERE clause.
Syntax:
Vidya Lunge 28
DM 22319 unit 1
Example:
Output:
Com1 5
Com2 3
3. ORDER BY
Syntax:
Where
Vidya Lunge 29
DM 22319 unit 1
CUSTOMER
12 Kathrin US
23 David Bangkok
34 Alina Dubai
45 John UK
56 Harry US
1. SELECT *
2. FROM CUSTOMER
3. ORDER BY NAME;
Output:
Vidya Lunge 30
DM 22319 unit 1
34 Alina Dubai
23 David Bangkok
56 Harry US
45 John UK
12 Kathrin US
1. SELECT *
2. FROM CUSTOMER
3. ORDER BY NAME DESC;
Output:
12 Kathrin US
45 John UK
Vidya Lunge 31
DM 22319 unit 1
56 Harry US
23 David Bangkok
34 Alina Dubai
++++++++++++++++++++
SQL JOIN
● As the name shows, JOIN means to combine something.
● In case of SQL, JOIN means "to combine two or more tables".
● In SQL, JOIN clause is used to combine the records from two or
more tables in a database.
Vidya Lunge 32
DM 22319 unit 1
5.
Sample Table
EMPLOYEE
Vidya Lunge 33
DM 22319 unit 1
PROJECT
PROJECT_NO EMP_ID DEPARTMENT
101 1 Testing
102 2 Development
103 3 Designing
104 4 Development
1. INNER JOIN
In SQL, INNER JOIN selects records that have matching values in both tables
as long as the condition is satisfied.
It returns the combination of all rows from both the tables where the
condition satisfies.
Syntax
Query
Output
EMP_NAME DEPARTMENT
Vidya Lunge 34
DM 22319 unit 1
Angelina Testing
Robert Development
Christian Designing
Kristen Development
2. LEFT JOIN
The SQL left join returns all the values from left table and the matching
values from the right table. If there is no matching join value, it will return
NULL.
Syntax
Query
Output
EMP_NAME DEPARTMENT
Vidya Lunge 35
DM 22319 unit 1
Angelina Testing
Robert Development
Christian Designing
Kristen Development
Russell NULL
Marry NULL
3. RIGHT JOIN
In SQL, RIGHT JOIN returns all the values from the values from the rows of
right table and the matched values from the left table. If there is no matching
in both tables, it will return NULL.
Syntax
Query
Vidya Lunge 36
DM 22319 unit 1
Output
EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
Christian Designing
Kristen Development
4. FULL JOIN
● In SQL, FULL JOIN is the result of a combination of both left and right
outer join.
● Join tables have all the records from both tables.
● It puts NULL on the place of matches not found.
Syntax
Query
Vidya Lunge 37
DM 22319 unit 1
Output
EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
Christian Designing
Kristen Development
Russell NULL
Marry NULL
Important Rule:
Vidya Lunge 38
DM 22319 unit 1
● The outer query is known as the main query, and the inner query is
known as a subquery.
Syntax
1. SELECT column_name
2. FROM table_name
3. WHERE column_name expression operator ( SELECT column_name
from table_name WHERE ... );
Example
Vidya Lunge 39
DM 22319 unit 1
1 John 20 US 2000.00
4 Alina 29 UK 6500.00
1. SELECT *
2. FROM EMPLOYEE
3. WHERE ID IN (SELECT ID FROM EMPLOYEE
4. WHERE SALARY > 4500);
------------------------------
5. SELECT *
6. FROM EMPLOYEE
7. WHERE ID IN (4,5,7);
Vidya Lunge 40
DM 22319 unit 1
4 Alina 29 UK 6500.00
● In the subquery, the selected data can be modified with any of the
character, date functions.
Syntax:
Example
Vidya Lunge 41
DM 22319 unit 1
Now use the following syntax to copy the complete EMPLOYEE table into the
EMPLOYEE_BKP table.
----------------
Syntax
1. UPDATE table
2. SET column_name = new_value
3. WHERE VALUE OPERATOR
4. (SELECT COLUMN_NAME
5. FROM TABLE_NAME
6. WHERE condition);
Example
Vidya Lunge 42
DM 22319 unit 1
1. UPDATE EMPLOYEE
2. SET SALARY = SALARY * 0.25
3. WHERE AGE IN (SELECT AGE FROM EMPLOYE_BKP
4. WHERE AGE >= 29);
This would impact three rows, and finally, the EMPLOYEE table would have
the following records.
1 John 20 US 2000.00
4 Alina 29 UK 1625.00
Vidya Lunge 43
DM 22319 unit 1
The subquery of SQL can be used in conjunction with the Delete statement
just like any other statements mentioned above.
Syntax
Example
---------
This would impact three rows, and finally, the EMPLOYEE table would have
the following records.
1 John 20 US 2000.00
Vidya Lunge 44
DM 22319 unit 1
1. store data.
2. reference data.
Some of the examples of database objects are : view, sequence, indexes, etc.
Vidya Lunge 45
DM 22319 unit 1
DESCRIBE dept;
View –
● This database object is used to create a view in database.
● A view is a logical table based on a table or another view.
● A view contains no data of its own but is like a window through
which data from tables can be viewed or changed.
● The tables on which a view is based are called base tables.
Vidya Lunge 46
DM 22319 unit 1
Views in SQL
● Views in SQL are considered as a virtual table.
● To create the view, we can select the fields from one or more tables present
in the database.
Vidya Lunge 47
DM 22319 unit 1
● A view can either have specific rows based on certain condition or all the
rows of a table.
Sample table:
Student_Detail
1 Stephan Delhi
2 Kathrin Noida
3 David Ghaziabad
4 Alina Gurugram
Student_Marks
1 Stephan 97 19
2 Kathrin 86 21
3 David 74 18
4 Alina 90 20
5 John 96 18
Vidya Lunge 48
DM 22319 unit 1
1. Creating view
A view can be created using the CREATE VIEW statement.
Syntax:
Query:
Just like table query, we can query the view to view the data.
Output:
Vidya Lunge 49
DM 22319 unit 1
NAME ADDRESS
Stephan Delhi
Kathrin Noida
David Ghaziabad
In the given example, a view is created named MarksView from two tables
Student_Detail and Student_Marks.
Query:
Vidya Lunge 50
DM 22319 unit 1
Stephan Delhi 97
Kathrin Noida 86
David Ghaziabad 74
Alina Gurugram 90
4. Deleting View
A view can be deleted using the Drop View statement.
Syntax
Example:
++++++++++++++++++++++++++++++++++++++++++
Sequence –
● This database object is used to create a sequence in database.
● A sequence is a user created database object that can be shared by multiple
users to generate unique integers.
● A typical usage for sequences is to create a primary key value, which
must be unique for each row.
● Sequence is a set of integers 1, 2, 3, … that are generated and supported
by some database systems to produce unique values on demand.
Vidya Lunge 51
DM 22319 unit 1
Vidya Lunge 52
DM 22319 unit 1
[{MINVALUE n | NOMINVALUE}]
[{CYCLE | NOCYCLE}]
[{CACHE n | NOCACHE}];
3,6,9,12,15,18,21,24,27,30------increment 1, max,
Vidya Lunge 53
DM 22319 unit 1
NOCYCLE;
Check if sequence is created by :
SELECT sequence_name, min_value, max_value,
increment_by, last_number
FROM user_sequences;
Example 1:
CREATE SEQUENCE sequence_1
start with 1
increment by 1
minvalue 0
maxvalue 100
cycle;
●
value 100.
● Sequence will repeat itself from start value after exceeding 100.
Example 2:
Following is the sequence query creating sequence in descending order.
CREATE SEQUENCE sequence_2
start with 100
increment by -1
minvalue 1
maxvalue 100
cycle;
●
Vidya Lunge 54
DM 22319 unit 1
● Sequence will start from 100 and should be less than or equal to maximum
This statement sets a new maximum value for the customers_seq sequence,
which was created in "Creating a Sequence: Example":
Vidya Lunge 55
DM 22319 unit 1
This statement turns on CYCLE and CACHE for the customers_seq sequence:
Index –
● This database object is used to create a indexes in database.
● An Oracle server index is a schema object that can speed up the retrieval of
rows by using a pointer.Indexes can be created explicitly or automatically.
● If you do not have an index on the column, then a full table scan occurs.
An index provides direct and fast access to rows in a table.
● Its purpose is to reduce the necessity of disk I/O by using an indexed path to
locate data quickly.
● The index is used and maintained automatically by the Oracle server.
● Once an index is created, no direct activity is required by the user
● Indexes are logically and physically independent of the table they index.
● This means that they can be created or dropped at any time and have no
effect on the base tables or other indexes.
● Indexes are special lookup tables.
Vidya Lunge 56
DM 22319 unit 1
● An Index is used to speed up select queries and where clauses. But it shows
down the data input with insert and update statements. Indexes can be
created or dropped without affecting the data.
● For example: When you reference all pages in a book that discusses a certain
topic, you first have to refer to the index, which alphabetically lists all the
topics and then referred to one or more specific page numbers.
Syntax
Vidya Lunge 57
DM 22319 unit 1
Example
Implicit indexes :
● These are indexes that are automatically created by the database server
when an object is created.
● Indexes are automatically created for primary key constraints and unique
constraints.
Syntax
Example
++++++++++++++++++++++++++++++++++++++++++
Synonym :
● A SYNONYM provides another name for database object, referred to as original
object, that may exist on a local or another server.
● A synonym belongs to schema, name of synonym should be unique.
Vidya Lunge 58
DM 22319 unit 1
FOR schema.object;
FOR inventories;
the stocks is synonym of the inventories table in the query like the following:
Vidya Lunge 59
DM 22319 unit 1
Extra:
To define the synonym offices for the table locations in the schema hr, issue the
following statement:
To create a PUBLIC synonym for the employees table in the schema hr on the remote
database, you could issue the following statement:
A synonym may have the same name as the underlying object, provided the
underlying object is contained in another schema.
If the user hr's schema does not contain an object named customers, and if hr has select
permission on oe.customers, then hr can access the customers table in oe's schema by
using the public synonym customers:
Vidya Lunge 60
DM 22319 unit 1
with the schema and provides you with an alternative name for a table,
view, sequence,procedure, or other objects. This method can be
especially useful with lengthy object names, such as views.
In the syntax:
PUBLIC : creates a synonym accessible to all users
synonym : is the name of the synonym to be created
object : identifies the object for which the synonym is created
Syntax :
●
CREATE [PUBLIC] SYNONYM synonym FOR object;
Example :
CREATE SYNONYM d_sum FOR dept_sum_vu;
__________________________________________________________
Vidya Lunge 61