Top Banner
Structured Query Language (SQL)
37

Structured Query Language (SQL)

Feb 23, 2016

Download

Documents

Ghalib

Structured Query Language (SQL). An example of the StayHome video rental database:. Row selection (WHERE clause). To restrict the rows that are retrieved The five basic search conditions (or predicates ) are as follows: - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Structured Query Language  (SQL)

Structured Query Language

(SQL)

Page 2: Structured Query Language  (SQL)

An example of the StayHome video rental database:

Page 3: Structured Query Language  (SQL)
Page 4: Structured Query Language  (SQL)

Row selection (WHERE clause)• To restrict the rows that are retrieved• The five basic search conditions (or predicates) are as

follows:– Comparison: compare the value of one expression to the value

of another expression;– Range: test whether the value of an expression falls within a

specified range of values;– Set membership: test whether the value of an expression equals

one of a set of values;– Pattern match: test whether a string matches a specified pattern;– Null: test whether a column has a null (unknown) value.

Page 5: Structured Query Language  (SQL)

Comparison search condition

• In SQL, the following simple comparison operators are available:= equals < > is not equal to< is less than <= is less than or equal to> is greater than >= is greater than or equal to

• More complex predicates can be generated using the logical operators AND, OR, and NOT, with parentheses (if needed or desired) to show the order of evaluation.

• The rules for evaluating a conditional expression are:– an expression is evaluated left to right;– subexpressions in parentheses are evaluated first;– NOTs are evaluated before ANDs and ORs;– ANDs are evaluated before ORs.

• The use of parentheses is always recommended to remove any possible ambiguities.

Page 6: Structured Query Language  (SQL)

List all staff with a salary greater than $40,000 :

Page 7: Structured Query Language  (SQL)

List all staff with a salary greater than $40,000 :

Page 8: Structured Query Language  (SQL)

• List all staff with a salary between $45,000 and $50,000.

Page 9: Structured Query Language  (SQL)

List all staff with a salary between $45,000 and $50,000.

Page 10: Structured Query Language  (SQL)

Range search condition (BETWEEN/NOT

BETWEEN)

Page 11: Structured Query Language  (SQL)

List all videos in the Action or Children categories.

Page 12: Structured Query Language  (SQL)

List all videos in the Action or Children categories.

Page 13: Structured Query Language  (SQL)

Set membership searchcondition

(IN/NOT IN)

Page 14: Structured Query Language  (SQL)

Pattern match search condition

(LIKE/NOT LIKE)• SQL has two special pattern-matching symbols:

– % percent character represents any sequence of zero or more characters (wildcard);

– _ underscore character represents any single character.

Page 15: Structured Query Language  (SQL)

For example:• name LIKE ‘S%’ means the first character must be S, but

the rest of the string can be anything.• name LIKE ‘S_ _ _ _’ means that there must be exactly

four characters in the string, the first of which must be an S.

• name LIKE ‘%S’ means any sequence of characters, of length at least 1, with the last character an S.

• name LIKE ‘%Sally%’ means a sequence of characters of any length containing Sally.

• name NOT LIKE ‘S%’ means the first character cannot be an S.

Page 16: Structured Query Language  (SQL)

List all staff whose first name is ‘Sally’.

Page 17: Structured Query Language  (SQL)

List all staff whose first name is ‘Sally’.

Page 18: Structured Query Language  (SQL)

NULL search condition (IS NULL/IS NOT NULL)

List the video rentals that have not yet been returned.

Page 19: Structured Query Language  (SQL)

List the video rentals that have not yet been returned.

Page 20: Structured Query Language  (SQL)

Sorting results (ORDER BY clause)

• The ORDER BY clause consists of a list of column names that the result is to be sorted on, separated by commas.

• The ORDER BY clause allows the retrieved rows to be ordered in ascending (ASC) or descending (DESC) order on any column or combination of columns, regardless of whether that column appears in the result.

Page 21: Structured Query Language  (SQL)

List all videos sorted in descending order of price.

Page 22: Structured Query Language  (SQL)

