Dbms Int302 Unit#2 A SQL Part I 2022 1
Dbms Int302 Unit#2 A SQL Part I 2022 1
Dbms Int302 Unit#2 A SQL Part I 2022 1
Slide #-2
Database state for Company database
Slide #-3
Database state for Company database
Slide #-4
Database state for company database
Slide #-5
Simple SQL Queries
All subsequent examples use the COMPANY database
Example of a simple query on one relation
Query 1:
Retrieve the birthdate and address of the employee whose name is
'John B. Smith'.
SELECT BDATE, ADDRESS
FROM EMPLOYEE
WHERE FNAME='John' AND MINIT='B’ AND
LNAME='Smith’;
Result
Slide #-6
Simple SQL Queries
Example on retrieving data from two tables
Query 2:
Retrieve the name and address of all employees who work for the
'Research' department.
SELECT FNAME, LNAME, ADDRESS
FROM EMPLOYEE, DEPARTMENT
WHERE DNUMBER=DNO AND DNAME='Research‘;
• (DNAME='Research') is a selection condition
• (DNUMBER=DNO) is a join condition
Slide #-7
Simple SQL Queries
Example on retrieving data from two tables
Query 2:
DNUMBER=DNO will result in creating the following table:
Result
Slide #-8
Simple SQL Queries
Example on retrieving data from three tables
Query 3:
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';
In Q2, there are two join conditions:
– The join condition DNUM=DNUMBER relates a project to its controlling
department
– The join condition MGRSSN=SSN relates the controlling department to the
employee who manages that department
Result
Slide #-9
Aliases
Aliases
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
To avoiding writing the full relation name, we can use aliases.
Query 4:
Write SQL query that list the name of each department and its location.
Select Dname, Dlocation
From Department D, Dept_Location L
Where D.Dnumber = L.Dnumber;
Slide #-10
Unspecified WHERE clause
Unspecified WHERE clause
A missing WHERE-clause indicates no condition; hence, all tuples
of the relations in the FROM-clause are selected Result
Query 5:
Retrieve the SSN values for all employees.
SELECT SSN
FROM EMPLOYEE;
Slide #-12
UNSPECIFIED WHERE-clause
Example:
Query 7:
Retrieve the SSN and DNAME values for all
employees.
Result
Q7: [Correct Solution]
SELECT SSN, DNAME
FROM EMPLOYEE, DEPARTMENT;
WHERE DNO = DNUMBER
Slide #-13
USE OF *
USE OF *
To retrieve all the attribute values of the selected tuples, a * is used,
which stands for all the attributes’ values
Query 8:
SELECT *
FROM EMPLOYEE
WHERE DNO=5;
Result
Slide #-14
USE of *
Query 9:
SELECT *
FROM EMPLOYEE, DEPARTMENT
WHERE DNAME='Research' AND DNO=DNUMBER;
Result
Slide #-15
USE OF 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
For example, the result of Q10 may have duplicate SALARY values
whereas Q10A does not have any duplicate values
Result Q11 Result Q10A
Q10: SELECT SALARY
FROM EMPLOYEE;
Slide #-16
Using a Relation Twice
Using a Relation Twice
Some queries need to refer to the same relation twice, in this case, aliases
are used.
Query 11:
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 E, EMPLOYEE S
WHERE E.SUPER_SSN = S.SSN;
– In the above query, 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 employees (supervisees) and S
represents employees in role of supervisors
Slide #-17
Aliases
Result
Slide #-18
Exercises
Exercise 1:
Make a list of all project numbers for projects that involve an employee whose
last name is 'Smith' as a worker.
Solution:?
Slide #-19
Exercises
Exercise 1:
Make a list of all project numbers for projects that involve an employee whose
last name is 'Smith' as a worker.
Solution
SELECT Distinct PNO
FROM WORKS_ON, EMPLOYEE
WHERE ESSN=SSN AND LNAME='Smith';
Slide #-20
Exercises
Exercise 2:
Make a list of all project names for projects that involve an employee whose
last name is 'Smith' as a worker.
Solution:?
Slide #-21
Exercises
Exercise 2:
Make a list of all project names for projects that involve an employee whose
last name is 'Smith' as a worker.
Solution
SELECT Distinct Pname
FROM PROJECT, WORKS_ON, EMPLOYEE
WHERE Pnumber = Pno AND ESSN = SSN AND LNAME = 'Smith';
Slide #-22
Exercises
Exercise 3:
Make a list of all project numbers for projects that involve an employee whose last
name is 'Smith' as a manager of the department that controls the project.
Solution?
Slide #-23
Exercises
Exercise 3:
Make a list of all project numbers for projects that involve an employee whose last
name is ‘Wong' as a manager of the department that controls the project.
Solution:
SELECT Distinct Pnumber
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE DNUM = DNUMBER AND MGR_SSN=SSN
AND LNAME=‘Smith';
Slide #-24
SET OPERATIONS
SQL support three set operations:
Slide #-25
SET OPERATIONS: UNION
Query 12:
Make a list of all project numbers for projects that involve an
employee whose last name is 'Smith' as a worker or his last name
‘Smith’ as a manager of the department that controls the project.
Slide #-26
SET OPERATIONS: UNION
Query 13:
Make a list of all project numbers for projects that involve an employee whose last
name is 'Smith' as a worker or his last name Wallace as a manager of the
department that controls the project.
Result PNUMBER
-----------------
1
2 Slide #-27
SET OPERATIONS: INTERSECT
Query 14:
Make a list of all project numbers for projects that involve an employee whose last
name is 'Smith' and an employee whose last name is ‘Wong’ as workers.
INTERSECT
Result PNUMBER
-----------------
2
Slide #-28
SET OPERATIONS: MINUS
Query 15:
Make a list of all project numbers for projects that involve an employee whose last
name is 'Smith' but not an employee whose last name is ‘Wong’ as workers.
Result PNUMBER
-----------------
1
Slide #-29
Substring Pattern Matching
The LIKE comparison operator is used to compare partial strings.
Slide #-30
Substring Pattern Matching
Query 16:
Retrieve all employees whose address is in Houston.
Here, the value of the ADDRESS attribute must contain the substring
'Houston‘:
SELECT FNAME, LNAME
FROM EMPLOYEE
WHERE ADDRESS LIKE '%Houston%’;
Slide #-31
Substring Pattern Matching
We can use NOT with LIKE as in the following example:
Query 17:
Retrieve all employees whose address is not in Houston.
Here, the value of the ADDRESS attribute must not contain the substring
'Houston':
Slide #-33
Substring Pattern Matching
The IN Condition Operators:
The IN condition operator is used for comparison with a set of values
as described in the following example.
Query 19
List the last name of all employees who work on project 2, 10, or 30.
SELECT DISTINCT LNAME Result
FROM EMPLOYEE, WORKS_ON LNAME
WHERE Ssn = Essn AND Pno IN (2, 10, 30);
------------
Wong
Zelaya
English
Jabbar
Smith
Wallace
Slide #-34
The IN Condition
The IN Condition Operator:
The SQL IN condition (also called the IN operator) allows us to easily
test if an expression matches any value in a list of values.
It is used to help reduce the need for multiple OR conditions
Query 20: Result
List the last name of all employees who work on LNAME
Slide #-35
Arithmetic Operators
The standard arithmetic operators '+', '-‘, '*', and '/' (for addition,
subtraction, multiplication, and division, respectively) can be applied
to numeric values in an SQL query result
Query 21:
List first name and last name of employees who work on project
‘ProductX’ with salary increased by 10%.
Slide #-36
Arithmetic Operators
Query 21 - Solution:
List first name and last name of employees who work on project ‘ProductX’
with salary increased by 10%.
SELECT FNAME, LNAME, 1.1*SALARY
FROM EMPLOYEE, WORKS_ON, PROJECT
WHERE SSN=ESSN AND PNO=PNUMBER
AND PNAME='ProductX’;
Result
FNAME LNAME 1.1*SALARY
John Smith
Joyce English 27500
Slide #-37
BETWEEN OPEARTOR
Another useful comparison operator is the BETWEEN operator.
The use of this operator is explained with the following example:
Query 22:
Retrieve all employees data in department 5 whose salary is between
30,000 and 40,000.
SELECT *
FROM EMPLOYEE
WHERE (SALARY BETWEEN 30000 AND 40000)
AND DNO = 5;
Result
Slide #-38
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 23:
Retrieve a list of all department names, last and first names of employees
working in each department, and the name of the projects these employees are
working on. ordered by the employee's department name, and within each
department ordered alphabetically by employee’s last name.
SELECT DNAME, LNAME, FNAME, PNAME
FROM EMPLOYEE, DEPARTMENT, WORKS_ON, PROJECT
WHERE DNO=DNUMBER AND SSN=ESSN
AND PNO=PNUMBER
ORDER BY DNAME, LNAME;
Slide #-39
ORDER BY
Result
DNAME LNAME FNAME PNAME
-------------------- ---------- ---------- ---------------
Administration Jabbar Ahmed Computerization
Administration Jabbar Ahmed Newbenefits
Administration Wallace Jennifer Newbenefits
Administration Wallace Jennifer Reorganization
Administration Zelaya Alicia Newbenefits
Administration Zelaya Alicia Computerization
Headquarters Borg James Reorganization
Research English Joyce ProductX
Research English Joyce ProductY
Research Narayan Ramesh ProductZ
Research Smith John ProductX
Research Smith John ProductY
Research Wong Franklin ProductY
Research Wong Franklin ProductZ
Research Wong Franklin Computerization
Research Wong Franklin Reorganization
Slide #-40
ORDER BY
The default order is in ascending order of values
Slide #-41
NULLS IN SQL QUERIES
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.
Query 24: Retrieve the names of all employees who do not have
supervisors.
SELECT FNAME, LNAME
FROM EMPLOYEE
WHERE SUPER_SSN IS NULL;
Note: If a join condition is specified, tuples with NULL values for the
join attributes are not included in the result.
FNAME LNAME
Result ---------- ----------
James Borg Slide #-42