You are on page 1of 52

Retrieving Result Sets

Objectives
In this lesson, you will learn to: Use wildcards Use the IS NULL and IS NOT NULL keywords Use the ORDER BY clause Use the TOP keyword Use the DISTINCT keyword Use aggregate functions in queries

Group result sets


Use the COMPUTE and COMPUTE BY clause
1 of 52

Retrieving Result Sets


2.D.1 Retrieving Rows Based on Pattern Matching
Jackson Demello of the Texas Times newspaper, is to be contacted. However, there are three newspapers that contain the words Texas Times along with other words. To ensure that the right newspaper is contacted, details like the names of the newspapers, the contact persons, and the telephone numbers of the newspapers, which have Texas Times in their names need to be displayed.

2 of 52

Retrieving Result Sets


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

3 of 52

Retrieving Result Sets


Create a format for the query output
Result: The required output from the report is the name of the newspaper, contact person, and the telephone numbers The column headings required in the report are cNewspaperName, vContactPerson and cPhone The newspaper name should have the phrase Texas Times in it

The format of the report is shown below:


cNewspaperName vContactPerson cPhone

4 of 52

Retrieving Result Sets


Draft the query
String Operator You can use the LIKE keyword to search for a string with the wildcard mechanism The LIKE keyword is used to select those rows that match the specified portion of character string Result: The required information is available in the Newspaper table

Since the newspaper name must have Texas Times, and it could be prefixed or suffixed by anything, the wild card to be used is %
5 of 52

Retrieving Result Sets


Draft the query (Contd.)
Therefore, the query using the SELECT statement should be: SELECT cNewspaperName, vContactPerson, cPhone

FROM Newspaper
WHERE cNewspaperName LIKE '%Texas Times%'

6 of 52

Retrieving Result Sets


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

7 of 52

Retrieving Result Sets


Verify that the query output is as per the required results
Check whether: The required columns are displayed All the rows that meet the condition specified in the WHERE clause are displayed

8 of 52

Retrieving Result Sets


Just a Minute
Write a query to display the details of all the contract recruiters whose names begin with J.

9 of 52

Retrieving Result Sets


2.D.2 Displaying Rows With Missing Values
The list of candidates for whom interviews are not yet scheduled is required.

10 of 52

Retrieving Result Sets


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

11 of 52

Retrieving Result Sets


Create a format for the query output
Result: The output requirement for the report is the names of the candidates whose interview has not yet been scheduled

12 of 52

Retrieving Result Sets


Draft the query
The IS NULL and IS NOT NULL Keywords NULL is an unknown value or a value for which data is not available Syntax

SELECT column_list FROM table_name


WHERE column_name unknown_value_operator Result:

The information is available in the ExternalCandidate table


The condition is that the test date should be NULL
13 of 52

Retrieving Result Sets


Draft the query
Therefore, the query using the SELECT statement should be: SELECT vFirstName, vLastName, dInterviewDate FROM ExternalCandidate

WHERE dInterviewDate IS NULL

14 of 52

Retrieving Result Sets


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

15 of 52

Retrieving Result Sets


Verify that the query output is as per the required results
Check whether: The required columns are displayed All rows that have a NULL value in the dInterviewDate attribute are displayed

16 of 52

Retrieving Result Sets


2.D.3 Displaying Data in a Specific Order
A report of all roles is required as inputs for further reviewing of the number of vacancies. A report in the ascending order of the position description is to be generated.

17 of 52

Retrieving Result Sets


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 52

Retrieving Result Sets


Create a format for the query output
Result: The output required in the report is the position code and a description of the position available The format of the report is given below:
cPositionCode cDescription

19 of 52

Retrieving Result Sets


