a guide to sql, sixth edition 1 chapter 3 single-table queries

35
1 A Guide to SQL, Sixth Edition Chapter 3 Single-Table Queries

Upload: eunice-woods

Post on 27-Dec-2015

216 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: A Guide to SQL, Sixth Edition 1 Chapter 3 Single-Table Queries

1

A Guide to SQL, Sixth Edition

Chapter 3Single-Table Queries

Page 2: A Guide to SQL, Sixth Edition 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

Page 3: A Guide to SQL, Sixth Edition 1 Chapter 3 Single-Table Queries

3

ObjectivesSort data using multiple keys and in ascending and descending orderUse SQL aggregate functionsUse subqueriesGroup data using the GROUP BY clauseSelect individual groups of data using the HAVING clauseRetrieve columns with null values

Page 4: A Guide to SQL, Sixth Edition 1 Chapter 3 Single-Table Queries

4

Simple Queries

Query - a question represented in a way that the DBMS can understand

Basic formatSELECT-FROM

OptionalSELECT-FROM-WHERE

Page 5: A Guide to SQL, Sixth Edition 1 Chapter 3 Single-Table Queries

5

SELECT Command

SELECT clause 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

Page 6: A Guide to SQL, Sixth Edition 1 Chapter 3 Single-Table Queries

6

Retrieving Certain Columns and All Rows

List columns to include in the SELECT clause and the table name in the FROM clause

Page 7: A Guide to SQL, Sixth Edition 1 Chapter 3 Single-Table Queries

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

Page 8: A Guide to SQL, Sixth Edition 1 Chapter 3 Single-Table Queries

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

Page 9: A Guide to SQL, Sixth Edition 1 Chapter 3 Single-Table Queries

9

Comparison Operators

Page 10: A Guide to SQL, Sixth Edition 1 Chapter 3 Single-Table Queries

10

Note

Generally SQL is not case-sensitive

Exception: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”

Page 11: A Guide to SQL, Sixth Edition 1 Chapter 3 Single-Table Queries

11

Compound Conditions

Compound conditionsFormed by connecting two or more simple

conditionsUses of AND, OR, and NOT operators

AND: all conditions must be trueOR: any one of the conditions is trueNOT: reverses the truth of the original condition

Page 12: A Guide to SQL, Sixth Edition 1 Chapter 3 Single-Table Queries

12

Use of BETWEEN

BETWEEN operatorNot an essential featureCan arrive at same answer without it using

ANDDoes make certain SELECT commands

simpler

Page 13: A Guide to SQL, Sixth Edition 1 Chapter 3 Single-Table Queries

13

Computed Columns

Computed columnsDo not exist in the databaseCan be computed using data in existing

columnsUse arithmetic operators

Page 14: A Guide to SQL, Sixth Edition 1 Chapter 3 Single-Table Queries

14

Arithmetic Operators

Page 15: A Guide to SQL, Sixth Edition 1 Chapter 3 Single-Table Queries

15

Use of LIKE

LIKE operator is used when exact matches will not work

Use LIKE with a wildcard symbol

Page 16: A Guide to SQL, Sixth Edition 1 Chapter 3 Single-Table Queries

16

Wildcard Symbols

Percent symbol (%) represents any collection of characters ‘%Pine%’

Underscore (_)Represents any individual character ‘T_m’

Page 17: A Guide to SQL, Sixth Edition 1 Chapter 3 Single-Table Queries

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

Page 18: A Guide to SQL, Sixth Edition 1 Chapter 3 Single-Table Queries

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

Page 19: A Guide to SQL, Sixth Edition 1 Chapter 3 Single-Table Queries

19

Use of ORDER BY

Use the ORDER BY command to list data in a specific orderThe 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 keyIf a sort order is not specified, the default is ascending

Page 20: A Guide to SQL, Sixth Edition 1 Chapter 3 Single-Table Queries

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

Page 21: A Guide to SQL, Sixth Edition 1 Chapter 3 Single-Table Queries

21

Using Functions

SQL has special functions, called aggregate functions, to calculate:SumsAveragesCountsMaximum valuesMinimum values

Page 22: A Guide to SQL, Sixth Edition 1 Chapter 3 Single-Table Queries

22

SQL Functions

Page 23: A Guide to SQL, Sixth Edition 1 Chapter 3 Single-Table Queries

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

Page 24: A Guide to SQL, Sixth Edition 1 Chapter 3 Single-Table Queries

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

Page 25: A Guide to SQL, Sixth Edition 1 Chapter 3 Single-Table Queries

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 valuesNull values can cause strange results

when calculated

Page 26: A Guide to SQL, Sixth Edition 1 Chapter 3 Single-Table Queries

26

Use of DISTINCT

DISTINCT operator is not a function

Useful when used in conjunction with COUNT function

Page 27: A Guide to SQL, Sixth Edition 1 Chapter 3 Single-Table Queries

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 APList the order numbers that contain an

order line for a part in class AP

Page 28: A Guide to SQL, Sixth Edition 1 Chapter 3 Single-Table Queries

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

Page 29: A Guide to SQL, Sixth Edition 1 Chapter 3 Single-Table Queries

29

Grouping

Grouping creates groups of rows that share some common characteristics

Calculations are performed for the entire group

Use the GROUP BY command

Page 30: A Guide to SQL, Sixth Edition 1 Chapter 3 Single-Table Queries

30

Using GROUP BY

GROUP BY command allows data to be grouped in a particular order

Statistics are calculated on the groups

Page 31: A Guide to SQL, Sixth Edition 1 Chapter 3 Single-Table Queries

31

Using a HAVING Clause

The HAVING clause is used to restrict the groups that will be included

HAVING vs. WHEREWHERE clause limits rowsHAVING clause limits groups

Page 32: A Guide to SQL, Sixth Edition 1 Chapter 3 Single-Table Queries

32

NullsSometimes 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

Page 33: A Guide to SQL, Sixth Edition 1 Chapter 3 Single-Table Queries

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

Page 34: A Guide to SQL, Sixth Edition 1 Chapter 3 Single-Table Queries

34

SummaryUse 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

Page 35: A Guide to SQL, Sixth Edition 1 Chapter 3 Single-Table Queries

35

SQL Project Three Completed

Good Luck

H. Zamanzadeh