SlideShare ist ein Scribd-Unternehmen logo
1 von 34
SQL
Agenda
 INSERT INTO SELECT Statement
 Function in SQL
 Triggers in SQL
INSERT INTO SELECT
INSERT INTO SELECT Statement
 The INSERT INTO SELECT statement copies data from
one table and inserts it into an existing table.
 The INSERT INTO SELECT statement selects data
from one table and inserts it into an existing table. Any
existing rows in the target table are unaffected.
 SQL INSERT INTO SELECT Syntax
 We can copy all columns from one table to another,
existing table:
 INSERT INTO table2 SELECT * FROM table1;
 Or we can copy only the columns we want to into
another, existing table:
 INSERT INTO table2 (column_name(s))
SELECT column_name(s)
Function in SQL
SQL Server Functions
 function is a database object in Sql Server.
 Basically it is a set of sql statements that accepts only
input parameters, perform actions and return the result.
 Function can return only single value or a table.
 We can’t use function to Insert, Update, Delete records
in the database table(s). F
 Types of Function
 System Defined Function
 Scalar Function
 Aggregate Function
 User Defined Function
 Scalar Function
 Inline Table-Valued Function
 Multi-Statement Table-Valued Function
System Defined Function
 These functions are defined by Sql Server for different
purpose. We have two types of system defined function
in Sql Server
 Scalar Function
 Scalar functions operates on a single value and returns a
single value. Below is the list of some useful Sql Server
Scalar functions.
Aggregate Function
 Aggregate functions operates on a collection of values and
returns a single value. Below is the list of some useful Sql
Server Aggregate functions.
User Defined Function
 These functions are created by user in system database
or in user defined database. We three types of user
defined functions.
 Scalar Function
 User defined scalar function also returns single value as a result of
actions perform by function. We return any datatype value from
function.
Inline Table-Valued Function
 User defined inline table-valued function returns a table
variable as a result of actions perform by function.
 The value of table variable should be derived from a
single SELECT statement.
Multi-Statement Table-Valued
Function
 User defined multi-statement table-valued function
returns a table variable as a result of actions perform
by function.
 In this a table variable must be explicitly declared and
defined whose value can be derived from a multiple
sql statements.
Procedure and Function in SQL
Server
 Stored Procedures are pre-compile objects which are
compiled for first time and its compiled format is
saved which executes (compiled code) whenever it is
called.
 But Function is compiled and executed every time
when it is called.
 Basic Difference
 unction must return a value but in Stored Procedure it
is optional( Procedure can return zero or n values).
 Functions can have only input parameters for it
whereas Procedures can have input/output
parameters .
 Functions can be called from Procedure whereas
 Advance Difference
 Procedure allows SELECT as well as
DML(INSERT/UPDATE/DELETE) statement in it
whereas Function allows only SELECT statement in it.
 Procedures can not be utilized in a SELECT statement
whereas Function can be embedded in a SELECT
statement.
 Stored Procedures cannot be used in the SQL
statements anywhere in the WHERE/HAVING/SELECT
section whereas Function can be.
 Functions that return tables can be treated as another
rowset. This can be used in JOINs with other tables.
 Exception can be handled by try-catch block in a
Procedure whereas try-catch block cannot be used in a
Function.
Trigger in SQL
Trigger in SQL
Syntax for Trigger
 Syntax of Trigger
 CREATE TRIGGER trigger_name ON {table|view}
 [WITH ENCRYPTION|EXECUTE AS]
 {FOR|AFTER|INSTEAD OF} {[CREATE|ALTER|DRO
P|INSERT|UPDATE|DELETE ]}
 [NOT FOR REPLICATION]
 AS
 sql_statement [1...n ]
Type of Trigger
 After Trigger: These kinds of triggers fire after the
execution of an action query that can be either DDL
statements like Create, Alter and Drop or DML
statements like Insert, Update and Delete.
 Instead of Trigger: These kinds of triggers fire before
the execution of an action query that can only be DML
statements like Insert, Update and Delete but after the
execution of that query. The table data will not be
affected, in other words if you want to insert or update
the data of the table then you need to write it in the
trigger using "inserted" or "deleted" virtual tables.
Group by and Having Clause
SQL HAVING Clause
 The HAVING clause was added to SQL because the
