SQL Queries
SQL Queries
Query Languages
Allow manipulation and retrieval of data from a database
Outline
●
Practical Language SQL
●
Formal Query Languages
– Relational Algebra
– Relational Calculus
●
Domain Relational calculus
●
Tuple Relational calculus
Query Languages
●
SQL is a computer language for storing, manipulating and
retrieving data stored in a relational database
●
Relational model supports simple, powerful Formal Query
Languages
●
Formal languages are important for several reasons
Query
Relational Algebra
●
Its operations can be divided into two groups
– Set operations
●
Each relation is defined to be a set of tuples in the
formal relational model
●
UNION, INTERSECTION, SET DIFFERENCE, and CARTESIAN
PRODUCT (also known as CROSS PRODUCT)
– Operations(specifically for relational databases)
●
SELECT, PROJECT(Unary),JOIN and other complex binary
operations
– Additional operations
●
Some common database requests cannot be performed with
the original relational algebra operations
●
Aggregate functions, additional types of JOIN and UNION
operations
Relational Algebra
●
Unary Relational Operations:
– SELECT(σ)
– PROJECT(π)
Select Operation
●
The SELECT operation is used to choose a subset of the tuples
from a relation that satisfies a selection condition.
●
The general form of the SELECT operation is
σ<selection condition>(R)
●
<selection condition> is specified on the attributes of relation R
●
R is generally a relational algebra expression whose result is a
relation
●
The relation resulting from the SELECT operation has the same
attributes as R
Projection Operation
●
SELECT operation chooses some of the rows from the table while
discarding other rows.
●
The PROJECT operation, on the other hand, selects certain
columns from the table and discards the other columns.
●
The general form of the PROJECT operation is
π<attribute list>(R)
●
<attribute list> is the desired sublist of attributes from the
attributes of relation R.
●
R is generally a relational algebra expression whose result is a
relation
Projection Operation
●
If the attribute list includes only non-key attributes of R, duplicate
tuples are likely to occur.
●
The PROJECT operation removes any duplicate tuples, so the
result of the PROJECT operation is a set of distinct tuples
●
In SQL queries duplicate tuples will not be removed
●
If We want to remove duplicate tuples in SQL we can use keyword
DISTINCT
SQL Select Statement
●
SQL has one basic statement for retrieving information from a
database; The SELECT statement
●
Basic form of the SQL SELECT statement is called a mapping
or a SELECT-FROM-WHERE block
SELECT *
σDno=4(EMPLOYEE)
FROM EMPLOYEE
WHERE Dno = 4;
SELECT *
σSalary>30000(EMPLOYEE)
FROM EMPLOYEE
WHERE Salary > 30000;
Simple Retrieval Queries
Q3: Select the tuples for all employees who work in department 4
and make over $25,000 per year
SELECT *
FROM EMPLOYEE
WHERE Dno = 4 and Salary > 25000;
Simple Retrieval Queries
Q3: Select the tuples for all employees who work in department 4
and make over $25,000 per year
Simple Retrieval Queries
Q4: Select the tuples for all employees who either work in
department 4 and make over $25,000 per year, or work in
department 5 and make over $30,000
SELECT *
FROM EMPLOYEE
WHERE (Dno = 4 AND Salary > 25000) OR (Dno = 5 AND
Salary > 30000);
Simple Retrieval Queries
Q4: Select the tuples for all employees who either work in
department 4 and make over $25,000 per year, or work in
department 5 and make over $30,000
Simple Retrieval Queries
Q4A. Retrieve all employees in department 5 whose
salary is between $30,000 and $40,000.
SELECT *
FROM EMPLOYEE
WHERE (Salary BETWEEN 30000 AND 40000) AND Dno = 5;
Simple Retrieval Queries
πFname,Lname, Salary(EMPLOYEE)
Q9: Retrieve the first name, last name, and salary of all
employees who work in department number 5
πFname,Lname, Salary(σDno=5(EMPLOYEE) )
Alterna-tively, we can explicitly show the sequence of
operations, giving a name to each intermediate relation, and
using the assignment operation,
denoted by ← (left arrow), as follows:
DEP5_EMPS ←σDno=5(EMPLOYEE)
RESULT ←πFname,Lname, Salary(DEP5_EMPS)
Sequence of Operations and Rename Operation
Q6: Retrieve the first name, last name, and salary of all
employees who work in department number 5
Sequence of Operations and Rename Operation
Q9A: Retrieve the first name, last name, and salary of all
employees who work in department number 5 by using
renaming
ρE(First_name, Last_name, Salary)(πFname,Lname, Salary(σDno=5(EMPLOYEE)))
SELECT *
σDno=4(EMPLOYEE)
FROM EMPLOYEE
WHERE Dno = 4;
●
The CARTESIAN PRODUCT operation applied by itself is generally
meaningless.
●
It is mostly useful when followed by a selection that matches
values of attributes coming from the component relations
Select * from (select Fname, Lname, ssn from employee where sex = 'F') EMP_names,
Dependent ;
Simple Retrieval Queries
FEMALE_EMPS ←σSex=‘F’(EMPLOYEE)
EMPNAMES ←πFname, Lname, Ssn(FEMALE_EMPS)
EMP_DEPENDENTS ← EMPNAMES × DEPENDENT
ACTUAL_DEPENDENTS ←σSsn=Essn(EMP_DEPENDENTS)
RESULT ←πFname, Lname, Dependent_name(ACTUAL_DEPENDENTS)
●
The CARTESIAN PRODUCT operation applied by itself is generally
meaningless.
●
It is mostly useful when followed by a selection that matches
values of attributes coming from the component relations
Select * from (select Fname, Lname, ssn from employee where sex = 'F') EMP_names,
Dependent ;
Select * from (select Fname, Lname, ssn from employee where sex = 'F')
EMP_names, Dependent where EMP_names.ssn = Dependent.essn;
SELECT *
FROM EMPLOYEE, DEPARTMENT
CARTESIAN PRODUCT (CROSS PRODUCT) Operation
SELECT *
FROM EMPLOYEE, DEPARTMENT
WHERE Dname = ‘Research’ AND Dnumber = Dno;
CARTESIAN PRODUCT (CROSS PRODUCT) Operation
R RUS
UNION
Q15. Retrieve the Social Security numbers of all employees
who either work in department 5 or directly supervise an
employee who works in department 5.
DEP5_EMPS ←σDno=5(EMPLOYEE)
RESULT1←πSsn(DEP5_EMPS)
RESULT2(Ssn)←πSuper_ssn(DEP5_EMPS)
RESULT ← RESULT1 ∪ RESULT2
or
Result ←πSsn (σDno=5 (EMPLOYEE) ) ∪πSuper_ssn (σDno=5 (EMPLOYEE)).
UNION
SELECT DISTINCT ssn FROM EMPLOYEE WHERE Dno = 5
UNION
SELECT DISTINCT Super_ssn FROM EMPLOYEE WHERE Dno = 5;
UNION
SELECT DISTINCT ssn FROM EMPLOYEE WHERE Dno = 5
UNION ALL
SELECT DISTINCT Super_ssn FROM EMPLOYEE WHERE Dno = 5;
UNION
Q16. 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.
P2_EMPS ←σPno=2(WORKS_ON)
RESULT1←πSsn(P2_EMPS)
P1_EMPS ←σPno=1(WORKS_ON)
RESULT2←πSsn(P1_EMPS)
RESULT ← RESULT1 - RESULT2
SET DIFFERENCE
Q17. Retrieve the Social Security numbers of all employees
who work on project 2 but not work on project 1.
P2_EMPS ←σPno=2(EMPLOYEE)
RESULT1←πSsn(P2_EMPS)
P1_EMPS ←σPno=1(EMPLOYEE)
RESULT2←πSsn(P1_EMPS)
RESULT ← RESULT1 ∩ RESULT2
INTERSECTION
Q18. Retrieve the Social Security numbers of all employees
who work on project 2 and 1.
DEPT_MGR←DEPARTMENT ⋈Mgr_ssn=SsnEMPLOYEE
R ⋈<join condition>S
● each <join condition> is of the form Ai θ Bj, Ai is an attribute of R, Bj
is an attribute of S
●
θ (theta) is one of the comparison operators {=, <, ≤, >, ≥, ≠}.
●
A JOIN operation with such a general join condition is called a THETA
JOIN
●
Tuples whose join attributes are NULL or for which the join
condition is FALSE do not appear in the result
Variations of JOIN : EQUIJION
●
The most common use of JOIN involves join conditions with
equality comparisons only.
●
Such a JOIN, where the only comparison operator used is
=, is called an EQUIJOIN
ACTUAL_DEPENDENTS←EMPLOYEE ⋈ Ssn=Essn
DEPENDENT
*Salary
(EMPLOYEE)
Generalized Projection
REPORT←ρ(Ssn, Net_salary, Bonus, Tax)(πSsn, Salary – Deduction, 2000 *Years_service, 0.25
*Salary
(EMPLOYEE))
Aggrigate Function R
Aggregate Functions
Q24: Find the sum of the salaries of all employees, the maximum
salary, the minimum salary, and the average salary.
ρ(Total_sal,Max_sal, Min_sal, Avg_sal)(ℑSUM salary, MAX salary, MIN salary, AVERAGE Salary(EMPLOYEE))
Dno
ℑCOUNT ssn, AVERAGE Salary(EMPLOYEE)
Pnumber, Pname
ℑPnumber, Pname, COUNT Ssn (PROJECT ⋈ Pnumber=Pno
WORKS_ON))
SELECT S.ssn
FROM (select ssn, super_ssn from employee ) S, (select ssn from
employee where Fname='James' AND Lname='Borg') B
WHERE S.super_ssn = B.ssn
Recursive Closure Operations
Q29: Retrieve all supervisees of a supervisory employee e at all levels