structured query language

34
4.1 Structured Query Language

Upload: tass

Post on 05-Jan-2016

40 views

Category:

Documents


0 download

DESCRIPTION

Structured Query Language. Basic Structure. SQL is based on set and relational operations with certain modifications and enhancements. A typical SQL query has the form: SELECT A 1 , A 2 , ..., A n FROM r 1 , r 2 , ..., r m WHERE P; A i s represent attributes - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Structured Query Language

4.1

Structured Query Language

Page 2: Structured Query Language

2

Basic Structure

SQL is based on set and relational operations with certain modifications and enhancements.

A typical SQL query has the form:SELECT A1, A2, ..., An

FROM r1, r2, ..., rm

WHERE P;

Ais represent attributes

ris represent relations

P is a predicate.

This query is equivalent to the relational algebra expression.

A1, A2, ..., An(P (r1 x r2 x ... x rm))

The result of an SQL query is a relation.

Page 3: Structured Query Language

3

The SELECT Clause

The SELECT clause corresponds to the projection operation of the relational algebra. It is used to list the attributes desired in the result of a query.

Query: Retrieve the SSN values of the employees.

SELECT SSN

FROM EMPLOYEE;

In the “pure” relational algebra syntax, the query would be:

SSN(EMPLOYEE)

An asterisk in the select clause denotes “all attributes”

SELECT *

FROM EMPLOYEE;

Page 4: Structured Query Language

4

The SELECT Clause (Cont.)

SQL allows duplicates in relations as well as in query results.

To force the elimination of duplicates, insert the keyword DISTINCT after SELECT.

Query: Retrieve all distinct salary of employees.

SELECT DISTINCT SALARYFROM EMPLOYEE;

The keyword ALL specifies that duplicates not be removed.

Query: Retrieve the salary of every employee.

SELECT ALL SALARYFROM EMPLOYEE;

Page 5: Structured Query Language

5

The SELECT Clause (Cont.)

The SELECT clause can contain arithmetic expressions involving the operation, +, –, , and /, and operating on constants or attributes of tuples.

Query: Show the resulting salaries if every employee is given a 10 percent raise.

SELECT FNAME, LNAME, 1.1*SALARYFROM EMPLOYEE;

would return the names of all employees and the attribute SALARY is multiplied by 110%.

Page 6: Structured Query Language

6

The WHERE ClauseThe WHERE clause corresponds to the selection predicate of the relational algebra. If consists of a predicate involving attributes of the relations that appear in the FROM clause.

Query: Retrieve the birth date and address of the employee(s) whose name is ‘John B. Smith’.

SELECT BDATE, ADDRESSFROM EMPLOYEEWHERE FNAME = ‘John’ AND MINIT = ‘B’ AND

LNAME = ‘Smith’;

Comparison results can be combined using the logical connectives AND, OR, and NOT. Comparisons can be applied to results of arithmetic expressions.

Page 7: Structured Query Language

7

The WHERE Clause (Cont.)

SQL includes a BETWEEN comparison operator in order to simplify WHERE clauses that specify that a value be less than or equal to some value and greater than or equal to some other value.

Query: Retrieve all employees in department 5 whose salary is between $30000 and $40000.

SELECT *FROM EMPLOYEEWHERE (SALARY BETWEEN 30000 AND

40000) AND DNO = 5;

The condition (SALARY BETWEEN 30000 AND 40000) in the above query is equivalent to the condition (SALARY >= 30000 AND SALARY <= 40000).

Page 8: Structured Query Language

8

The FROM Clause

The FROM clause corresponds to the Cartesian product operation of the relational algebra. It lists the relations to be scanned in the evaluation of the expression.

Query: Find the Cartesian product EMPLOYEE X DEPARTMENT.SELECT FROM EMPLOYEE, DEPARTMENT;

Query: Retrieve the name and address of all employees who work for the ‘Research’ department.

SELECT FNAME, LNAME, ADDRESSFROM EMPLOYEE, DEPARTMENTWHERE DNAME=‘Research’ AND DNUMBER=DNO;

The above query is similar to a SELECT-PROJECT-JOIN sequence of relational algebra where the condition DNAME=‘Research’ is a selection condition and DNUMBER=DNO is a join condition.

Page 9: Structured Query Language