WHERE keyword could not be used with aggregate
functions.
 SQL HAVING Syntax
 SELECT column_name,
aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
HAVING aggregate_function(column_name) operator
value;
GROUP BY Statement
 Aggregate functions often need an added
GROUP BY statement.
 The GROUP BY statement is used in conjunction
with the aggregate functions to group the result-
set by one or more columns.
 SQL GROUP BY Syntax
 SELECT column_name,
aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name;
Variable in SQL
Declare Variables
 In SQL Server a variable allows a programmer to
store data temporarily during the execution of
code.
 Syntax
 DECLARE @variable_name datatype [ =
initial_value ], @variable_name datatype [ =
initial_value ],
...;
Loops and Conditional Statement
BREAK statement
 In SQL Server, the BREAK statement is used when
you want to exit from a WHILE LOOP and execute the
next statements after the loop's END statement.
 Syntax
 BREAK;
CONTINUE Statement
 In SQL Server, the CONTINUE statement is used
when you are want a WHILE LOOP to execute again.
It will ignore any statements after the CONTINUE
statement
 Syntax
 CONTINUE;
GOTO Statement
 The GOTO statement causes the code to branch to
the label after the GOTO statement.
 GOTO statement
 The GOTO statement consists of the GOTO
keyword, followed by a label_name.
 GOTO label_name;Label Declaration
 The Label Declaration consists of
the label_name, followed by at least one statement to
execute.
 label_name: {...statements...}
IF...ELSE statement
 In SQL Server, the IF...ELSE statement is used to
execute code when a condition is TRUE, or execute
different code if the condition evaluates to FALSE.
 Syntax
 IF condition
{...statements to execute when condition is TRUE...}
[ ELSE {...statements to execute when condition is
FALSE...} ]
WHILE LOOP in SQL Server
 In SQL Server, you use a WHILE LOOP when you are
not sure how many times you will execute the loop
body and the loop body may not execute even once.
 Syntax
 WHILE condition BEGIN {...statements...} END;
FOR LOOP
 In SQL Server, there is no FOR LOOP. However, you
simulate the FOR LOOP using the WHILE LOOP.
 Syntax
 DECLARE @cnt INT = 0;
 WHILE @cnt < cnt_total
 BEGIN {...statements...}
 SET @cnt = @cnt + 1;
 END;
Union and Union All
Difference Between Union and Union
All in SQL Server
UNION UNION ALL
UNION removes duplicate rows.
“UNION ALL” does not remove the
duplicate row. It returns all from all
queries.
UNION uses a distinct sort
“UNION ALL” does not use a distinct
sort, so the performance of “UNION
ALL” is slightly higher than “UNION”.
UNION cannot work with a column that
has a TEXT data type.
UNION ALL can work with all data type
columns.

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Basic cursors in oracle
Basic cursors in oracleBasic cursors in oracle
Basic cursors in oracle
 
Trigger
TriggerTrigger
Trigger
 
Stored procedure
Stored procedureStored procedure
Stored procedure
 
Sql server ___________session_19(triggers)
Sql server  ___________session_19(triggers)Sql server  ___________session_19(triggers)
Sql server ___________session_19(triggers)
 
PL/SQL TRIGGERS
PL/SQL TRIGGERSPL/SQL TRIGGERS
PL/SQL TRIGGERS
 
Review of SQL
Review of SQLReview of SQL
Review of SQL
 
Oracle: Functions
Oracle: FunctionsOracle: Functions
Oracle: Functions
 
Using triggers in my sql database
Using triggers in my sql databaseUsing triggers in my sql database
Using triggers in my sql database
 
Function & procedure
Function & procedureFunction & procedure
Function & procedure
 
PLSQL Cursors
PLSQL CursorsPLSQL Cursors
PLSQL Cursors
 
[Www.pkbulk.blogspot.com]dbms07
[Www.pkbulk.blogspot.com]dbms07[Www.pkbulk.blogspot.com]dbms07
[Www.pkbulk.blogspot.com]dbms07
 
Oracle: PLSQL Introduction
Oracle: PLSQL IntroductionOracle: PLSQL Introduction
Oracle: PLSQL Introduction
 
