0% found this document useful (0 votes)
23 views11 pages

Imgt Finals

This document discusses various SQL functions and joins in Microsoft SQL Server. It covers: 1) Single-row functions that manipulate data items and accept arguments to return one value per row, including string, date, conversion and mathematical functions. 2) Joins allow merging rows from one data set with another, requiring a minimum of (n-1) join conditions to join n tables. 3) Equijoins, or inner joins, return all columns from both tables and only rows that have equal values in the join column. Additional search conditions can be added using the AND operator.

Uploaded by

Renier Berano
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views11 pages

Imgt Finals

This document discusses various SQL functions and joins in Microsoft SQL Server. It covers: 1) Single-row functions that manipulate data items and accept arguments to return one value per row, including string, date, conversion and mathematical functions. 2) Joins allow merging rows from one data set with another, requiring a minimum of (n-1) join conditions to join n tables. 3) Equijoins, or inner joins, return all columns from both tables and only rows that have equal values in the join column. Additional search conditions can be added using the AND operator.

Uploaded by

Renier Berano
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

IMGT FINALS Case Conversion Functions

- Convert case for character strings


WEEK 13: FUNCTIONS IN MSSQL

SQL Functions

- Display the employee ID, name for employee


King.

Types of SQL Functions

LEN
Single-Row Functions - Returns the number of characters of the
- Manipulate data items specified string expression.
- Accept arguments and return one value
- Act on each row returned
- Return one result per row
- May modify the datatype
- Can be nested

LEFT
Single Functions - Returns the part of a character string starting at
a specified number of characters from the left.
RIGHT - Replaces all occurrences of the second given
- Returns the part of a character string starting at string expression in the first string expression
a specified number of characters from the right. with a third expression.

LTRIM
- Returns a character expression after removing
leading blanks.
REPLICATE
- Repeats a character expression for a specified
number of times.

RTRIM
- Returns a character expression after removing SUBSTRING
trailing spaces. - Returns part of a character, binary, text or image
expression

REPLACE
Mathematical Functions

CHARINDEX
- Returns the starting position of a string specify.

Mathematical Functions

ABS: Return the absolute value of the number.

POWER : Returns the value of the given expression to


the specified power

CEILING: Returns the smallest integer greater than, or


equal to, the given numeric expressions.

String Manipulation Functions


- Manipulate character strings

FLOOR: Returns the largest integer less than, or equal


to, the given numeric expression.

ROUND: Returns a numeric expression, rounded to the


specified length or precision.

Using the ROUND Function


Date Functions

DATEDIFF
- Returns the number of date and time boundaries
crossed between two specified dates
- Sometimes we only want a portion of a date or
we want to do date arithmetic

To do this we will use the following functions:


- datename
- datepart
- dateadd
- datediff

Arithmetic with Dates


- Add or subtract a number to or from a date for a
resultant date value.
- Subtract two dates to find the number of days
between those dates.
- Add hours to a date by dividing the number of
hours by 24.

GETDATE
- Returns the current system date and time in the
Microsoft SQL Server standard internal format
for datetime values.

DATENAME
- Returns a character string representing the
specified datepart of the specified date.

DATEADD
- Returns a new datetime value based on adding
an interval to the specified date.

DATEPART
- Returns an integer representing the specified
datepart of the specified date,
CAST

Conversion Functions

CONVERT

data_type
- Is the target system-supplied data type
CAST and CONVERT
length
- CAST and CONVERT provide similar
- Is an optional parameter of nchar, nvarchar,
functionality,
char, varchar, binary, or varbinary data types.
- Explicitly converts an expression of one data
type to another.
expression
- Is any valid Microsoft SQL Server expression.
CAST

style
- Is the style of date format used to convert
datetime or smalldatetime data to character
Expression
data.
- Is any valid Microsoft SQL Server
expression
Data_type
- Is the target system-supplied data type.

What is Wrong with this Statement?


Guidelines in Using a Join
- When writing a SELECT statement that joins
tables, precede the column name with the table
name for clarity and to enhance database
access.
- If the same column name appears in more than
one table, the column name must be prefixed
with the table name.
- The join n tables together, you need a minimum
of (n-1) join conditions. Therefore, to join four
tables, a minimum of three joins are required.

Types of Joins

General Functions

WEEK 14-15: WHAT IS JOIN IN MSSQL

Displaying Data from Multiple Tables What is an Equijoin?

Obtaining Data from Multiple Tables

What is an Equijoin?

What is a Join?

- The inner join includes only those rows from


each side of the join that are contained within
the intersection of the two data sources.
- A join merges rows from one data set with rows - Equijoins are also called inner joins.
from another data set, creating a new sets of - It returns all the columns in both tables, and
rows that includes columns from both. returns only the rows for which there is an equal
- Use a join to query data from more than one value in the join column.
table.

