SQL Task-1: Table Name: Employee
SQL Task-1: Table Name: Employee
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
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
12. Get First_Name from employee table after replacing 'o' with '$'
13. Get First_Name and Last_Name as single column from employee table
separated by a '_'
SQL Server Equivalent of MySQL concat is '+', Query : Select FIRST_NAME + '_'
14. Get FIRST_NAME ,Joining year,Joining Month and Joining Date from
employee table
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
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
"SQL Where Condition" Interview Questions
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')
22. Get employee details from employee table whose first name contains 'o'
23. Get employee details from employee table whose first name ends with 'n'
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
26. Get employee details from employee table whose Salary greater than 600000
27. Get employee details from employee table whose Salary less than 800000
28. Get employee details from employee table whose Salary between 500000 and
800000
29. Get employee details from employee table whose name is 'John' and 'Michael'
30. Get employee details from employee table whose joining year is “2013”
to_char(joining_date,'YYYY')='2013'
SQL Queries in SQL Server, Select * from EMPLOYEE where
SUBSTRING(convert(varchar,joining_date,103),7,4)='2013'
31. Get employee details from employee table whose joining month is “January”
SQL Queries in Oracle, Select * from EMPLOYEE where to_char(joining_date,'MM')='01'
SUBSTRING(convert(varchar,joining_date,100),1,3)='Jan'
32. Get employee details from employee table who joined before January 1st 2013
SQL Queries in Oracle, Select * from EMPLOYEE where JOINING_DATE
<to_date('01/01/2013','dd/mm/yyyy')
SQL Queries in SQL Server (Format - “MM/DD/YYYY”), Select * from EMPLOYEE where
joining_date <'01/01/2013'
joining_date <'2013-01-01'
33. Get employee details from employee table who joined after January 31st
SQL Queries in Oracle, Select * from EMPLOYEE where JOINING_DATE
>to_date('31/01/2013','dd/mm/yyyy')
SQL Queries in SQL Server and MySQL (Format - “MM/DD/YYYY”), Select * from
joining_date >'2013-01-31'
EMPLOYEE
EMPLOYEE
incentives B on A.EMPLOYEE_ID=B.EMPLOYEE_REF_ID
39. Get names of employees from employee table who has '%' in Last_Name. Tip :
Escape character for special characters in a query.
SQL Queries in Oracle, Select FIRST_NAME from employee where Last_Name like
'%?%%'
SQL Queries in SQL Server, Select FIRST_NAME from employee where Last_Name like
'%[%]%'
SQL Queries in MySQL, Select FIRST_NAME from employee where Last_Name like
'%\%%'
40. Get Last Name from employee table after replacing special character with
white space
SQL Queries in Oracle, Select translate(LAST_NAME,'%',' ') from employee
SQL Queries in SQL Server and MySQL, Select REPLACE(LAST_NAME,'%',' ') from
employee
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
44. Get department wise average salary from employee table order by salary
ascending
45. Get department wise maximum salary from employee table order by salary
ascending
46. Get department wise minimum salary from employee table order by salary
ascending
47. Select no of employees joined with respect to year and month from employee
table
(JOINING_DATE,'YYYY'),to_char(JOINING_DATE,'MM')
year(JOINING_DATE), month(JOINING_DATE)
49. Select employee details from employee table if data exists in incentive table ?
Explanation : Here "exists" statement helps us to do the job of If statement. Main query
will get executed if the sub query returns at least one row. So we can consider the sub
query as "If condition" and the main query as "code block" inside the If condition. We
can use any SQL commands (Joins, Group By , having etc) in sub query. This
command will be useful in queries which need to detect an event and do some activity.
50. How to fetch data that are common in two query results ?
select * from EMPLOYEE where EMPLOYEE_ID INTERSECT select * from EMPLOYEE
Explanation : Here "INTERSECT" command is used to fetch data that are common in 2
queries. In this example, we had taken EMPLOYEE table in both the queries.We can
apply INTERSECT command on different tables. The result of the above query will
return employee details of "ROY" because, employee id of ROY is 3, and both query
results have the information about ROY.
51. Get Employee ID's of those employees who didn't receive incentives without
using sub query ?
select EMPLOYEE_ID from EMPLOYEE
MINUS
Explanation : To filter out certain information we use MINUS command. What MINUS
Command odes is that, it returns all the results from the first query, that are not part of
the second query. In our example, first three employees received the incentives. So
query will return employee id's 4 to 8.
52. Select 20 % of salary from John , 10% of Salary for Roy and for other 15 % of
salary from employee table
SELECT FIRST_NAME, CASE FIRST_NAME WHEN 'John' THEN SALARY * .2 WHEN
'Roy' THEN SALARY * .10 ELSE SALARY * .15 END "Deduced_Amount" FROM
EMPLOYEE
Explanation : Here, we are using "SQL CASE" statement to achieve the desired
results. After case statement, we had to specify the column on which filtering is applied.
In our case it is "FIRST_NAME". And in then condition, specify the name of filter like
John, Roy etc. To handle conditions outside our filter, use else block where every one
other than John and Roy enters.
53. Select Banking as 'Bank Dept', Insurance as 'Insurance Dept' and Services as
'Services Dept' from employee table
SQL Queries in Oracle, SELECT distinct DECODE (DEPARTMENT, 'Banking', 'Bank Dept',
SQL Queries in SQL Server and MySQL, SELECT case DEPARTMENT when 'Banking'
then 'Bank Dept' when 'Insurance' then 'Insurance Dept' when 'Services' then 'Services
54. Delete employee data from employee table who got incentives in incentive
table
INCENTIVES)
Explanation : Trick about this question is that we can't delete data from a table based
on some condition in another table by joining them. Here to delete multiple entries from
EMPLOYEE table, we need to use Subquery. Entries will get deleted based on the
result of Subquery.
55. Insert into employee table Last Name with " ' " (Single Quote - Special
Character)
56. Select Last Name from employee table which contain only numbers
Explanation : In order to achieve the desired result, we use "ASCII" property of the
database. If we get results for a column using Lower and Upper commands, ASCII of
both results will be same for numbers. If there is any alphabets in the column, results
will differ.
57. Write a query to rank employees based on their incentives for a month
select FIRST_NAME,INCENTIVE_AMOUNT,DENSE_RANK() OVER (PARTITION BY
FIRST_NAME='John' )
Explanation : We need to join Employee and Incentive Table for updating the incentive
amount. But for update statement joining query wont work. We need to use sub query to
update the data in the incentive table. SQL Query is as shown below.
A.EMPLOYEE_ID=B.EMPLOYEE_REF_ID
60. Select first_name, incentive amount from employee and incentives table for
those employees who have incentives and incentive amount greater than 3000
Select FIRST_NAME,INCENTIVE_AMOUNT from employee a inner join incentives B on
61. Select first_name, incentive amount from employee and incentives table for all
employes even if they didn't get incentives
Select FIRST_NAME,INCENTIVE_AMOUNT from employee a left join incentives B on
A.EMPLOYEE_ID=B.EMPLOYEE_REF_ID
62. Select first_name, incentive amount from employee and incentives table for all
employees even if they didn't get incentives and set incentive amount as 0 for
those employees who didn't get incentives.
SQL Queries in Oracle, Select FIRST_NAME,nvl(INCENTIVE_AMOUNT,0) from
63. Select first_name, incentive amount from employee and incentives table for all
employees who got incentives using left join
SQL Queries in Oracle, Select FIRST_NAME,nvl(INCENTIVE_AMOUNT,0) from
64. Select max incentive with respect to employee from employee and incentives
table using sub query
SQL Queries in Oracle, select DEPARTMENT,(select nvl(max(INCENTIVE_AMOUNT),0)
EMPLOYEE
SQL Queries in Oracle, select * from (select * from employee order by SALARY desc)
SQL Queries in SQL Server, select top 2 * from employee order by salary desc
SQL Queries in MySQL, select * from employee order by salary desc limit 2
SQL Queries in Oracle, select * from (select * from employee order by SALARY desc)
SQL Queries in MySQL, select * from employee order by salary desc limit N
SQL Queries in Oracle, select min(salary) from (select * from (select * from employee
SQL Queries in SQL Server, select min(SALARY) from (select top 2 * from employee) a
SQL Queries in MySQL, select min(SALARY) from (select * from employee order by
SQL Queries in Oracle, select min(salary) from (select * from (select * from employee
SQL Queries in SQL Server, select min(SALARY) from (select top N * from employee) a
SQL Queries in MySQL, select min(SALARY) from (select * from employee order by
Both UNION and UNION ALL is used to select information from structurally similar tables.
That means corresponding columns specified in the union should have same data type. For
above query wont work. Since the data type of both the columns are VARCHAR, union is
made possible. Difference between UNION and UNION ALL is that , UNION query return
EMPLOYEE_ID NUMBER,
SALARY FLOAT(126),
KEY(EMPLOYEE_ID)
KEY(EMPLOYEE_ID,FIRST_NAME)
76. Write Sql Syntax to create EMPLOYEE_REF_ID in INCENTIVES table as foreign key
with respect to EMPLOYEE_ID in employee table
DECLARE
seq_no number(12);
BEGIN
END;
SHOW ERRORS;
REFRESH COMPLETE
NEXT SYSDATE + 1 AS
INCENTIVES b
where a.EMPLOYEE_ID=b.EMPLOYEE_REF_ID
83. Oracle materialized view - Fast Refresh on Commit
Create materialized view log for fast refresh. Following materialized view script wont get
BUILD IMMEDIATE
SQL Injection is one of the the techniques uses by hackers to hack a website by injecting
Page 7 of 7