Procedure and Functions in pl/sql
Procedure and Functions in pl/sqlProcedure and Functions in pl/sql
Procedure and Functions in pl/sql
 
Triggers
TriggersTriggers
Triggers
 
Lab1 select statement
Lab1 select statementLab1 select statement
Lab1 select statement
 
PLSQL Tutorial
PLSQL TutorialPLSQL Tutorial
PLSQL Tutorial
 
Introduction to oracle functions
Introduction to oracle functionsIntroduction to oracle functions
Introduction to oracle functions
 
5. Group Functions
5. Group Functions5. Group Functions
5. Group Functions
 
PLSQL Note
PLSQL NotePLSQL Note
PLSQL Note
 
Sql dml & tcl 2
Sql   dml & tcl 2Sql   dml & tcl 2
Sql dml & tcl 2
 

Andere mochten auch

Triggers
TriggersTriggers
Triggerswork
 
SQL – A Tutorial I
SQL – A Tutorial  ISQL – A Tutorial  I
SQL – A Tutorial IGagan Deep
 
Python于Web 2.0网站的应用 - QCon Beijing 2010
Python于Web 2.0网站的应用 - QCon Beijing 2010Python于Web 2.0网站的应用 - QCon Beijing 2010
Python于Web 2.0网站的应用 - QCon Beijing 2010Qiangning Hong
 
PHP mysql Aggregate functions
PHP mysql Aggregate functionsPHP mysql Aggregate functions
PHP mysql Aggregate functionsMudasir Syed
 
Procedures and triggers in SQL
Procedures and triggers in SQLProcedures and triggers in SQL
Procedures and triggers in SQLVikash Sharma
 
Liferay Portal Introduction
Liferay Portal IntroductionLiferay Portal Introduction
Liferay Portal IntroductionNguyen Tung
 
Bài 1: Làm quen với SQL Server 2008 - Giáo trình FPT
Bài 1: Làm quen với SQL Server 2008 - Giáo trình FPTBài 1: Làm quen với SQL Server 2008 - Giáo trình FPT
Bài 1: Làm quen với SQL Server 2008 - Giáo trình FPTMasterCode.vn
 
Bài 6: Thiết kế cơ sở dữ liệu - Giáo trình FPT
Bài 6: Thiết kế cơ sở dữ liệu - Giáo trình FPTBài 6: Thiết kế cơ sở dữ liệu - Giáo trình FPT
Bài 6: Thiết kế cơ sở dữ liệu - Giáo trình FPTMasterCode.vn
 
Bài 2: Các khái niệm trong CSDL quan hệ - Giáo trình FPT
Bài 2: Các khái niệm trong CSDL quan hệ - Giáo trình FPTBài 2: Các khái niệm trong CSDL quan hệ - Giáo trình FPT
Bài 2: Các khái niệm trong CSDL quan hệ - Giáo trình FPTMasterCode.vn
 
Lập trình sáng tạo creative computing textbook mastercode.vn
Lập trình sáng tạo creative computing textbook mastercode.vnLập trình sáng tạo creative computing textbook mastercode.vn
Lập trình sáng tạo creative computing textbook mastercode.vnMasterCode.vn
 
SQL Tutorial - Basic Commands
SQL Tutorial - Basic CommandsSQL Tutorial - Basic Commands
SQL Tutorial - Basic Commands1keydata
 

Andere mochten auch (15)

Bai giang hqtcsdl
Bai giang hqtcsdlBai giang hqtcsdl
Bai giang hqtcsdl
 
Triggers
TriggersTriggers
Triggers
 
SQL – A Tutorial I
SQL – A Tutorial  ISQL – A Tutorial  I
SQL – A Tutorial I
 
Python于Web 2.0网站的应用 - QCon Beijing 2010
Python于Web 2.0网站的应用 - QCon Beijing 2010Python于Web 2.0网站的应用 - QCon Beijing 2010
Python于Web 2.0网站的应用 - QCon Beijing 2010
 
Trigger in SQL
Trigger in SQLTrigger in SQL
Trigger in SQL
 
