SlideShare a Scribd company logo
Mrs. Ujjwala S Patil (SITCOE Yadrav) 1
Interactive SQL and Advance SQL: SQL Performance
Tuning
Inbuilt Functions: String, Arithmetic(Numeric
Functions)
Functions are sub programs which are written for a particular
purpose or a task.
Types of Functions:
1. Inbuilt functions
2. User defined functions.
Mrs. Ujjwala S Patil (SITCOE Yadrav) 2
String Functions:
String functions are used to perform an operation on input string
and return an output string.
Arithmetic Function (Numeric Functions):
Numeric Functions are used to perform operations on numbers
and return numbers.
Mrs. Ujjwala S Patil (SITCOE Yadrav) 3
Arithmetic Function (Numeric Functions):
Numeric Functions are used to perform operations on numbers
and return numbers.
Mrs. Ujjwala S Patil (SITCOE Yadrav) 4
Date and Time Functions:
In SQL, dates are complicated since while working with a database,
the format of the date in the table must be matched with the input
date in order to insert. In various scenarios instead of date, datetime
(time is also involved with date) is used.
Mrs. Ujjwala S Patil (SITCOE Yadrav) 5
Aggregate Functions:
An aggregate function performs a calculation on a set of
values, and returns a single value. Except for COUNT(*),
aggregate functions ignore null values. Aggregate functions
are often used with the GROUP BY clause of the SELECT
statement.
• AVG()
• COUNT()
• FIRST()
• LAST()
• MAX()
• MIN()
• SUM()
Mrs. Ujjwala S Patil (SITCOE Yadrav) 6
3.3 Queries using Group by, Having, and Order by
clause, Joins- Inner and Outer Join, Sub Queries.
Employee Table:
Mrs. Ujjwala S Patil (SITCOE Yadrav) 7
Queries using Group by:
The GROUP BY Statement in SQL is used to arrange identical
data into groups with the help of some functions. i.e if a particular
column has same values in different rows then it will arrange these
rows in a group.
Important Points:
GROUP BY clause is used with the SELECT statement.
In the query, GROUP BY clause is placed after the WHERE
clause.
In the query, GROUP BY clause is placed before ORDER BY
clause if used any.
Mrs. Ujjwala S Patil (SITCOE Yadrav) 8
Syntax for Group by clause:
SELECT column1, function_name(column2)
FROM table_name
WHERE condition
GROUP BY column1, column2;
function_name: Name of the function used for example, SUM() ,
AVG().
table_name: Name of the table.
condition: Condition used.
Mrs. Ujjwala S Patil (SITCOE Yadrav) 9
Example:
SELECT NAME, SUM(SALARY) FROM Employee
GROUP BY NAME;
Output:
Mrs. Ujjwala S Patil (SITCOE Yadrav) 10
3.3 Queries using Having clause:
We can use HAVING clause to place conditions to decide which
group will be the part of final result-set. Also we can not use the
aggregate functions like SUM(), COUNT() etc. with WHERE
clause. So we have to use HAVING clause if we want to use any of
these functions in the conditions.
Mrs. Ujjwala S Patil (SITCOE Yadrav) 11
Syntax for Having clause:
SELECT column1, function_name(column2)
FROM table_name
WHERE condition
GROUP BY column1, column2
HAVING condition;
function_name: Name of the function used for example, SUM() ,
AVG().
table_name: Name of the table.
condition: Condition used.
Mrs. Ujjwala S Patil (SITCOE Yadrav) 12
Example:
SELECT NAME, SUM(SALARY) FROM Employee
GROUP BY NAME
HAVING SUM(SALARY)>3000;
Output:
Mrs. Ujjwala S Patil (SITCOE Yadrav) 13
3.3 Queries using Order by clause:
The ORDER BY statement in sql is used to sort the fetched data in
either ascending or descending according to one or more columns.
By default ORDER BY sorts the data in ascending order.
We can use the keyword DESC to sort the data in descending order
and the keyword ASC to sort in ascending order.
To sort in ascending or descending order we can use the keywords
ASC or DESC respectively.
Syntax:
SELECT * FROM table_name ORDER BY column_name ASC|
DESC
Mrs. Ujjwala S Patil (SITCOE Yadrav) 14
Example:
Mrs. Ujjwala S Patil (SITCOE Yadrav) 15
SELECT * FROM Student ORDER BY ROLL_NO DESC;
Output:
ROLL_NO NAME ADDRESS PHONE Age
8 NIRAJ ALIPUR XXXXXXXXXX 19
7 ROHIT BALURGHAT XXXXXXXXXX 18
6 DHANRAJ BARABAJAR XXXXXXXXXX 20
5 SAPTARHI KOLKATA XXXXXXXXXX 19
4 DEEP RAMNAGAR XXXXXXXXXX 18
3 RIYANKA SILIGURI XXXXXXXXXX 20
2 PRATIK BIHAR XXXXXXXXXX 19
1 HARSH DELHI XXXXXXXXXX 18
Mrs. Ujjwala S Patil (SITCOE Yadrav) 16
Joins- Inner and Outer Join:
A SQL Join statement is used to combine data or rows from two or
more tables based on a common field between them. Different
types of Joins are:
•INNER JOIN
•LEFT JOIN
•RIGHT JOIN
•FULL JOIN
Mrs. Ujjwala S Patil (SITCOE Yadrav) 17
Inner Join:
The INNER JOIN keyword selects all rows from both the tables as
long as the condition satisfies. This keyword will create the result-
set by combining all rows from both the tables where the condition
satisfies i.e value of the common field will be same.
Mrs. Ujjwala S Patil (SITCOE Yadrav) 18
Inner Join Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
INNER JOIN table2
ON table1.matching_column = table2.matching_column;
table1: First table.
table2: Second table
matching_column: Column common to both the tables.
Mrs. Ujjwala S Patil (SITCOE Yadrav) 19
Student Table: StudentCourse Table:
Mrs. Ujjwala S Patil (SITCOE Yadrav) 20
Example:
SELECT StudentCourse.COURSE_ID, Student.NAME,
Student.AGE FROM StudentINNER JOIN StudentCourseON
Student.ROLL_NO = StudentCourse.ROLL_NO;
Output:
Mrs. Ujjwala S Patil (SITCOE Yadrav) 21
Left Join (Left Outer Join):
This join returns all the rows of the table on the left side of the join
and matching rows for the table on the right side of join. The rows
for which there is no matching row on right side, the result-set will
contain null. LEFT JOIN is also known as LEFT OUTER JOIN.
Mrs. Ujjwala S Patil (SITCOE Yadrav) 22
Syntax :
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1 LEFT JOIN table2
ON table1.matching_column = table2.matching_column;
table1: First table.
table2: Second table
matching_column: Column common to both the tables
Mrs. Ujjwala S Patil (SITCOE Yadrav) 23
Example for Left outer Join:
SELECT Student.NAME, StudentCourse.COURSE_ID
FROM Student
LEFT JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;
Output:
Mrs. Ujjwala S Patil (SITCOE Yadrav) 24
RIGHT JOIN (RIGHT OUTER JOIN) :
RIGHT JOIN is similar to LEFT JOIN. This join returns
all the rows of the table on the right side of the join and
matching rows for the table on the left side of join. The
rows for which there is no matching row on left side, the
result-set will contain null. RIGHT JOIN is also known
as RIGHT OUTER JOIN.
Mrs. Ujjwala S Patil (SITCOE Yadrav) 25
Syntax for Right Outer Join:
SELECT able1.column1,table1.column2,table2.column1,....
FROM table1
RIGHT JOIN table2
ON table1.matching_column = table2.matching_column;
table1: First table.
table2: Second table
matching_column: Column common to both the tables
Mrs. Ujjwala S Patil (SITCOE Yadrav) 26
Example for Right Outer Join:
SELECT Student.NAME, Student Course.COURSE_ID FROM
Student
RIGHT JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;
Output:
Mrs. Ujjwala S Patil (SITCOE Yadrav) 27
FULL JOIN :
FULL JOIN creates the result-set by combining result of both
LEFT JOIN and RIGHT JOIN. The result-set will contain all the
rows from both the tables. The rows for which there is no
matching, the result-set will contain NULL values.
Mrs. Ujjwala S Patil (SITCOE Yadrav) 28
Syntax for full Outer Join:
SELECT table1.column1,table1.column2,table2.column1,...
FROM table1
FULL JOIN table2
ON table1.matching_column = table2.matching_column;
table1: First table.
table2: Second table
matching_column: Column common to both the tables.
Mrs. Ujjwala S Patil (SITCOE Yadrav) 29
Example of Full Join:
SELECT Student.NAME,StudentCourse.COURSE_ID FROM
StudentFULL JOIN StudentCourse ON StudentCourse.ROLL_NO
= Student.ROLL_NO;
Output:
Mrs. Ujjwala S Patil (SITCOE Yadrav) 30
Sub Query:
In SQL a Subquery can be simply defined as a query within
another query. In other words we can say that a Subquery is a
query that is embedded in WHERE clause of another SQL query.
Syntax:
There is not any general syntax for Subqueries. However,
Subqueries are seen to be used most frequently with SELECT
statement as shown below:
SELECT column_name FROM table_name WHERE
column_name expression operator ( SELECT
COLUMN_NAME from TABLE_NAME WHERE ... );
Mrs. Ujjwala S Patil (SITCOE Yadrav) 31
Important rules for Subqueries:
• Subqueries can be used with SELECT, UPDATE, INSERT, DELETE
statements along with expression operator. It could be equality
operator or comparison operator such as =, >, =, <= and Like
operator.
• A subquery is a query within another query. The outer query is called
as main query and inner query is called as subquery.
• The subquery generally executes first, and its output is used to
complete the query condition for the main or outer query.
• Subquery must be enclosed in parentheses.
• Subqueries are on the right side of the comparison operator.
• ORDER BY command cannot be used in a Subquery. GROUPBY
command can be used to perform same function as ORDER BY
command.
• Use single-row operators with singlerow Subqueries. Use multiple-
row operators with multiple-row Subqueries.
Mrs. Ujjwala S Patil (SITCOE Yadrav) 32
Example:
Database :
Student :
NAME ROLL_NO LOCATION PHONE_NUMBER
Ram 101 Chennai 9988775566
Raj 102 Coimbatore 8877665544
Sasi 103 Madurai 7766553344
Ravi 104 Salem 8989898989
Sumathi 105 Kanchipuram 8989856868
NAME ROLL_NO SECTION
Ravi 104 A
Sumathi 105 B
Raj 102 A
Mrs. Ujjwala S Patil (SITCOE Yadrav) 33
To display NAME, LOCATION, PHONE_NUMBER of the
students from DATABASE table whose section is A
Select NAME, LOCATION, PHONE_NUMBER
from DATABASE WHERE ROLL_NO
IN (SELECT ROLL_NO from STUDENT where SECTION=’A’);
Output:
NAME ROLL_NO LOCATION PHONE_NUMBER
Ravi 104 Salem 8989898989
Raj 102 Coimbatore 8877665544
Mrs. Ujjwala S Patil (SITCOE Yadrav) 34
3.4 Views: Concept of View, The create view
Command, Updating views, Dropping views.
View can be defined as a logical part (marked for some special
purpose) of the total data present in the table.
View can be created with the help of Select statement.
View can be read only or they could be used to insert, update, and
delete the table data and view data.
Views are used for the purpose of data security and minimizing
data redundancy.
Mrs. Ujjwala S Patil (SITCOE Yadrav) 35
Create View:
Database views are created using the CREATE VIEW statement.
Views can be created from a single table, multiple tables or another
view.
Syntax:
CREATE VIEW view_name AS
SELECT column1, column2.....
FROM table_name WHERE [condition];
Mrs. Ujjwala S Patil (SITCOE Yadrav) 36
Example:
CUSTOMERS Table:
Mrs. Ujjwala S Patil (SITCOE Yadrav) 37
CREATE VIEW CUSTOMERS_VIEW AS
SELECT name, age
FROM CUSTOMERS;
Output:
View Created
0.06 seconds
Mrs. Ujjwala S Patil (SITCOE Yadrav) 38
How to Display the View:
SELECT * FROM CUSTOMERS_VIEW;
Output:
Mrs. Ujjwala S Patil (SITCOE Yadrav) 39
Update View:
View can be used to update the data from the table as well as view.
Example:
UPDATE CUSTOMERS_VIEW SET AGE = 35 WHERE name =
'Ramesh';
Output:
One row(s) updated
0.03 seconds
Mrs. Ujjwala S Patil (SITCOE Yadrav) 40
Now we can check the updated data by using Select
statement:
SELECT * FROM CUSTOMERS;
Output:
Mrs. Ujjwala S Patil (SITCOE Yadrav) 41
Dropping View:
Drop view command is used for dropping the view.
Syntax:
Drop vew <View nmae>;
Example:
DROP VIEW CUSTOMERS_VIEW;
Output:
View dropped
0.12 seconds.
Mrs. Ujjwala S Patil (SITCOE Yadrav) 42
3.5 Sequences: Creating Sequence, Altering Sequence,
Dropping Sequence:
"A Sequence is simply an automatic counter, which generates
sequential numbers whenever required.“
A sequence can be defined to:
•Generate numbers in ascending or descending order.
•Provide intervals between numbers.
•Caching of sequence numbers in memory to speed up their
availability .
A sequence is an independent object and can be used with any
table that requires its output.
Mrs. Ujjwala S Patil (SITCOE Yadrav) 43
Creating a Sequences :
The minimum information required for generating numbers using a
sequence is:
• The starting number.
• The maximum number that can be generated by a sequence.
• The increment value for generating the next number.
Syntax:
CREATE SEQUENCE <Sequence Name> [INCREMENT BY
<IntegerValue> START WITH <Integer Value> MAXVALUE
<Integer Value> MINVALUE <Integer Value>
[CYCLE/NOCYCLE] [ CACHE/NOCACHE] ;
Mrs. Ujjwala S Patil (SITCOE Yadrav) 44
INCREMENT BY:
Specifies the interval between sequence numbers. It can be any positive or
negative value but not zero. If this clause is omitted, the default value is 1.
MAXVALUE And MINVALUE :
Specifies the maximum or minmum value that a sequence can generate.
START WITH:
Specifies the first sequence number to be generated.
CYCLE:
Specifies that the sequence continues to generate repeat values after reaching
either its maximum value.
CACHE:
Specifies how many values to generate in advance and to keep in memory for
faster access.Minimum value is two for this option.
Mrs. Ujjwala S Patil (SITCOE Yadrav) 45
Example for Creating Sequence:
Create sequence stud_seq
incremented by 1
Start with 100
Maxvalue 10000
Minvalue 100
Cycle cache 7;
Output:
Sequence created
0.04 seconds.
Mrs. Ujjwala S Patil (SITCOE Yadrav) 46
After creating a sequence we can access its value with the help
of nextval and currval
Nextval:
This returns original value of the sequence.
Example:
Select stud_seq.nextval from dual;
Output: NEXTVAL
100
1 row(s) returned in 0.02 seconds.
Mrs. Ujjwala S Patil (SITCOE Yadrav) 47
Currval:
This returns the current value of the sequence which is next value
to the next value.
Example:
Select stud_seq.currval from dual;
Output: CURRVAL
101
1 row(s) returned in 0.02 seconds.
Mrs. Ujjwala S Patil (SITCOE Yadrav) 48
Altering a Sequence :
A sequence once created can be altered. This is achieved by
using the ALTER SEQUENCE statement.
The START value of the sequence cannot be altered .
Syntax:
ALTER SEQUENCE <SequenceName> [INCREMENT BY
<IntegerValue> MINVALUE <IntegerValue> ];
Example:
Alter sequence stud_seq maxval 2000;
Output:
Sequence altered
0.02 seconds
Mrs. Ujjwala S Patil (SITCOE Yadrav) 49
Dropping Sequence:
Sequence can be delete or dropped using drop sequence
command.
Syntax:
Drop sequence <Sequence_Name>;
Example:
Drop sequence stud_seq;
Output:
Sequence dropped
0.02 seconds
Mrs. Ujjwala S Patil (SITCOE Yadrav) 50
3.6- Indexes- Index Types, creating of an Index,
Simple, Unique and Composite Index, Dropping
Indexes.
An index helps to speed up SELECT queries and
WHERE clauses, but it slows down data input, with
the UPDATE and the INSERT statements. Indexes can be created
or dropped with no effect on the data.
Types of Index:
1. Simple Index
2. Composite Index
3. Unique index.
Mrs. Ujjwala S Patil (SITCOE Yadrav) 51
Create Simple Index:
When we create index on single column of the table, it is called as
simple index.
Syntax:
CREATE INDEX index_name
ON table_name (column1);
Example:
CREATE INDEX stud_Indx
ON Student (Roll_No);
Output:
Index created
0.12 seconds
Mrs. Ujjwala S Patil (SITCOE Yadrav) 52
Create Composite Index:
When we create index on multiple columns of the table, it is called
as Composite index.
Syntax:
CREATE INDEX index_name
ON table_name (column1, Column2,…);
Example:
CREATE INDEX stud_Indx
ON Student (Roll_No, Name);
Output:
Index created
0.12 seconds
Mrs. Ujjwala S Patil (SITCOE Yadrav) 53
Create Unique Index:
When we create index on single column or multiple columns by
using “Unique” Keywordof the table, it is called Unique index.
Syntax:
CREATE UNIQUE INDEX index_name
ON table_name (column1);
Example:
CREATE UNIQUE INDEX stud_Indx
ON Student (Roll_No);
Output:
Index created
0.12 seconds
Mrs. Ujjwala S Patil (SITCOE Yadrav) 54
Dropping Index:
Index can be dropped using drop index command.
Syntax:
Drop Index Index_name;
Example:
Drop index stud_indx;
Output:
Index dropped
0.12 seconds.
Mrs. Ujjwala S Patil (SITCOE Yadrav) 55
3.7 Synonyms: Creating Synonyms, Dropping
Synonyms:
A synonym is an alternative name for objects such as tables,
views, sequences, stored procedures, and other database objects.
You generally use synonyms when you are granting access to an
object from another schema and you don't want the users to have to
worry about knowing which schema owns the object.
Mrs. Ujjwala S Patil (SITCOE Yadrav) 56
Creating Synonyms:
To create the synonyms create synonym command is used.
Syntax:
Create Synonym synonym_Name For Object_Name;
Example:
Create synonym sit_stud for student;
Output:
Synonym created
1.02 seconds
Mrs. Ujjwala S Patil (SITCOE Yadrav) 57
Dropping Synonyms:
A synonym can be deleted or dropped by using drop synonym
command
Syntax:
Drop synonym synonym_Name;
Example:
Drop synonym sit_stud;
Output:
Synonym dropped
1.02 seconds
Mrs. Ujjwala S Patil (SITCOE Yadrav) 58
Thank You

More Related Content

Similar to Interactive SQL and Advance SQL SQL Performance Tuning.pptx (20)

PPTX
Computer Science:Sql Set Operation
St Mary's College,Thrissur,Kerala
 
PPTX
Joins in SQL
Pooja Dixit
 
PDF
SQL dabatase interveiw pdf for interveiw preparation
kumarvikesh2841998
 
PPTX
SQL Server Learning Drive
TechandMate
 
PPTX
SQL Tutorial for Beginners
Abdelhay Shafi
 
DOCX
Query
Raj Devaraj
 
PDF
Interview Questions.pdf
TarunKumar893717
 
PDF
Structure query language, database course
yunussufyan2024
 
PPTX
Its about a sql topic for basic structured query language
IMsKanchanaI
 
PDF
0716330552518_DBMS_LAB_THEORY_SQL_OPERATOR (1).pdf
sahilurrahemankhan
 
PDF
DBMS Nested & Sub Queries Set operations
Sreedhar Chowdam
 
PPTX
SQL Operators.pptx
ssuserb8d5cb
 
PDF
Chapter9 more on database and sql
KV(AFS) Utarlai, Barmer (Rajasthan)
 
PPT
Ms sql server ii
Iblesoft
 
PPTX
SQL
Shyam Khant
 
PPTX
Day-06-Joining presentation of SQL lecture form uni .pptx
joynulabeden2
 
PPTX
Data Manipulation Language.pptx
EllenGracePorras
 
PPTX
Oracle: Joins
DataminingTools Inc
 
PPTX
Oracle: Joins
oracle content
 
PPTX
sqlyyybdbyehduheufhuehfuheuwehfiewifhewihfiehfiwf
kailasmanoj
 
Computer Science:Sql Set Operation
St Mary's College,Thrissur,Kerala
 
Joins in SQL
Pooja Dixit
 
SQL dabatase interveiw pdf for interveiw preparation
kumarvikesh2841998
 
SQL Server Learning Drive
TechandMate
 
SQL Tutorial for Beginners
Abdelhay Shafi
 
Interview Questions.pdf
TarunKumar893717
 
Structure query language, database course
yunussufyan2024
 
Its about a sql topic for basic structured query language
IMsKanchanaI
 
0716330552518_DBMS_LAB_THEORY_SQL_OPERATOR (1).pdf
sahilurrahemankhan
 
DBMS Nested & Sub Queries Set operations
Sreedhar Chowdam
 
SQL Operators.pptx
ssuserb8d5cb
 
Chapter9 more on database and sql
KV(AFS) Utarlai, Barmer (Rajasthan)
 
Ms sql server ii
Iblesoft
 
Day-06-Joining presentation of SQL lecture form uni .pptx
joynulabeden2
 
Data Manipulation Language.pptx
EllenGracePorras
 
Oracle: Joins
DataminingTools Inc
 
Oracle: Joins
oracle content
 
sqlyyybdbyehduheufhuehfuheuwehfiewifhewihfiehfiwf
kailasmanoj
 

Recently uploaded (20)

PPTX
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
PPTX
MODULE 05 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
PDF
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
PDF
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
PDF
aAn_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
PPTX
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
PPTX
MODULE 03 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
PPTX
Introduction to Internal Combustion Engines - Types, Working and Camparison.pptx
UtkarshPatil98
 
PPTX
Water Resources Engineering (CVE 728)--Slide 3.pptx
mohammedado3
 
PPTX
Final Major project a b c d e f g h i j k l m
bharathpsnab
 
PPTX
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
PPTX
澳洲电子毕业证澳大利亚圣母大学水印成绩单UNDA学生证网上可查学历
Taqyea
 
PPTX
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
PPTX
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
PDF
REINFORCEMENT LEARNING IN DECISION MAKING SEMINAR REPORT
anushaashraf20
 
PPT
Footbinding.pptmnmkjkjkknmnnjkkkkkkkkkkkkkk
mamadoundiaye42742
 
PPTX
OCS353 DATA SCIENCE FUNDAMENTALS- Unit 1 Introduction to Data Science
A R SIVANESH M.E., (Ph.D)
 
PPT
New_school_Engineering_presentation_011707.ppt
VinayKumar304579
 
PDF
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
MODULE 05 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
aAn_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
MODULE 03 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
Introduction to Internal Combustion Engines - Types, Working and Camparison.pptx
UtkarshPatil98
 
Water Resources Engineering (CVE 728)--Slide 3.pptx
mohammedado3
 
Final Major project a b c d e f g h i j k l m
bharathpsnab
 
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
澳洲电子毕业证澳大利亚圣母大学水印成绩单UNDA学生证网上可查学历
Taqyea
 
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
REINFORCEMENT LEARNING IN DECISION MAKING SEMINAR REPORT
anushaashraf20
 
Footbinding.pptmnmkjkjkknmnnjkkkkkkkkkkkkkk
mamadoundiaye42742
 
OCS353 DATA SCIENCE FUNDAMENTALS- Unit 1 Introduction to Data Science
A R SIVANESH M.E., (Ph.D)
 
New_school_Engineering_presentation_011707.ppt
VinayKumar304579
 
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 
Ad

Interactive SQL and Advance SQL SQL Performance Tuning.pptx

  • 1. Mrs. Ujjwala S Patil (SITCOE Yadrav) 1 Interactive SQL and Advance SQL: SQL Performance Tuning Inbuilt Functions: String, Arithmetic(Numeric Functions) Functions are sub programs which are written for a particular purpose or a task. Types of Functions: 1. Inbuilt functions 2. User defined functions.
  • 2. Mrs. Ujjwala S Patil (SITCOE Yadrav) 2 String Functions: String functions are used to perform an operation on input string and return an output string. Arithmetic Function (Numeric Functions): Numeric Functions are used to perform operations on numbers and return numbers.
  • 3. Mrs. Ujjwala S Patil (SITCOE Yadrav) 3 Arithmetic Function (Numeric Functions): Numeric Functions are used to perform operations on numbers and return numbers.
  • 4. Mrs. Ujjwala S Patil (SITCOE Yadrav) 4 Date and Time Functions: In SQL, dates are complicated since while working with a database, the format of the date in the table must be matched with the input date in order to insert. In various scenarios instead of date, datetime (time is also involved with date) is used.
  • 5. Mrs. Ujjwala S Patil (SITCOE Yadrav) 5 Aggregate Functions: An aggregate function performs a calculation on a set of values, and returns a single value. Except for COUNT(*), aggregate functions ignore null values. Aggregate functions are often used with the GROUP BY clause of the SELECT statement. • AVG() • COUNT() • FIRST() • LAST() • MAX() • MIN() • SUM()
  • 6. Mrs. Ujjwala S Patil (SITCOE Yadrav) 6 3.3 Queries using Group by, Having, and Order by clause, Joins- Inner and Outer Join, Sub Queries. Employee Table:
  • 7. Mrs. Ujjwala S Patil (SITCOE Yadrav) 7 Queries using Group by: The GROUP BY Statement in SQL is used to arrange identical data into groups with the help of some functions. i.e if a particular column has same values in different rows then it will arrange these rows in a group. Important Points: GROUP BY clause is used with the SELECT statement. In the query, GROUP BY clause is placed after the WHERE clause. In the query, GROUP BY clause is placed before ORDER BY clause if used any.
  • 8. Mrs. Ujjwala S Patil (SITCOE Yadrav) 8 Syntax for Group by clause: SELECT column1, function_name(column2) FROM table_name WHERE condition GROUP BY column1, column2; function_name: Name of the function used for example, SUM() , AVG(). table_name: Name of the table. condition: Condition used.
  • 9. Mrs. Ujjwala S Patil (SITCOE Yadrav) 9 Example: SELECT NAME, SUM(SALARY) FROM Employee GROUP BY NAME; Output:
  • 10. Mrs. Ujjwala S Patil (SITCOE Yadrav) 10 3.3 Queries using Having clause: We can use HAVING clause to place conditions to decide which group will be the part of final result-set. Also we can not use the aggregate functions like SUM(), COUNT() etc. with WHERE clause. So we have to use HAVING clause if we want to use any of these functions in the conditions.
  • 11. Mrs. Ujjwala S Patil (SITCOE Yadrav) 11 Syntax for Having clause: SELECT column1, function_name(column2) FROM table_name WHERE condition GROUP BY column1, column2 HAVING condition; function_name: Name of the function used for example, SUM() , AVG(). table_name: Name of the table. condition: Condition used.
  • 12. Mrs. Ujjwala S Patil (SITCOE Yadrav) 12 Example: SELECT NAME, SUM(SALARY) FROM Employee GROUP BY NAME HAVING SUM(SALARY)>3000; Output:
  • 13. Mrs. Ujjwala S Patil (SITCOE Yadrav) 13 3.3 Queries using Order by clause: The ORDER BY statement in sql is used to sort the fetched data in either ascending or descending according to one or more columns. By default ORDER BY sorts the data in ascending order. We can use the keyword DESC to sort the data in descending order and the keyword ASC to sort in ascending order. To sort in ascending or descending order we can use the keywords ASC or DESC respectively. Syntax: SELECT * FROM table_name ORDER BY column_name ASC| DESC
  • 14. Mrs. Ujjwala S Patil (SITCOE Yadrav) 14 Example:
  • 15. Mrs. Ujjwala S Patil (SITCOE Yadrav) 15 SELECT * FROM Student ORDER BY ROLL_NO DESC; Output: ROLL_NO NAME ADDRESS PHONE Age 8 NIRAJ ALIPUR XXXXXXXXXX 19 7 ROHIT BALURGHAT XXXXXXXXXX 18 6 DHANRAJ BARABAJAR XXXXXXXXXX 20 5 SAPTARHI KOLKATA XXXXXXXXXX 19 4 DEEP RAMNAGAR XXXXXXXXXX 18 3 RIYANKA SILIGURI XXXXXXXXXX 20 2 PRATIK BIHAR XXXXXXXXXX 19 1 HARSH DELHI XXXXXXXXXX 18
  • 16. Mrs. Ujjwala S Patil (SITCOE Yadrav) 16 Joins- Inner and Outer Join: A SQL Join statement is used to combine data or rows from two or more tables based on a common field between them. Different types of Joins are: •INNER JOIN •LEFT JOIN •RIGHT JOIN •FULL JOIN
  • 17. Mrs. Ujjwala S Patil (SITCOE Yadrav) 17 Inner Join: The INNER JOIN keyword selects all rows from both the tables as long as the condition satisfies. This keyword will create the result- set by combining all rows from both the tables where the condition satisfies i.e value of the common field will be same.
  • 18. Mrs. Ujjwala S Patil (SITCOE Yadrav) 18 Inner Join Syntax: SELECT table1.column1,table1.column2,table2.column1,.... FROM table1 INNER JOIN table2 ON table1.matching_column = table2.matching_column; table1: First table. table2: Second table matching_column: Column common to both the tables.
  • 19. Mrs. Ujjwala S Patil (SITCOE Yadrav) 19 Student Table: StudentCourse Table:
  • 20. Mrs. Ujjwala S Patil (SITCOE Yadrav) 20 Example: SELECT StudentCourse.COURSE_ID, Student.NAME, Student.AGE FROM StudentINNER JOIN StudentCourseON Student.ROLL_NO = StudentCourse.ROLL_NO; Output:
  • 21. Mrs. Ujjwala S Patil (SITCOE Yadrav) 21 Left Join (Left Outer Join): This join returns all the rows of the table on the left side of the join and matching rows for the table on the right side of join. The rows for which there is no matching row on right side, the result-set will contain null. LEFT JOIN is also known as LEFT OUTER JOIN.
  • 22. Mrs. Ujjwala S Patil (SITCOE Yadrav) 22 Syntax : SELECT table1.column1,table1.column2,table2.column1,.... FROM table1 LEFT JOIN table2 ON table1.matching_column = table2.matching_column; table1: First table. table2: Second table matching_column: Column common to both the tables
  • 23. Mrs. Ujjwala S Patil (SITCOE Yadrav) 23 Example for Left outer Join: SELECT Student.NAME, StudentCourse.COURSE_ID FROM Student LEFT JOIN StudentCourse ON StudentCourse.ROLL_NO = Student.ROLL_NO; Output:
  • 24. Mrs. Ujjwala S Patil (SITCOE Yadrav) 24 RIGHT JOIN (RIGHT OUTER JOIN) : RIGHT JOIN is similar to LEFT JOIN. This join returns all the rows of the table on the right side of the join and matching rows for the table on the left side of join. The rows for which there is no matching row on left side, the result-set will contain null. RIGHT JOIN is also known as RIGHT OUTER JOIN.
  • 25. Mrs. Ujjwala S Patil (SITCOE Yadrav) 25 Syntax for Right Outer Join: SELECT able1.column1,table1.column2,table2.column1,.... FROM table1 RIGHT JOIN table2 ON table1.matching_column = table2.matching_column; table1: First table. table2: Second table matching_column: Column common to both the tables
  • 26. Mrs. Ujjwala S Patil (SITCOE Yadrav) 26 Example for Right Outer Join: SELECT Student.NAME, Student Course.COURSE_ID FROM Student RIGHT JOIN StudentCourse ON StudentCourse.ROLL_NO = Student.ROLL_NO; Output:
  • 27. Mrs. Ujjwala S Patil (SITCOE Yadrav) 27 FULL JOIN : FULL JOIN creates the result-set by combining result of both LEFT JOIN and RIGHT JOIN. The result-set will contain all the rows from both the tables. The rows for which there is no matching, the result-set will contain NULL values.
  • 28. Mrs. Ujjwala S Patil (SITCOE Yadrav) 28 Syntax for full Outer Join: SELECT table1.column1,table1.column2,table2.column1,... FROM table1 FULL JOIN table2 ON table1.matching_column = table2.matching_column; table1: First table. table2: Second table matching_column: Column common to both the tables.
  • 29. Mrs. Ujjwala S Patil (SITCOE Yadrav) 29 Example of Full Join: SELECT Student.NAME,StudentCourse.COURSE_ID FROM StudentFULL JOIN StudentCourse ON StudentCourse.ROLL_NO = Student.ROLL_NO; Output:
  • 30. Mrs. Ujjwala S Patil (SITCOE Yadrav) 30 Sub Query: In SQL a Subquery can be simply defined as a query within another query. In other words we can say that a Subquery is a query that is embedded in WHERE clause of another SQL query. Syntax: There is not any general syntax for Subqueries. However, Subqueries are seen to be used most frequently with SELECT statement as shown below: SELECT column_name FROM table_name WHERE column_name expression operator ( SELECT COLUMN_NAME from TABLE_NAME WHERE ... );
  • 31. Mrs. Ujjwala S Patil (SITCOE Yadrav) 31 Important rules for Subqueries: • Subqueries can be used with SELECT, UPDATE, INSERT, DELETE statements along with expression operator. It could be equality operator or comparison operator such as =, >, =, <= and Like operator. • A subquery is a query within another query. The outer query is called as main query and inner query is called as subquery. • The subquery generally executes first, and its output is used to complete the query condition for the main or outer query. • Subquery must be enclosed in parentheses. • Subqueries are on the right side of the comparison operator. • ORDER BY command cannot be used in a Subquery. GROUPBY command can be used to perform same function as ORDER BY command. • Use single-row operators with singlerow Subqueries. Use multiple- row operators with multiple-row Subqueries.
  • 32. Mrs. Ujjwala S Patil (SITCOE Yadrav) 32 Example: Database : Student : NAME ROLL_NO LOCATION PHONE_NUMBER Ram 101 Chennai 9988775566 Raj 102 Coimbatore 8877665544 Sasi 103 Madurai 7766553344 Ravi 104 Salem 8989898989 Sumathi 105 Kanchipuram 8989856868 NAME ROLL_NO SECTION Ravi 104 A Sumathi 105 B Raj 102 A
  • 33. Mrs. Ujjwala S Patil (SITCOE Yadrav) 33 To display NAME, LOCATION, PHONE_NUMBER of the students from DATABASE table whose section is A Select NAME, LOCATION, PHONE_NUMBER from DATABASE WHERE ROLL_NO IN (SELECT ROLL_NO from STUDENT where SECTION=’A’); Output: NAME ROLL_NO LOCATION PHONE_NUMBER Ravi 104 Salem 8989898989 Raj 102 Coimbatore 8877665544
  • 34. Mrs. Ujjwala S Patil (SITCOE Yadrav) 34 3.4 Views: Concept of View, The create view Command, Updating views, Dropping views. View can be defined as a logical part (marked for some special purpose) of the total data present in the table. View can be created with the help of Select statement. View can be read only or they could be used to insert, update, and delete the table data and view data. Views are used for the purpose of data security and minimizing data redundancy.
  • 35. Mrs. Ujjwala S Patil (SITCOE Yadrav) 35 Create View: Database views are created using the CREATE VIEW statement. Views can be created from a single table, multiple tables or another view. Syntax: CREATE VIEW view_name AS SELECT column1, column2..... FROM table_name WHERE [condition];
  • 36. Mrs. Ujjwala S Patil (SITCOE Yadrav) 36 Example: CUSTOMERS Table:
  • 37. Mrs. Ujjwala S Patil (SITCOE Yadrav) 37 CREATE VIEW CUSTOMERS_VIEW AS SELECT name, age FROM CUSTOMERS; Output: View Created 0.06 seconds
  • 38. Mrs. Ujjwala S Patil (SITCOE Yadrav) 38 How to Display the View: SELECT * FROM CUSTOMERS_VIEW; Output:
  • 39. Mrs. Ujjwala S Patil (SITCOE Yadrav) 39 Update View: View can be used to update the data from the table as well as view. Example: UPDATE CUSTOMERS_VIEW SET AGE = 35 WHERE name = 'Ramesh'; Output: One row(s) updated 0.03 seconds
  • 40. Mrs. Ujjwala S Patil (SITCOE Yadrav) 40 Now we can check the updated data by using Select statement: SELECT * FROM CUSTOMERS; Output:
  • 41. Mrs. Ujjwala S Patil (SITCOE Yadrav) 41 Dropping View: Drop view command is used for dropping the view. Syntax: Drop vew <View nmae>; Example: DROP VIEW CUSTOMERS_VIEW; Output: View dropped 0.12 seconds.
  • 42. Mrs. Ujjwala S Patil (SITCOE Yadrav) 42 3.5 Sequences: Creating Sequence, Altering Sequence, Dropping Sequence: "A Sequence is simply an automatic counter, which generates sequential numbers whenever required.“ A sequence can be defined to: •Generate numbers in ascending or descending order. •Provide intervals between numbers. •Caching of sequence numbers in memory to speed up their availability . A sequence is an independent object and can be used with any table that requires its output.
  • 43. Mrs. Ujjwala S Patil (SITCOE Yadrav) 43 Creating a Sequences : The minimum information required for generating numbers using a sequence is: • The starting number. • The maximum number that can be generated by a sequence. • The increment value for generating the next number. Syntax: CREATE SEQUENCE <Sequence Name> [INCREMENT BY <IntegerValue> START WITH <Integer Value> MAXVALUE <Integer Value> MINVALUE <Integer Value> [CYCLE/NOCYCLE] [ CACHE/NOCACHE] ;
  • 44. Mrs. Ujjwala S Patil (SITCOE Yadrav) 44 INCREMENT BY: Specifies the interval between sequence numbers. It can be any positive or negative value but not zero. If this clause is omitted, the default value is 1. MAXVALUE And MINVALUE : Specifies the maximum or minmum value that a sequence can generate. START WITH: Specifies the first sequence number to be generated. CYCLE: Specifies that the sequence continues to generate repeat values after reaching either its maximum value. CACHE: Specifies how many values to generate in advance and to keep in memory for faster access.Minimum value is two for this option.
  • 45. Mrs. Ujjwala S Patil (SITCOE Yadrav) 45 Example for Creating Sequence: Create sequence stud_seq incremented by 1 Start with 100 Maxvalue 10000 Minvalue 100 Cycle cache 7; Output: Sequence created 0.04 seconds.
  • 46. Mrs. Ujjwala S Patil (SITCOE Yadrav) 46 After creating a sequence we can access its value with the help of nextval and currval Nextval: This returns original value of the sequence. Example: Select stud_seq.nextval from dual; Output: NEXTVAL 100 1 row(s) returned in 0.02 seconds.
  • 47. Mrs. Ujjwala S Patil (SITCOE Yadrav) 47 Currval: This returns the current value of the sequence which is next value to the next value. Example: Select stud_seq.currval from dual; Output: CURRVAL 101 1 row(s) returned in 0.02 seconds.
  • 48. Mrs. Ujjwala S Patil (SITCOE Yadrav) 48 Altering a Sequence : A sequence once created can be altered. This is achieved by using the ALTER SEQUENCE statement. The START value of the sequence cannot be altered . Syntax: ALTER SEQUENCE <SequenceName> [INCREMENT BY <IntegerValue> MINVALUE <IntegerValue> ]; Example: Alter sequence stud_seq maxval 2000; Output: Sequence altered 0.02 seconds
  • 49. Mrs. Ujjwala S Patil (SITCOE Yadrav) 49 Dropping Sequence: Sequence can be delete or dropped using drop sequence command. Syntax: Drop sequence <Sequence_Name>; Example: Drop sequence stud_seq; Output: Sequence dropped 0.02 seconds
  • 50. Mrs. Ujjwala S Patil (SITCOE Yadrav) 50 3.6- Indexes- Index Types, creating of an Index, Simple, Unique and Composite Index, Dropping Indexes. An index helps to speed up SELECT queries and WHERE clauses, but it slows down data input, with the UPDATE and the INSERT statements. Indexes can be created or dropped with no effect on the data. Types of Index: 1. Simple Index 2. Composite Index 3. Unique index.
  • 51. Mrs. Ujjwala S Patil (SITCOE Yadrav) 51 Create Simple Index: When we create index on single column of the table, it is called as simple index. Syntax: CREATE INDEX index_name ON table_name (column1); Example: CREATE INDEX stud_Indx ON Student (Roll_No); Output: Index created 0.12 seconds
  • 52. Mrs. Ujjwala S Patil (SITCOE Yadrav) 52 Create Composite Index: When we create index on multiple columns of the table, it is called as Composite index. Syntax: CREATE INDEX index_name ON table_name (column1, Column2,…); Example: CREATE INDEX stud_Indx ON Student (Roll_No, Name); Output: Index created 0.12 seconds
  • 53. Mrs. Ujjwala S Patil (SITCOE Yadrav) 53 Create Unique Index: When we create index on single column or multiple columns by using “Unique” Keywordof the table, it is called Unique index. Syntax: CREATE UNIQUE INDEX index_name ON table_name (column1); Example: CREATE UNIQUE INDEX stud_Indx ON Student (Roll_No); Output: Index created 0.12 seconds
  • 54. Mrs. Ujjwala S Patil (SITCOE Yadrav) 54 Dropping Index: Index can be dropped using drop index command. Syntax: Drop Index Index_name; Example: Drop index stud_indx; Output: Index dropped 0.12 seconds.
  • 55. Mrs. Ujjwala S Patil (SITCOE Yadrav) 55 3.7 Synonyms: Creating Synonyms, Dropping Synonyms: A synonym is an alternative name for objects such as tables, views, sequences, stored procedures, and other database objects. You generally use synonyms when you are granting access to an object from another schema and you don't want the users to have to worry about knowing which schema owns the object.
  • 56. Mrs. Ujjwala S Patil (SITCOE Yadrav) 56 Creating Synonyms: To create the synonyms create synonym command is used. Syntax: Create Synonym synonym_Name For Object_Name; Example: Create synonym sit_stud for student; Output: Synonym created 1.02 seconds
  • 57. Mrs. Ujjwala S Patil (SITCOE Yadrav) 57 Dropping Synonyms: A synonym can be deleted or dropped by using drop synonym command Syntax: Drop synonym synonym_Name; Example: Drop synonym sit_stud; Output: Synonym dropped 1.02 seconds
  • 58. Mrs. Ujjwala S Patil (SITCOE Yadrav) 58 Thank You