Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3 Single-Table Queries

Similar presentations


Presentation on theme: "Chapter 3 Single-Table Queries"— Presentation transcript:

1 Chapter 3 Single-Table Queries

2 Objectives Retrieve data from a database using SQL commands
Use compound conditions Use computed columns Use the SQL LIKE operator Use the SQL IN operator Sort data using the ORDER BY clause

3 Objectives Sort data using multiple keys and in ascending and descending order Use SQL aggregate functions Use subqueries Group data using the GROUP BY clause Select individual groups of data using the HAVING clause Retrieve columns with null values

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

5 SELECT Command SELECT clause FROM clause WHERE clause (optional)
Followed by 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

6 Retrieving Certain Columns and All Rows
List columns to include in the SELECT clause and the table name in the FROM clause

7 Retrieving All Columns and All Rows
An asterisk (*) indicates that you want to include all columns The result shows all columns in the order in which they were described to the system when the table was created

8 Using a WHERE Clause The WHERE clause retrieves rows that satisfy some condition A simple condition has the form: Column name, comparison operator, either another column name or a value

9 Comparison Operators

10 Note Generally SQL is not case-sensitive Exception: Example:
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”

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

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

13 Computed Columns Computed columns Do not exist in the database
Can be computed using data in existing columns Use arithmetic operators

14 Arithmetic Operators

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

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

17 Use of IN The IN clause provides a concise way of phrasing certain conditions The IN clause consists of the IN operator followed by a collection of values

18 Sorting Generally, the order of rows is immaterial to the DBMS
There is no defined order in which results are displayed In SQL, the results order can be specified by using the ORDER BY clause

19 Use of ORDER BY Use the ORDER BY command to list data in a specific order The ORDER BY clause is followed by the sort key The column on which data is to be sorted is called a sort key or simply key If a sort order is not specified, the default is ascending

20 Additional Sorting Options
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 in a descending order by using the DESC operator

21 Using Functions SQL has special functions, called aggregate functions, to calculate: Sums Averages Counts Maximum values Minimum values

22 SQL Functions

23 Use of COUNT Function Count function counts the number of rows in a table The specific row to be counted is not important because each count should provide the same answer Most implementations of SQL use the asterisk (*) to represent any column

24 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

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

26 Use of DISTINCT DISTINCT operator is not a function
Useful when used in conjunction with COUNT function

27 Nesting Queries Sometimes obtaining the results you need is a two-step process (or more) Problem: List the number of each part in class AP List the order numbers that contain an order line for a part in class AP

28 Subqueries It is possible to place one query inside another
An inner query is called a subquery and it is evaluated first An outer query can use the results of the subquery to find its results

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

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

31 Using a HAVING Clause The HAVING clause is used to restrict the groups that will be included HAVING vs. WHERE WHERE clause limits rows HAVING clause limits groups

32 Nulls Sometimes a condition involves a column that can be null
Problem: List the number and name of each customer with a null (unknown) street value The correct format of the condition to use for this problem is STREET IS NULL To select a customer whose street is not null, the condition STREET IS NOT NULL is used

33 Summary The basic form of the SQL SELECT command is SELECT-FROM-WHERE
You can form compound conditions by combining simple conditions Use the BETWEEN operator to indicate a range of values in a condition Use the LIKE operator to check for a value in a character column that is similar to a particular string of characters

34 Summary Use the IN operator to determine whether a column contains one of a particular set of values Use an ORDER BY clause to sort data SQL processes the aggregate functions COUNT, SUM, AVG, MAX, and MIN When one SQL query is placed inside another, it is called a subquery Use a GROUP BY clause to group data Use a HAVING clause to restrict the output to certain groups

35 SQL Project Three Completed
Good Luck H. Zamanzadeh


Download ppt "Chapter 3 Single-Table Queries"

Similar presentations


Ads by Google