Sample Queries
Sample Queries
1 01-FEB-13 5000
2 01-FEB-13 3000
3 01-FEB-13 4000
1 01-JAN-13 4500
2 01-JAN-13 3500
SQL Queries
1. Get all employee details from the employee table
Select * from employee
3. Get First_Name from employee table using alias name “Employee Name”
Select first_name Employee Name from employee
10. Get FIRST_NAME from employee table after removing white spaces from left side
select LTRIM(FIRST_NAME) from employee
12. Get First_Name from employee table after replacing 'o' with '$'
select REPLACE(FIRST_NAME,'o','$') from employee
13. Get First_Name and Last_Name as single column from employee table separated by a
'_'.
Select FIRST_NAME|| '_' ||LAST_NAME from EMPLOYEE
14. Get FIRST_NAME ,Joining year,Joining Month and Joining Date from employee table
Select FIRST_NAME, to_char(joining_date,'YYYY') JoinYear , to_char(joining_date,'Mon'),
to_char(joining_date,'dd') from EMPLOYEE
15. Get all employee details from the employee table order by First_Name Ascending
Select * from employee order by FIRST_NAME asc
16. Get all employee details from the employee table order by First_Name descending
Select * from employee order by FIRST_NAME desc
17. Get all employee details from the employee table order by First_Name Ascending and
Salary descending
Select * from employee order by FIRST_NAME asc,SALARY desc
18. Get employee details from employee table whose employee name is “John”
Select * from EMPLOYEE where FIRST_NAME='John'
19. Get employee details from employee table whose employee name are “John” and “Roy”
Select * from EMPLOYEE where FIRST_NAME in ('John','Roy')
20. Get employee details from employee table whose employee name are not “John” and
“Roy”
Select * from EMPLOYEE where FIRST_NAME not in ('John','Roy')
21. Get employee details from employee table whose first name starts with 'J'
Select * from EMPLOYEE where FIRST_NAME like 'J%'
22. Get employee details from employee table whose first name contains 'o'
Select * from EMPLOYEE where FIRST_NAME like '%o%'
23. Get employee details from employee table whose first name ends with 'n'
Select * from EMPLOYEE where FIRST_NAME like '%n'
"SQL Pattern Matching" Interview Questions
24. Get employee details from employee table whose first name ends with 'n' and name
contains 4 letters
Select * from EMPLOYEE where FIRST_NAME like '___n' (Underscores)
25. Get employee details from employee table whose first name starts with 'J' and name
contains 4 letters
Select * from EMPLOYEE where FIRST_NAME like 'J___' (Underscores)
26. Get employee details from employee table whose Salary greater than 600000
Select * from EMPLOYEE where Salary >600000
27. Get employee details from employee table whose Salary less than 800000
Select * from EMPLOYEE where Salary <800000
28. Get employee details from employee table whose Salary between 500000 and 800000
Select * from EMPLOYEE where Salary between 500000 and 800000
29. Get employee details from employee table whose name is 'John' and 'Michael'
Select * from EMPLOYEE where FIRST_NAME in ('John','Michael')
30. Get employee details from employee table whose joining year is “2013”
Select * from EMPLOYEE where to_char(joining_date,'YYYY')='2013'
31. Get employee details from employee table whose joining month is “January”
Select * from EMPLOYEE where to_char(joining_date,'MM')='01' or Select * from
EMPLOYEE where to_char(joining_date,'Mon')='Jan'
32. Get employee details from employee table who joined before January 1st 2013
Select * from EMPLOYEE where JOINING_DATE <to_date('01/01/2013','dd/mm/yyyy')
33. Get employee details from employee table who joined after January 31st
Select * from EMPLOYEE where JOINING_DATE >to_date('31/01/2013','dd/mm/yyyy')
39. Get names of employees from employee table who has '%' in Last_Name.
Select FIRST_NAME from employee where Last_Name like '%?%%'
40. Get Last Name from employee table after replacing special character with white space
Select translate(LAST_NAME,'%',' ') from employee
41. Get department,total salary with respect to a department from employee table.
Select DEPARTMENT,sum(SALARY) Total_Salary from employee group by department
42. Get department,total salary with respect to a department from employee table order by
total salary descending
Select DEPARTMENT,sum(SALARY) Total_Salary from employee group by DEPARTMENT
order by Total_Salary descending
44. Get department wise average salary from employee table order by salary ascending
select DEPARTMENT,avg(SALARY) AvgSalary from employee group by DEPARTMENT
order by AvgSalary asc
45. Get department wise maximum salary from employee table order by salary ascending
select DEPARTMENT,max(SALARY) MaxSalary from employee group by DEPARTMENT
order by MaxSalary asc
46. Get department wise minimum salary from employee table order by salary ascending
select DEPARTMENT,min(SALARY) MinSalary from employee group by DEPARTMENT
order by MinSalary asc
47. Select no of employees joined with respect to year and month from employee table
select to_char (JOINING_DATE,'YYYY') Join_Year,to_char (JOINING_DATE,'MM')
Join_Month,count(*) Total_Emp from employee group by to_char
(JOINING_DATE,'YYYY'),to_char(JOINING_DATE,'MM')
48. Select department,total salary with respect to a department from employee table where
total salary greater than 800000 order by Total_Salary descending
Select DEPARTMENT,sum(SALARY) Total_Salary from employee group by DEPARTMENT
having sum(SALARY) >800000 order by Total_Salary desc