SlideShare a Scribd company logo
1 of 14
Lab Session 1
Learning Objective:
At the end of this lesson, students will be able to:
• Declare variables
• Understand the usage of If...Else statements
• Understand the usage of WHILE Loop With CONTINUE and BREAK Keywords
• Understand the usage of CASE...WHEN expressions
• Use control structures to solve problems
Lab Session 1
Learning Objective:
At the end of this lesson, students will be able to:
• Declare variables
• Understand the usage of If...Else statements
• Understand the usage of WHILE Loop With CONTINUE and BREAK Keywords
• Understand the usage of CASE...WHEN expressions
• Use control structures to solve problems
Lab Session 1
Functions in SQL Server /Oracle
There are two categories of functions
1. Built in functions
a. SQL Server: ABS,SQRT, GETDATE(),DATEPART, CONVERT,DATEDIFF, etc....
b. Oracle: ABS, SQRT ,SYSDATE() ,COS,
2. User defined functions
a. Scalar user defined functions
b. Inline table valued functions
c. Multi statements table valued functions
Lab Session 1
2.a Creating scalar user defined functions using sql server:
Syntax:
CREATE FUNCTION [schema_name.]function_name (@parameter_name parameter_data_type)
RETURNS
return_data_type
[ WITH <function_option>
AS
BEGIN
function_body
RETURN
scalar_expression
END
Lab Session 1
Example:
create a function that calculates the net pay of employees
create database EMP
use EMP
create table employee
(
id int primary key,
fname varchar(20),
sex char,
salary int
)
insert into employee values(1,’Abel’,'M’,10000)
insert into employee values(2,’mariyan','F’,11000)
Lab Session 1
go
create function netPay(@sal int)
returns float
as
begin
declare @np float
if @sal<150
set @np=@sal
else
set @np=@sal-0.10*@sal
return @np
end
go
Lab Session 1
To call the fuction, we have to prefix the funcation_name with the default schema dbo.
Example of calling the function:
select dbo.netPay(employee.salary) from employee
Lab Session 1
2.b. Inline table table valued functions
Syntax:
CREATE FUNCTION [schema_name.]function_name ([ { @parameter_name] parameter_data_type[ ,...n ] ]
)
RETURNS TABLE
[ WITH <function_option> [ ,...n] ]
[ AS ]
RETURN [ ( ]
select_stmt [ )]
[ ; ]
Lab Session 1
Example
Create function emp (@id int)
returns table
as
return select * from employee
Where id =@id
To call the function, run the following query:
Select * from dbo.emp(1)
Lab Session 1
3.Multi statements table valued functions
CREATE FUNCTION [ schema_name.] function_name ([ { @parameter_name
[ AS ] [
type_schema_name.]parameter_data_type[ = default ] [READONLY] }
[ ,...n]
]
)
RETURNS @return_variable
TABLE <table_type_definition>
[ WITH <function_option> [ , ...n ] ]
[ AS ]
BEGIN
function_body
RETURN
END
[ ; ]
Lab Session 1
Example
go
create function empnames(@id int)
returns @report table(fname varchar(50))
as
begin
insert@report select fname from employee where id =@id
return
end
To call the funciton, run the following query
Select * from dbo.empnames(1)
Lab Session 1
Exercise
1. Create a function which gives you the maximum of two integers.
2. Suppose that we have two tables named as:
a. Student(fname, studID, sex)
b. Stud_Grade (studID, courseNo,CourseTitle, CrHr, Grade)
Based on the values that could be populated to these tables, create a function that returns
the GPA of a student when you pass studID as an argument to the function
Use the following table structures taken from the Library management of Raya University. Refer
the table structures shown below and write T-SQL statements to answer questions that follow.
Take the following assumptions:
The attribute “BookTitle ” in the book table can have values like C++, Database, Java, etc...
The attribute “City”in the publisher table can have values like Mekelle,Addis Abeba,Maichew, Adigrat, etc...
Lab Session 1
Lab Session 1
Exercise
1) Write a scalar function that returns the average price of Books published by male authors.
2) Write a scalar function that returns the total number of female Authors who published database books
by Mekelle publishers.
3) Write a scalar function that takes ‘Publisher_Name’ as an argument and return total number of authors
who published books in the past ten years.
4) Create an inline table valued function that generate the list of all of who published java books
5) Create a multi statement table valued function to find ID, name and sex of all authors who already
published books. Call this function under a new scalar function that returns the total number of books
published by female authors.

