SQL Query Interview Questions and Answers With Examples
SQL Query Interview Questions and Answers With Examples
Examples
Interview Questions on SQL are based on following two tables, Employee Table
and Employee Incentive Table.
Table Name : Employee
EMPLOYEE_ID
FIRST_NAME
LAST_NAME
SALARY
JOINING_DATE
DEPARTMENT
1
2
3
4
5
6
7
8
John
Michael
Roy
Tom
Jerry
Philip
TestName1
TestName2
Abraham
Clarke
Thomas
Jose
Pinto
Mathew
123
Lname%
1000000
800000
700000
600000
650000
750000
650000
600000
01-JAN-13 12.00.00 AM
01-JAN-13 12.00.00 AM
01-FEB-13 12.00.00 AM
01-FEB-13 12.00.00 AM
01-FEB-13 12.00.00 AM
01-JAN-13 12.00.00 AM
01-JAN-13 12.00.00 AM
01-FEB-13 12.00.00 AM
Banking
Insurance
Banking
Insurance
Insurance
Services
Services
Insurance
INCENTIVE_DATE
01-FEB-13
01-FEB-13
01-FEB-13
01-JAN-13
01-JAN-13
INCENTIVE_AMOUNT
5000
3000
4000
4500
3500
3. Get First_Name from employee table using alias name Employee Name
Select first_name Employee Name from employee
Oracle Equivalent of
substr(FIRST_NAME,0,3)
SQL
Server
SUBSTRING is
from
SUBSTR,
Query
select
employee
Oracle
SUBSTR
is SUBSTRING,
from
Query
select
employee
INSTR, Query :
first_name
=
Select
'John'
is CHARINDEX, Query:
where
first_name
=
Select
'John'
MySQL
Server
Equivalent
of
Oracle
INSTR
is
LOCATE,
Query:
LOCATE('o',FIRST_NAME) from employee where first_name = 'John'
Select
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
Oracle,MYSQL Equivalent
length(FIRST_NAME)
of
SQL
Server
Len
from
is
Length
Length
is
Query
:select
employee
Len,
Query
:select
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
'_'
Oracle Equivalent of MySQL concat is '||', Query : Select FIRST_NAME|| '_' ||
LAST_NAME
from
EMPLOYEE
SQL Server Equivalent of MySQL concat is '+', Query : Select FIRST_NAME + '_'
+LAST_NAME
from
EMPLOYEE
MySQL
Equivalent
of
Oracle
'||'
is
concat(FIRST_NAME,'_',LAST_NAME) from EMPLOYEE
concat,
Query
Select
14. Get FIRST_NAME ,Joining year,Joining Month and Joining Date from employee table
SQL Queries in Oracle, Select FIRST_NAME, to_char(joining_date,'YYYY')
JoinYear , to_char(joining_date,'Mon'), to_char(joining_date,'dd') from
EMPLOYEE
SQL Queries in SQL Server, select SUBSTRING
(convert(varchar,joining_date,103),7,4) , SUBSTRING
(convert(varchar,joining_date,100),1,3) , SUBSTRING
(convert(varchar,joining_date,100),5,2) from EMPLOYEE
SQL Queries in MySQL, select year(joining_date),month(joining_date),
DAY(joining_date) from EMPLOYEE
15. Get all employee details from the employee table order by First_Name Ascending
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
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'
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'
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
SQL Queries in Oracle, Select * from EMPLOYEE where to_char(joining_date,'YYYY') =
'2013'
SQL
Queries
in
SQL
Server,
Select
SUBSTRING(convert(varchar,joining_date,103),7,4)
from
EMPLOYEE
=
where
'2013'
EMPLOYEE
where
32. Get employee details from employee table who joined before January 1st 2013
SQL Queries in Oracle, Select
to_date('01/01/2013','dd/mm/yyyy')
from
EMPLOYEE
where
JOINING_DATE
<
33. Get employee details from employee table who joined after January 31st
SQL Queries in Oracle, Select
to_date('31/01/2013','dd/mm/yyyy')
from
EMPLOYEE
where
JOINING_DATE
>
SQL Queries in SQL Server and MySQL (Format - MM/DD/YYYY), Select * from
EMPLOYEE
where
joining_date
>'01/31/2013'
SQL Queries in MySQL (Format - YYYY-DD-MM), Select * from EMPLOYEE where
joining_date
>
'2013-01-31'
Queries
in
SQL
in
Server,
MySQL,
select
Select
convert(varchar,joining_date,121)
MICROSECOND(joining_date)
from
from
EMPLOYEE
37. Get difference between JOINING_DATE and INCENTIVE_DATE from employee and
incentives table
Select FIRST_NAME,INCENTIVE_DATE - JOINING_DATE from employee a inner join
incentives B on A.EMPLOYEE_ID = B.EMPLOYEE_REF_ID
Queries
in
Oracle,
select
sysdate
from
dual
SQL
SQL
Queries
Query
in
SQL
in
Server,
MySQL,
select
getdate()
select
now()
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 DEPARTMENT
order by Total_Salary descending
from
employee
44. Get department wise average salary from employee table order by salary ascending
select DEPARTMENT,avg(SALARY)
order by AvgSalary asc
AvgSalary
from
employee
group
by
DEPARTMENT
45. Get department wise maximum salary from employee table order by salary ascending
select DEPARTMENT,max(SALARY)
order by MaxSalary asc
MaxSalary
from
employee
group
by
DEPARTMENT
46. Get department wise minimum salary from employee table order by salary ascending
select DEPARTMENT,min(SALARY)
order by MinSalary asc
MinSalary
from
employee
group
by
DEPARTMENT
47. Select no of employees joined with respect to year and month from employee table
SQL Queries in Oracle, 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')
SQL
Queries
in
SQL
Server,
select
datepart
(YYYY,JOINING_DATE)
Join_Year,datepart
(MM,JOINING_DATE)
Join_Month,count(*)
Total_Emp
from
employee group by datepart(YYYY,JOINING_DATE), datepart(MM,JOINING_DATE)
SQL
Queries
in
MySQL,
select
(JOINING_DATE)
Join_Month,count(*)
year(JOINING_DATE),
year
(JOINING_DATE)
Join_Year,month
Total_Emp
from
employee
group
by
month(JOINING_DATE)
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
49. Select first_name, incentive amount from employee and incentives table for those
employees who have incentives
51. 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
52. 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 employee
a
left
join
incentives
B
on
A.EMPLOYEE_ID
=
B.EMPLOYEE_REF_ID
SQL Queries in SQL Server, Select FIRST_NAME, ISNULL(INCENTIVE_AMOUNT,0) from
employee a left join incentives B on A.EMPLOYEE_ID = B.EMPLOYEE_REF_ID
SQL Queries in MySQL, Select FIRST_NAME, IFNULL(INCENTIVE_AMOUNT,0) from
employee a left join incentives B on A.EMPLOYEE_ID = B.EMPLOYEE_REF_ID
53. Select first_name, incentive amount from employee and incentives table for all
employees who got incentives using left join
Ads not by this site
54. 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)
from INCENTIVES where EMPLOYEE_REF_ID = EMPLOYEE_ID) Max_incentive from
EMPLOYEE
SQL
Queries
in
SQL
Server,
select
ISNULL(max(INCENTIVE_AMOUNT),0) from INCENTIVES where
EMPLOYEE_ID)
Max_incentive
from
DEPARTMENT,(select
EMPLOYEE_REF_ID =
EMPLOYEE
SQL
Queries
in
SQL
Server,
select
DEPARTMENT,(select
IFNULL
(max(INCENTIVE_AMOUNT),0) from INCENTIVES where EMPLOYEE_REF_ID = EMPLOYEE_ID)
Max_incentive
from
EMPLOYEE
SQL Queries in Oracle, select * from (select * from employee order by SALARY
desc)
where
rownum
<
N
+
1
SQL
Queries
in
SQL
Server,
select
top
from
employee
SQL Queries in MySQL, select * from employee order by salary desc limit N
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 example, in the above query, if FIRST_NAME is DOUBLE
and LAST_NAME is STRING 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 only distinct values.
61. Select employee details from employee table if data exists in incentive table ?
63. Get Employee ID's of those employees who didn't receive incentives without using sub
query ?
select
EMPLOYEE_ID
from
EMPLOYEE
EMPLOYEE_REF_ID
from
INCENTIVES
MINUS
select
64. Select 20 % of salary from John , 10% of Salary for Roy and for other 15 % of salary
from employee table
Ads not by this site
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.
65. 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', 'Insurance', 'Insurance Dept', 'Services', 'Services Dept') FROM
EMPLOYEE
SQL Queries in SQL Server and MySQL, SELECT case DEPARTMENT when 'Banking'
then 'Bank Dept' when 'Insurance' then 'Insurance Dept' when 'Services' then
'Services
Dept'
end
FROM
EMPLOYEE
Explanation : Here DECODE keyword is used to specify the alias name. In
oracle we had specify, Column Name followed by Actual Name and Alias Name as
arguments. In SQL Server and MySQL, we can use the earlier switch case
statements for alias names.
66. Delete employee data from employee table who got incentives in incentive table
delete from
INCENTIVES)
EMPLOYEE
where
EMPLOYEE_ID
in
(select
EMPLOYEE_REF_ID
from
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.
67. Insert into employee table Last Name with " ' " (Single Quote - Special Character)
Tip
Use
another
single
quote
before
special
character
68. Select Last Name from employee table which contain only numbers
Ads not by this site
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.
69. Write a query to rank employees based on their incentives for a month
select
FIRST_NAME,INCENTIVE_AMOUNT,DENSE_RANK()
OVER
(PARTITION
BY
INCENTIVE_DATE ORDER BY
INCENTIVE_AMOUNT DESC) AS Rank from EMPLOYEE a,
INCENTIVES b where a.EMPLOYEE_ID = b.EMPLOYEE_REF_ID
Explanation : Here in order to rank employees
month, DENSE_RANK keyword is used. Here partition
the column with which filtering is done. Rank
specified in the order by statement. The above
respect to their incentives for a given month.
add
CONSTRAINT
EMPLOYEE_PK
PRIMARY
76. Write Sql Syntax to create EMPLOYEE_REF_ID in INCENTIVES table as foreign key
with respect to EMPLOYEE_ID in employee table
ALTER
TABLE
INCENTIVES
ADD
CONSTRAINT
INCENTIVES_FK
(EMPLOYEE_REF_ID) REFERENCES EMPLOYEE(EMPLOYEE_ID)
FOREIGN
KEY
79. Write Sql syntax to create Oracle Trigger before insert of each row in employee table
CREATE
OR
BEFORE
REPLACE
INSERT
TRIGGER
ON
EMPLOYEE_ROW_ID_TRIGGER
EMPLOYEE
FOR
EACH
ROW
DECLARE
seq_no
number(12);
BEGIN
select
EMPLOYEE_ID_SEQ.nextval
:new
into
seq_no
EMPLOYEE_ID
from
:=
dual
seq_no;
END;
SHOW ERRORS;
example
oracle
view
script
is
given
below
CREATE
MATERIALIZED
VIEW
Employee_Incentive
REFRESH
COMPLETE
START
NEXT
WITH
SYSDATE
SYSDATE
+
AS
CREATE
MATERIALIZED
VIEW
MAT_Employee_Incentive_Refresh
BUILD
REFRESH
IMMEDIATE
FAST
ON
COMMIT
AS
select
FIRST_NAME,max(INCENTIVE_AMOUNT)
from
EMPLOYEE
a,
INCENTIVES