Simple Queries:
-
List all the employee details.
SELECT * FROM employees; -
List all the department details.
SELECT * FROM departments; -
List all job details.
SELECT * FROM jobs; -
List all the locations.
SELECT * FROM locations; -
List out the first name, last name, salary, and commission for all employees.
SELECT first_name, last_name, salary, commission FROM employees; -
List out employee_id, last_name, and department_id for all employees, and rename:
employee_idas "ID of the employee"last_nameas "Name of the employee"department_idas "Department ID"
SELECT employee_id AS "ID of the employee", last_name AS "Name of the employee", department_id AS "Department ID" FROM employees; -
List out the employees' annual salary with their names only.
SELECT first_name, last_name, (salary * 12) AS annual_salary FROM employees;
Where Conditions:
-
List the details about "SMITH".
SELECT * FROM employees WHERE last_name = 'SMITH'; -
List out the employees who are working in department 20.
SELECT * FROM employees WHERE department_id = 20; -
Find out the employees who are not working in department 10 or 30.
SELECT * FROM employees WHERE department_id NOT IN (10, 30); -
List out the employees whose name starts with "S".
SELECT * FROM employees WHERE first_name LIKE 'S%'; -
List out the employees whose names start with "S" and end with "H".
SELECT * FROM employees WHERE first_name LIKE 'S%H'; -
List out the employees whose name length is 4 and starts with "S".
SELECT * FROM employees WHERE LENGTH(first_name) = 4 AND first_name LIKE 'S%'; -
List out the employees who are working in department 10 and draw salaries more than 3500.
SELECT * FROM employees WHERE department_id = 10 AND salary > 3500; -
List out the employees who are not receiving commissions.
SELECT * FROM employees WHERE commission IS NULL;
Order By Clause:
-
List out the employee ID and last name in ascending order based on the employee ID.
SELECT employee_id, last_name FROM employees ORDER BY employee_id ASC; -
List out the employee ID and name in descending order based on the salary column.
SELECT employee_id, first_name, last_name FROM employees ORDER BY salary DESC; -
List out the employee details according to their last name in ascending order and salaries in descending order.
SELECT * FROM employees ORDER BY last_name ASC, salary DESC; -
List out the employee details according to their last name in ascending order and then on department ID in descending order.
SELECT * FROM employees ORDER BY last_name ASC, department_id DESC;
Group By & Having Clause:
- How many employees are working in different departments in the organization?
SELECT department_id, COUNT(*) AS employee_count FROM employees GROUP BY department_id;
Department-Wise Queries:
-
List out the department-wise maximum salary, minimum salary, and average salary of the employees.
SELECT department_id, MAX(salary) AS max_salary, MIN(salary) AS min_salary, AVG(salary) AS avg_salary FROM employees GROUP BY department_id; -
List out the job-wise maximum salary, minimum salary, and average salary of the employees.
SELECT job_id, MAX(salary) AS max_salary, MIN(salary) AS min_salary, AVG(salary) AS avg_salary FROM employees GROUP BY job_id; -
List out the number of employees joined in every month in ascending order.
SELECT MONTH(hire_date) AS month, COUNT(*) AS employee_count FROM employees GROUP BY MONTH(hire_date) ORDER BY month ASC; -
List out the number of employees joined each month and year in ascending order based on year and month.
SELECT YEAR(hire_date) AS year, MONTH(hire_date) AS month, COUNT(*) AS employee_count FROM employees GROUP BY YEAR(hire_date), MONTH(hire_date) ORDER BY year ASC, month ASC; -
List out the department IDs having at least four employees.
SELECT department_id FROM employees GROUP BY department_id HAVING COUNT(*) >= 4; -
How many employees joined in January?
SELECT COUNT(*) AS employee_count FROM employees WHERE MONTH(hire_date) = 1; -
How many employees joined in January or September?
SELECT COUNT(*) AS employee_count FROM employees WHERE MONTH(hire_date) IN (1, 9); -
How many employees joined in 1985?
SELECT COUNT(*) AS employee_count FROM employees WHERE YEAR(hire_date) = 1985; -
How many employees joined in March 1985?
SELECT COUNT(*) AS employee_count FROM employees WHERE YEAR(hire_date) = 1985 AND MONTH(hire_date) = 3; -
Which is the department ID having three or more employees who joined in April 1985?
SELECT department_id FROM employees WHERE YEAR(hire_date) = 1985 AND MONTH(hire_date) = 4 GROUP BY department_id HAVING COUNT(*) >= 3;
Subqueries:
-
List out the employees who earn more than every employee in a specific department.
SELECT * FROM employees WHERE salary > ALL ( SELECT salary FROM employees WHERE department_id = <specific_department_id> ); -
List out the employees who earn more than the lowest salary in a specific department.
SELECT * FROM employees WHERE salary > ( SELECT MIN(salary) FROM employees WHERE department_id = <specific_department_id> ); -
Find out whose department has no employees.
SELECT department_id FROM departments WHERE department_id NOT IN ( SELECT DISTINCT department_id FROM employees ); -
Find out which department does not have any employees.
SELECT * FROM departments WHERE department_id NOT IN ( SELECT DISTINCT department_id FROM employees );
Joins:
-
List employees with their department names.
SELECT e.employee_id, e.first_name, d.department_name FROM employees e JOIN departments d ON e.department_id = d.department_id; -
Display employees with their designations (jobs).
SELECT e.employee_id, e.first_name, j.job_title FROM employees e JOIN jobs j ON e.job_id = j.job_id;

