Cse-V-database Management Systems U4
Cse-V-database Management Systems U4
com
Database Management System 10CS54
UNIT 4
SQL The Relational Database Standard
SQL The Relational Database Standard
UNIT 4
x SQL allows a table (relation) to have two or more tuples that are identical in all their
attributes values. Hence, an SQL table is not a set of tuple, because a set does not allow
two identical members; rather it is a multiset of tuples.
x A basic query statement in SQL is the SELECT statement.
x The SELECT statement used in SQL has no relationship to the SELECT operation of
relational algebra.
Some example:
Query 0: Retrieve the birthday and address of the employee(s) whose name is ‘John B. Smith’
FROM EMPLOYEE
Query 1: Retrieve the name and address of all employee who work for the ‘Research’ Dept.
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 birthdate.
Ambiguity in the case where attributes are same name need to qualify the attribute using DOT
separator
More Ambiguity in the case of queries that refer to the same relation twice
Query 8: For each employee, retrieve the employee’s first and last name and the first and last
name of his or her immediate supervisor
WHERE E.SUPERSSN=S.SSN
A missing WHEREclause indicates no conditions, which means all tuples are selected
In case of two or more table, then all possible tuple combinations are selected
Example: Q10: Select all EMPLOYEE SSNs , and all combinations of EMPLOYEE SSN and
DEPARTMENT DNAME
More
SELECT *
FROM EMPLOYEE
WHERE DNO=5
(name like ’%a_’) is true for all names having ‘a’ as second letter from the end.
x In order to list all employee who were born during 1960s we have the followings:
x SELECT FNAME, LNAME
. FROM EMPLOYEE
WHERE BDATE LIKE '6_______';
Examples: Show the resulting salaries if every employee working on the 'ProductX' project is
given a 10 percent raise.
Retrieve all employees in department number 5 whose salary between $30000 and $40000.
SELECT *
FROM EMPLOYEE
WHERE (SALARY BETWEEN 30000 AND 40000) AND DNO=5;
SQL does not delete duplicate because Duplicate elimination is an expensive operation (sort and
delete) user may be interested in the result of a query in case of aggregate function, we do not
want to eliminate duplicates
examples
Q11: Retrieve the salary of every employee , and (Q!2) all distinct salary values
FROM EMPLOYEE
FROM EMPLOYEE
Example: Q4: Make a list of Project numbers for projects that involve an employee whose last
name is ‘Smith’, either as a worker or as a manger of the department that controls the project
FROM PROJECT
Example:
FROM WORKS_ON
FROM WORKS_ON
WHERE SSN=‘123456789’
In addition to the IN operator, a number of other comparison operators can be used to compare a
single value v to a set of multiset V.
ALL V returns TRUE if v is greater than all the value in the set
Select the name of employees whose salary is greater than the salary of all the employees in
department 5
In general, any nested query involving the = or comparison operator IN can always be
rewritten as a single block query
Query 12: Retrieve the name of each employee who has a dependent with the same first name as
the employee.
Q12: SELECT E.FNAME, E.LNAME
FROM EMPLOYEE AS E
WHERE E.SSN IN (SELECT ESSN
FROM DEPENDENT
WHEREESSN=E.SSN AND
E.FNAME=DEPENDENT_NAME)
In Q12, the nested query has a different result for each tuple in the outer query.
The original SQL as specified for SYSTEM R also had a CONTAINS comparison operator,
which is used in conjunction with nested correlated queries This operator was dropped from the
language, possibly because of the difficulty in implementing it efficiently Most implementations
of SQL do not have this operator The CONTAINS operator compares two sets of values , and
returns TRUE if one set contains all values in the other set (reminiscent of the division operation
of algebra).
Query 3: Retrieve the name of each employee who works on all the projects controlled by
department number 5.
In Q3, the second nested query, which is not correlated with the outer query, retrieves the project
numbers of all projects controlled by department 5.
The first nested query, which is correlated, retrieves the project numbers on which the employee
works, which is different for each employee tuple because of the correlation.
EXISTS is used to check whether the result of a correlated nested query is empty (contains no
tuples) or not We can formulate Query 12 in an alternative form that uses EXISTS as Q12B
below.
Query 12: Retrieve the name of each employee who has a dependent with the same first name as
the employee.
FROM EMPLOYEE
WHERE NOT EXISTS (SELECT *
FROM DEPENDENT
WHERESSN=ESSN)
In Q6, the correlated nested query retrieves all DEPENDENT tuples related to an EMPLOYEE
tuple. If none exist , the EMPLOYEE tuple is selected EXISTS is necessary for the expressive
power of SQL
It is also possible to use an explicit (enumerated) set of values in the WHEREclause rather than
a nested query Query 13: Retrieve the social security numbers of all employees who work on
project number 1, 2, or 3.
Null example
SQL allows queries that check if a value is NULL (missing or undefined or not applicable) SQL
uses IS or IS NOT to compare NULLs because it considers each NULL value distinct from other
NULL values, so equality comparison is not appropriate .
FROM EMPLOYEE
Note: If a join condition is specified, tuples with NULL values for the join attributes are not
included in the result
Join Revisit
Retrieve the name and address of every employee who works for ‘Search’ department
Aggregate Functions
Query 15: Find the sum of the salaries of all employees the ‘Research’ dept, and the max salary,
the min salary, and average:
FROM EMPLOYEE
Query 16: Find the maximum salary, the minimum salary, and the average salary among
employees who work for the 'Research' department.
Queries 17 and 18: Retrieve the total number of employees in the company (Q17), and the
number of employees in the 'Research' department (Q18).
Example of grouping
In many cases, we want to apply the aggregate functions to subgroups of tuples in a relation
Each subgroup of tuples consists of the set of tuples that have the same value for the grouping
attribute(s)
SQL has a GROUP BYclause for specifying the grouping attributes, which must also appear in
the SELECTclause
For each project, select the project number, the project name, and the number of employees
who work on that projet
WHERE PNUMBER=PNO
In Q20, the EMPLOYEE tuples are divided into groupseach group having the same value for the
grouping attribute DNO
The COUNT and AVG functions are applied to each such group of tuples separately.The
SELECTclause includes only the grouping attribute and the functions to be applied on each
group of tuples. A join condition can be used in conjunction with grouping
Query 21: For each project, retrieve the project number, project name, and the number of
employees who work on that project.
Q21: SELECT PNUMBER, PNAME, COUNT (*)
FROM PROJECT, WORKS_ON
WHERE PNUMBER=PNO
GROUP BY PNUMBER, PNAME
In this case, the grouping and functions are applied after the joining of the two
relations
THE HAVINGCLAUSE:
Sometimes we want to retrieve the values of these functions for only those groups that satisfy
certain conditions. The HAVINGclause is used for specifying a selection condition on groups
(rather than on individual tuples)
Query 22: For each project on which more than two employees work , retrieve the project
number, project name, and the number of employees who work on that project.