DB08 Query
DB08 Query
Query
FIT
Outline
• Select Basic
• Comparison operators
• Between … And, In
• Like
• Not – And – Or
• More than one table query
• Aggregate Functions
• Grouping – Having Clause
• Ref: Chapter 6, 7
• Retrieve the First name, Last name and address of the employees
Select Fname, Lname, Address
From Employee;
Comparison operators
• Retrieve the last name, birth date and address of the employees
whose last name is not ‘Borg‘ (string).
SELECT Lname, Bdate, Address
FROM EMPLOYEE
WHERE Lname <>‘Borg’;
• Retrieve the first name and last name of the employee whose
DNo is 5 (number).
SELECT Fname, Lname
FROM EMPLOYEE
WHERE DNo = 5;
• Retrieve the first name and last name of the employee whose
salary is greater or equal 40000 (number).
SELECT Fname, Lname
FROM EMPLOYEE
WHERE Salary >= 40000;
Null
• Retrieve the first name and last name of the employees who does not
have his supper.
SELECT Fname, Minit, Lname
FROM EMPLOYEE
WHERE Supper_SSN is null;
• Note that NULL indicates a value which is missing, not known, inappropriate, etc.
NULL is not a blank or zero. NULL cannot be tested for equality with other NULL
values.
String Operations
• Pattern matching
• Simple pattern matching is carried out using LIKE:
LIKE ’pattern-to-match’
• Where the pattern can include special wildcard characters:
• % (percent) 0 or more arbitrary characters
• _ (underscore) any one character
• Retrieve all employees whose address is in Houston, Texas. Here, the value of the
ADDRESS attribute must contain the substring 'Houston,TX‘ in it.
SELECT Fname, Lname
FROM EMPLOYEE
WHERE Address LIKE ‘Houston,TX%‘;
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
Distinct
• 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
SELECT Salary
FROM EMPLOYEE;
Order By
• 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:
SELECT Dname, Lname, Fname, Pname
FROM DEPARTMENT, EMPLOYEE, WORKS_ON, PROJECT
WHERE Dnumber=Dno AND Ssn=Essn AND Pno=Pnumber
ORDER BY Dname, Lname DESC;
• Not
Select *
From Employee
Where Supper_SSn is Not Null;
• And
Select *
From Employee
Where Fname = ‘Joyce’ And Lname = ‘English’;
• Or
Select *
From Employee
Where Lname = ‘English’ Or Fname = ‘James’;
• Between … And …
• Retrieve the first name, last name and address of the employee whose
birthdates from June-01-1959 to Dec-31-1959.
SELECT Fname, Lname, address
FROM EMPLOYEE
WHERE Bdate Between ‘1959-01-01’ And ‘1959-06-31’;
Operator: In
• In
• Retrieve the first name, last name and address of the employee
whose Dno is 1 or 4.
SELECT Fname, Lname, address
FROM EMPLOYEE
WHERE Dno In (1,4);
• Syntax
SELECT table1.column, table2.column
FROM table1, table2
WHERE table1.column1 = table2.column2;
• Ex:
Select Fname, Lname, Dname
From Employee, Department
Where Employee.Dno = Department.Dnumber;
Aliases
• For each employee, retrieve the employee's name, and the name of his or her
immediate supervisor.
SELECT E.Fname, E.Lname, S.Fname, S.Lname
FROM EMPLOYEE as E, EMPLOYEE as S
WHERE E.Super_ssn = S.Ssn;
• 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
EMPLOYEE E EMPLOYEE S
UNSPECIFIED WHERE-clause
• Example:
SELECT Ssn, Dname
FROM EMPLOYEE, DEPARTMENT;
• For every project located in 'Stafford', list the project number, the controlling
department number, and the department manager's last name, address, and
birthdate.
SELECT Pnumber, Dnum, Lname, Bdate, Address
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE DNum=DNumber AND MgrSSN=SSN
AND Plocation='Stafford‘;
Set Operations
• A complete SELECT query, called a nested query, can be specified within the WHERE-clause of
another query, called the outer query
• Many of the previous queries can be specified in an alternative form using nesting
• Retrieve the name and address of all employees who work for the 'Research' department.
SELECT Fname, Lname, Address
FROM EMPLOYEE
WHERE Dno IN
(SELECT Dnumber
FROM DEPARTMENT
WHERE Dname='Research');
• Find names of employees with salary greater than that of some (at least one)
employee in the department 5.
Select Distinct E.Fname, E.Lname, E.Salary
From Employee as E, Employee as E5
Where E.salary > E5.salary and E5.DNo = 5;
• Same query using > some clause
Select Fname, LName, Salary
From Employee
Where salary > Some (Select Salary From Employee
Where DNo = 5);
• Find the names of all employees whose salary is greater than the
salary of all employees in the department 5.
Select Fname, LName, Salary
From Employee
Where salary >
All (Select Salary From Employee
Where DNo = 5);
• The exists construct returns the value true if the argument subquery is
nonempty.
• exists r r Ø
• not exists r r = Ø
• Retrieve the name of all employees who work for the 'Research'
department.
Select Fname, Lname
From Employee
Where Exists (Select * From Department
Where DNo = Dnumber And Dname='Research' );
•Find
all employees who have taken all projects managed in the
department 5.
Select E.FName, E.Lname
From employee E
Where not exists
( (select Pnumber from project where DNum = 5)
except
(Select W.Pno From works_on W where W.ESSN = E.SSN));
•First nested query lists all projects managed in department 5.
•Second nested query lists all projects a particular employee works on
•Note
• X – Y = Ø X Y
•Cannot write this query using = all and its variants
• SQL:
Select A.FName, A.Lname
From employee A
Where Not Exists
(Select *
From Works_On B
Where B.PNo in
(Select PNumber
From Project
where DNum = 5) And Not Exists
(Select *
From Works_On C
Where A.SSN = C.ESSN And C.PNo =
B.PNo));
• Examples:
SELECT Fname, Lname, Address
FROM EMPLOYEE, DEPARTMENT
WHERE Dname='Research' AND Dnumber=Dno;
• or as:
SELECT Fname, Lname, Address
FROM (EMPLOYEE NATURAL JOIN EPARTMENT
AS DEPT(Dname, Dno, Mssn,Msdate)
WHERE Dname='Research’;
• Another Example:
For every project located in 'Stafford', list the project number, the controlling department
number, and the department manager's last name, address, and birthdate.
SELECT Pnumber, Dnum, Lname, Bdate, Address
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE Dnum=Dnumber AND Mgr_ssn=Ssn
AND Plocation='Stafford‘;
Could be written as follows; this illustrates multiple joins in the joined tables
SELECT Pnumber, Dnum, Lname, Bdate, Address
FROM (PROJECT JOIN DEPARTMENT ON
(Dnum=Dnumber) JOIN EMPLOYEE ON
(Mgr_ssn=Ssn) )
WHERE Plocation='Stafford’;
Outer Join
• Relation Course
• Relation GradeReport
• Observe that
Course information is missing BA140
GradeReport information is missing BA250
Aggregate Functions
• Find the maximum salary, the minimum salary, the average salary and numbers of
employees.
SELECT MAX(Salary), MIN(Salary), AVG(Salary), Count(*)
FROM EMPLOYEE;
• Some SQL implementations may not allow more than one function in the SELECT-clause
• Find the maximum salary, the minimum salary, and the average
salary among employees who work for the 'Research'
department.
SELECT MAX(Salary), MIN(Salary), AVG(Salary)
FROM EMPLOYEE, DEPARTMENT
WHERE Dno=Dnumber AND Dname='Research‘;
Result?
Result?
Result?
Grouping
• For each project, retrieve the project number, project name, and
the number of employees who work on that project.
SELECT Pnumber, Pname, COUNT (*)
FROM PROJECT, WORKS_ON
WHERE Pnumber=Pno
GROUP BY Pnumber, Pname;
The HAVING-clause
• 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.
SELECT Pnumber, Pname, COUNT(*)
FROM PROJECT, WORKS_ON
WHERE Pnumber=Pno
GROUP BY Pnumber, Pname
HAVING COUNT (*) > 2;