More Related Content

Similar to Lab Session for sql programming language 1.pptx

Comp 220 ilab 7 of 7
Comp 220 ilab 7 of 7Comp 220 ilab 7 of 7
Comp 220 ilab 7 of 7
ashhadiqbal
 
Comp 220 ilab 6 of 7
Comp 220 ilab 6 of 7Comp 220 ilab 6 of 7
Comp 220 ilab 6 of 7
ashhadiqbal
 
PofEAA and SQLAlchemy
PofEAA and SQLAlchemyPofEAA and SQLAlchemy
PofEAA and SQLAlchemy
Inada Naoki
 
Ejb3 Struts Tutorial En
Ejb3 Struts Tutorial EnEjb3 Struts Tutorial En
Ejb3 Struts Tutorial En
Ankur Dongre
 
Ejb3 Struts Tutorial En
Ejb3 Struts Tutorial EnEjb3 Struts Tutorial En
Ejb3 Struts Tutorial En
Ankur Dongre
 

Similar to Lab Session for sql programming language 1.pptx (20)

JavaScript in 2016 (Codemotion Rome)
JavaScript in 2016 (Codemotion Rome)JavaScript in 2016 (Codemotion Rome)
JavaScript in 2016 (Codemotion Rome)
 
JavaScript in 2016
JavaScript in 2016JavaScript in 2016
JavaScript in 2016
 
Comp 220 ilab 7 of 7
Comp 220 ilab 7 of 7Comp 220 ilab 7 of 7
Comp 220 ilab 7 of 7
 
Comp 220 ilab 7 of 7
Comp 220 ilab 7 of 7Comp 220 ilab 7 of 7
Comp 220 ilab 7 of 7
 
Database structure Structures Link list and trees and Recurison complete
Database structure Structures Link list and trees and Recurison complete  Database structure Structures Link list and trees and Recurison complete
Database structure Structures Link list and trees and Recurison complete
 
Scala and Spring
Scala and SpringScala and Spring
Scala and Spring
 
Comp 220 ilab 6 of 7
Comp 220 ilab 6 of 7Comp 220 ilab 6 of 7
Comp 220 ilab 6 of 7
 
Sql Server 2014 Course Content
Sql Server 2014 Course ContentSql Server 2014 Course Content
Sql Server 2014 Course Content
 
Intro To C++ - Class 05 - Introduction To Classes, Objects, & Strings
Intro To C++ - Class 05 - Introduction To  Classes, Objects, & StringsIntro To C++ - Class 05 - Introduction To  Classes, Objects, & Strings
Intro To C++ - Class 05 - Introduction To Classes, Objects, & Strings
 
Mastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_argumentsMastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_arguments
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Rails
 
PofEAA and SQLAlchemy
PofEAA and SQLAlchemyPofEAA and SQLAlchemy
PofEAA and SQLAlchemy
 
Sql lite android
Sql lite androidSql lite android
Sql lite android
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 
Ejb3 Struts Tutorial En
Ejb3 Struts Tutorial EnEjb3 Struts Tutorial En
Ejb3 Struts Tutorial En
 
Ejb3 Struts Tutorial En
Ejb3 Struts Tutorial EnEjb3 Struts Tutorial En
Ejb3 Struts Tutorial En
 
Programming in java basics
Programming in java  basicsProgramming in java  basics
Programming in java basics
 
