Professional Documents
Culture Documents
NOTES 12 Assignment Sub Query, Joins Solutions
NOTES 12 Assignment Sub Query, Joins Solutions
NOTES 12 Assignment Sub Query, Joins Solutions
4. WAQ to display the dept_id,first_name and the job_id for all the employees who
work in
IT_PROG
SELECT department_id,first_name,job_id
FRom employees
Where job_id='IT_PROG'
SELECT first_name,salary,dept_no
From Employees
WHERE salary =(SELECT min(salary)
FROM employees )
6. You have two tables in an Oracle database: `employees` and `departments`. The
`employees` table contains the following columns: `employee_id`, `first_name`,
`last_name`, `email`, `hire_date`, `salary`, and `department_id`. The `departments`
table contains the following columns: `department_id`, `department_name`, and
`location`.
table 1
EMPLOYEES
emp_id
fn
ln
email
hire_date
salary
dept_id
table 2
DEPARTMENTS
dept_id
dept_name
location
Write a SQL query that returns the department name and the average salary of
employees in that department.
Use a subquery to calculate the average salary for each department.
Sort the results by average salary in descending order.
Here's an example of the expected output:
department_name avg_salary
--------------- ----------
Sales 75000
Marketing 60000
Accounting 55000
7. You have two tables in an Oracle database: `orders` and `customers`. The
`orders` table contains the following columns: `order_id`, `customer_id`,
`order_date`, and `total_amount`. The `customers` table contains the following
columns: `customer_id`, `first_name`, `last_name`, `email`, and `phone`.
Write a SQL query that returns a list of all orders along with the first name, last
name, and email of the customer who placed the order.
Sort the results by order date in descending order.
Use an INNER JOIN to join the two tables.
Here's an example of the expected output:
Orders:
order_id,
cus_id,
order_date,
total_amount
Customers:
cus_id,
fN,
ln,
email,
phone
8.Retrieve the names of all employees who work in the same department as the
employee named John Smith.
SELECT name
FROM employees
WHERE department_id=(SELECT department_id
FROM employees
WHERE name='John Smith')
1 = 8
5 =12
10 =88
7 =2
9. Retrieve the names of all departments that have more than 10 employees.
SELECT name
FROM EMployees
WHERE department_id IN
(
SELECT department_id
FROM employees
group by department_id
HAVING count(*)>10
)
10.Retrieve the names of all employees who earn more than rs50,000 per year
SELECT name
FROM employee
WHERE salary > 50000;
11.Retrieve the names of all employees who work in the department named 'Sales'
SELECT name
FROM employee
WHERE department_id = (
SELECT id FROM department WHERE name = 'Sales'
);
12.Retrieve the names of all employees who have a manager with the last name
'Smith'
SELECT name
FROM employee
WHERE manager_id = (
SELECT id FROM employee WHERE last_name = 'Smith'
);