Download presentation
Presentation is loading. Please wait.
1
Retrieval Queries in SQL(DML)
SQL has one basic statement for retrieving information from a database; the SELECT statement This is not the same as the SELECT operation of the relational algebra. Important distinction between SQL and the formal relational model: SQL allows a table (relation) to have two or more tuples that are identical in all their attribute values Hence, an SQL relation (table) is a multi-set (sometimes called a bag) of tuples; it is not a set of tuples. SQL relations can be constrained to be sets by specifying PRIMARY KEY or UNIQUE attributes, or by using the DISTINCT option in a query.
2
Retrieval Queries in SQL (contd.)
A bag or multi-set is like a set, but an element may appear more than once. Example: {A, B, C, A} is a bag. {A, B, C} is also a bag that also is a set. Bags also resemble lists, but the order is irrelevant in a bag. Example: {A, B, A} = {B, A, A} as bags However, [A, B, A] is not equal to [B, A, A] as lists.
3
Retrieval Queries in SQL (contd.)
Basic form of the SQL SELECT statement is called a mapping or a SELECT-FROM-WHERE block SELECT <attribute list> FROM <table list> WHERE <condition> <attribute list> is a list of attribute names whose values are to be retrieved by the query. <table list> is a list of the relation names required to process the query. <condition> is a conditional (Boolean) expression that identifies the tuples to be retrieved by the query.
4
Relational Database Schema--Figure 5.7
5
Populated Database--Fig.5.6
6
Simple SQL Queries Basic SQL queries correspond to using the following operations of the relational algebra: SELECT PROJECT JOIN All subsequent examples use the COMPANY database.
7
Simple SQL Queries (contd.)
Example of a simple query on one relation Query 0: Retrieve the birth date and address of the employee whose name is 'John B. Smith'. Q0: SELECT Bdate, Address FROM EMPLOYEE WHERE Fname='John' AND Minit='B’ AND Lname='Smith’; Similar to a SELECT-PROJECT pair of relational algebra operations: The SELECT-clause specifies the projection attributes and the WHERE-clause specifies the selection condition. However, the result of the query may contain duplicate tuples.
8
Simple SQL Queries (contd.)
Query 1: Retrieve the name and address of all employees who work for the 'Research' department. Q1: SELECT Fname, Lname, Address FROM EMPLOYEE, DEPARTMENT WHERE Dname='Research' AND Dnumber=Dno; Similar to a SELECT-PROJECT-JOIN sequence of relational algebra operations (Dname='Research') is a selection condition (corresponds to a SELECT operation in relational algebra) (Dnumber=Dno) is a join condition (corresponds to a JOIN operation in relational algebra)
9
Simple SQL Queries (contd.)
Query 2: For every project located in 'Stafford', list the project number, the controlling department number, and the department manager's last name, address, and birth date. Q2:SELECT Pnumber, Dnum, Lname, Address, Bdate FROM PROJECT, DEPARTMENT, EMPLOYEE WHERE Dnum=Dnumber AND Mgr_ssn=Ssn AND Plocation='Stafford’; In Q2, there are two join conditions The join condition Dnum=Dnumber relates a project to its controlling department. The join condition Mgr_ssn=Ssn relates the controlling department to the employee who manages that department.
10
Aliases, * and DISTINCT, Empty WHERE-clause
In SQL, we can use the same name for two (or more) attributes as long as the attributes are in different relations. A query that refers to two or more attributes with the same name must qualify the attribute name with the relation name by prefixing the relation name to the attribute name. Example: EMPLOYEE.Lname, DEPARTMENT.Dname
11
ALIASES Some queries need to refer to the same relation twice
In this case, aliases are given to the relation name Query 8: For each employee, retrieve the employee's name, and the name of his or her immediate supervisor. Q8: SELECT E.Fname, E.Lname, S.Fname, S.Lname FROM EMPLOYEE E S WHERE E.Super_ssn=S.Ssn; In Q8, the alternate relation names E and S are called aliases or tuple variables for the EMPLOYEE relation. We can think of E and S as two different copies of EMPLOYEE; E represents employees in role of supervisees and S represents employees in role of supervisors.
12
ALIASES (contd.) Aliasing can also be used in any SQL query for convenience Can also use the AS keyword to specify aliases Q8: SELECT E.Fname, E.Lname, S.Fname, S.Lname FROM EMPLOYEE AS E, EMPLOYEE AS S WHERE E.Super_ssn=S.Ssn;
13
UNSPECIFIED WHERE-clause
A missing WHERE-clause indicates no condition; hence, all tuples of the relations in the FROM-clause are selected This is equivalent to the condition WHERE TRUE Query 9: Retrieve the SSN values for all employees. Q9: SELECT Ssn FROM EMPLOYEE If more than one relation is specified in the FROM-clause and there is no join condition, then the CARTESIAN PRODUCT of tuples is selected.
14
UNSPECIFIED WHERE-clause (contd.)
Example: Q10: SELECT Ssn, Dname FROM EMPLOYEE, DEPARTMENT It is extremely important not to overlook specifying any selection and join conditions in the WHERE-clause; otherwise, incorrect and very large relations may result.
15
USE OF * To retrieve all the attribute values of the selected tuples, a * is used, which stands for all the attributes Examples: Q1C: SELECT * FROM EMPLOYEE WHERE Dno=5 Q1D: SELECT * FROM EMPLOYEE, DEPARTMENT WHERE Dname='Research' AND Dno=Dnumber
16
USE OF DISTINCT SQL does not treat a relation as a set; duplicate tuples can appear. To eliminate duplicate tuples in a query result, the keyword DISTINCT is used. For example, the result of Q11 may have duplicate SALARY values whereas Q11A does not have any duplicate values. Q11: SELECT Salary FROM EMPLOYEE Q11A: SELECT DISTINCT Salary FROM EMPLOYEE
17
SET OPERATIONS SQL has directly incorporated some set operations.
There is a union operation (UNION), and in some versions of SQL there are set difference (MINUS or EXCEPT ) and intersection (INTERSECT) operations. The resulting relations of these set operations are sets of tuples; duplicate tuples are eliminated from the result. The set operations apply only to union compatible relations; the two relations must have the same attributes must appear in the same order. For some database products, the union compatible relations means those relations with same number, order and datatype of columns, but not necessarily have the same names. Usually, the result assumes the column names of the first relation. and The results of UNION ALL, EXCEPT ALL, and INTERSECT ALL are multilists and duplicates are not eliminated.
18
SET OPERATIONS (contd.)
Query 4: Make a list of all project numbers for projects that involve a an employee whose last name is 'Smith as a manager of the department that controls the project or as a worker on the project. Q4: (SELECT DISTINCT Pnumber FROM PROJECT, DEPARTMENT, EMPLOYEE WHERE Dnum=Dnumber AND Mgr_ssn=Ssn AND Lname='Smith') UNION (SELECT Pnumber FROM PROJECT, WORKS_ON, EMPLOYEE WHERE Pnumber=Pno AND Essn=Ssn AND Lname='Smith');
19
SUBSTRING COMPARISON The LIKE comparison operator is used to compare partial strings Two reserved characters are used: '%' (or '*' in some implementations) replaces an arbitrary number of characters, and '_' replaces a single arbitrary character.
20
SUBSTRING COMPARISON (contd.)
Query 12: Retrieve all employees whose address is in Houston, Texas. Here, the value of the ADDRESS attribute must contain the substring 'Houston,TX‘ in it. Q12: SELECT FNAME, LNAME FROM EMPLOYEE WHERE ADDRESS LIKE '%Houston,TX;
21
SUBSTRING COMPARISON (contd.)
Query 12A Find all employees who were born during the 1950s. SELECT Fname, Lname FROM EMPLOYEE WHERE Bdate LIKE ‘_ _5_ _ _ _ _ _ _’; Note that the date is represented in the following format: four digits for the year-two digits for the month- two digits for day. However, in Oracle the default format for date is dd-mm-yy.
22
ESCAPE Characters IF an underscore or % is needed as a literal character in the string, the character be preceded by an escape character , which is specified after the string using the keyword ESCAPE. Any character not used in the string can be chosen as the escape character. For example , ‘AB\_CD\%EF’ ESCAPE ‘\’ represents the string ‘AB_CD%EF’. If the apostrophes(‘ or ’) or single quotation marks (‘’) is needed, they are represented by repeating the symbol twice. For example, (‘) is represented by (‘‘).
23
ARITHMETIC OPERATIONS
The standard arithmetic operators '+', '-'. '*', and '/' (for addition, subtraction, multiplication, and division, respectively) can be applied to numeric values in an SQL query result Query 13: Show the effect of giving all employees who work on the 'ProductX' project a 10% raise. SELECT Fname, Lname ,1.1*Salary AS Increased_sal FROM EMPLOYEE, WORKS_ON, PROJECT WHERE Ssn=Essn AND Pno=Pnumber AND Pname='ProductX’;
24
The BETWEEN Statement Query 14 : Retrieve all employees in department 5 whose salary is between $30,000 and $40,000. SELECT * FROM EMPLOYEE WHERE ( Salary BETWEEN AND 40000) AND Dno=5;
25
ORDER BY The ORDER BY clause is used to sort the tuples in a query result based on the values of some attribute (s) Query 15: Retrieve a list of employees and the projects each works in, ordered by the employee's department, and within each department ordered alphabetically by employee last name. Q15: SELECT Dname, Lname, Fname, Pname FROM DEPARTMENT, EMPLOYEE, WORKS_ON, PROJECT WHERE Dnumber=Dno AND Ssn=Essn AND Pno=Pnumber ORDER BY Dname, Lname;
26
ORDER BY (contd.) The default order is in ascending order of values
We can specify the keyword DESC if we want a descending order; the keyword ASC can be used to explicitly specify ascending order, even though it is the default. Example: ORDER BY Dname DESC, Lname ASC, Fname ASC
27
NULL You may test an attribute or an expression whether is or is not NULL. Query 18: Retrieve the names of all employees who do not have supervisors. SELECT Fname, Lname FROM EMPLOYEE WHERE Super_ssn IS NULL; Query 18A: Retrieve the names of all employees who have supervisors. WHERE Super_ssn IS NOT NULL;
28
Meaning of NULL The following is not correct WHY? SELECT Fname, Lname
FROM EMPLOYEE WHERE Super_ssn = NULL; NULL represents a missing value, but that it usually has one of three different interpretations: -Unknown value - Unavailable or withheld value( for example, unlisted home phone number) - Not applicable attribute( for example the name of the spouse for a single person). - In general , each NULL is considered to be different from other NULL in the database.
29
NULL in Conditions If a NULL value is involve in a comparison or arithmetic operation or (=,<,>,<=,>=,<>or !) the result is considered Unknown (NULL). The value of a logical expression ( condition) that involve NULL values is determined using the three –values logic. The truth tables for AND, OR, NOT are show below:
30
NULL AND TRUE FALSE UNKOWN
31
NULL in Conditions (contd.)
OR TRUE FALSE UNKOWN NOT TRUE FALSE UNKWON UNKOWN
32
NULL in Conditions (contd.)
In SELECT-Project-Join queries , the general rule is that only those combinations of tuples that evaluate the logical expression (join condition) of the query( the condition within WHERE) to TRUE are selected. For the Outer Join, this rule is not correct as we will see later. Since each NULL value as being distinct from every other NULL value, so equality comparison is not appropriate. For example, the following query will not return the employees who dot have supervisors. SELECT Fname, Lname FROM EMPLOYEE WHERE Super_ssn = NULL;
33
Summary WHERE is for selecting rows (from tables) which satisfy the given condition ( the condition must be true not false, neither NULL) . SELECT is for selecting columns(attributes) or computing expressions or functions after selecting rows. You may rename the columns and name the expressions and functions in the SELECT. If you want the selected values without duplicate, you may use DISTINCT. Note that for more than one value , a single value may have duplicate but the combination of values must not have duplicate.
34
Summary FROM is for selecting the tables. Only the tables related to the values in the SELECT part and WHERE part. If more than one table is needed, the system performs the join operation. You may think of the join operation as merging the tables into a single table by matching the rows of the tables which satisfy a given condition.
35
Summary For example given the following tables R1 and R2. R1 R2 A B C
4 g D E c w d g e h
36
Summary The joint of R1 and R2 under the condition R1.B= R2.D is
The condition of join can be any condition (=,<,> !=) but the most common condition is the equality of primary key and the corresponding foreign key R1.A R1.B R1.C R2.D R2.E a c w
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.