List all videos sorted in descending order of price.

Page 23: Structured Query Language  (SQL)

Using the SQL aggregate functions

• Aggregate functions ~ built-in function:1. COUNT Returns the number of values in a

specified column.2. SUM Returns the sum of the values in a

specified column.3. AVG Returns the average of the values in a

specified column.4. MIN Returns the minimum value in a specified

column.5. MAX Returns the maximum value in a specified

column.

Page 24: Structured Query Language  (SQL)

• These functions operate on a single column of a table and return a single value.

• Aggregate function can be used only in the SELECT list and in the HAVING clause.

• COUNT, MIN, and MAX apply to both numeric and non-numeric fields

• SUM and AVG may be used on numeric fields only.

Page 25: Structured Query Language  (SQL)

• Each function eliminates nulls first and operates only on the remaining nonnull values.

• COUNT(*) is a special use of COUNT, which counts all the rows of a table, regardless of whether nulls or duplicate values occur.

• To eliminate duplicates before the function is applied, use the keyword DISTINCT before the column name in the function.

Page 26: Structured Query Language  (SQL)

• DISTINCT has no effect with the MIN and MAX functions.

• DISTINCT can be specified only once in a query.

• If the SELECT list includes an aggregate function and no GROUP BY clause is being used to group data together, then no item in the SELECT list can include any reference to a column unless that column is the argument to an aggregate function.

• For example, the following query is illegal:SELECT staffNo, COUNT(salary)FROM Staff;

because the query does not have a GROUP BY clause and the column staffNo in the SELECT list is used outside an aggregate function.

Page 27: Structured Query Language  (SQL)

Use of COUNT and SUM

List the total number of staff with a salary greater than $40,000 and the sum of their salaries.

Page 28: Structured Query Language  (SQL)

List the total number of staff with a salary greater than $40,000 and the sum of their salaries.

Page 29: Structured Query Language  (SQL)

Use of MIN, MAX, and AVG

List the minimum, maximum, and average staff salary.

Page 30: Structured Query Language  (SQL)

List the minimum, maximum, and average staff salary.

Page 31: Structured Query Language  (SQL)

Grouping results (GROUP BY clause)

• A query that includes the GROUP BY clause is called a grouped query, because it groups the data from the SELECT table(s) and produces a single summary row for each group.

• The columns named in the GROUP BY clause are called the grouping columns.

• The SELECT clause and the GROUP BY clause are closely integrated.

• When GROUP BY is used, each item in the SELECT list must be single-valued per group.

• the SELECT clause may contain only:– column names,– aggregate functions,– constants,– an expression involving combinations of the above.

Page 32: Structured Query Language  (SQL)

• All column names in the SELECT list must appear in the GROUP BY clause unless the name is used only in an aggregate function.

• There may be column names in the GROUP BY clause that do not appear in the SELECT list.

• When the WHERE clause is used with GROUP BY, the WHERE clause is applied first, then groups are formed from the remaining rows that satisfy the search condition.

Page 33: Structured Query Language  (SQL)

Use of GROUP BY

Find the number of staff working in each branch and the sum of their salaries.

Page 34: Structured Query Language  (SQL)

Find the number of staff working in each branch and the sum of their salaries.

Page 35: Structured Query Language  (SQL)

Restricting groupings (HAVING clause)

• The HAVING clause is designed for use with the GROUP BY clause to restrict the groups that appear in the final result table

• The WHERE clause filters individual rows going into the final result table, whereas HAVING filters groups going into the final result table.

• Column names used in the HAVING clause must also appear in the GROUP BY list or be contained within an aggregate function.

• The search condition in the HAVING clause always includes at least one aggregate function, otherwise the search condition could be moved to the WHERE clause and applied to individual rows.

Page 36: Structured Query Language  (SQL)

Use of HAVING

For each branch office with more than one member of staff, find the number of staff working in each branch and the sum of their salaries.

Page 37: Structured Query Language  (SQL)

For each branch office with more than one member of staff, find the number of staff working in each branch and the sum of their salaries.