9

The RENAME Operation

The SQL allows renaming relations and attributes using the AS clause:

old-name AS new-name

Query: Show the resulting salaries if every employee is given a 10 percent raise.

SELECT FNAME, LNAME,

1.1*SALARY AS INCREASED_SALARY

FROM EMPLOYEE;

Page 10: Structured Query Language

10

Tuple Variables

Tuple variables are defined in the FROM clause via the use of the AS clause.

Query: For each employee, retrieve the employee’s first and last name and the first and last name of his or her immediate supervisor.

SELECT E.FNAME, E.LNAME, S.FNAME, S.LNAMEFROM EMPLOYEE AS E, EMPLOYEE AS SWHERE E.SUPERSSN=S.SSN;

We can think of E and S as two different copies of the EMPLOYEE relation. It is possible to rename the relation attributes:

EMPLOYEE AS E(FN, MI, LN, SSN, BD, ADDR, SEX, SAL, SSN, DNO)

Page 11: Structured Query Language

11

String Operations

SQL includes a string-matching operator for comparisons on character strings. Patterns are described using two special characters:

percent (%). The % character matches any substring.

underscore (_). The _ character matches any character.

Query: Retrieve all employees whose address is in Houston, Texas.

SELECT FNAME, LNAMEFROM EMPLOYEEWHERE ADDRESS LIKE ‘%Houston, TX

%’;

Query: Find all employees who were born during the 1950s.

SELECT FNAME, LNAMEFROM EMPLOYEEWHERE BDATE LIKE ‘195_ _ _ _ _ _ _’;

Page 12: Structured Query Language

12

String Operations

Match the name ‘AB_CD%EF’

LIKE ‘AB\_CD\%EF’ ESCAPE ‘\’

SQL supports a variety of string operations such as

concatenation (using “||”)

converting from upper to lower case (and vice versa)

finding string length, extracting substrings, etc.

Page 13: Structured Query Language

13

Ordering the Display of Tuples

Query: Retrieve a list of employees and the projects they are working on, ordered by department and within each department, ordered alphabetically by last name, first name.

SELECT DISTINCT DNAME, LNAME, FNAME, PNAMEFROM DEPARTMENT, EMPLOYEE, WORKS_ON, PROJECTWHERE DNUMBER=DNO AND SSN=ESSN AND PNO=PNUMBERORDER BY DNAME, LNAME, FNAME;

We may specify DESC for descending order or ASC for ascending order, for each attribute; ascending order is the default.

E.g. ORDER BY DNAME DESC, LNAME ASC, FNAME ASC

Page 14: Structured Query Language

14

Set Operations

The set operations UNION, INTERSECT, and EXCEPT operate on relations and correspond to the relational algebra operations Each of the above operations automatically eliminates duplicates; to retain all duplicates use the corresponding multiset versions UNION ALL, INTERSECT ALL and EXCEPT ALL.

Suppose a tuple occurs m times in r and n times in s, then, it occurs:

m + n times in r UNION ALL s

min(m,n) times in r INTERSECT ALL s

max(0, m – n) times in r EXCEPT ALL s

Page 15: Structured Query Language

15

Set Operations

Query: Make a list of all project numbers for projects that involve an employee whose last name is ‘Smith’, either as a worker or as a manager of the department that controls the project.

(SELECT DISTINCT PNUMBER FROM PROJECT, DEPARTMENT, EMPLOYEE WHERE DNUM=DNUMBER AND MGRSSN=SSN AND

LNAME=‘Smith’);UNION

(SELECT DISTINCT PNUMBER FROM PROJECT, WORKS_ON, EMPLOYEE WHERE PNUMBER=PNO AND ESSN=SSN AND

LNAME=‘Smith’);

Page 16: Structured Query Language

16

Aggregate Functions

These functions operate on the multiset of values of a column of a relation, and return a value.

AVG: average valueMIN: minimum valueMAX: maximum valueSUM: sum of valuesCOUNT: number of values

Page 17: Structured Query Language

17

Aggregate Functions (Cont.)

Query: Find the sum of the salaries of all employees, the maximum salary, the minimum salary, and the average.SELECT SUM(SALARY), MAX(SALARY), MIN(SALARY),

AVG(SALARY), FROM EMPLOYEE;

