Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
*
*
*
*
*
*
FROM
FROM
FROM
FROM
FROM
FROM
EMPLOYEE
DEPARTMENT
DEPT_LOCATIONS
PROJECT
WORKS_ON
DEPENDENT
Query 0
SELECT Bdate, Address
FROM EMPLOYEE
WHERE Fname='John' AND
Minit='B' AND
Lname='Smith';
Query Exercise
List the names of all employees who have a
dependent with the same first name as
themselves.
Query Exercise
List the names of all employees who have a
dependent with the same first name as themselves.
SELECT LNAME, FNAME
FROM EMPLOYEE, DEPENDENT
WHERE SSN=ESSN AND FNAME=DEPENDENT_NAME
Retrieve the names of all employees in Department 5
who work more than 10 hours per week on the
ProductX project.
SELECT LNAME, FNAME
FROM EMPLOYEE, WORKS_ON, PROJECT
WHERE DNO=5 AND SSN=ESSN AND
PNO=PNUMBER AND PNAME='ProductX' AND
HOURS>10
x Dnumber
Name
x Dnumber
Name
becomes -->
Query Exercise
Find the names of all employees who are
directly supervised by 'Franklin Wong.
For each department, retrieve the
department name, and the average salary of
employees working in that department.
Retrieve the average salary of all female
employees.
Query Exercise
Find the names of all employees who are directly supervised by 'Franklin
Wong.
SELECT LNAME, FNAME
FROM EMPLOYEE
WHERE SUPERSSN IN ( SELECT SSN
FROM EMPLOYEE
WHERE FNAME='Franklin' AND LNAME='Wong' );
For each department, retrieve the department name, and the average
salary of employees working in that department.
SELECT DNAME, AVG (SALARY)
FROM DEPARTMENT, EMPLOYEE
WHERE DNUMBER=DNO
GROUP BY DNAME;
Retrieve the average salary of all female employees.
SELECT AVG (SALARY)
FROM EMPLOYEE
WHERE SEX='F;
Examples:
same as...
SELECT *
FROM EMPLOYEE
WHERE (Salary >= 20000 AND Salary <= 30000) AND Dno=4;
Functions
A function is a simple to moderately complex
operation that the usual SQL commands dont
perform but that comes up often in practice. SQL
provides functions that perform tasks that the
application code in the host language (within
which you embed your SQL statements) would
otherwise need to perform.
SQL has two main categories of functions: set
(or aggregate) functions and value/scalar
functions.
COUNT
The COUNT function tells you how many
rows are in the table or how many rows in
the table meet certain conditions. The
simplest usage of this function is as
follows:
SELECT COUNT (*)
FROM FOODS ;
SELECT COUNT
(DISTINCT Fat)
FROM FOODS ;
SELECT COUNT
(Carbohydrate)
FROM FOODS ;
AVG
The AVG function calculates and returns
the average of the values in the specified
column. Of course, you can use the AVG
function only on columns that
contain numeric data, as in the following
example:
SELECT AVG (Fat)
FROM FOODS ;
SELECT AVG (Fat)
FROM FOODS
WHERE Food <> Butter ;
MAX
The MAX function returns the maximum
value found in the specified column.
SELECT MAX (Fat)
FROM FOODS ;
MIN
The MIN function returns the minimum
value found in the specified column.
SELECT MIN
(Carbohydrate)
FROM FOODS ;
SUM
The SUM function returns the sum of all
the values found in the specified
column.
SELECT SUM (Calories)
FROM FOODS ;
Query Exercise
1.
For each project, list the project name and the total hours per week (by
all employees) spent on that project.
2.
3.
4.
For each department, retrieve the department name, and the average
salary of employees working in that department.
5.
Query Exercise
For each project, list the project name and the total hours per week (by all
employees) spent on that project.
SELECT PNAME, SUM (HOURS)
FROM PROJECT, WORKS_ON
WHERE PNUMBER=PNO
GROUP BY PNAME
Retrieve the names of employees who work on every project
SELECT LNAME, FNAME
FROM EMPLOYEE
WHERE NOT EXISTS ( SELECT PNUMBER
FROM PROJECT
WHERE NOT EXISTS ( SELECT *
FROM WORKS_ON
WHERE PNUMBER=PNO AND ESSN=SSN ) )
Query Exercise
Delete an employee