Вы находитесь на странице: 1из 49

Functions and Joins

Objectives
In this lesson, you will learn to: Use functions Use different types of joins

1 of 49

Functions and Joins


3.D.1 Displaying Data in Upper Case Using String Functions
A report containing the newspaper name, the name of the contact person, and the telephone numbers is required to contact various newspapers to place an advertisement. The newspaper name should be displayed in uppercase.

2 of 49

Functions and Joins


Task List
Create a format for the query output Draft the query Execute the query Verify that the query output is as per the required results

3 of 49

Functions and Joins


Create a format for the query output
Result: The output required in the report is the newspaper name (in upper case), the contact person, and the telephone number from the Newspaper table

The column headings in the report are cNewspaperName, vContactPerson, and cPhone

4 of 49

Functions and Joins


Draft the query
String Functions: Are used to format data as per specific requirements Syntax SELECT function_name (parameters) Result: The information is available in the Newspaper table The string function to be used to display the names of newspapers in uppercase is UPPER

Therefore, the query using the SELECT statement should be:


SELECT 'Newspaper Name'=UPPER (cNewspaperName), vContactPerson, cPhone FROM Newspaper
5 of 49

Functions and Joins


Execute the query
Action: In the Query Analyzer window, type: Execute the query

6 of 49

Functions and Joins


Verify that the query output is as per the required results
Check whether: All the required rows are displayed The newspaper names are displayed in upper case

7 of 49

Functions and Joins


Just a Minute...
The names, addresses, and phone numbers of the recruitment agencies in Houston are required. However, only the first 10 characters of the address should be displayed.

8 of 49

Functions and Joins


3.D.2 Adding Days to a Date Using Date Functions
The proposed deadline for campus recruitment is 10 days from the start date of the recruitment process. A report containing the college code, the start date for recruitment, and the proposed deadline for all colleges the company is visiting needs to be displayed.

9 of 49

Functions and Joins


Task List
Create a format for the query output Draft the query Execute the query Verify that the query output is as per the required results

10 of 49

Functions and Joins


Create a format for the query output
Result: The output required in the report is the college code, the recruitment start date, and the proposed deadline The column headings of the report are cCollegeCode, dRecruitmentStartDate, and ProposedDeadline, which is 10 days from the start date

11 of 49

Functions and Joins


Draft the query
Date functions: Are used to manipulate datetime values, perform arithmetic operations, and perform date parsing (extract components like the day, the month, and the year) Syntax

SELECT date_function (parameters)


Result: The required information is available in the CampusRecruitment table The date function to be used is DATEADD

12 of 49

Functions and Joins


Draft the query (Contd.)
Therefore, the query using the SELECT statement should be: SELECT cCollegeCode, dRecruitmentStartDate, 'Proposed Deadline' = DATEADD(dd, 10, dRecruitmentStartDate) FROM CampusRecruitment

13 of 49

Functions and Joins


Execute the query
Action: In the Query Analyzer window, type the query Execute the query

14 of 49

Functions and Joins


Verify that the query output is as per the required results
Check whether: All the required rows are displayed The proposed deadline is 10 days from the start date

15 of 49

Functions and Joins


Just a Minute...
A schedule for interviews is required. The name of the candidate, the interviewers employee code, the date, and the weekday of the interview are to be printed in alphabetical order of candidate names in the following format:
Candidate Name Interviewer Code Date Week Day

16 of 49

Functions and Joins


3.D.3 Rounding off Values Using Mathematical Functions
The test scores of candidates have been declared. Helen White has scored 79.9 marks, and she is to be informed of the same. Her first name, telephone number, and score have to be displayed. The score should be rounded off to the nearest integer.

17 of 49

Functions and Joins


Task List
Create a format for the query output Draft the query Execute the query Verify that the query output is as per the required results

18 of 49

Functions and Joins


Create a format for the query output
Result: The output required in the report is the first name, telephone number, and score of the candidate The column headings of the report are vFirstName, cPhone, and Score

19 of 49

Functions and Joins


Draft the query
Mathematical Functions: Are used to perform numerical operations on mathematical data Syntax ROUND (numeric_expression, length) Result: The required information is available in the ExternalCandidate table

The function to be used is ROUND

20 of 49

Functions and Joins


Draft the query (Contd.)
Therefore, the query using the SELECT statement should be: SELECT vFirstName, cPhone,'Score'= ROUND(79.9,0)

FROM ExternalCandidate
WHERE vFirstName='Helen'

21 of 49

Functions and Joins


Execute the query
Action: In the Query Analyzer window, type: Execute the query

22 of 49

Functions and Joins


Verify that the query output is as per the required results
Check whether the marks are rounded off to the nearest numeric value

23 of 49

Functions and Joins


System Functions
System functions provide a method of querying the system tables of SQL Server These functions are used to access SQL Server, databases, or user-related information

They enable quick conversion of system and object information without writing several queries

24 of 49

Functions and Joins


Data Conversion
The CONVERT function is used to change data from one type to another when SQL Server cannot implicitly understand a conversion Syntax

CONVERT (datatype [(length)], expression [, style])

25 of 49

Functions and Joins


Joins
A join can be defined as an operation that includes the retrieval of data from more than one table at a time Syntax SELECT column_name, column_name [,column_name] FROM table_name [CROSS|INNER|[LEFT | RIGHT]OUTER] JOIN table_name [ON table_name.ref_column_name join_operator table_name.ref_column_name] [WHERE search_condition]

