Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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

2 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.

3 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

4 SQL/lesson 2/Slide 4 of 45 Retrieving Result Sets Draft the query * String Operator 3You can use the LIKE keyword to search for a string with the wildcard mechanism 3The LIKE keyword is used to select those rows that match the specified portion of character string * Result: 3 The required information is available in the Authors table 3 Since the address must have “Blonde”, and it could be prefixed or suffixed by anything, the wild card to be used is %

5 SQL/lesson 2/Slide 5 of 45 Retrieving Result Sets Draft the query (Contd.) 3 Therefore, the query using the SELECT statement should be: select * from authors where address like '%Blonde%'

6 SQL/lesson 2/Slide 6 of 45 Retrieving Result Sets Execute the query *Action: 3In the Query Analyzer window, type the query 3Execute the query

7 SQL/lesson 2/Slide 7 of 45 Retrieving Result Sets Verify that the query output is as per the required results *Check whether: 3The required columns are displayed 3All the rows that meet the condition specified in the WHERE clause are displayed

8 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”. 3Use Jobs Table of pubs.

9 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.

10 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

11 SQL/lesson 2/Slide 11 of 45 Retrieving Result Sets Draft the query *The IS NULL and IS NOT NULL Keywords 3NULL is an unknown value or a value for which data is not available 3Syntax SELECT column_list FROM table_name WHERE column_name unknown_value_operator *Result: 3 The information is available in the Titles table of pubs 3 The condition is that the royalty should be NULL

12 SQL/lesson 2/Slide 12 of 45 Retrieving Result Sets Draft the query 3 Therefore, the query using the SELECT statement should be: SELECT * FROM titles WHERE royalty IS NULL

13 SQL/lesson 2/Slide 13 of 45 Retrieving Result Sets Verify that the query output is as per the required results *Check whether: 3The required columns are displayed 3All rows that have a NULL value in the Royalty attribute are displayed

14 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. 3Use publishers table of pubs

15 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.

16 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

17 SQL/lesson 2/Slide 17 of 45 Retrieving Result Sets Draft the query *The ORDER BY Clause 3Syntax SELECT select_list FROM table_name [ORDER BY column_name [ASC|DESC ]]

18 SQL/lesson 2/Slide 18 of 45 Retrieving Result Sets Draft the query (Contd.) 3Result: ä The information is available in the Employee table ä Therefore, the query using the SELECT statement should be: SELECT * FROM employee ORDER BY fname

19 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.

20 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

21 SQL/lesson 2/Slide 21 of 45 Retrieving Result Sets Create a format for the query output *Result: 3 The output required from the query is details of the top 3 costlier Books. 3 The column headings required by the report are the attribute names of the table Titles.

22 SQL/lesson 2/Slide 22 of 45 Retrieving Result Sets Draft the query *The TOP Keyword 3The TOP clause limits the number of rows returned in the result set 3Syntax SELECT [TOP n [PERCENT]] column_name [,column_name…] FROM table_name WHERE search_conditions [ORDER BY [column_name[,column_name…]

23 SQL/lesson 2/Slide 23 of 45 Retrieving Result Sets Draft the query (Contd.) *Result: 3 Therefore, the query using the SELECT statement should be: 3 SELECT TOP 3 * FROM titles ORDER BY Price DESC

24 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

25 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. 3 Use publishers table.

26 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.

27 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

28 SQL/lesson 2/Slide 28 of 45 Retrieving Result Sets Draft the query *Aggregate Functions 3 Summarize the values for a column or a group of columns within a table for which they are applied, and produce a single value

29 SQL/lesson 2/Slide 29 of 45 Retrieving Result Sets * Result: 3 The information is available in the Authors table 3 The aggregate function to be used is COUNT 3 Therefore, the query using the SELECT statement should be: 3 select count(*) from authors where contract = 1

30 SQL/lesson 2/Slide 30 of 45 Retrieving Result Sets Grouping Result Sets *The following clauses are used to group result sets: 3GROUP BY: Summarizes the result set into groups defined in the query using aggregate functions 3 GROUP BY ALL: The ALL keyword of the GROUP BY clause is used to display all groups, including those excluded from the WHERE clause 3COMPUTE 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

31 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

32 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

33 SQL/lesson 2/Slide 33 of 45 Retrieving Result Sets Draft the query * The GROUP BY Clause 3 Syntax SELECT column_list FROM table_name WHERE condition GROUP BY expression [, expression] [HAVING search_condition]

34 SQL/lesson 2/Slide 34 of 45 Retrieving Result Sets Draft the query (Contd.) * Result: 3 The information is available in the Employee table 3 The output needs to be grouped Job_ID wise, so the GROUP BY clause has to be used

35 SQL/lesson 2/Slide 35 of 45 Retrieving Result Sets Draft the query (Contd.) 3 Therefore, the query using the SELECT statement should be: SELECT job_id, 'Total Count'=COUNT(emp_id) FROM employee Group BY job_id

36 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

37 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

38 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]...]

39 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.

40 SQL/lesson 2/Slide 40 of 45 Retrieving Result Sets *The query can be written as: select * from Titles compute sum(advance)

41 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. 3The COMPUTE BY clause can be used to calculate summary values of the result set on a group of data

42 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

43 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

44 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

45 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


Download ppt "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."

Similar presentations


Ads by Google