Draft the query
The ORDER BY Clause Syntax SELECT select_list FROM table_name [ORDER BY column_name | select_list_number | expression [ASC|DESC][, column_name | select_list_number | expression [ASC|DESC]...]

20 of 52

Retrieving Result Sets


Draft the query (Contd.)
Result: The information is available in the Position table Therefore, the query using the SELECT statement should be: SELECT cPositionCode, vDescription FROM Position ORDER BY vDescription ASC

21 of 52

Retrieving Result Sets


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

22 of 52

Retrieving Result Sets


Verify that the query output is as per the required results
Check whether: The required columns are displayed All rows are displayed by vDescription in ascending order

23 of 52

Retrieving Result Sets


2.D.4 Displaying the Top Few Rows
Based on test scores, the top 3 external candidates have to be short-listed for an interview. The tests were taken in March 2001. All details of these candidates are required.

24 of 52

Retrieving Result Sets


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

25 of 52

Retrieving Result Sets


Create a format for the query output
Result: The output required from the query is details of the top 3 candidates The column headings required by the report are the attribute names of the table ExternalCandidate

26 of 52

Retrieving Result Sets


Draft the query
The TOP Keyword The TOP clause limits the number of rows returned in the result set Syntax SELECT [TOP n [PERCENT]] column_name [,column_name] FROM table_name

WHERE search_conditions
[ORDER BY [column_name[,column_name]

27 of 52

Retrieving Result Sets


Draft the query (Contd.)
Result: The information required is available in the ExternalCandidate table All the external candidate details are required

Therefore, the query using the SELECT statement should be:


SELECT TOP 3 * FROM ExternalCandidate WHERE dTestDate >= '3/1/01' AND dTestDate <= '3/31/01' ORDER BY siTestScore DESC
28 of 52

Retrieving Result Sets


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

29 of 52

Retrieving Result Sets


Verify that the query output is as per the required results
Check whether: The query output is as per the required result The rows are in descending order of the test scores

30 of 52

Retrieving Result Sets


The Distinct Keyword
The DISTINCT keyword removes duplicate rows from the result set Syntax SELECT [ALL|DISTINCT] column_names FROM table_name WHERE search_condition

31 of 52

Retrieving Result Sets


Just a Minute
Write a query that displays a list of cities from where applications of external candidates have been received.

32 of 52

Retrieving Result Sets


2.D.5 Displaying Aggregate Functions
The total number of newspapers in which advertisements for recruitments are published is required.

33 of 52

Retrieving Result Sets


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

34 of 52

Retrieving Result Sets


Create a format for the query output
Result: The output requirement in the report is to display the total number of newspapers in which advertisements for recruitments are published

The heading to be used is No. of Newspapers


The format of the output is shown below:
No. of Newspapers

35 of 52

Retrieving Result Sets


Draft the query
Aggregate Functions Summarize the values for a column or a group of columns within a table for which they are applied, and produce a single value

Result:
The information is available in the Newspaper table The aggregate function to be used is COUNT

Therefore, the query using the SELECT statement should be: SELECT 'No.Of Newspapers = COUNT(cNewspaperCode) FROM Newspaper
36 of 52

Retrieving Result Sets


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

37 of 52

Retrieving Result Sets


Verify that the query output is as per the required results
Check whether the correct count of newspapers is displayed

38 of 52

Retrieving Result Sets


Grouping Result Sets The following clauses are used to group result sets: GROUP BY: Summarizes the result set into groups defined in the query using aggregate functions GROUP BY ALL: The ALL keyword of the GROUP BY clause is used to display all groups, including those excluded from the WHERE clause COMPUTE and COMPUTE BY: The COMPUTE clause with the SELECT statement is used to generate summary rows using the aggregate functions in the query results. The COMPUTE BY clause further summarizes the result set by columns

39 of 52

Retrieving Result Sets


2.D.6 Generating a Summary Report
The effectiveness of advertisements for recruitments placed in various newspapers needs to be analyzed. As a first step, the number of advertisements placed in each newspaper is required in the following format:
Newspaper Code

No. Of Advts. Placed

40 of 52

Retrieving Result Sets


Task List
Draft the query Execute the query Verify that the query output is as per the required results

41 of 52

Retrieving Result Sets


Draft the query The GROUP BY Clause Syntax SELECT column_list FROM table_name WHERE condition GROUP BY [ALL] expression [, expression] [HAVING search_condition]

42 of 52

Retrieving Result Sets


Draft the query (Contd.)
The HAVING keyword in the SELECT query can be used to select rows from the intermediate result set Result: The information is available in the NewsAd table The number of advertisements placed in each newspaper is required The output needs to be grouped newspaper wise, so the GROUP BY clause has to be used

43 of 52

Retrieving Result Sets


Draft the query (Contd.)
Therefore, the query using the SELECT statement should be: SELECT 'Newspaper Code' = cNewspaperCode, 'No. Of Advts. Placed' = COUNT(cNewspaperCode) FROM NewsAd GROUP BY cNewspaperCode

44 of 52

Retrieving Result Sets


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

45 of 52

Retrieving Result Sets


Verify that the query output is as per the planned format
Check whether: The required columns are displayed The number of advertisements published in each newspaper is correct

46 of 52

Retrieving Result Sets


GROUP BY ALL
The ALL keyword of the GROUP BY clause is used to display all groups, including those excluded from the WHERE clause Example SELECT Type, Advance = SUM (Advance) FROM Titles WHERE Type IN ('business', 'mod_cook', 'trad_cook')

GROUP BY ALL Type

47 of 52

Retrieving Result Sets


COMPUTE and COMPUTE BY The COMPUTE clause with the SELECT statement is used to generate summary rows using aggregate functions in the query results The COMPUTE BY clause can be used to calculate summary values of the result set on a group of data

Syntax
SELECT column_list FROM table_name ORDER BY column_name

COMPUTE aggregate_function (column_name) [, aggregate_function (column_name)...] [BY column_name [, column_name]...]


48 of 52

Retrieving Result Sets


Just a Minute
A list of external candidates who took a test, along with their test scores, is required. The average of the test scores needs to be printed at the bottom of the list.

49 of 52

Retrieving Result Sets


Summary
In this lesson, you learned that: SQL Server provides a pattern-matching method for string expressions by using the LIKE keyword with the wildcard mechanism

The LIKE keyword is used to select those rows that match the specified portion of character string
In SQL Server terms, NULL is an unknown value or a value for which data is not available The NULL values can be retrieved from the table using the IS NULL keyword in the WHERE clause
50 of 52

Retrieving Result Sets


Summary (Contd.)
The DISTINCT keyword in the SELECT statement is used to eliminate duplicate rows The TOP clause limits the number of rows returned in the result set

The GROUP BY clause organizes the summarized result set into groups defined in a table with the help of the aggregate functions
The HAVING clause restricts the result set to produce the data based on a condition

51 of 52

Retrieving Result Sets


Summary (Contd.)
The ALL keyword of the GROUP BY clause is used to display all groups, including those excluded from the WHERE clause SQL Server provides the COMPUTE clause with the SELECT statement to produce summary rows using aggregate functions in the query results The COMPUTE BY clause can be used to calculate summary values of the result set on a group of data

52 of 52

You might also like