Lab 3 SQL
Lab 3 SQL
Ruba Sultan
GROUP FUNCTIONS
MIN
MAX
COUNT
STDDEV
VARIANCE
2
QUESTIONS
Question1
Write a query to display maximum and minimum
salaries for department 10 and 30.
Question2
Write a query to display hire date for first hired
employee and hire date for the newest hired employee.
3
QUESTIONS
Question3
Write a query to display total ,average, maximum and
minimum for the paid salaries.
Question4
Write a query to display total ,average, maximum and
minimum for the paid commissions.
4
GROUP BY
Question5
Write a query to display the total paid salaries for
each department.
5
HAVING
WHERE clause can only be used with non-aggregate
functions, it couldn’t be used with aggregate functions.
HAVING clause used when there is a condition on
aggregate values.
SELECT column,….,aggregate func.
FROM table name
WHERE condition(s)
GROUP BY column1,..,column m
HAVING condition(s)
ORDER BY column1,..,column m
6
QUESTIONS
Question6
Write a query to display the total paid salaries for
each department, exclude any departments that their
total salary less than $10000.
Question7
State whether the following query valid or not
SELECT deptno,job,SUM(sal)
FROM emp
GROUP BY deptno;
7
QUESTIONS
Question8
State whether the following query valid or not
SELECT deptno,SUM(sal)
FROM emp
GROUP BY deptno,job;
8
USING A SUBQUERY
TO SOLVE A PROBLEM
Main Query:
Subquery
?
What is Abel’s salary?
SUBQUERY SYNTAX
10
SUBQUERIES
Question9
Write a query to display all employee’s names, their
salaries and their job whose jobs same as job of
William Smith.
11
SUBQUERY SYNTAX
SELECT select_list
FROM table
WHERE expr operator
(SELECT select_list
FROM table);
• Single-row subquery
Main query
returns
Subquery ST_CLERK
• Multiple-row subquery
Main query
returns ST_CLERK
Subquery
SA_MAN
SINGLE-ROW SUBQUERIES
= Equal to
ERROR at line 4:
ORA-01427: single-row subquery returns more than
one row
WILL THIS STATEMENT RETURN ROWS?
no rows selected
MULTIPLE-ROW SUBQUERIES
…
USING THE ALL OPERATOR
IN MULTIPLE-ROW SUBQUERIES
SELECT emp.last_name
FROM employees emp
WHERE emp.employee_id NOT IN
(SELECT mgr.manager_id
FROM employees mgr);
no rows selected
QUESTIONS
Question10
Write a query to display all employees names, their
hire dates and their departments numbers that was
hired after Tayler Fox and get more than employee
184
Question11
Write a query to display all employees information who
have the get maximum paid salary.
26
QUESTIONS
Question12
Write a query to display departments and their
total paid salaries, exclude any department
that its total salary less than that of
department 30.
Question13
State weather the following query is valid or not
SELECT empno , ename
FROM emp
WHERE sal = (SELECT MIN(sal)
FROM emp 27
GROUP BY deptno);
MULTIPLE ROW OPERATOR
Operator Description
28
QUESTIONS
Question14
Write a query to display all employee’s
information whose less than any Stock Clerk
and they are not Stock Clerk.
Question15
Write a query to display employee’s numbers,
names and their salaries for employees who
earn more than the average salary and who
work in department with any employee with a
letter T in their names. 29