SlideShare a Scribd company logo
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

PDF
MA3696 Lecture 9
PDF
Spring Day | Spring and Scala | Eberhard Wolff
PPT
Spsl v unit - final
PPT
Spsl vi unit final
ODP
DOCX
PDF
Sql functions
MA3696 Lecture 9
Spring Day | Spring and Scala | Eberhard Wolff
Spsl v unit - final
Spsl vi unit final
Sql functions

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

PDF
JavaScript in 2016
PPTX
JavaScript in 2016 (Codemotion Rome)
DOCX
Comp 220 ilab 7 of 7
DOCX
Comp 220 ilab 7 of 7
PPT
Database structure Structures Link list and trees and Recurison complete
PDF
Scala and Spring
DOCX
Comp 220 ilab 6 of 7
PDF
Sql Server 2014 Course Content
PPTX
Intro To C++ - Class 05 - Introduction To Classes, Objects, & Strings
PPTX
Mastering Python lesson 4_functions_parameters_arguments
PDF
Ruby On Rails
PDF
PofEAA and SQLAlchemy
ODP
Sql lite android
PDF
Lecture 5
PDF
Ejb3 Struts Tutorial En
PDF
Ejb3 Struts Tutorial En
PPTX
Introduction to scientific computing with matlab.pptx
PPTX
Programming in java basics
PDF
Chapter Functions for grade 12 computer Science
PDF
Scala active record
JavaScript in 2016
JavaScript in 2016 (Codemotion Rome)
Comp 220 ilab 7 of 7
Comp 220 ilab 7 of 7
Database structure Structures Link list and trees and Recurison complete
Scala and Spring
Comp 220 ilab 6 of 7
Sql Server 2014 Course Content
Intro To C++ - Class 05 - Introduction To Classes, Objects, & Strings
Mastering Python lesson 4_functions_parameters_arguments
Ruby On Rails
PofEAA and SQLAlchemy
Sql lite android
Lecture 5
Ejb3 Struts Tutorial En
Ejb3 Struts Tutorial En
Introduction to scientific computing with matlab.pptx
Programming in java basics
Chapter Functions for grade 12 computer Science
Scala active record
Ad

More from meharikiros2 (17)

PPTX
CHapter four database managementm04.pptx
PPT
CHAPTERGAHAhsghdfsfdsfsdfsfdsfsdfAGSHagsh-7.ppt
PPTX
chapter three 3-part II-1lecture slide.pptx
PPTX
ExitExam Tutorial gfgfgfdfdfdfdf (1).pptx
PPT
CHAPTER-7 C++ PROGRAMMING ( STRUCTURE IN C++)
PPT
Query optimization and processing for advanced database systems
PPTX
Emerging chap asasasasasawwqwqwwqwewewr4.pptx
PPTX
Chapter-1-IntroDistributeddffsfdfsdf-1.pptx
PPT
chapter1lecturenotes sdsdasdddadad(2).ppt
PPTX
RIFLI-Computer-Basics-Part-1-1 lecture not
PPT
Lecture 01 - CS193Jxcxcxcx Summer 2003.ppt
PPT
JavaAdvanced programming for expertes dsd
PPTX
Computer_Programming_Fundamentals in cpp
PPT
SystemsProgrammingCourse FSDFFSFDSDSDSFSFS
PPTX
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
PPT
This is introduction to distributed systems for the revised curiculum
PPTX
ITET-4.pptx
CHapter four database managementm04.pptx
CHAPTERGAHAhsghdfsfdsfsdfsfdsfsdfAGSHagsh-7.ppt
chapter three 3-part II-1lecture slide.pptx
ExitExam Tutorial gfgfgfdfdfdfdf (1).pptx
CHAPTER-7 C++ PROGRAMMING ( STRUCTURE IN C++)
Query optimization and processing for advanced database systems
Emerging chap asasasasasawwqwqwwqwewewr4.pptx
Chapter-1-IntroDistributeddffsfdfsdf-1.pptx
chapter1lecturenotes sdsdasdddadad(2).ppt
RIFLI-Computer-Basics-Part-1-1 lecture not
Lecture 01 - CS193Jxcxcxcx Summer 2003.ppt
JavaAdvanced programming for expertes dsd
Computer_Programming_Fundamentals in cpp
SystemsProgrammingCourse FSDFFSFDSDSDSFSFS
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
This is introduction to distributed systems for the revised curiculum
ITET-4.pptx
Ad

Recently uploaded (20)

PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PDF
PPT on Performance Review to get promotions
PPTX
OOP with Java - Java Introduction (Basics)
PDF
Well-logging-methods_new................
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPT
Mechanical Engineering MATERIALS Selection
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
Digital Logic Computer Design lecture notes
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
Lecture Notes Electrical Wiring System Components
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
Construction Project Organization Group 2.pptx
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPT on Performance Review to get promotions
OOP with Java - Java Introduction (Basics)
Well-logging-methods_new................
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Mechanical Engineering MATERIALS Selection
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Digital Logic Computer Design lecture notes
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Lecture Notes Electrical Wiring System Components
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
UNIT 4 Total Quality Management .pptx
UNIT-1 - COAL BASED THERMAL POWER PLANTS
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Construction Project Organization Group 2.pptx
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx

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.