Query: Retrieve the total number of employees in the research department SELECT COUNT (*)FROM EMPLOYEE, DEPARTMENTWHERE DNO=DNUMBER AND DNAME=‘Research’;

Query: Count the number of distinct salary values in the database.SELECT COUNT (DISTINCT SALARY)FROM EMPLOYEE;

Page 18: Structured Query Language

18

Aggregate Functions – GROUP BY

Query: For each department, retrieve the department number, the number of employees in the department and their salary.

SELECT DNO, COUNT(*), AVG(SALARY)FROM EMPLOYEEGROUP BY DNO;

Note: Attributes in SELECT clause outside of aggregate functions must appear in GROUP BY list.

Query: For each project retrieve the project number, the project name, and the number of employees who work on that project.

SELECT PNUMBER, PNAME, COUNT(*)FROM PROJECT, WORKS_ONWHERE PNUMBER=PNOGROUP BY PNUMBER, PNAME;

Page 19: Structured Query Language

19

Aggregate Functions – HAVING Clause

Query: For each project on which more than two employees work, retrieve the project number, the project name, and the number of employees who work on the project.

SELECT PNUMBER, PNAME, COUNT(*)FROM PROJECT, WORKS_ONWHERE PNUMBER=PNOGROUP BY PNUMBER, PNAMEHAVING COUNT(*) > 2;

Note: predicates in the HAVING clause are applied after the formation of groups whereas predicates in the WHERE clause are applied before forming groups.

Page 20: Structured Query Language

20

NULL Values

It is possible for tuples to have a null value, denoted by NULL, for some of their attributes.NULL signifies an unknown value or that a value does not exist.The predicate is NULL can be used to check for null values.

Query: Retrieve the names of all employees who do not have supervisors.

SELECT FNAME, LNAMEFROM EMPLOYEEWHERE SUPERSSN is NULL;

The result of any arithmetic expression involving NULL is NULLE.g. 5 + NULL returns NULL.

All aggregate operations except COUNT(*) ignore tuples with null values on the aggregated attributes.

Page 21: Structured Query Language

21

NULL Values and Three Valued Logic

Any comparison with NULL returns UNKNOWNE.g. 5 < NULL or NULL <> NULL or NULL = NULL

Three-valued logic using the truth value UNKNOWN:OR: (UNKNOWN OR TRUE) = TRUE,

(UNKNOWN OR FALSE) = UNKNOWN (UNKNOWN OR UNKNOWN) = UNKNOWNAND: (TRUE AND UNKNOWN) = UNKNOWN,

(FALSE AND UNKNOWN) = FALSE, (UNKNOWN AND UNKNOWN) = UNKNOWNNOT: (NOT UNKNOWN) = UNKNOWN“P is UNKNOWN” evaluates to true if predicate P evaluates to UNKNOWN

Result of WHERE clause predicate is treated as FALSE if it evaluates to UNKNOWN.

Page 22: Structured Query Language

22

Nested Subqueries

SQL provides a mechanism for the nesting of subqueries.

A subquery is a SELECT-FROM-WHERE expression that is nested within another query.

A common use of subqueries is to perform tests for set membership, set comparisons, and set cardinality.

Page 23: Structured Query Language

23

Example Query

Query 1: Retrieve the name and address of all employees who work for the 'Research' department.

SELECT FNAME, LNAME, ADDRESSFROM EMPLOYEEWHERE DNO IN (SELECT DNUMBER

FROM DEPARTMENT WHERE

DNAME='Research' );

The nested query selects the number of the 'Research' department.

The outer query select an EMPLOYEE tuple if its DNO value is in the result of either nested query.

The comparison operator IN compares a value v with a set of values V, and evaluates to TRUE if v is one of the elements in V.

Page 24: Structured Query Language

24

Example Query

Query: Find the SSN of all employees who work the same (project, hours) combination on some project that employee whose SSN=‘123456789’ works on.

SELECT DISTINCT ESSNFROM WORKS_ONWHERE (PNO, HOURS) IN (SELECT PNO, HOURS

FROM WORKS_ON

WHERE SSN=‘123456789’);

Page 25: Structured Query Language

25

Nested Subqueries

Correlated nested queries:

If a condition in the WHERE-clause of a nested query references an attribute of a relation declared in the outer query , the two queries are said to be correlated.

