0% found this document useful (0 votes)
88 views

Tema Nr. 8: Observaţie!

The document contains 12 questions about writing SQL queries to analyze data from employee and department tables. The questions ask the student to identify and correct errors in sample queries, write queries to return aggregate data like minimum/maximum salaries grouped by department, and queries to return employee details matching certain criteria. The student provides responses directly in the document by writing the necessary SQL queries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views

Tema Nr. 8: Observaţie!

The document contains 12 questions about writing SQL queries to analyze data from employee and department tables. The questions ask the student to identify and correct errors in sample queries, write queries to return aggregate data like minimum/maximum salaries grouped by department, and queries to return employee details matching certain criteria. The student provides responses directly in the document by writing the necessary SQL queries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Tema nr.

8
Observaţie!
Scrieţi rezolvarea direct în acest document!

1. Each of the following SQL queries has an error. Find the error and correct it. Use Oracle
Application Express to verify that your corrections produce the desired results.

a. SELECT manager_id ,AVG(salary);


FROM employees
WHERE AVG(salary) <16000
GROUP BY manager_id;
HAVING AVG(salary)<16000;

b. SELECT cd_number, COUNT (title)


FROM d_cds
WHERE cd_number < 93;
GROUP BY cd_number;

c. SELECT ID,artist AS Artist


FROM d_song
WHERE duration IN(‘3min’,’6min’,’10min’)

d. SELECT loc_type, rental_fee AS Fee


FROM d_venues
WHERE id <100
ORDER BY 2;

2. Write a query that will return both the maximum and minimum average salary grouped by
department from the employees table.

SELECT MAX(AVG(e.salary)),MIN(AVG(e.salary))
FROM employees e , departments d
WHERE e.department_id = d.department_id
Group BY e.department_id ;

3. Write a query that will return the average of the maximum salaries in each department for the
employees table.

SELECT FLOOR(AVG(MAX(e.salary)))
FROM employees e , departments d
WHERE e.department_id=d.department_id
Group BY d.department_name

4. Which Oracle employees have the same department ID as the IT department?


SELECT first_name , last_name
FROM employees
WHERE department_id= (Select department_id from departments WHERE department_name='IT');

5. What are the department names of the Oracle departments that have the same location ID as
Seattle?

SELECT department_name
FROM departments
WHERE location_id=(SELECT location_id from locations WHERE city='Seattle');

6. Write a query to return all those employees who have a salary greater than that of Lorentz and are
in the same department as Abel.

SELECT first_name , last_name


FROM employees
WHERE salary >(SELECT salary
FROM employees
WHERE last_name='Lorentz')
AND department_id=(SELECT department_id
FROM employees
WHERE last_name='Abel');

7. Write a query to return all those employees who have the same job id as Rajs and were hired
after Davies.

SELECT first_name, last_name


FROM employees
WHERE job_id=(SELECT job_id FROM employees WHERE last_name='Rajs') AND
hire_date>(SELECT hire_date FROM employees WHERE last_name='Davies');

8. Write a query to return a list of department id’s and average salaries where the average salary is
greater than Ernst’s salary.

SELECT department_id, AVG(salary)


FROM employees
GROUP BY department_id
HAVING AVG(salary)>(SELECT salary FROM employees WHERE last_name='Ernst') ;

9. Return the department ID and minimum salary of all employees, grouped by department ID,
having a minimum salary greater than the minimum salary of those employees whose department
ID is not equal to 50.

SELECT department_id, MIN(salary)


FROM employees
GROUP BY department_id
HAVING MIN(salary)>(SELECT MIN(salary) FROM employees WHERE department_id!=50);
10. Find the last names of all employees whose salaries are the same as the minimum salary for any
department.

select last_name
from employees
where salary=any(select min(salary) from employees group by department_id);

11. The goal of the following query is to display the minimum salary for each department whose
minimum salary is less than the lowest salary of the employees in department 50. However, the
subquery does not execute because it has five errors. Find them, correct them, and run the query.

SELECT department_id
FROM employees
WHERE MIN(salary)
HAVING MIN(salary) >
GROUP BY department_id
SELECT MIN(salary)
WHERE department_id < 50;

select min(salary)
from employees
where min(salary)<(select min(salary) from employees where department_id=50);

12. Write a query that lists the highest earners for each department. Include the last_name,
department_id and the salary for each employee.

SELEct last_name,salary,department_id
FROM employees
WHERE salary=any(SELECT max(salary) FROM employees GROUP by department_id);

You might also like