PHP mysql Aggregate functions
PHP mysql Aggregate functionsPHP mysql Aggregate functions
PHP mysql Aggregate functions
 
Procedures and triggers in SQL
Procedures and triggers in SQLProcedures and triggers in SQL
Procedures and triggers in SQL
 
Liferay Portal Introduction
Liferay Portal IntroductionLiferay Portal Introduction
Liferay Portal Introduction
 
Bài 1: Làm quen với SQL Server 2008 - Giáo trình FPT
Bài 1: Làm quen với SQL Server 2008 - Giáo trình FPTBài 1: Làm quen với SQL Server 2008 - Giáo trình FPT
Bài 1: Làm quen với SQL Server 2008 - Giáo trình FPT
 
Bài 6: Thiết kế cơ sở dữ liệu - Giáo trình FPT
Bài 6: Thiết kế cơ sở dữ liệu - Giáo trình FPTBài 6: Thiết kế cơ sở dữ liệu - Giáo trình FPT
Bài 6: Thiết kế cơ sở dữ liệu - Giáo trình FPT
 
Bài 2: Các khái niệm trong CSDL quan hệ - Giáo trình FPT
Bài 2: Các khái niệm trong CSDL quan hệ - Giáo trình FPTBài 2: Các khái niệm trong CSDL quan hệ - Giáo trình FPT
Bài 2: Các khái niệm trong CSDL quan hệ - Giáo trình FPT
 
Lập trình sáng tạo creative computing textbook mastercode.vn
Lập trình sáng tạo creative computing textbook mastercode.vnLập trình sáng tạo creative computing textbook mastercode.vn
Lập trình sáng tạo creative computing textbook mastercode.vn
 
SQL : introduction
SQL : introductionSQL : introduction
SQL : introduction
 
SQL Tutorial - Basic Commands
SQL Tutorial - Basic CommandsSQL Tutorial - Basic Commands
SQL Tutorial - Basic Commands
 
Sql ppt
Sql pptSql ppt
Sql ppt
 

Ähnlich wie SQL Functions, Triggers and Joins

Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Trainingbixxman
 
Les08 (manipulating data)
Les08 (manipulating data)Les08 (manipulating data)
Les08 (manipulating data)Achmad Solichin
 
What is the purpose of a PL-SQL cursor- Give an example-SolutionAns- 1.docx
What is the purpose of a PL-SQL cursor- Give an example-SolutionAns- 1.docxWhat is the purpose of a PL-SQL cursor- Give an example-SolutionAns- 1.docx
What is the purpose of a PL-SQL cursor- Give an example-SolutionAns- 1.docxearleanp
 
React table tutorial use filter (part 2)
React table tutorial use filter (part 2)React table tutorial use filter (part 2)
React table tutorial use filter (part 2)Katy Slemon
 
Oracle PL/SQL online training | PL/SQL online Training
Oracle PL/SQL online training | PL/SQL online TrainingOracle PL/SQL online training | PL/SQL online Training
Oracle PL/SQL online training | PL/SQL online Trainingsuresh
 
3963066 pl-sql-notes-only
3963066 pl-sql-notes-only3963066 pl-sql-notes-only
3963066 pl-sql-notes-onlyAshwin Kumar
 
DAC training-batch -2020(PLSQL)
DAC training-batch -2020(PLSQL)DAC training-batch -2020(PLSQL)
DAC training-batch -2020(PLSQL)RajKumarSingh213
 
Mysql creating stored function
Mysql  creating stored function Mysql  creating stored function
Mysql creating stored function Prof.Nilesh Magar
 
Dynamic websites lec3
Dynamic websites lec3Dynamic websites lec3
Dynamic websites lec3Belal Arfa
 

Ähnlich wie SQL Functions, Triggers and Joins (20)

ADVANCED MODELLING.pptx
ADVANCED MODELLING.pptxADVANCED MODELLING.pptx
ADVANCED MODELLING.pptx
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Training
 
Les08 (manipulating data)
Les08 (manipulating data)Les08 (manipulating data)
Les08 (manipulating data)
 
What is the purpose of a PL-SQL cursor- Give an example-SolutionAns- 1.docx
What is the purpose of a PL-SQL cursor- Give an example-SolutionAns- 1.docxWhat is the purpose of a PL-SQL cursor- Give an example-SolutionAns- 1.docx
What is the purpose of a PL-SQL cursor- Give an example-SolutionAns- 1.docx
 
