SlideShare a Scribd company logo
1 of 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

©NIIT                                  SQL/lesson 2/Slide 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.




©NIIT                                       SQL/lesson 2/Slide 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




©NIIT                                         SQL/lesson 2/Slide 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



©NIIT                                        SQL/lesson 2/Slide 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 %

©NIIT                                         SQL/lesson 2/Slide 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%'




©NIIT                                     SQL/lesson 2/Slide 6 of 52
Retrieving Result Sets

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




©NIIT                                       SQL/lesson 2/Slide 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




©NIIT                                          SQL/lesson 2/Slide 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”.




©NIIT                                          SQL/lesson 2/Slide 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.




©NIIT                                        SQL/lesson 2/Slide 10 of
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
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
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
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
Retrieving Result Sets

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




©NIIT                                       SQL/lesson 2/Slide 15 of
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
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
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
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
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
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
Retrieving Result Sets

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




©NIIT                                       SQL/lesson 2/Slide 22 of
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
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
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
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
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
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
Retrieving Result Sets

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




©NIIT                                       SQL/lesson 2/Slide 29 of
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
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
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
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
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
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
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
Retrieving Result Sets

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




©NIIT                                       SQL/lesson 2/Slide 37 of
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
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
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
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
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
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
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
Retrieving Result Sets

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




©NIIT                                      SQL/lesson 2/Slide 45 of
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
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
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
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
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
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
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

More Related Content

What's hot

FP304 DATABASE SYSTEM PAPER FINAL EXAM AGAIN
FP304 DATABASE SYSTEM  PAPER FINAL EXAM AGAINFP304 DATABASE SYSTEM  PAPER FINAL EXAM AGAIN
FP304 DATABASE SYSTEM PAPER FINAL EXAM AGAINSyahriha Ruslan
 
Dms 22319 micro project
Dms 22319 micro projectDms 22319 micro project
Dms 22319 micro projectARVIND SARDAR
 
FINAL PAPER FP304 DATABASE SYSTEM
FINAL PAPER FP304 DATABASE SYSTEMFINAL PAPER FP304 DATABASE SYSTEM
FINAL PAPER FP304 DATABASE SYSTEMAmira Dolce Farhana
 
CIS 336 Final Exam 2 (Devry)
CIS 336 Final Exam 2 (Devry) CIS 336 Final Exam 2 (Devry)
CIS 336 Final Exam 2 (Devry) critter03
 
CIS 336 Final Exam 2 (Devry)s
CIS 336 Final Exam 2 (Devry)sCIS 336 Final Exam 2 (Devry)s
CIS 336 Final Exam 2 (Devry)scritterc07
 
Cis 336 final exam 2
Cis 336 final exam 2Cis 336 final exam 2
Cis 336 final exam 2lifesgood12
 
Cis 336 final exam 2
Cis 336 final exam 2Cis 336 final exam 2
Cis 336 final exam 2prasaaanna2
 
CIS 336 Final Exam 2 (Devry)p
CIS 336 Final Exam 2 (Devry)pCIS 336 Final Exam 2 (Devry)p
CIS 336 Final Exam 2 (Devry)pcritterc07
 
CIS 336 Redefined Education--cis336.com
CIS 336 Redefined Education--cis336.comCIS 336 Redefined Education--cis336.com
CIS 336 Redefined Education--cis336.comagathachristie208
 
CIS 336 Achievement Education --cis336.com
CIS 336 Achievement Education --cis336.comCIS 336 Achievement Education --cis336.com
CIS 336 Achievement Education --cis336.comagathachristie171
 
CIS336 Education for Service--cis336.com
CIS336 Education for Service--cis336.comCIS336 Education for Service--cis336.com
CIS336 Education for Service--cis336.comwilliamwordsworth11
 
CIS 336 Inspiring Innovation -- cis336.com
CIS 336 Inspiring Innovation -- cis336.comCIS 336 Inspiring Innovation -- cis336.com
CIS 336 Inspiring Innovation -- cis336.comkopiko105
 
CIS 336 Life of the Mind/newtonhelp.com   
CIS 336 Life of the Mind/newtonhelp.com   CIS 336 Life of the Mind/newtonhelp.com   
CIS 336 Life of the Mind/newtonhelp.com   bellflower3
 
