SQL
SQL
>Write a query to get the average salary for each job ID excluding programmer.
SELECT job_id, AVG(salary)
FROM employees
WHERE job_id <> 'IT_PROG'
GROUP BY job_id;
>Write a query to list the number of jobs available in the employees table.
SELECT COUNT(DISTINCT job_id)
FROM employees;
>Write a query to get the job ID and maximum salary of the employees where maximum
salary is greater than or equal to $4000.
SELECT job_id, MAX(salary)
FROM employees
GROUP BY job_id
HAVING MAX(salary) >=4000;
>Write a query to get the average salary and number of employees working the
department 90
SELECT AVG(salary),count(*)
FROM employees
WHERE department_id = 90;
>Write a query to get the number of employees with the same job.
SELECT job_id, COUNT(*)
FROM employees
GROUP BY job_id;
>Write a query to get the difference between the highest and lowest salaries.
SELECT MAX(salary) - MIN(salary) DIFFERENCE
FROM employees;
>Write a query to find the 4th minimum salary in the employees table
-SELECT DISTINCT salary
FROM employees e1
WHERE 4 = (SELECT COUNT(DISTINCT salary)
FROM employees e2
WHERE e2.salary <= e1.salary);
>Write a query to find the name (first_name, last_name) and the salary of the
employees who have
a higher salary than the employee whose last_name='Bull'
-SELECT FIRST_NAME, LAST_NAME, SALARY
FROM employees
WHERE SALARY >
(SELECT salary FROM employees WHERE last_name = 'Bull');
>write a query to find the name (first_name, last_name), and salary of the
employees who earns more than the average salary and works in any of the IT
departments
-SELECT first_name, last_name, salary
FROM employees
WHERE department_id IN
(SELECT department_id FROM departments WHERE department_name LIKE 'IT%')
AND salary > (SELECT avg(salary) FROM employees);
>Write a query to find the name (first_name, last_name) and the salary of the
employees who have a higher salary than the employee whose last_name='Bull'
-SELECT FIRST_NAME, LAST_NAME, SALARY FROM employees WHERE SALARY > (SELECT salary
FROM employees WHERE last_name = 'Bull');
>Write a query to display the employee ID, first name, last name, and department
names of all employees.
-SELECT employee_id, first_name, last_name,
(SELECT department_name FROM departments d
WHERE e.department_id = d.department_id) department
FROM employees e ORDER BY department;
>Write a query to find the name (first_name, last_name), and salary of the
employees who earn the same salary as the minimum salary for all departments.
-SELECT * FROM employees
WHERE salary = (SELECT MIN(salary) FROM employees);
>Write a query to display the employee ID, first name, last name, salary of all
employees whose salary is above average for their departments
-SELECT employee_id, first_name
FROM employees AS A
WHERE salary >
(SELECT AVG(salary) FROM employees WHERE department_id = A.department_id);
>Write a query to find the name (first_name, last_name) of the employees who are
manager
- SELECT e.first_name, e.last_name FROM employees AS e WHERE e.employee_id IN
(SELECT e.manager_id FROM employees AS e);