Week 5 - SQL - Select
Week 5 - SQL - Select
SQL
CS220 – DATABASE SYSTEMS
Notes:
1) The "*" is used to select all attributes.
is equivalent to:
Foreign keys:
emp: emp.supereno to emp.eno, emp.dno to dept.dno
proj dept
pno pname budget dno
P1 Instruments 150000 D1 dno dname mgreno
P2 DB Develop 135000 D2 D1 Management E8
P3 Budget 250000 D3 D2 Consulting E7
P4 Maintenance 310000 D2 D3 Accounting E5
P5 CAD/CAM 500000 D2 D4 Development null
ONE RELATION QUERY EXAMPLE
Return the employee name and salary of all employees whose title
is 'EE':
SELECT ename, salary
FROM emp
WHERE title = 'EE'
Emp Relation
eno ename bdate title salary supereno dno
E1 J. Doe 01-05-75 EE 30000 E2 null Result
E2 M. Smith 06-04-66 SA 50000 E5 D3 ename salary
E3 A. Lee 07-05-66 ME 40000 E7 D2 J. Doe 30000
E4 J. Miller 09-01-50 PR 20000 E6 D3 L. Chu 30000
E5 B. Casey 12-25-71 SA 50000 E8 D3
E6 L. Chu 11-30-65 EE 30000 E7 D2
E7 R. Davis 09-08-77 ME 40000 E8 D1
E8 J. Jones 10-11-72 SA 50000 null D1
Algorithm: Scan each tuple in table and check
if matches condition in WHERE clause.
ONE RELATION QUERY EXAMPLES
Return the birth date and salary of employee 'J. Doe':
SELECT title
FROM emp
Emp Relation Result
eno ename bdate title salary title
E1 J. Doe 01-05-75 EE 30000 dno E2
supereno null E
E2 M. Smith 06-04-66 SA 50000 E5 D3 SA
E3 A. Lee 07-05-66 ME 40000 E7 D2 ME
E4 J. Miller 09-01-50 PR 20000 E6 D3 PR
E5 B. Casey 12-25-71 SA 50000 E8 D3 SA
E6 L. Chu 11-30-65 EE 30000 E7 D2 EE
E7 R. Davis 09-08-77 ME 40000 E8 D1 ME
E8 J. Jones 10-11-72 SA 50000 null D1 SA
DUPLICATES IN SQL - DISTINCT CLAUSE
Result
EE
SA
ME
PR
SQL PRACTICE QUESTIONS SINGLE TABLE
Relational database schema:
emp (eno, ename, bdate, title, salary, supereno,
dno) proj (pno, pname, budget, dno)
dept (dno, dname, mgreno)
workson (eno, pno, resp, hours)
1) Return the project names that have a budget > 250000.
2)Return the employee numbers who make less than $30000.
3)Return the list of workson responsibilities (resp) with no
duplicates.
4)Return the employee (names) born after July 1, 1970 that have
a salary > 35000 and have a title of 'SA' or 'PR'.
Write the equivalent relational algebra expression.
JOIN QUERY EXAMPLE
Multiple tables can be queried in a single SQL statement by
listing them in the FROM clause.
Note that if you do not specify any join condition to relate them in
the WHERE clause, you get a cross product of the tables.
1)For each employee, return their name and their department name.
2) Return the list of project names for the department with name
'Consulting'.
3)Return workson records (eno, pno, resp, hours) where project budget
is > $50000 and hours worked is < 20.
4)Return a list of all department names, the names of the projects of that
department, and the name of the manager of each department.
CALC ULATED FIELDS
▶ Mathematical expressions are allowed in the SELECT clause to
perform simple calculations.
▶ When an expression is used to define an attribute,
the DBMS gives the attribute a unique name such as
col1, col2, etc.
▶ Example: Return how much em ployee 'A. Le e' will get
paid for his work on each project.
Result
ename pname pay
A. Lee Budget 192.31
A. Lee Maintenance 923.08
Note: AS keyword is
RENAMING AND ALIASING (2)
Renaming is also used when two or more copies of the same table
are in a query. Using aliases allows you to uniquely identify
what table you are talking about.
SELECT ename
FROM emp
WHERE salary >= 20000 and salary <= 45000
SELECT ename
FROM emp
WHERE dno IN ('D1','D2','D3')
Note that this is equivalent to using OR:
SELECT ename
FROM emp
WHERE dno = 'D1' OR dno = 'D2' OR dno =
'D3'
However, we will see more practical uses of IN and NOT
when we study nested subqueries.
ADV ANCE D CON DITIONS - NULL
Remember NULL is used to indicate that a given attribute does not
have a value. To determine if an attribute is NULL, we use the clause
IS NULL.
Note that you cannot test NULL values using = and <>.