26 of 49

Functions and Joins


Joins (Contd.)
The various types of joins are: Inner Join Outer Join Cross Join Equi Join Natural Join

Self Join

27 of 49

Functions and Joins


3.D.4 Displaying Data From Two Tables Using Inner Joins
The names of the candidates and their recruitment agencies are required for an analysis by senior management. A report displaying these details is to be generated.

28 of 49

Functions and Joins


Task List
Create a format for the query output Draft the query Execute the query Verify that the query output is as per the required results

29 of 49

Functions and Joins


Create a format for the query output
Result: The required output from the query is a list of candidate names and the names of the recruitment agencies that sent them

30 of 49

Functions and Joins


Draft the query
Inner Joins Syntax SELECT column_name, column_name [,column_name] FROM table_name JOIN table_name ON table_name.ref_column_name join_operator table_name.ref_column_name

Cartesian Product: A join that includes more than one table without any condition in the ON clause creates a cartesian product between the two tables
31 of 49

Functions and Joins


Draft the query (Contd.)
Result: The required information is available in the ExternalCandidate and RecruitmentAgencies tables Therefore, the query using the SELECT statement should be: SELECT 'Candidate Name' = vFirstName, 'Recruitment Agency' = cName

FROM ExternalCandidate JOIN RecruitmentAgencies


ON ExternalCandidate.cAgencyCode = RecruitmentAgencies.cAgencyCode
32 of 49

Functions and Joins


Execute the query
Action: In the Query Analyzer window, type the query Execute the query

33 of 49

Functions and Joins


Verify that the query output is as per the required results
Check whether: The required columns from different tables are displayed The required rows are displayed

34 of 49

Functions and Joins


Just a Minute
The names of candidates and the newspapers they referred for recruitment advertisements are required for an analysis. A report displaying these details is to be generated.

35 of 49

Functions and Joins


3.D.5 Displaying Data From Two Tables Using Outer Joins
The names of all the external candidates along with the names of their recruitment agencies, wherever applicable, are required for an analysis. A report displaying these details is to be generated.

36 of 49

Functions and Joins


Task List
Create a format for the query output Draft the query Execute the query Verify that the query output is as per the required results

37 of 49

Functions and Joins


Create a format for the query output
Result: The required output from the query is the names of all the external candidates and their recruitment agencies (wherever applicable)

38 of 49

Functions and Joins


Draft the query
Outer Joins A join can be termed an outer join when the result set contains all rows from one table and the matching rows from another

Syntax
SELECT column_name, column_name [,column_name]

FROM table_name [LEFT | RIGHT] OUTER JOIN table_name


ON table_name.ref_column_name join_operator table_name.ref_column_name
39 of 49

Functions and Joins


Draft the query (Contd.)
Result: The required information is available in the ExternalCandidate and RecruitmentAgencies tables Therefore, the query using the SELECT statement should be: SELECT vFirstName, vLastName, cName FROM ExternalCandidate LEFT OUTER JOIN RecruitmentAgencies ON ExternalCandidate.cAgencyCode = RecruitmentAgencies.cAgencyCode
40 of 49

Functions and Joins


Execute the query
Action: In the Query Analyzer window, type: Execute the query

41 of 49

Functions and Joins


Verify that the query output is as per the required results
Action: Check whether:

The required columns are displayed All rows from the first table are displayed The require rows from the second table are displayed

42 of 49

Functions and Joins


Cross Join
A join that includes more than one table using the keyword CROSS is called a cross join. Example SELECT * FROM Titles CROSS JOIN Publishers

43 of 49

Functions and Joins


Equi Join
A join that uses an asterisk (*) sign in the SELECT list and displays redundant column data in the result set is termed as an equi join Example

SELECT *
FROM Sales s JOIN Titles t ON s.Title_Id = t.Title_Id

JOIN Publishers p
ON t.Pub_Id = p.Pub_Id

44 of 49

Functions and Joins


Natural Join
A join that restricts the redundant column data from the result set is known as a natural join Example SELECT t.Title, p.Pub_Name FROM Titles t JOIN Publishers p ON t.Pub_Id = p.Pub_Id

45 of 49

Functions and Joins


Self Join
A join is said to be a self join when one row in a table correlates with other rows in the same table Example SELECT t1.title,t2.title , t1.price FROM titles t1 t1.price=t2.price WHERE t1.price=2.99 JOIN titles t2 ON

46 of 49

Functions and Joins


Summary
In this lesson you learned that: SQL Server uses string functions, which can be used as part of any character expression. SQL Server includes date functions for performing date parsing and date arithmetic. The CONVERT function is used to change data from one type to another when SQL Server cannot implicitly understand a conversion. SQL Server provides a method of retrieving data from more than one table using joins.

47 of 49

Functions and Joins


Summary (Contd.)
In inner join, data from multiple tables is displayed after comparing values present in a common column. Only rows with values satisfying the join condition in the common column are displayed. A join can be termed an outer join when the result set contains all rows from one table and the matching rows from another. A join that includes more than one table using the keyword CROSS is called a cross join.

A join that uses an asterisk (*) sign in the SELECT list and displays redundant column data in the result set is termed as an equi join.
48 of 49

Functions and Joins


Summary (Contd.)
A join that restricts the redundant column data from the result set is known as a natural join. A join is said to be a self join when one row in a table correlates with other rows in the same table.

49 of 49

Вам также может понравиться