Chapter Functions for grade 12 computer Science
Chapter Functions for grade 12 computer ScienceChapter Functions for grade 12 computer Science
Chapter Functions for grade 12 computer Science
 
Scala active record
Scala active recordScala active record
Scala active record
 
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)
 

More from meharikiros2

More from meharikiros2 (13)

CHAPTER-7 C++ PROGRAMMING ( STRUCTURE IN C++)
CHAPTER-7 C++ PROGRAMMING ( STRUCTURE IN C++)CHAPTER-7 C++ PROGRAMMING ( STRUCTURE IN C++)
CHAPTER-7 C++ PROGRAMMING ( STRUCTURE IN C++)
 
Query optimization and processing for advanced database systems
Query optimization and processing for advanced database systemsQuery optimization and processing for advanced database systems
Query optimization and processing for advanced database systems
 
Emerging chap asasasasasawwqwqwwqwewewr4.pptx
Emerging chap asasasasasawwqwqwwqwewewr4.pptxEmerging chap asasasasasawwqwqwwqwewewr4.pptx
Emerging chap asasasasasawwqwqwwqwewewr4.pptx
 
Chapter-1-IntroDistributeddffsfdfsdf-1.pptx
Chapter-1-IntroDistributeddffsfdfsdf-1.pptxChapter-1-IntroDistributeddffsfdfsdf-1.pptx
Chapter-1-IntroDistributeddffsfdfsdf-1.pptx
 
chapter1lecturenotes sdsdasdddadad(2).ppt
chapter1lecturenotes sdsdasdddadad(2).pptchapter1lecturenotes sdsdasdddadad(2).ppt
chapter1lecturenotes sdsdasdddadad(2).ppt
 
RIFLI-Computer-Basics-Part-1-1 lecture not
RIFLI-Computer-Basics-Part-1-1  lecture notRIFLI-Computer-Basics-Part-1-1  lecture not
RIFLI-Computer-Basics-Part-1-1 lecture not
 
Lecture 01 - CS193Jxcxcxcx Summer 2003.ppt
Lecture 01 - CS193Jxcxcxcx Summer 2003.pptLecture 01 - CS193Jxcxcxcx Summer 2003.ppt
Lecture 01 - CS193Jxcxcxcx Summer 2003.ppt
 
JavaAdvanced programming for expertes dsd
JavaAdvanced programming for expertes dsdJavaAdvanced programming for expertes dsd
JavaAdvanced programming for expertes dsd
 
Computer_Programming_Fundamentals in cpp
Computer_Programming_Fundamentals in cppComputer_Programming_Fundamentals in cpp
Computer_Programming_Fundamentals in cpp
 
SystemsProgrammingCourse FSDFFSFDSDSDSFSFS
SystemsProgrammingCourse FSDFFSFDSDSDSFSFSSystemsProgrammingCourse FSDFFSFDSDSDSFSFS
SystemsProgrammingCourse FSDFFSFDSDSDSFSFS
 
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJIntroduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
 
This is introduction to distributed systems for the revised curiculum
This is introduction to distributed systems for the revised curiculumThis is introduction to distributed systems for the revised curiculum
This is introduction to distributed systems for the revised curiculum
 
ITET-4.pptx
ITET-4.pptxITET-4.pptx
ITET-4.pptx
 

Recently uploaded

Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Dr.Costas Sachpazis
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
rknatarajan
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
Tonystark477637
 

Recently uploaded (20)

Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICSUNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spain
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 