CIS 336 PAPERS Lessons in Excellence--cis336papers.com
CIS 336 PAPERS Lessons in Excellence--cis336papers.comCIS 336 PAPERS Lessons in Excellence--cis336papers.com
CIS 336 PAPERS Lessons in Excellence--cis336papers.comthomashard82
 
CIS 336 STUDY Education Begins--cis336study.com
CIS 336 STUDY Education Begins--cis336study.comCIS 336 STUDY Education Begins--cis336study.com
CIS 336 STUDY Education Begins--cis336study.comagathachristie235
 

What's hot (20)

Sql xp 07
Sql xp 07Sql xp 07
Sql xp 07
 
Sql xp 03
Sql xp 03Sql xp 03
Sql xp 03
 
FP304 DATABASE SYSTEM PAPER FINAL EXAM AGAIN
FP304 DATABASE SYSTEM  PAPER FINAL EXAM AGAINFP304 DATABASE SYSTEM  PAPER FINAL EXAM AGAIN
FP304 DATABASE SYSTEM PAPER FINAL EXAM AGAIN
 
Dms 22319 micro project
Dms 22319 micro projectDms 22319 micro project
Dms 22319 micro project
 
FINAL PAPER FP304 DATABASE SYSTEM
FINAL PAPER FP304 DATABASE SYSTEMFINAL PAPER FP304 DATABASE SYSTEM
FINAL PAPER FP304 DATABASE SYSTEM
 
CIS 336 Final Exam 2 (Devry)
CIS 336 Final Exam 2 (Devry) CIS 336 Final Exam 2 (Devry)
CIS 336 Final Exam 2 (Devry)
 
CIS 336 Final Exam 2 (Devry)s
CIS 336 Final Exam 2 (Devry)sCIS 336 Final Exam 2 (Devry)s
CIS 336 Final Exam 2 (Devry)s
 
Cis 336 final exam 2
Cis 336 final exam 2Cis 336 final exam 2
Cis 336 final exam 2
 
Cis 336 final exam 2
Cis 336 final exam 2Cis 336 final exam 2
Cis 336 final exam 2
 
CIS 336 Final Exam 2 (Devry)p
CIS 336 Final Exam 2 (Devry)pCIS 336 Final Exam 2 (Devry)p
CIS 336 Final Exam 2 (Devry)p
 
CIS 336 Redefined Education--cis336.com
CIS 336 Redefined Education--cis336.comCIS 336 Redefined Education--cis336.com
CIS 336 Redefined Education--cis336.com
 
CIS 336 Achievement Education --cis336.com
CIS 336 Achievement Education --cis336.comCIS 336 Achievement Education --cis336.com
CIS 336 Achievement Education --cis336.com
 
CIS336 Education for Service--cis336.com
CIS336 Education for Service--cis336.comCIS336 Education for Service--cis336.com
CIS336 Education for Service--cis336.com
 
Assignment#01
Assignment#01Assignment#01
Assignment#01
 
CIS 336 Inspiring Innovation -- cis336.com
CIS 336 Inspiring Innovation -- cis336.comCIS 336 Inspiring Innovation -- cis336.com
CIS 336 Inspiring Innovation -- cis336.com
 
CIS 336 Life of the Mind/newtonhelp.com   
CIS 336 Life of the Mind/newtonhelp.com   CIS 336 Life of the Mind/newtonhelp.com   
CIS 336 Life of the Mind/newtonhelp.com   
 
CIS 336 PAPERS Lessons in Excellence--cis336papers.com
CIS 336 PAPERS Lessons in Excellence--cis336papers.comCIS 336 PAPERS Lessons in Excellence--cis336papers.com
CIS 336 PAPERS Lessons in Excellence--cis336papers.com
 
CIS 336 STUDY Education Begins--cis336study.com
CIS 336 STUDY Education Begins--cis336study.comCIS 336 STUDY Education Begins--cis336study.com
CIS 336 STUDY Education Begins--cis336study.com
 
Assignment#04
Assignment#04Assignment#04
Assignment#04
 
Assignment#02
Assignment#02Assignment#02
Assignment#02
 

Similar to Sql xp 02

Intake 38 data access 3
Intake 38 data access 3Intake 38 data access 3
Intake 38 data access 3Mahmoud Ouf
 