Rows in one table can be joined to rows in another


table according to common values existing in
corresponding columns, that is, usually primary and
foreign key columns.
What is a Join?

Table1.column - denotes the table and column from


which data is retrieved.

Join - identifies the second table.


ON - defines the relation between two the tables
table1.column1 = table2.column2
- Is the condition that joins (or relates) the tables Additional Search Conditions Using the AND
together. Operator

Retrieving Records with Equijoins

Additional Search Conditions Using the AND


Operator

Qualifying Ambiguous Column Names


- Use table prefixes to qualify column names that
are in multiple tables.
- Improve performance by using table prefixes.
- Distinguish columns that have identical names
but reside in different tables by using column
aliases.

Using Table Aliases


- Use table aliases instead of table names
particularly if table names are lengthy.
- Just as a column alias gives a column another
name, a table alias gives a table another name.
- If a table alias is used for a particular table name
Joining More than Two Tables
in the FROM clause, then that table alias must
be substituted for the table name throughout the
SELECT statement.
4. The Product table for the “Beverages”
5. The ProductCategory table for the “Beverages”

Joining More Than Two Tables

Outer Joins

1. The Suppliers for the “Supplier Name”


2. The Product table for the “Products”
3. The Categories table for the “Dairy Products”

1. The Orders table for the “purchased” - An outer join includes not only rows from the two
2. The [Order Detail] table for the “purchased” data sources with a match but also unmatched
3. The Product table for the “Products” rows from outside the intersection.
- An outer join extends the inner join by adding
the nonmatching data from the left or right data
set.
- Some of the data in the result set produced by
an outer join will look just like the data from an
inner join.
- Any rows from the outer-join table that do not
have a match in the other side of the join will
return data only from the outer-join table. In this
case, columns from the other data source will
have null values.
- Outer joins are often employed when a
secondary table has a foreign-key constraint to
the primary table and permits nulls in the foreign
key.

1. The Customer table for the “who”


2. The Orders table for the “purchased”
3. The [Order Details] table for the “purchased”
Cross Join
- The cross join, also called an unrestricted join, is
a pure relational algebra multiplication of the two
source tables.
- Without a join condition restricting the result set,
the result set includes every possible
combination of rows from the data.
All customers with order details if present: - Each row in data set one is matched with every
row in data set two
- This type of result set is referred to as a
Cartesian product.

Cartesian Product
A Cartesian product is formed when:
- A join condition is omitted.
- A join condition is invalid
Customers that did not place orders: - All rows in the first table are joined to all rows in
the second table.

To avoid a Cartesian product, always include a valid join


condition.

Generating a Cartesian Product

Self Joins
- A self-join is a join that refers back to the same
table.

WEEK 16 : VIEWS

Views
- Virtual table whose contents are defined by a
query.
- based on the result set of a SELECT statement.
- Contains rows and columns just like a real table
- The rows and columns of data come from tables
referenced in the query defining the view and
are produced dynamically when the view is
referenced.
- you can add TSQL arguments to a view
definition such as SELECT, WHERE, JOIN to
- To find the name of each employee’s manager, present the data as if the data was coming from
you need to join the EMPLOYEES table to itself, a single table.
or perform a self join. - A view can be used inside a query, a stored
procedure or another view.
- Restrict a user to specific rows in a table.
- Restrict a user to specific columns.
- Join columns from multiple tables so that they
look like a single table.
- Aggregate information instead of supplying
details.
Simple Stored Procedure

Referencing Views

Stored Procedure Execution

Modifying View

Stored Procedure with Parameters

WEEK 17: DEVELOPMENT AND CASE STUDY

Stored Procedure
- Collection of SQL statements.
- Can accept parameters
- Contain programming statements that perform
operations in the database, including calling
other procedures.
Stored Procedure Execution
Creating a Stored Procedure

AS
- Specifies the actions the procedure is to take.

Sql_statement
- Is any number and type of Transact-SQL
statements to be included in the procedure.
Altering Stored Procedure
- Alters a previously created procedure, created
by executing the CREATE PROCEDURE
statement, without changing permissions and
without affecting any dependent stored
procedures.

Dynamic SQL
- Dynamic SQL is T-SQL code assembled into a
character string, interpreted as a command and
executed.
- Dynamic SQL provides flexibility for
administrative, programming tasks.

Two methods for dynamically executing SQL


statements:
- EXEC command can accept a string as input in
parentheses. No parameters may be passed in.
- System stored procedure sp_executesql
(preferred) supports parameters

Beware of risks from unvalidated inputs in dynamic


SQL!

Writing Queries with Dynamic SQL

Using sp_executesql
- Accepts string as code to be run
- Allows parameterized code without risk of SQL
injection
- Can perform better than EXEC due to query plan
reuse

You might also like