NOTES 12 Assignment Sub Query, Joins Solutions

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

1.

WAQ to find count of first_names from Employees


SELECT count(First_name)
From Employees

2. WAQ to fetch the first 50% record from a table Employees


SELECT *
FROM Employees
WHERE Employee_id<=(SELECT count(Employee_id)/2 FROM Employees)

3.WAQ to fetch the last record from the table Employees


SELECT *
FROM Employees
Where Employee_id =(SELECT MAX(Employee_id) FROM Employees)

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'

5. WAQ to display first_name,salary,dept_no for those employees who earn such


amount of salary which is the smallest salary of all the department.

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

SELECT d.department_name,AVG(e.salary) AS avg_salary


FROM employees e
JOIN departments d
ON e.dept_id=d.dept_id
GROUP BY d.department_name
Order by avg_salary DESC;

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

SELECT c.fn,c.ln,c.email, o.order_id


FROM customers c
INNER JOIN orders o
ON c.cus_id=o.cus_id
ORDER BY o.order_date DESC

order_id customer_id order_date total_amount first_name last_name


email
-------- ----------- ------------------- ------------ ---------- ----------
--------------------
1 1001 2021-01-01 00:00:00 100.00 John Doe
john.doe@example.com
2 1002 2021-01-02 00:00:00 200.00 Jane Smith
jane.smith@example.com
3 1003 2021-01-03 00:00:00 300.00 Bob Johnson
bob.johnson@example.com

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'
);

You might also like