SQL/lesson 2/Slide 1 of 45
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
SQL/lesson 2/Slide 2 of 45
Retrieving Result Sets
Retrieving Rows Based on Pattern Matching
A author Sheryl, staying at Blonde street is to be contacted. However, there are many address that contain the word “Blonde” along with other words. To ensure that the right person is contacted, details like the names of the person, the address, and the telephone numbers of the author, which have “Blonde” in their address need to be displayed.
SQL/lesson 2/Slide 3 of 45
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
SQL/lesson 2/Slide 4 of 45
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 Authors table
Since the address must have “Blonde”, and it could be prefixed or suffixed by anything, the wild card to be used is %
SQL/lesson 2/Slide 5 of 45
Retrieving Result Sets
Draft the query (Contd.)
Therefore, the query using the SELECT statement should be:
select * from authors where address like '%Blonde%'
SQL/lesson 2/Slide 6 of 45
Retrieving Result Sets
Execute the query
Action:
In the Query Analyzer window, type the query
Execute the query
SQL/lesson 2/Slide 7 of 45
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
SQL/lesson 2/Slide 8 of 45
Retrieving Result Sets
Wait a while…
Write a query to display the details of all the Jobsjob description begin with “M”.
Use Jobs Table of pubs.
SQL/lesson 2/Slide 9 of 45
Retrieving Result Sets
Displaying Rows With Missing Values
The list of books for whom royalty is not yet decided is required.
SQL/lesson 2/Slide 10 of 45
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
SQL/lesson 2/Slide 11 of 45
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 Titles table of pubs
The condition is that the royalty should be NULL
SQL/lesson 2/Slide 12 of 45
Retrieving Result Sets
Draft the query
Therefore, the query using the SELECT statement should be:
SELECT * FROM titles WHERE royalty IS NULL
SQL/lesson 2/Slide 13 of 45
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 Royalty attribute are displayed
SQL/lesson 2/Slide 14 of 45
Retrieving Result Sets
Wait a while…
A list of publishers is required where state information must be available. Use publishers table of pubs
SQL/lesson 2/Slide 15 of 45
Retrieving Result Sets
Displaying Data in a Specific Order
A report of all employee is required as inputs for further reviewing. A report in the ascending order of the FirstName is to be generated.
SQL/lesson 2/Slide 16 of 45
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
SQL/lesson 2/Slide 17 of 45
Retrieving Result Sets
Draft the query
The ORDER BY Clause
Syntax
SELECT select_list FROM table_name [ORDER BY column_name
[ASC|DESC ] ]
SQL/lesson 2/Slide 18 of 45
Retrieving Result Sets
Draft the query (Contd.) Result:
The information is available in the Employee table Therefore, the query using the SELECT statement
should be:
SELECT * FROM employee ORDER BY fname
SQL/lesson 2/Slide 19 of 45
Retrieving Result Sets
Displaying the Top Few Rows
Based on price, the top 3 Book details have to be displayed.
SQL/lesson 2/Slide 20 of 45
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
SQL/lesson 2/Slide 21 of 45
Retrieving Result Sets
Create a format for the query output
Result:
The output required from the query is details of the top 3 costlier Books.
The column headings required by the report are the attribute names of the table Titles.
SQL/lesson 2/Slide 22 of 45
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…]
SQL/lesson 2/Slide 23 of 45
Retrieving Result Sets
Draft the query (Contd.)
Result:
Therefore, the query using the SELECT statement should be:
SELECT TOP 3 * FROM titles ORDER BY Price DESC
SQL/lesson 2/Slide 24 of 45
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
SQL/lesson 2/Slide 25 of 45
Retrieving Result Sets
Wait a while…
Write a query that displays a list of states from where publishers are staying.
Use publishers table.
SQL/lesson 2/Slide 26 of 45
Retrieving Result Sets
Displaying Aggregate Functions
The total number of authors who are having contract value as 1 is required.
SQL/lesson 2/Slide 27 of 45
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
SQL/lesson 2/Slide 28 of 45
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
SQL/lesson 2/Slide 29 of 45
Retrieving Result Sets
Result:
The information is available in the Authors table
The aggregate function to be used is COUNT
Therefore, the query using the SELECT statement should be:
select count(*) from authors where contract = 1
SQL/lesson 2/Slide 30 of 45
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
SQL/lesson 2/Slide 31 of 45
Retrieving Result Sets
Generating a Summary Report
The information of employees is required in the following format:
Job ID No. Of Employees
SQL/lesson 2/Slide 32 of 45
Retrieving Result Sets
Task List
Draft the query
Execute the query
Verify that the query output is as per the required results
SQL/lesson 2/Slide 33 of 45
Retrieving Result Sets
Draft the query
The GROUP BY Clause
SyntaxSELECT column_listFROM table_nameWHERE condition GROUP BY expression [,
expression][HAVING search_condition]
SQL/lesson 2/Slide 34 of 45
Retrieving Result Sets
Draft the query (Contd.)
Result:
The information is available in the Employee table
The output needs to be grouped Job_ID wise, so the GROUP BY clause has to be used
SQL/lesson 2/Slide 35 of 45
Retrieving Result Sets
Draft the query (Contd.)
Therefore, the query using the SELECT statement should be:
SELECT job_id, 'Total Count'=COUNT(emp_id) FROM employee Group BY job_id
SQL/lesson 2/Slide 36 of 45
Retrieving Result Sets
WHERE CLAUSE IN GROUP BY
Example
SELECT job_id, 'Total Count'=COUNT(emp_id) FROM employee WHERE job_lvl IN (100,200) Group BY job_id
SQL/lesson 2/Slide 37 of 45
Retrieving Result Sets
The HAVING keyword in the SELECT query can be used to select rows from the intermediate result set
SELECT job_id, 'Total Count'=COUNT(emp_id) FROM employee Group BY job_id HAVING COUNT(job_id) >= 4
SQL/lesson 2/Slide 38 of 45
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]...]
SQL/lesson 2/Slide 39 of 45
Retrieving Result Sets
A list of all the books from the titles table is to be displayed along with the sum of advance.
The COMPUTE clause with the SELECT statement will have to be used.
SQL/lesson 2/Slide 40 of 45
Retrieving Result Sets
The query can be written as:
select * from Titles compute sum(advance)
SQL/lesson 2/Slide 41 of 45
Retrieving Result Sets
A list of all the books from the titles table is to be displayed along with the sum of advance for each publisher id. The COMPUTE BY clause can be used to calculate
summary values of the result set on a group of data
SQL/lesson 2/Slide 42 of 45
Retrieving Result Sets
The query can be written as:
SELECT * FROM Titles ORDER BY pub_id COMPUTE SUM(advance)BY pub_id
SQL/lesson 2/Slide 43 of 45
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
SQL/lesson 2/Slide 44 of 45
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
SQL/lesson 2/Slide 45 of 45
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