Monday, November 9, 2015

SQL Query interview questions with answers

In this post you will learn SQL Query interview Questions for freshers and Experienced candidates. These areFrequently asked basic and advanced SQL Queries Interview Questions and Answers with Examples for Business Analyst, Data Analyst, DBA, Freshers and Experienced Java, PHP, Dot Net programmers in Oracle, MySQL and MS SQL Database.



Consider this table to use in our queries:

Table Name :  Employee

create table employee
(
Emp_Id int,
First_Name varchar2(20),
Last_Name varchar2(20),
salary number,
joining_date date,
Department varchar2(20));

After inserting some data into employee table the following result will come

 Result:


Table Name : Incentives

create table Incentives
(
Emp_Id int,
Incentive_Date date,
Incenttive_amount number);


Result:


 






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;


Result:
 

 








3)Get First_Name from employee table in upper case

  select upper(first_name)from employee;


Result:


 







4) Get First_Name from employee table in lower case

  select lower(first_name) from employee;


5) Get unique DEPARTMENT from employee table


select distinct DEPARTMENT from Employee;

Result:


 





6) Get FIRST_NAME ,Joining year,Joining Month and Joining Date from                 employee table

Select FIRST_NAME, to_char(joining_date,'YYYY') Join_Year ,

to_char(joining_date,'Mon') join_Month,
to_char(joining_date,'dd') join_date from EMPLOYEE;

Result:

 

 






7) Get all employee details from the employee table order by First_Name Ascending    
                 
Select * from employee order by FIRST_NAME asc;

Result:


 






8) 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;

Result:


 






9)  Get employee details from employee table whose employee name is “John”

  Select * from EMPLOYEE where FIRST_NAME='pavan';


10) Get employee details from employee table whose Salary greater than15000


  Select * from EMPLOYEE where Salary >15000;


Result:


11) Get employee details from employee table whose Salary less than15000


  Select * from EMPLOYEE where Salary <15000;



12) Get employee details from employee table whose Salary between 15000 and 25000

Select * from EMPLOYEE where Salary between 15000 and 25000;

Result:


13) Get employee details from employee table whose joining year is “2015


 Select * from EMPLOYEE where to_char(joining_date,'YYYY')='2015';


Result:



 


14) 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';

15) 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 EMPLOYEE where joining_date >'01/31/2013';

SQL Queries in MySQL (Format - “YYYY-DD-MM”):


Select * from EMPLOYEE where joining_date >'2013-01-31';

16) 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.EMP_ID=B.EMP_ID;


Result:


 









17) Get database date

SQL Queries in Oracle:

  
select sysdate from dual;

SQL Queries in SQL Server: 


select getdate();

SQL Query in MySQL:


select now();

18) Get department,total salary with respect to a department from employee table.


Select DEPARTMENT,sum(SALARY) Total_Salary from employee group by department;

Result:


 





19) 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 desc;

Result:


 





20) 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 desc;


Result:



21) 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;


Result:


 





22)  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;




Result:

 






23) 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;


24)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.


25) Delete employee data from employee table who got incentives in incentive table


delete from EMPLOYEE where EMPLOYEE_ID in (select EMPLOYEE_REF_ID from 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


26) Write a query to rank employees based on their incentives for a month


select FIRST_NAME,INCENTTIVE_AMOUNT,DENSE_RANK() OVER (PARTITION BY INCENTIVE_DATE ORDER BY INCENTTIVE_AMOUNT DESC) AS Rank from EMPLOYEE a, INCENTIVES b where a.EMP_ID=b.EMP_ID;


Result:


 









Explanation: In order to rank employees based on their rank for a month, "DENSE_RANK" keyword is used. Here partition by keyword helps us to sort the column with which filtering is done. Rank is provided to the column specified in the order by statement. The above query ranks employees with respect to their incentives for a given month.

27) Update incentive table where employee name is 'pavan'


update INCENTIVES set INCENTTIVE_AMOUNT='9000' where EMP_ID=(select EMP_ID from EMPLOYEE where FIRST_NAME='pavan' );



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.


28) select first_name, incentive amount from employee and incentives table for those employees who have incentives


Select FIRST_NAME,INCENTTIVE_AMOUNT from employee a inner join incentives B on A.EMP_ID=B.EMP_ID;


Result:


 









29) select first_name, incentive amount from employee and incentives table for those employees who have incentives and incentive amount greater than 5000

Select FIRST_NAME,INCENTTIVE_AMOUNT from employee a inner join incentives B on A.EMP_ID=B.EMP_ID and INCENTTIVE_AMOUNT >5000;


30) Select first_name, incentive amount from employee and incentives table for all employes even if they didn't get incentives

Select FIRST_NAME,INCENTTIVE_AMOUNT from employee a left join incentives B on A.EMP_ID=B.EMP_ID;


31) 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(INCENTTIVE_AMOUNT,0) from employee a right join incentives B on A.EMP_ID=B.EMP_ID;

SQL Queries in SQL Server:


Select FIRST_NAME, isnull(INCENTTIVE_AMOUNT,0) from employee a right join incentives B on A.EMP_ID=B.EMP_ID;

SQL Queries in MySQL:


Select FIRST_NAME, IFNULL(INCENTTIVE_AMOUNT,0) from employee a right join incentives B on A.EMP_ID=B.EMP_ID;

32) Select TOP 2 salary from employee table


SQL Queries in Oracle:


select * from (select * from employee order by SALARY desc) where rownum <3;

Result:


 



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;

33) Select TOP N salary from employee table


SQL Queries in Oracle:


 select * from (select * from employee order by SALARY desc) where rownum <N + 1;

SQL Queries in SQL Server:


 select top N * from employee;

SQL Queries in MySQL:

  
select * from employee order by salary desc limit N

34) Select 2nd Highest salary from employee table


SQL Queries in Oracle:

  
select min(salary) from (select * from (select * from employee order by SALARY desc) where rownum <3);

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 salary desc limit 2)a ;

35) Select Nth Highest salary from employee table


SQL Queries in Oracle:


select min(salary) from (select * from (select * from employee order by SALARY desc) where rownum <N + 1);

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 salary desc limit N) a;





I hope you people enjoy this post and share to your friends and social websites also,write comments on this topic. Keep follow me for latest updates



No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...