Lab Session for sql programming language 1.pptx

  • 1. Lab Session 1 Learning Objective: At the end of this lesson, students will be able to: • Declare variables • Understand the usage of If...Else statements • Understand the usage of WHILE Loop With CONTINUE and BREAK Keywords • Understand the usage of CASE...WHEN expressions • Use control structures to solve problems
  • 2. Lab Session 1 Learning Objective: At the end of this lesson, students will be able to: • Declare variables • Understand the usage of If...Else statements • Understand the usage of WHILE Loop With CONTINUE and BREAK Keywords • Understand the usage of CASE...WHEN expressions • Use control structures to solve problems
  • 3. Lab Session 1 Functions in SQL Server /Oracle There are two categories of functions 1. Built in functions a. SQL Server: ABS,SQRT, GETDATE(),DATEPART, CONVERT,DATEDIFF, etc.... b. Oracle: ABS, SQRT ,SYSDATE() ,COS, 2. User defined functions a. Scalar user defined functions b. Inline table valued functions c. Multi statements table valued functions
  • 4. Lab Session 1 2.a Creating scalar user defined functions using sql server: Syntax: CREATE FUNCTION [schema_name.]function_name (@parameter_name parameter_data_type) RETURNS return_data_type [ WITH <function_option> AS BEGIN function_body RETURN scalar_expression END
  • 5. Lab Session 1 Example: create a function that calculates the net pay of employees create database EMP use EMP create table employee ( id int primary key, fname varchar(20), sex char, salary int ) insert into employee values(1,’Abel’,'M’,10000) insert into employee values(2,’mariyan','F’,11000)
  • 6. Lab Session 1 go create function netPay(@sal int) returns float as begin declare @np float if @sal<150 set @np=@sal else set @np=@sal-0.10*@sal return @np end go
  • 7. Lab Session 1 To call the fuction, we have to prefix the funcation_name with the default schema dbo. Example of calling the function: select dbo.netPay(employee.salary) from employee
  • 8. Lab Session 1 2.b. Inline table table valued functions Syntax: CREATE FUNCTION [schema_name.]function_name ([ { @parameter_name] parameter_data_type[ ,...n ] ] ) RETURNS TABLE [ WITH <function_option> [ ,...n] ] [ AS ] RETURN [ ( ] select_stmt [ )] [ ; ]
  • 9. Lab Session 1 Example Create function emp (@id int) returns table as return select * from employee Where id =@id To call the function, run the following query: Select * from dbo.emp(1)
  • 10. Lab Session 1 3.Multi statements table valued functions CREATE FUNCTION [ schema_name.] function_name ([ { @parameter_name [ AS ] [ type_schema_name.]parameter_data_type[ = default ] [READONLY] } [ ,...n] ] ) RETURNS @return_variable TABLE <table_type_definition> [ WITH <function_option> [ , ...n ] ] [ AS ] BEGIN function_body RETURN END [ ; ]
  • 11. Lab Session 1 Example go create function empnames(@id int) returns @report table(fname varchar(50)) as begin insert@report select fname from employee where id =@id return end To call the funciton, run the following query Select * from dbo.empnames(1)
  • 12. Lab Session 1 Exercise 1. Create a function which gives you the maximum of two integers. 2. Suppose that we have two tables named as: a. Student(fname, studID, sex) b. Stud_Grade (studID, courseNo,CourseTitle, CrHr, Grade) Based on the values that could be populated to these tables, create a function that returns the GPA of a student when you pass studID as an argument to the function Use the following table structures taken from the Library management of Raya University. Refer the table structures shown below and write T-SQL statements to answer questions that follow. Take the following assumptions: The attribute “BookTitle ” in the book table can have values like C++, Database, Java, etc... The attribute “City”in the publisher table can have values like Mekelle,Addis Abeba,Maichew, Adigrat, etc...
  • 14. Lab Session 1 Exercise 1) Write a scalar function that returns the average price of Books published by male authors. 2) Write a scalar function that returns the total number of female Authors who published database books by Mekelle publishers. 3) Write a scalar function that takes ‘Publisher_Name’ as an argument and return total number of authors who published books in the past ten years. 4) Create an inline table valued function that generate the list of all of who published java books 5) Create a multi statement table valued function to find ID, name and sex of all authors who already published books. Call this function under a new scalar function that returns the total number of books published by female authors.