Les08
Les08Les08
Les08
 
MySQL Pro
MySQL ProMySQL Pro
MySQL Pro
 
Les09
Les09Les09
Les09
 
SQL
SQLSQL
SQL
 
React table tutorial use filter (part 2)
React table tutorial use filter (part 2)React table tutorial use filter (part 2)
React table tutorial use filter (part 2)
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
 
Oracle PL/SQL online training | PL/SQL online Training
Oracle PL/SQL online training | PL/SQL online TrainingOracle PL/SQL online training | PL/SQL online Training
Oracle PL/SQL online training | PL/SQL online Training
 
3963066 pl-sql-notes-only
3963066 pl-sql-notes-only3963066 pl-sql-notes-only
3963066 pl-sql-notes-only
 
DAC training-batch -2020(PLSQL)
DAC training-batch -2020(PLSQL)DAC training-batch -2020(PLSQL)
DAC training-batch -2020(PLSQL)
 
Mysql creating stored function
Mysql  creating stored function Mysql  creating stored function
Mysql creating stored function
 
Dynamic websites lec3
Dynamic websites lec3Dynamic websites lec3
Dynamic websites lec3
 
SQL Procedures & Functions
SQL Procedures & FunctionsSQL Procedures & Functions
SQL Procedures & Functions
 
Module07
Module07Module07
Module07
 
Module06
Module06Module06
Module06
 
Function
FunctionFunction
Function
 

Kürzlich hochgeladen

How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 

Kürzlich hochgeladen (20)

How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 