Unit 3-Select Options and Aggregate Functions in SQL (1).pptx
Unit 3-Select Options and Aggregate Functions in SQL (1).pptxUnit 3-Select Options and Aggregate Functions in SQL (1).pptx
Unit 3-Select Options and Aggregate Functions in SQL (1).pptxHAMEEDHUSSAINBU21CSE
 
Exam viewtestmanageruserguide
Exam viewtestmanageruserguideExam viewtestmanageruserguide
Exam viewtestmanageruserguideWilliam McIntosh
 
Charles WilliamsCS362Unit 3 Discussion BoardStructured Query Langu.docx
Charles WilliamsCS362Unit 3 Discussion BoardStructured Query Langu.docxCharles WilliamsCS362Unit 3 Discussion BoardStructured Query Langu.docx
Charles WilliamsCS362Unit 3 Discussion BoardStructured Query Langu.docxchristinemaritza
 
Examview testmanager userguide 8.1
Examview testmanager userguide 8.1Examview testmanager userguide 8.1
Examview testmanager userguide 8.1William McIntosh
 
Mid term sem 2 1415 sol
Mid term sem 2 1415 solMid term sem 2 1415 sol
Mid term sem 2 1415 solIIUM
 
Oracle OCP 1Z0-007题库
Oracle OCP 1Z0-007题库Oracle OCP 1Z0-007题库
Oracle OCP 1Z0-007题库renguzi
 
student application form Java Netbeans
student application form Java Netbeansstudent application form Java Netbeans
student application form Java Netbeansreshmajohney
 
Structured Query Language for Data Management 2 Sructu.docx
Structured Query Language for Data Management      2 Sructu.docxStructured Query Language for Data Management      2 Sructu.docx
Structured Query Language for Data Management 2 Sructu.docxjohniemcm5zt
 
BCS4L1-Database Management lab.pdf
BCS4L1-Database Management lab.pdfBCS4L1-Database Management lab.pdf
BCS4L1-Database Management lab.pdfKeerthanaP37
 
The ultimate-guide-to-sql
The ultimate-guide-to-sqlThe ultimate-guide-to-sql
The ultimate-guide-to-sqlMcNamaraChiwaye
 
22316-2019-Summer-model-answer-paper.pdf
22316-2019-Summer-model-answer-paper.pdf22316-2019-Summer-model-answer-paper.pdf
22316-2019-Summer-model-answer-paper.pdfPradipShinde53
 

Similar to Sql xp 02 (20)

Sql xp 08
Sql xp 08Sql xp 08
Sql xp 08
 
1 z1 051
1 z1 0511 z1 051
1 z1 051
 
Sql xp 05
Sql xp 05Sql xp 05
Sql xp 05
 
Dbms record
Dbms recordDbms record
Dbms record
 
Intake 37 linq2
Intake 37 linq2Intake 37 linq2
Intake 37 linq2
 
Intake 38 data access 3
Intake 38 data access 3Intake 38 data access 3
Intake 38 data access 3
 
Unit 3-Select Options and Aggregate Functions in SQL (1).pptx
Unit 3-Select Options and Aggregate Functions in SQL (1).pptxUnit 3-Select Options and Aggregate Functions in SQL (1).pptx
Unit 3-Select Options and Aggregate Functions in SQL (1).pptx
 
Exam viewtestmanageruserguide
Exam viewtestmanageruserguideExam viewtestmanageruserguide
Exam viewtestmanageruserguide
 
Charles WilliamsCS362Unit 3 Discussion BoardStructured Query Langu.docx
Charles WilliamsCS362Unit 3 Discussion BoardStructured Query Langu.docxCharles WilliamsCS362Unit 3 Discussion BoardStructured Query Langu.docx
Charles WilliamsCS362Unit 3 Discussion BoardStructured Query Langu.docx
 
Examview testmanager userguide 8.1
Examview testmanager userguide 8.1Examview testmanager userguide 8.1
Examview testmanager userguide 8.1
 
Mid term sem 2 1415 sol
Mid term sem 2 1415 solMid term sem 2 1415 sol
Mid term sem 2 1415 sol
 
Oracle OCP 1Z0-007题库
Oracle OCP 1Z0-007题库Oracle OCP 1Z0-007题库
Oracle OCP 1Z0-007题库
 