The result of a correlated nested query is different for each tuple (or combination of tuples) of the relation(s) the outer query.

Page 26: Structured Query Language

26

Example Query

Query: Retrieve the name of each employee who has a dependent with the same first name as the employee.

SELECT E.FNAME, E.LNAMEFROM EMPLOYEE AS EWHERE E.SSN IN (SELECT ESSN

FROM DEPENDENT WHERE ESSN=E.SSN AND

E.FNAME=DEPENDENT_NAME);

The nested query has a different result for each tuple in the outer query.

Page 27: Structured Query Language

27

Example Query

A query written with nested SELECT... FROM... WHERE... blocks and using the = or IN comparison operators can always be expressed as a single block query. The formulation above is simply to illustrate SQL features. For example, the previous query can be written in a much simpler manner:

SELECT E.FNAME, E.LNAME

FROM EMPLOYEE E, DEPENDENT D

WHERE E.SSN=D.ESSN ANDE.FNAME=D.DEPENDENT_NAME;

Page 28: Structured Query Language

28

Definition of SOME ClauseF <comp> SOME r t r s.t. (F <comp> t)Where <comp> can be:

056

(5< SOME ) = true

05

0

) = false

5

05(5 SOME ) = true (since 0 5)

(5< SOME

) = true(5 = SOME

(= SOME) INHowever, ( SOME) NOT IN

(read: 5 < some tuple in the relation)

Page 29: Structured Query Language

29

Definition of ALL Clause

F <comp> ALL r t r (F <comp> t)

056

(5< ALL ) = false

610

4

) = true

5

46(5 ALL

(5< ALL

) = false(5 = ALL

( ALL) NOT INHowever, (= ALL) IN

) = true (since 5 4 and 5 6)

Page 30: Structured Query Language

30

Set Comparison

Query: Return the names of employees whose salary is greater than the salary of all the employees in department 5.

SELECT LNAME, FNAMEFROM EMPLOYEEWHERE SALARY > ALL (SELECT SALARY FROM EMPLOYEE WHERE DNO = 5);

Page 31: Structured Query Language

31

Test for Empty Relations

The EXISTS construct returns the value TRUE if the argument subquery is nonempty.

EXISTS r r Ø

NOT EXISTS r r = Ø

Page 32: Structured Query Language

32

Example Query

Query: Retrieve the name of each employee who has a dependent with the first name as the employee.

SELECT E.LNAME, E.FNAMEFROM EMPLOYEE AS EWHERE EXISTS (SELECT * FROM DEPENDENT

WHERE E.SSN = ESSN AND

E.FNAME=DEPENDENT_NAME);

Query: Retrieve the name of each employee who has no dependent.SELECT E.LNAME, E.FNAMEFROM EMPLOYEE AS EWHERE NOT EXISTS (SELECT * FROM DEPENDENT

WHERE E.SSN = ESSN);

Page 33: Structured Query Language

33

Example Query

Query: Retrieve the name of each employee who works on all the projects controlled by department 5.

SELECT FNAME, LNAMEFROM EMPLOYEEWHERE NOT EXISTS (

(SELECT PNUMBER FROM PROJECT WHERE DNUM=5

EXCEPT(SELECT PNO FROM WORKS_ON WHERE SSN=ESSN));

Note that X – Y = Ø X YNote: Cannot write this query using = ALL and its variants.

Page 34: Structured Query Language

34

Summary of SQL QueriesA query in SQL can consist of up to six clauses, but only the first two, SELECT and FROM, are mandatory. The clauses are specified in the following order:

SELECT <attribute list>FROM <table list>[WHERE <condition>][GROUP BY <grouping attribute(s)>][HAVING <group condition>][ORDER BY <attribute list>]

The SELECT-clause lists the attributes or functions to be retrievedThe FROM-clause specifies all relations (or aliases) needed in the query but not those needed in nested queriesThe WHERE-clause specifies the conditions for selection and join of tuples from the relations specified in the FROM-clauseGROUP BY specifies grouping attributesHAVING specifies a condition for selection of groupsORDER BY specifies an order for displaying the result of a queryA query is evaluated by first applying the WHERE-clause, then GROUP BY and HAVING, and finally the SELECT-clause.