0% found this document useful (0 votes)
247 views4 pages

Sample Queries

The document contains details of employees in a table including their ID, name, salary, joining date and department. It also includes a table with incentive details for some employees. The document then lists 48 SQL queries on the employee table to retrieve various details like names, departments, salaries etc. either alone or filtered based on conditions. The queries use functions like upper, lower, substr, trim, replace and aggregate functions like sum, count, avg, max, min along with group by, order by, distinct, like, between etc.

Uploaded by

Vinobala Vino
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
247 views4 pages

Sample Queries

The document contains details of employees in a table including their ID, name, salary, joining date and department. It also includes a table with incentive details for some employees. The document then lists 48 SQL queries on the employee table to retrieve various details like names, departments, salaries etc. either alone or filtered based on conditions. The queries use functions like upper, lower, substr, trim, replace and aggregate functions like sum, count, avg, max, min along with group by, order by, distinct, like, between etc.

Uploaded by

Vinobala Vino
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Table Name : Employee

Employee_id First_name Last_name Salary Joining_date Department

1 John Abraham 1000000 01-JAN-13 12.00.00 AM Banking

2 Michael Clarke 800000 01-JAN-13 12.00.00 AM Insurance

3 Roy Thomas 700000 01-FEB-13 12.00.00 AM Banking

4 Tom Jose 600000 01-FEB-13 12.00.00 AM Insurance

5 Jerry Pinto 650000 01-FEB-13 12.00.00 AM Insurance

6 Philip Mathew 750000 01-JAN-13 12.00.00 AM Services

7 TestName1 123 650000 01-JAN-13 12.00.00 AM Services

8 TestName2 Lname% 600000 01-FEB-13 12.00.00 AM Insurance


Table Name : Incentives
Employee_ref_id Incentive_date Incentive_amount

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

2. Get First_Name,Last_Name from employee table


Select first_name, Last_Name from employee

3. Get First_Name from employee table using alias name “Employee Name”
Select first_name Employee Name from employee

4. Get First_Name from employee table in upper case


Select upper(FIRST_NAME) from EMPLOYEE

5. Get First_Name from employee table in lower case


Select lower(FIRST_NAME) from EMPLOYEE

6. Get unique DEPARTMENT from employee table


select distinct DEPARTMENT from EMPLOYEE

7. Select first 3 characters of FIRST_NAME from EMPLOYEE


Select substr(FIRST_NAME,0,3) from employee

8. Get position of 'o' in name 'John' from employee table


Select instr(FIRST_NAME,'o') from employee where first_name='John'
9. Get FIRST_NAME from employee table after removing white spaces from right side
select RTRIM(FIRST_NAME) from employee

10. Get FIRST_NAME from employee table after removing white spaces from left side
select LTRIM(FIRST_NAME) from employee

11. Get length of FIRST_NAME from employee table


select length(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')

35. Get Joining Date and Time from employee table


select to_char(JOINING_DATE,'dd/mm/yyyy hh:mi:ss') from EMPLOYEE

36. Get Joining Date,Time including milliseconds from employee table


select to_char(JOINING_DATE,'dd/mm/yyyy HH:mi:ss.ff') from EMPLOYEE .

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

43. Get department,no of employees in a department,total salary with respect to a


department from employee table order by total salary descending
Select DEPARTMENT,count(FIRST_NAME),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

You might also like