60SQLQuery Answers
60SQLQuery Answers
SIMPLE Queries:
--1. List all the employees details
SELECT * FROM EMPLOYEES
--2. List all the department details
SELECT * FROM DEPARTMENTS
--3. List all jobs details and order by the Max-Salary.
SELECT * FROM JOBS
--4. List all the locations order by the city in alphabetical order.
select * from locations order by city
--5. . List only the fields first name,last name,salary, commission for all employees
SELECT FIRST_NAME,LAST_NAME,SALARY,COMMISSION_PCT FROM
EMPLOYEES
--6. List out employee_id,last name,department id for all employees and rename
employee id as ID of the employee, last name as Name of the employee, department
id as department ID
SELECT EMPLOYEE_ID AS "ID OF THE EMPLOYEE",LAST_NAME AS "NAME
OF THE EMPLOYEE",DEPARTMENT_ID AS "DEPARTMENT_ID FROM
EMPLOYEES
--7. List out the employees annual salary with their names only.
SELECT FIRST_NAME,LAST_NAME ,SALARY FROM EMPLOYEES
--WHERE Conditions:
--8. List the details about SMITH
SELECT * FROM EMPLOYEES WHERE LAST_NAME='Smith' or
FIRST_NAME='Smith'
--9. List out the employees who are working in department 20
SELECT * FROM EMPLOYEES WHERE DEPARTMENT_ID=20
--10. List out the employees who are earning salary between 3000 and 4500
SELECT * FROM EMPLOYEES WHERE SALARY BETWEEN 3000 AND 4500
--11. List out the employees who are working in department 10 or 20
SELECT * FROM EMPLOYEES WHERE DEPARTMENT IN(10,20)
--12. Find out the employees who are not working in department 10 or 30
SELECT * FROM EMPLOYEES WHERE DEPARTMENT NOT IN(10,30)
--13. List out the employees whose name starts with S
SELECT * FROM EMPLOYEES WHERE LAST_NAME LIKE 'S%'
--14. List out the employees whose name start with S and end with H
SELECT LAST_NAME FROM EMPLOYEES WHERE LAST_NAME LIKE 'S%H'
--15. List out the employees whose name length is 5 and start with S
SELECT LAST_NAME FROM EMPLOYEES WHERE LENGTH(LAST_NAME)=5
AND LAST_NAME LIKE'S%'
--16. List out the employees who are working in department 10 and draw the salaries
more than 3500
SELECT * FROM EMPLOYEES WHERE DEPARTMENT_ID=10 AND
SALARY>3500
--17. List out the employees who are not receiving commission.
SELECT * FROM EMPLOYEES WHERE COMMISSION_PCT IS NULL
ORDER BY Clause:
--18. List out the employee id, last name in ascending order based on the employee id.
SELECT EMPLOYEE_ID,LAST_NAME FROM EMPLOYEES ORDER BY
EMPLOYEE_ID
--19. List out the employee id, name in descending order based on salary column
SELECT EMPLOYEE_ID,LAST_NAME,FIRST_NAME FROM EMPLOYEES
ORDER BY SALARY
--20. 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
--21. 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
--24. List out the job wise maximum salary, minimum salary, average salaries of the
employees.
SELECT JOB_ID,ROUND(MAX(SALARY))AS "MAX",ROUND(MIN(SALARY))AS
"MIN",ROUND(AVG(SALARY))AS "AVG" FROM EMPLOYEES
GROUP BY JOB_ID
--25. List out the no.of employees joined in every month in ascending order.
SELECT COUNT(EMPLOYEE_ID),TO_CHAR(HIRE_DATE,'MON')
FROM EMPLOYEES GROUP BY TO_CHAR(HIRE_DATE,'MON')
ORDER BY COUNT(EMPLOYEE_ID) ASC
--26. List out the no.of employees for each month and year, in the ascending order based
on the year, month.
SELECT COUNT(EMPLOYEE_ID),TO_CHAR(HIRE_DATE,'MON-YY')
FROM EMPLOYEES GROUP BY TO_CHAR(HIRE_DATE,'MON-YY')
ORDER BY TO_CHAR(HIRE_DATE,'MON-YY')ASC
--27. List all the department ids having atleast four employees.
SELECT DEPARTMENT_ID,COUNT(EMPLOYEE_ID)AS "NO OF EMPLOYEES"
FROM EMPLOYEES
GROUP BY DEPARTMENT_ID
HAVING COUNT(DEPARTMENT_ID)>=4
--29. How many employees who are joined in January or September month.
SELECT COUNT(EMPLOYEE_ID) FROM EMPLOYEES
WHERE HIRE_DATE LIKE '%JAN%' OR HIRE_DATE LIKE '%SEP%'
--30. How many employees who are joined in 2006.
SELECT COUNT(EMPLOYEE_ID) FROM EMPLOYEES
WHERE TO_CHAR(HIRE_DATE,'MON-YY') LIKE '%-06'
--31. How many employees joined each month in 2006.
SELECT HIRE_DATE, COUNT(EMPLOYEE_ID) FROM EMPLOYEES
WHERE TO_CHAR(HIRE_DATE,'MON-YY') LIKE '%-06'
GROUP BY HIRE_DATE
ORDER BY HIRE_DATE
--32. How many employees who are joined in March 2006.
SELECT COUNT(EMPLOYEE_ID) FROM EMPLOYEES
WHERE TO_CHAR(HIRE_DATE,'MON-YY')='MAR-06'
--33. Which department id is having greater than or equal to 2 employees joined in April
2006.
SELECT DEPARTMENT_ID,COUNT(DEPARTMENT_ID) FROM EMPLOYEES
WHERE TO_CHAR(HIRE_DATE,'YYYY')=2006
GROUP BY DEPARTMENT_ID
HAVING COUNT(DEPARTMENT_ID)>=2
ORDER BY DEPARTMENT_ID
--34. Display the countries from the countries table , but display them only once.(use
distinct)
SELECT DISTINCT(COUNTRY_NAME) FROM COUNTRIES
--35. List all employees joined in the year 2005
SELECT COUNT(EMPLOYEE_ID) FROM EMPLOYEES
WHERE TO_CHAR(HIRE_DATE,'YY')='05'
GROUP BY HIRE_DATE
--36. Display how many employees joined after 15th of the month.
SELECT COUNT(EMPLOYEE_ID) FROM EMPLOYEES
WHERE TO_CHAR(HIRE_DATE,'DD') >15
--37. Display the employees who are working in Oxford (should use sub query)
SELECT
EMPLOYEES.EMPLOYEE_ID,EMPLOYEES.FIRST_NAME,EMPLOYEES.LAST_N
AME,LOCATIONS.CITY FROM EMPLOYEES
JOIN DEPARTMENTS
ON DEPARTMENTS.DEPARTMENT_ID=EMPLOYEES.DEPARTMENT_ID
JOIN LOCATIONS
ON LOCATIONS.LOCATION_ID=DEPARTMENTS.LOCATION_ID
WHERE LOCATIONS.CITY='Oxford'
--38. Display daily pay of employee of departmet 100 truncated to the nearest dollar
--(hint for one day pay formula is trunc(salary/30) Employees salary that you see is a
monthy salarr. To get annual salary multiply with 12 and then to get a daily salary divide
that by 365
SELECT EMPLOYEE_ID,FIRST_NAME,LAST_NAME,TRUNC(SALARY/365)
FROM EMPLOYEES WHERE DEPARTMENT_ID=100
--39. Display date in this format
--08:10:19 01/07/2013 Which is 'hh:mi:ss mm/dd/yyyy'
SELECT TO_CHAR(SYSDATE,'HH:MI:SS MM/DD/YYYY') FROM DUAL
Sub-Queries
--40. Display the details of the employee drawing the second highest salary
-Select * from employees where salary=(select max(salary) from employees where salary
<(select max(salary) from employees))
Joins
--41. List Employee id ,last name and their department name for all employees
SELECT
EMPLOYEES.EMPLOYEE_ID,EMPLOYEES.LAST_NAME,DEPARTMENTS.DEPA
RTMENT_NAME
FROM EMPLOYEES
JOIN DEPARTMENTS
ON EMPLOYEES.DEPARTMENT_ID=DEPARTMENTS.DEPARTMENT_ID
--46. List the departments having greater than or equal to 5 employees and display the
department names in ascending order.
SELECT
COUNT(EMPLOYEES.EMPLOYEE_ID),DEPARTMENTS.DEPARTMENT_NAME
FROM EMPLOYEES
JOIN DEPARTMENTS
ON EMPLOYEES.DEPARTMENT_ID=DEPARTMENTS.DEPARTMENT_ID
GROUP BY DEPARTMENTS.DEPARTMENT_NAME
HAVING COUNT(EMPLOYEES.EMPLOYEE_ID)>=5
ORDER BY DEPARTMENTS.DEPARTMENT_NAME ASC
--47. How many employees are there for each job_title (designation)
SELECT COUNT(EMPLOYEES.EMPLOYEE_ID),JOBS.JOB_TITLE FROM
EMPLOYEES,JOBS
WHERE JOBS.JOB_ID=EMPLOYEES.JOB_ID
GROUP BY JOBS.JOB_TITLE
--49. Display employee ID , employee last name and department id for employees who
did more than one job in the past.(use job_history table)
--clue(join job history and employees table)
SELECT
JOB_HISTORY.EMPLOYEE_ID,EMPLOYEES.LAST_NAME,EMPLOYEES.DEPAR
TMENT_ID FROM EMPLOYEES
JOIN JOB_HISTORY
ON EMPLOYEES.EMPLOYEE_ID=JOB_HISTORY.EMPLOYEE_ID
WHERE JOB_HISTORY.EMPLOYEE_ID IN (SELECT
JOB_HISTORY.EMPLOYEE_ID FROM JOB_HISTORY GROUP BY
JOB_HISTORY.EMPLOYEE_ID HAVING COUNT(*)>=2);
Self-Join:
--50. Display the employee details who earn more than their managers salaries.
--51. show the count of employees under a manager ( this is example for self join)
----Use the employees table twice in the select clause
SELECT E1.MANAGER_ID,COUNT(E1.EMPLOYEE_ID)
FROM EMPLOYEES E1,EMPLOYEES E2
WHERE E1.EMPLOYEE_ID =E2.EMPLOYEE_ID
GROUP BY E1.MANAGER_ID
ORDER BY MANAGER_ID
--52. Display employee details for all departments (even if there is no employee in a
department.
SELECT
DEPARTMENTS.DEPARTMENT_ID,EMPLOYEES.FIRST_NAME,EMPLOYEES.L
AST_NAME
FROM EMPLOYEES
FULL OUTER JOIN DEPARTMENTS
ON DEPARTMENTS.DEPARTMENT_ID=EMPLOYEES.DEPARTMENT_ID
--54. List distinct job_title from jobs table for employees whose department names are
Sales and AccountingDepartments.
SELECT DISTINCT
JOB_TITLE,DEPARTMENTS.DEPARTMENT_ID,DEPARTMENTS.DEPARTMENT
_NAME FROM EMPLOYEES
JOIN DEPARTMENTS
ON EMPLOYEES.DEPARTMENT_ID =DEPARTMENTS.DEPARTMENT_ID
JOIN JOBS
ON JOBS.JOB_ID =EMPLOYEES.JOB_ID
WHERE DEPARTMENTS.DEPARTMENT_name in('Sales','Accounting');
--55 Syntax for instr is INSTR (string, character[ or substring], position, occurrence)
--RETURNs a NUMBER
--Output of below query is what?
SELECT INSTR('CORPORATE FLOOR','OR', 3, 2) FROM DUAL;
SELECT INSTR('CORPORATE FLOOR','OR', -3, 2) FROM DUAL;
--56. There STATE_PROVINCE column values that are null in the locations table.
Write a query to display values as N/A where there is null in the STATE_PROVINCE
field.
---NVL function lets you substitute a value when a null value is encountered.
----Example : SELECT NVL(alphabets, 'XXX')FROM onetable;