SQL MCQ
SQL MCQSQL MCQ
SQL MCQ
 
student application form Java Netbeans
student application form Java Netbeansstudent application form Java Netbeans
student application form Java Netbeans
 
Structured Query Language for Data Management 2 Sructu.docx
Structured Query Language for Data Management      2 Sructu.docxStructured Query Language for Data Management      2 Sructu.docx
Structured Query Language for Data Management 2 Sructu.docx
 
BCS4L1-Database Management lab.pdf
BCS4L1-Database Management lab.pdfBCS4L1-Database Management lab.pdf
BCS4L1-Database Management lab.pdf
 
AJP
AJPAJP
AJP
 
01 basic orders
01   basic orders01   basic orders
01 basic orders
 
The ultimate-guide-to-sql
The ultimate-guide-to-sqlThe ultimate-guide-to-sql
The ultimate-guide-to-sql
 
22316-2019-Summer-model-answer-paper.pdf
22316-2019-Summer-model-answer-paper.pdf22316-2019-Summer-model-answer-paper.pdf
22316-2019-Summer-model-answer-paper.pdf
 

More from Niit Care (20)

Ajs 1 b
Ajs 1 bAjs 1 b
Ajs 1 b
 
Ajs 4 b
Ajs 4 bAjs 4 b
Ajs 4 b
 
Ajs 4 a
Ajs 4 aAjs 4 a
Ajs 4 a
 
Ajs 4 c
Ajs 4 cAjs 4 c
Ajs 4 c
 
Ajs 3 b
Ajs 3 bAjs 3 b
Ajs 3 b
 
Ajs 3 a
Ajs 3 aAjs 3 a
Ajs 3 a
 
Ajs 3 c
Ajs 3 cAjs 3 c
Ajs 3 c
 
Ajs 2 b
Ajs 2 bAjs 2 b
Ajs 2 b
 
Ajs 2 a
Ajs 2 aAjs 2 a
Ajs 2 a
 
Ajs 2 c
Ajs 2 cAjs 2 c
Ajs 2 c
 
Ajs 1 a
Ajs 1 aAjs 1 a
Ajs 1 a
 
Ajs 1 c
Ajs 1 cAjs 1 c
Ajs 1 c
 
Dacj 4 2-c
Dacj 4 2-cDacj 4 2-c
Dacj 4 2-c
 
Dacj 4 2-b
Dacj 4 2-bDacj 4 2-b
Dacj 4 2-b
 
Dacj 4 2-a
Dacj 4 2-aDacj 4 2-a
Dacj 4 2-a
 
Dacj 4 1-c
Dacj 4 1-cDacj 4 1-c
Dacj 4 1-c
 
Dacj 4 1-b
Dacj 4 1-bDacj 4 1-b
Dacj 4 1-b
 
Dacj 4 1-a
Dacj 4 1-aDacj 4 1-a
Dacj 4 1-a
 
Dacj 1-2 b
Dacj 1-2 bDacj 1-2 b
Dacj 1-2 b
 
Dacj 1-3 c
Dacj 1-3 cDacj 1-3 c
Dacj 1-3 c
 

Recently uploaded

Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 

Recently uploaded (20)

Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 

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
  • 7. Retrieving Result Sets Execute the query Action: In the Query Analyzer window, type the query Execute the query ©NIIT SQL/lesson 2/Slide 7 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
  • 15. Retrieving Result Sets Execute the query Action: In the Query Analyzer window, type the query Execute the query ©NIIT SQL/lesson 2/Slide 15 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
  • 22. Retrieving Result Sets Execute the query Action: In the Query Analyzer window, type the query Execute the query ©NIIT SQL/lesson 2/Slide 22 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
  • 29. Retrieving Result Sets Execute the query Action: In the Query Analyzer window, type: Execute the query ©NIIT SQL/lesson 2/Slide 29 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
  • 37. Retrieving Result Sets Execute the query Action: In the Query Analyzer window, type: Execute the query ©NIIT SQL/lesson 2/Slide 37 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
  • 45. Retrieving Result Sets Execute the query Action In the Query Analyzer window, type the query Execute the query ©NIIT SQL/lesson 2/Slide 45 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