More Related Content
Similar to Sql xp 02 (20)
Sql xp 02
- 1. 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
©NIIT SQL/lesson 2/Slide 1 of 52
- 2. 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.
©NIIT SQL/lesson 2/Slide 2 of 52
- 3. 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
©NIIT SQL/lesson 2/Slide 3 of 52
- 4. 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
©NIIT SQL/lesson 2/Slide 4 of 52
- 5. 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 %
©NIIT SQL/lesson 2/Slide 5 of 52
- 6. 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%'
©NIIT SQL/lesson 2/Slide 6 of 52
- 8. 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
©NIIT SQL/lesson 2/Slide 8 of 52
- 9. Retrieving Result Sets
Just a Minute…
Write a query to display the details of all the contract
recruiters whose names begin with “J”.
©NIIT SQL/lesson 2/Slide 9 of 52
- 10. Retrieving Result Sets
2.D.2 Displaying Rows With Missing Values
The list of candidates for whom interviews are not yet
scheduled is required.
©NIIT SQL/lesson 2/Slide 10 of
- 11. 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
©NIIT SQL/lesson 2/Slide 11 of
- 12. 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
©NIIT SQL/lesson 2/Slide 12 of
- 13. 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
©NIIT SQL/lesson 2/Slide 13 of
- 14. 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
©NIIT SQL/lesson 2/Slide 14 of
- 16. 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
©NIIT SQL/lesson 2/Slide 16 of
- 17. 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.
©NIIT SQL/lesson 2/Slide 17 of
- 18. 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
©NIIT SQL/lesson 2/Slide 18 of
- 19. 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
©NIIT SQL/lesson 2/Slide 19 of
- 20. 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]...]
©NIIT SQL/lesson 2/Slide 20 of
- 21. 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
©NIIT SQL/lesson 2/Slide 21 of
- 23. 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
©NIIT SQL/lesson 2/Slide 23 of
- 24. 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.
©NIIT SQL/lesson 2/Slide 24 of
- 25. 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
©NIIT SQL/lesson 2/Slide 25 of
- 26. 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
©NIIT SQL/lesson 2/Slide 26 of
- 27. 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…]
©NIIT SQL/lesson 2/Slide 27 of
- 28. 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
©NIIT SQL/lesson 2/Slide 28 of
- 30. 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
©NIIT SQL/lesson 2/Slide 30 of
- 31. 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
©NIIT SQL/lesson 2/Slide 31 of
- 32. Retrieving Result Sets
Just a Minute…
Write a query that displays a list of cities from where
applications of external candidates have been received.
©NIIT SQL/lesson 2/Slide 32 of
- 33. Retrieving Result Sets
2.D.5 Displaying Aggregate Functions
The total number of newspapers in which advertisements for
recruitments are published is required.
©NIIT SQL/lesson 2/Slide 33 of
- 34. 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
©NIIT SQL/lesson 2/Slide 34 of
- 35. 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
©NIIT SQL/lesson 2/Slide 35 of
- 36. 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
©NIIT SQL/lesson 2/Slide 36 of
- 38. Retrieving Result Sets
Verify that the query output is as per the required
results
Check whether the correct count of newspapers is displayed
©NIIT SQL/lesson 2/Slide 38 of
- 39. 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
©NIIT SQL/lesson 2/Slide 39 of
- 40. 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
©NIIT SQL/lesson 2/Slide 40 of
- 41. Retrieving Result Sets
Task List
Draft the query
Execute the query
Verify that the query output is as per the required results
©NIIT SQL/lesson 2/Slide 41 of
- 42. 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]
©NIIT SQL/lesson 2/Slide 42 of
- 43. 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
©NIIT SQL/lesson 2/Slide 43 of
- 44. 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
©NIIT SQL/lesson 2/Slide 44 of
- 46. 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
©NIIT SQL/lesson 2/Slide 46 of
- 47. 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
©NIIT SQL/lesson 2/Slide 47 of
- 48. 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]...]
©NIIT SQL/lesson 2/Slide 48 of
- 49. 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.
©NIIT SQL/lesson 2/Slide 49 of
- 50. 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
©NIIT SQL/lesson 2/Slide 50 of
- 51. 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
©NIIT SQL/lesson 2/Slide 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
©NIIT SQL/lesson 2/Slide 52 of