Retrieving Result SetsObjectivesIn this lesson, you will learn to: Use wildcards Use the IS NULL and IS NOT NULL keywords ...
Retrieving Result Sets2.D.1 Retrieving Rows Based on Pattern Matching Jackson Demello of the Texas Times newspaper, is to ...
Retrieving Result SetsTask List Create a format for query output Draft the query Execute the query Verify that the query o...
Retrieving Result SetsCreate a format for the query output Result:     The required output from the report is the name of ...
Retrieving Result SetsDraft the query String Operator     You can use the LIKE keyword to search for a string with      th...
Retrieving Result SetsDraft the query (Contd.)     Therefore, the query using the SELECT statement      should be:        ...
Retrieving Result SetsExecute the query Action:     In the Query Analyzer window, type the query     Execute the query©NII...
Retrieving Result SetsVerify that the query output is as per the requiredresults Check whether:     The required columns a...
Retrieving Result SetsJust a Minute… Write a query to display the details of all the contract  recruiters whose names begi...
Retrieving Result Sets2.D.2 Displaying Rows With Missing Values The list of candidates for whom interviews are not yet  sc...
Retrieving Result SetsTask List Create a format for the query output Draft the query Execute the query Verify that the que...
Retrieving Result SetsCreate a format for the query output Result:     The output requirement for the report is the names ...
Retrieving Result SetsDraft the query The IS NULL and IS NOT NULL Keywords     NULL is an unknown value or a value for whi...
Retrieving Result SetsDraft the query     Therefore, the query using the SELECT statement should      be:        SELECT vF...
Retrieving Result SetsExecute the query Action:     In the Query Analyzer window, type the query     Execute the query©NII...
Retrieving Result SetsVerify that the query output is as per the requiredresults Check whether:     The required columns a...
Retrieving Result Sets2.D.3 Displaying Data in a Specific Order A report of all roles is required as inputs for further re...
Retrieving Result SetsTask List Create a format for the query output Draft the query Execute the query Verify that the que...
Retrieving Result SetsCreate a format for the query output Result:     The output required in the report is the position c...
Retrieving Result SetsDraft the query The ORDER BY Clause     Syntax       SELECT select_list       FROM table_name       ...
Retrieving Result SetsDraft the query (Contd.)     Result:       ® The information is available in the Position table     ...
Retrieving Result SetsExecute the query Action:     In the Query Analyzer window, type the query     Execute the query©NII...
Retrieving Result SetsVerify that the query output is as per the requiredresults Check whether:     The required columns a...
Retrieving Result Sets2.D.4 Displaying the Top Few Rows Based on test scores, the top 3 external candidates have to  be sh...
Retrieving Result SetsTask List Create a format for the query output Draft the query Execute the query Verify that the que...
Retrieving Result SetsCreate a format for the query output Result:     The output required from the query is details of th...
Retrieving Result SetsDraft the query The TOP Keyword     The TOP clause limits the number of rows returned in the      re...
Retrieving Result SetsDraft the query (Contd.) Result:     The information required is available in the      ExternalCandi...
Retrieving Result SetsExecute the query Action:     In the Query Analyzer window, type:     Execute the query©NIIT        ...
Retrieving Result SetsVerify that the query output is as per the requiredresults Check whether:     The query output is as...
Retrieving Result SetsThe Distinct Keyword The DISTINCT keyword removes duplicate rows from the  result set Syntax    SELE...
Retrieving Result SetsJust a Minute… Write a query that displays a list of cities from where  applications of external can...
Retrieving Result Sets2.D.5 Displaying Aggregate Functions The total number of newspapers in which advertisements for  rec...
Retrieving Result SetsTask List Create a format for the query output Draft the query Execute the query Verify that the que...
Retrieving Result SetsCreate a format for the query output Result:     The output requirement in the report is to display ...
Retrieving Result SetsDraft the query Aggregate Functions     Summarize the values for a column or a group of columns     ...
Retrieving Result SetsExecute the query Action:     In the Query Analyzer window, type:     Execute the query©NIIT        ...
Retrieving Result SetsVerify that the query output is as per the requiredresults Check whether the correct count of newspa...
Retrieving Result SetsGrouping Result Sets The following clauses are used to group result sets:     GROUP BY: Summarizes t...
Retrieving Result Sets2.D.6 Generating a Summary Report The effectiveness of advertisements for recruitments placed  in va...
Retrieving Result SetsTask List Draft the query Execute the query Verify that the query output is as per the required resu...
Retrieving Result SetsDraft the query The GROUP BY Clause     Syntax      SELECT column_list      FROM table_name      WHE...
Retrieving Result SetsDraft the query (Contd.) The HAVING keyword in the SELECT query can be used to  select rows from the...
Retrieving Result SetsDraft the query (Contd.)     Therefore, the query using the SELECT statement should      be:        ...
Retrieving Result SetsExecute the query Action    In the Query Analyzer window, type the query    Execute the query©NIIT  ...
Retrieving Result SetsVerify that the query output is as per the plannedformat Check whether:     The required columns are...
Retrieving Result SetsGROUP BY ALL The ALL keyword of the GROUP BY clause is used to display  all groups, including those ...
Retrieving Result SetsCOMPUTE and COMPUTE BY The COMPUTE clause with the SELECT statement is used  to generate summary row...
Retrieving Result SetsJust a Minute… A list of external candidates who took a test, along with their  test scores, is requ...
Retrieving Result SetsSummaryIn this lesson, you learned that: SQL Server provides a pattern-matching method for string  e...
Retrieving Result SetsSummary (Contd.) The DISTINCT keyword in the SELECT statement is used to  eliminate duplicate rows T...
Retrieving Result SetsSummary (Contd.) The ALL keyword of the GROUP BY clause is used to  display all groups, including th...
Upcoming SlideShare
Loading in …5
×

