Basic Query
Basic Query
3. Retrieve the employee ID, first name, and last name of all employees in the HR schema.
SELECT employee_id, first_name, last_name
FROM employees;
4. Get the job IDs, job titles, and minimum salaries for all job positions.
SELECT job_id, job_title, min_salary
FROM jobs;
5. Find the employee IDs, first names, and last names of employees who hold the job title
"'IT_PROG'"
SELECT employee_id, first_name, last_name
FROM employees
WHERE job_id = 'IT_PROG';
6. Retrieve the employee IDs, first names, and last names of employees who have a salary
greater than $10,000.
SELECT employee_id, first_name, last_name
FROM employees
WHERE salary > 10000;
7. List the employee IDs, first names, and last names of employees who work in department
id 50.
SELECT employee_id, first_name, last_name
FROM employees
WHERE department_id = 50;
8. Get the names of employees who have a salary greater than $10,000 and are in
department 30.
SELECT first_name, last_name
FROM employees
WHERE salary > 10000 AND department_id = 30;
9. List the job titles and hire dates of employees hired after January 1, 2008.
SELECT jobs.job_title, employees.hire_date
FROM employees
JOIN jobs ON employees.job_id = jobs.job_id
WHERE hire_date > DATE '2008-01-01';
10. Retrieve the employee IDs and last names of employees who have a commission
percentage between 0.1 and 0.2.
SELECT employee_id, last_name
FROM employees
WHERE commission_pct BETWEEN 0.1 AND 0.2;
11. Retrieve the distinct job titles from the employees table.
SELECT DISTINCT job_title
FROM employees
JOIN jobs ON employees.job_id = jobs.job_id;
12. Retrieve the employee details (employee ID, first name, last name, and department name)
for employees in the HR schema, along with their corresponding department information.
Inner Join:
An inner join returns only the rows that have matching values in both tables involved in
the join.
In this example, the LEFT JOIN is used to combine the "employees" and "departments"
tables. All rows from the "employees" table are included, and if there is a match on
department ID, the corresponding department name is included. If there is no match, NULL
values are included for the department-related columns.
14. Retrieve the employee details (employee ID, first name, last name, and department name)
for employees in the HR schema, along with their corresponding department information.
Include all departments, even if they have no employees.
Right Join:
A right join returns all the rows from the right table and the matched rows from the left
table. If there is no match, NULL values are included for the columns of the left table.
15. Retrieve the employee details (employee ID, first name, last name, and department name)
for employees in the HR schema, along with their corresponding department information.
Include all employees and departments, even if they do not have a matching entry.
16. Retrieve the employee ID, first name, last name, and hire date of employees in the HR
schema, sorted by hire date in ascending order.
SELECT employee_id, first_name, last_name, hire_date
FROM employees
ORDER BY hire_date ASC;
17. Retrieve the employee ID, first name, last name, and salary of employees in the HR
schema, sorted by salary in descending order.
SELECT employee_id, first_name, last_name, salary
FROM employees
ORDER BY salary DESC;
18. Retrieve the job title, minimum salary, and maximum salary of jobs in the HR schema,
sorted by maximum salary in descending order.
SELECT job_title, min_salary, max_salary
FROM jobs
ORDER BY max_salary DESC;
19. Insert a new employee record into the employees table in the HR schema.
Employee ID: 1000
First Name: John
Last Name: Doe
Email: [email protected]
Hire Date: Current system date (SYSDATE)
Job ID: IT_PROG
Salary: 5000
Department ID: 60
20. Retrieve the employee ID, first name, last name, and hire date of employees in the HR
schema who do not have a manager assigned.
SELECT employee_id, first_name, last_name, hire_date
FROM employees
WHERE manager_id IS NULL;
21. Retrieve the employee ID, first name, last name, and email of employees in the HR
schema who have an email address recorded.
SELECT employee_id, first_name, last_name, email
FROM employees
WHERE email IS NOT NULL;
22. Retrieve the top 5 employees with the highest salary from the HR schema.
SELECT employee_id, first_name, last_name, salary
FROM employees
ORDER BY salary DESC
FETCH FIRST 5 ROWS ONLY;
24. Retrieve the employee ID, first name, last name, and salary of employees in the HR
schema who have the minimum and maximum salaries.
SELECT employee_id, first_name, last_name, salary
FROM employees
WHERE salary = (SELECT MIN(salary) FROM employees)
OR salary = (SELECT MAX(salary) FROM employees);
25. Retrieve the employee ID, first name, last name, and commission amount of employees
in the HR schema who have the maximum commission amount.
SELECT employee_id, first_name, last_name, commission_pct
FROM employees
WHERE commission_pct = (SELECT MAX(commission_pct) FROM employees);
26. Retrieve the count of employees in the HR schema who have a salary greater than 5000.
SELECT COUNT(*) AS employee_count
FROM employees
WHERE salary > 5000;
27. Retrieve the average salary of employees in the HR schema who work in department 50.
SELECT AVG(salary) AS average_salary
FROM employees
WHERE department_id = 50;
28. Retrieve the sum of salaries for employees in the HR schema who have a job ID of
'SA_REP'.
SELECT SUM(salary) AS total_salary
FROM employees
WHERE job_id = 'SA_REP';
29. Retrieve the employee ID, first name, last name, and email of employees in the HR
schema whose email addresses contain 'example.com'.
SELECT employee_id, first_name, last_name, email
FROM employees
WHERE email LIKE '%@example.com%';
30. Retrieve the employee ID, first name, last name, and job title of employees in the HR
schema whose job titles start with 'Sales'.
31. Retrieve the employee ID, first name, last name, and phone number of employees in the
HR schema whose phone numbers end with '1234'.
SELECT employee_id, first_name, last_name, phone_number
FROM employees
WHERE phone_number LIKE '%1234';
32. Retrieve the employee ID, first name, last name, and email of employees in the HR
schema whose email addresses contain 'john' in any position.
SELECT employee_id, first_name, last_name, email
FROM employees
WHERE email LIKE '%john%';
33. List the all employees whose first name of the second character is a.
SELECT * FROM employees
WHERE first_Name LIKE '_a%';
34. List the all employees whose first name of the first character is ‘’ and contain at least 3
characters of the firstname.
SELECT * FROM employees
WHERE first_Name LIKE 'D___%'; (_ _ _ )
35. List the all employees whose first name of the first character is “B” ,”S” and “P”.
SELECT *
FROM employees
WHERE first_name LIKE 'B%' OR first_name LIKE 'S%' OR first_name LIKE 'P%';
36. Retrieve the employee ID, first name, last name, and job title of employees in the HR
schema whose job titles are either 'Programmer' or 'Analyst'.
SELECT employee_id, first_name, last_name, job_title
FROM employees
JOIN jobs ON employees.job_id = jobs.job_id
WHERE jobs.job_title IN ('Programmer', 'Analyst');
37. Retrieve the employee ID, first name and last name of employees in the HR schema
whose employee IDs are either 100, 200, 300, or 400.
SELECT employee_id, first_name, last_name
FROM employees
WHERE employee_id IN (100, 200, 300, 400);
38. Retrieve the employee ID, first name, and last name of employees in the HR schema who
have a salary greater than the average salary of their department.
39. Retrieve the employee ID, first name and last name of employees in the HR schema who
work in departments located in countries associated with region 1.
SELECT employee_id, first_name, last_name
FROM employees
WHERE department_id IN (
SELECT department_id
FROM departments
WHERE location_id IN (
SELECT location_id
FROM locations
WHERE country_id IN (
SELECT country_id
FROM countries
WHERE region_id = 1
)
)
);
40. Retrieve the employee ID, first name, last name, and department name of employees in
the HR schema, but rename the columns as "ID", "First Name", "Last Name", and
"Department".
SELECT emp.employee_id AS ID, emp.first_name AS "First Name", emp.last_name AS
"Last Name", dept.department_name AS Department
FROM employees emp
JOIN departments dept ON emp.department_id = dept.department_id;
41. Retrieve the employee ID, first name, and last name from the employees table, and the
department ID and department name from the departments table in the HR schema.
Combine the results of both tables using UNION.
42. Retrieve the employee ID, first name, and last name from the employees table for
employees whose job titles are 'Programmer', and retrieve the department ID and
department name from the departments table for departments located in region 2.
Combine the results of both tables using UNION, and order the result set by the
employee ID in ascending order.
43. Retrieve the department ID and the count of employees in each department from the
employees table.
SELECT department_id, COUNT(*) AS employee_count
FROM employees
GROUP BY department_id;
44. Retrieve the job title and the average salary for each job title.
SELECT job_title, AVG(salary) AS average_salary
FROM employees emp, jobs job
WHERE emp.job_id=job.job_id
GROUP BY job_title;
45. Retrieve the department ID, job ID, and the maximum salary for each department and job
from the employees table in the HR schema.
SELECT department_id, job_id, MAX(salary) AS max_salary
FROM employees
GROUP BY department_id, job_id;
46. Retrieve the department ID, job ID, and the total salary for each department and job from
the employees table in the HR schema.
SELECT department_id, job_id, SUM(salary) AS total_salary
FROM employees
GROUP BY department_id, job_id;
47. Retrieve the manager ID and the count of employees managed by each manager.
SELECT manager_id, COUNT(*) AS employee_count
FROM employees
GROUP BY manager_id;
48. Retrieve the department ID and the average salary for departments with an average salary
greater than 5000 from the employees table.
SELECT department_id, AVG(salary) AS average_salary
FROM employees
GROUP BY department_id
HAVING AVG(salary) > 5000;
49. Retrieve the job ID and the count of employees for jobs with more than 5 employees in
the employees table.
SELECT job_id, COUNT(*) AS employee_count
FROM employees
GROUP BY job_id
HAVING COUNT(*) > 5;
50. Retrieve the department ID and the maximum salary for departments with a maximum
salary greater than 10000.
SELECT department_id, MAX(salary) AS max_salary
FROM employees
GROUP BY department_id
HAVING MAX(salary) > 10000;
51. Retrieve the manager ID and the average salary for managers with an average salary less
than 8000 from the employees table in the HR schema.
52. Retrieve the employee ID, first name, last name, department name, and location city for
employees in the HR schema, along with their corresponding department and location
information.
SELECT e.employee_id, e.first_name, e.last_name, d.department_name, l.city
FROM employees e
JOIN departments d ON e.department_id = d.department_id
JOIN locations l ON d.location_id = l.location_id;
53. Retrieve the country name, number of departments, and total number of employees in
each country.
SELECT c.country_name, COUNT(DISTINCT d.department_id) AS department_count,
COUNT(e.employee_id) AS employee_count
FROM countries c
LEFT JOIN locations l ON c.country_id = l.country_id
LEFT JOIN departments d ON l.location_id = d.location_id
LEFT JOIN employees e ON d.department_id = e.department_id
GROUP BY c.country_name;
54. Retrieve the employee ID, first name, and last name of employees who have the same job
title and salary as their manager.
SELECT e.employee_id, e.first_name, e.last_name
FROM employees e
JOIN employees m ON e.manager_id = m.employee_id
JOIN jobs j ON e.job_id = j.job_id
WHERE j.job_id = m.job_id AND e.salary = m.salary;
55. Retrieve the average salary for each manager in the HR schema, along with the number
of employees they manage.
SELECT m.employee_id AS manager_id, m.first_name AS manager_first_name,
m.last_name AS manager_last_name,
AVG(e.salary) AS average_salary, COUNT(*) AS employee_count
FROM employees e
JOIN employees m ON e.manager_id = m.employee_id
GROUP BY m.employee_id, m.first_name, m.last_name;