SQL Functions, Triggers and Joins

  • 1. SQL
  • 2. Agenda  INSERT INTO SELECT Statement  Function in SQL  Triggers in SQL
  • 4. INSERT INTO SELECT Statement  The INSERT INTO SELECT statement copies data from one table and inserts it into an existing table.  The INSERT INTO SELECT statement selects data from one table and inserts it into an existing table. Any existing rows in the target table are unaffected.  SQL INSERT INTO SELECT Syntax  We can copy all columns from one table to another, existing table:  INSERT INTO table2 SELECT * FROM table1;  Or we can copy only the columns we want to into another, existing table:  INSERT INTO table2 (column_name(s)) SELECT column_name(s)
  • 6. SQL Server Functions  function is a database object in Sql Server.  Basically it is a set of sql statements that accepts only input parameters, perform actions and return the result.  Function can return only single value or a table.  We can’t use function to Insert, Update, Delete records in the database table(s). F  Types of Function  System Defined Function  Scalar Function  Aggregate Function  User Defined Function  Scalar Function  Inline Table-Valued Function  Multi-Statement Table-Valued Function
  • 7. System Defined Function  These functions are defined by Sql Server for different purpose. We have two types of system defined function in Sql Server  Scalar Function  Scalar functions operates on a single value and returns a single value. Below is the list of some useful Sql Server Scalar functions.
  • 8. Aggregate Function  Aggregate functions operates on a collection of values and returns a single value. Below is the list of some useful Sql Server Aggregate functions.
  • 9.
  • 10. User Defined Function  These functions are created by user in system database or in user defined database. We three types of user defined functions.  Scalar Function  User defined scalar function also returns single value as a result of actions perform by function. We return any datatype value from function.
  • 11. Inline Table-Valued Function  User defined inline table-valued function returns a table variable as a result of actions perform by function.  The value of table variable should be derived from a single SELECT statement.
  • 12. Multi-Statement Table-Valued Function  User defined multi-statement table-valued function returns a table variable as a result of actions perform by function.  In this a table variable must be explicitly declared and defined whose value can be derived from a multiple sql statements.
  • 13.
  • 14. Procedure and Function in SQL Server  Stored Procedures are pre-compile objects which are compiled for first time and its compiled format is saved which executes (compiled code) whenever it is called.  But Function is compiled and executed every time when it is called.  Basic Difference  unction must return a value but in Stored Procedure it is optional( Procedure can return zero or n values).  Functions can have only input parameters for it whereas Procedures can have input/output parameters .  Functions can be called from Procedure whereas
  • 15.  Advance Difference  Procedure allows SELECT as well as DML(INSERT/UPDATE/DELETE) statement in it whereas Function allows only SELECT statement in it.  Procedures can not be utilized in a SELECT statement whereas Function can be embedded in a SELECT statement.  Stored Procedures cannot be used in the SQL statements anywhere in the WHERE/HAVING/SELECT section whereas Function can be.  Functions that return tables can be treated as another rowset. This can be used in JOINs with other tables.  Exception can be handled by try-catch block in a Procedure whereas try-catch block cannot be used in a Function.
  • 18. Syntax for Trigger  Syntax of Trigger  CREATE TRIGGER trigger_name ON {table|view}  [WITH ENCRYPTION|EXECUTE AS]  {FOR|AFTER|INSTEAD OF} {[CREATE|ALTER|DRO P|INSERT|UPDATE|DELETE ]}  [NOT FOR REPLICATION]  AS  sql_statement [1...n ]
  • 19. Type of Trigger  After Trigger: These kinds of triggers fire after the execution of an action query that can be either DDL statements like Create, Alter and Drop or DML statements like Insert, Update and Delete.  Instead of Trigger: These kinds of triggers fire before the execution of an action query that can only be DML statements like Insert, Update and Delete but after the execution of that query. The table data will not be affected, in other words if you want to insert or update the data of the table then you need to write it in the trigger using "inserted" or "deleted" virtual tables.
  • 20. Group by and Having Clause
  • 21. SQL HAVING Clause  The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions.  SQL HAVING Syntax  SELECT column_name, aggregate_function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name HAVING aggregate_function(column_name) operator value;
  • 22. GROUP BY Statement  Aggregate functions often need an added GROUP BY statement.  The GROUP BY statement is used in conjunction with the aggregate functions to group the result- set by one or more columns.  SQL GROUP BY Syntax  SELECT column_name, aggregate_function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name;
  • 24. Declare Variables  In SQL Server a variable allows a programmer to store data temporarily during the execution of code.  Syntax  DECLARE @variable_name datatype [ = initial_value ], @variable_name datatype [ = initial_value ], ...;
  • 26. BREAK statement  In SQL Server, the BREAK statement is used when you want to exit from a WHILE LOOP and execute the next statements after the loop's END statement.  Syntax  BREAK;
  • 27. CONTINUE Statement  In SQL Server, the CONTINUE statement is used when you are want a WHILE LOOP to execute again. It will ignore any statements after the CONTINUE statement  Syntax  CONTINUE;
  • 28. GOTO Statement  The GOTO statement causes the code to branch to the label after the GOTO statement.  GOTO statement  The GOTO statement consists of the GOTO keyword, followed by a label_name.  GOTO label_name;Label Declaration  The Label Declaration consists of the label_name, followed by at least one statement to execute.  label_name: {...statements...}
  • 29.
  • 30. IF...ELSE statement  In SQL Server, the IF...ELSE statement is used to execute code when a condition is TRUE, or execute different code if the condition evaluates to FALSE.  Syntax  IF condition {...statements to execute when condition is TRUE...} [ ELSE {...statements to execute when condition is FALSE...} ]
  • 31. WHILE LOOP in SQL Server  In SQL Server, you use a WHILE LOOP when you are not sure how many times you will execute the loop body and the loop body may not execute even once.  Syntax  WHILE condition BEGIN {...statements...} END;
  • 32. FOR LOOP  In SQL Server, there is no FOR LOOP. However, you simulate the FOR LOOP using the WHILE LOOP.  Syntax  DECLARE @cnt INT = 0;  WHILE @cnt < cnt_total  BEGIN {...statements...}  SET @cnt = @cnt + 1;  END;
  • 34. Difference Between Union and Union All in SQL Server UNION UNION ALL UNION removes duplicate rows. “UNION ALL” does not remove the duplicate row. It returns all from all queries. UNION uses a distinct sort “UNION ALL” does not use a distinct sort, so the performance of “UNION ALL” is slightly higher than “UNION”. UNION cannot work with a column that has a TEXT data type. UNION ALL can work with all data type columns.