Sql xp 02

512 views

Published on

Published in: Technology, Design
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
512
On SlideShare
0
From Embeds
0
Number of Embeds
10
Actions
Shares
0
Downloads
39
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Sql xp 02

  1. 1. Retrieving Result SetsObjectivesIn 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. 2. Retrieving Result Sets2.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. 3. Retrieving Result SetsTask 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. 4. Retrieving Result SetsCreate 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. 5. Retrieving Result SetsDraft 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. 6. Retrieving Result SetsDraft 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
  7. 7. Retrieving Result SetsExecute the query Action: In the Query Analyzer window, type the query Execute the query©NIIT SQL/lesson 2/Slide 7 of 52
  8. 8. Retrieving Result SetsVerify that the query output is as per the requiredresults 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. 9. Retrieving Result SetsJust 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. 10. Retrieving Result Sets2.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. 11. Retrieving Result SetsTask 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. 12. Retrieving Result SetsCreate 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. 13. Retrieving Result SetsDraft 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. 14. Retrieving Result SetsDraft 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
  15. 15. Retrieving Result SetsExecute the query Action: In the Query Analyzer window, type the query Execute the query©NIIT SQL/lesson 2/Slide 15 of
  16. 16. Retrieving Result SetsVerify that the query output is as per the requiredresults 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. 17. Retrieving Result Sets2.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. 18. Retrieving Result SetsTask 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. 19. Retrieving Result SetsCreate 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. 20. Retrieving Result SetsDraft 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. 21. Retrieving Result SetsDraft 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
  22. 22. Retrieving Result SetsExecute the query Action: In the Query Analyzer window, type the query Execute the query©NIIT SQL/lesson 2/Slide 22 of
  23. 23. Retrieving Result SetsVerify that the query output is as per the requiredresults Check whether: The required columns are displayed All rows are displayed by vDescription in ascending order©NIIT SQL/lesson 2/Slide 23 of
  24. 24. Retrieving Result Sets2.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. 25. Retrieving Result SetsTask 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. 26. Retrieving Result SetsCreate 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. 27. Retrieving Result SetsDraft 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. 28. Retrieving Result SetsDraft 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
  29. 29. Retrieving Result SetsExecute the query Action: In the Query Analyzer window, type: Execute the query©NIIT SQL/lesson 2/Slide 29 of
  30. 30. Retrieving Result SetsVerify that the query output is as per the requiredresults 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. 31. Retrieving Result SetsThe 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. 32. Retrieving Result SetsJust 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. 33. Retrieving Result Sets2.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. 34. Retrieving Result SetsTask 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. 35. Retrieving Result SetsCreate 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. 36. Retrieving Result SetsDraft 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
  37. 37. Retrieving Result SetsExecute the query Action: In the Query Analyzer window, type: Execute the query©NIIT SQL/lesson 2/Slide 37 of
  38. 38. Retrieving Result SetsVerify that the query output is as per the requiredresults Check whether the correct count of newspapers is displayed©NIIT SQL/lesson 2/Slide 38 of
  39. 39. Retrieving Result SetsGrouping 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. 40. Retrieving Result Sets2.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. 41. Retrieving Result SetsTask 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. 42. Retrieving Result SetsDraft 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. 43. Retrieving Result SetsDraft 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. 44. Retrieving Result SetsDraft 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
  45. 45. Retrieving Result SetsExecute the query Action In the Query Analyzer window, type the query Execute the query©NIIT SQL/lesson 2/Slide 45 of
  46. 46. Retrieving Result SetsVerify that the query output is as per the plannedformat 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. 47. Retrieving Result SetsGROUP 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. 48. Retrieving Result SetsCOMPUTE 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. 49. Retrieving Result SetsJust 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. 50. Retrieving Result SetsSummaryIn 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. 51. Retrieving Result SetsSummary (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. 52. Retrieving Result SetsSummary (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

×