Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Chapter 3 Single Table Queries. 2 Simple Queries Query - a question represented in a way that the DBMS can understand Basic format SELECT-FROM Optional.

Similar presentations


Presentation on theme: "1 Chapter 3 Single Table Queries. 2 Simple Queries Query - a question represented in a way that the DBMS can understand Basic format SELECT-FROM Optional."— Presentation transcript:

1 1 Chapter 3 Single Table Queries

2 2 Simple Queries Query - a question represented in a way that the DBMS can understand Basic format SELECT-FROM Optional SELCT-FROM-WHERE

3 3 SELECT Command SELECT clause Followed by the columns to be included in the query. FROM clause followed by name of the table that contains the data to query WHERE clause (optional) followed by conditions that apply to the data to be retrieved

4 4 SELECT Command There are no special formatting rules for SQL FROM command and WHERE command should appear on separate lines to aid readability and understanding

5 5 SELECT Command to Select Certain Columns (i.e., a Projections)

6 6 SELECT Command With a Condition

7 7 Comparison Operators

8 8 Note Generally SQL is not case sensitive Exception: Character values within quotation marks Use the correct case for these values. Example: WHERE LAST ‘adams’ will not select any rows if the stored value is “Adams”

9 9 SELECT Command Involving a Comparison

10 10 Compound Conditions Compound conditions Formed by connecting two or more simple conditions Uses AND, OR, and NOT operators AND: all conditions must be true OR: any one of conditions is true NOT: reverses the truth of the original condition

11 11 SELECT Command Involving an AND Condition

12 12 SELECT Command Involving an OR Condition

13 13 SELECT Command Involving a NOT Condition

14 14 Use of BETWEEN BETWEEN operator Not an essential feature Can arrive at same answer without it using AND Does make certain SELECT commands simpler

15 15 SELECT Command Involving a BETWEEN Condition

16 16 Computed Columns Computed columns Results do not exist in the database Can be computed using data in existing columns Use arithmetic operators

17 17 SELECT Command With a Computation in the Condition

18 18 Use of LIKE LIKE operator is used when exact matches will not work Use LIKE with a wildcard symbol

19 19 Example 12 List the customer number, last name, first name, and complete address of every customer who lives on Pine; that is, whose address contains the letters “Pine”

20 20 SELECT Command With Wildcards

21 21 Wildcard Symbols Percent symbol (%) represents any collection of characters ‘%Pine%’ Underscore (_) Represents any individual character ‘T_m’

22 22 Use of IN The IN clause provides concise way of phrasing certain conditions

23 23 Example 13 List the customer number, last name, and first name for every customer with a credit limit of $1,000, $1,500, or $2,000

24 24 SELECT Command Involving an IN Condition

25 25 Sorting Generally, the order of rows is immaterial to the DBMS There is no defined order in which results are displayed Rows can displayed in the order in which they were entered

26 26 Use of ORDER BY Use the ORDER BY command to list data in a specific order The column on which data is to be sorted is called a sort key or simply key

27 27 SELECT Command to Order Rows

28 28 Sorting with Multiple Keys in Descending Order When sorting more than one column the more important column is called the major key (or primary sort key) the less important column is called the minor key (or secondary sort key) List keys in the order of importance in the ORDER BY clause Sort descending by using the DESC operator

29 29 SELECT Command With Multiple Sort Keys

30 30 Using Functions SQL has functions to calculate Sums Averages Counts Maximum values Minimum values

31 31 SQL Functions

32 32 SELECT Command to Count Rows

33 33 Count without the Asterisk SELECT COUNT(PART_NUMBER) FROM PART WHERE ITEM_CLASS = ‘HW’;

34 34 Use of the SUM Function The SUM function is used to calculate totals The column to be totaled must be specified The column to be totaled must be numeric

35 35 SELECT Command to Calculate a COUNT and a SUM

36 36 Using AVG, MAX, and MIN AVG, MAX and MIN functions are similar to the SUM SUM, AVG, MAX and MIN functions ignore (eliminate) null values Null values can cause strange results when calculated

37 37 SELECT Command With Several Functions

38 38 Nesting Queries Sometimes obtaining the results you need is a two-step process (or more).

39 39 Example 21 What is the largest credit limit given to any customer of sales rep 06?

40 40 Selecting the Maximum Credit Limit

41 41 Query Using Previous Result

42 42 Subqueries It is possible to place one query inside another Inner query is called a subquery and it is evaluated first Outer query can use the results of the subquery to find its results

43 43 Using IN and a Subquery

44 44 Query Using an EQUAL Condition and a Subquery

45 45 Note SQL will not allow the use of the condition BALANCE > AVG(BALANCE) in the WHERE clause A subquery must be used to obtain the average balance Results of the subquery can be used in the condition as shown in Figure 3.29

46 46 Grouping Grouping creates groups of rows that share some common characteristics Calculations are performed for the entire group Use the GROUP BY command

47 47 Using GROUP BY GROUP BY command allows data to be grouped in a particular order Statistics are calculated on the groups

48 48 Grouping Column

49 49 HAVING HAVING command is used for groups

50 50 Query Using a HAVING Clause

51 51 HAVING vs. WHERE WHERE clause limits rows HAVING clause limits groups

52 52 Displaying Groups That Contain More Than One Row

53 53 Restricting the Rows and Groups

54 54 Selecting Rows Containing Null Values

55 55 SQL Query Clauses and Operators


Download ppt "1 Chapter 3 Single Table Queries. 2 Simple Queries Query - a question represented in a way that the DBMS can understand Basic format SELECT-FROM Optional."

Similar presentations


Ads by Google