Home

Saturday, June 14, 2014

SQL Query Interview Questions and Answers with Examples

Interview Questions on SQL are based on following two tables, Employee Table and Employee Incentive Table.
Table Name : Employee
EMPLOYEE_IDFIRST_NAMELAST_NAMESALARYJOINING_DATEDEPARTMENT
1JohnAbraham100000001-JAN-13 12.00.00 AMBanking
2MichaelClarke80000001-JAN-13 12.00.00 AMInsurance
3RoyThomas70000001-FEB-13 12.00.00 AMBanking
4TomJose60000001-FEB-13 12.00.00 AMInsurance
5JerryPinto65000001-FEB-13 12.00.00 AMInsurance
6PhilipMathew75000001-JAN-13 12.00.00 AMServices
7TestName112365000001-JAN-13 12.00.00 AMServices
8TestName2Lname%60000001-FEB-13 12.00.00 AMInsurance
Table Name : Incentives
EMPLOYEE_REF_IDINCENTIVE_DATEINCENTIVE_AMOUNT
101-FEB-135000
201-FEB-133000
301-FEB-134000
101-JAN-134500
201-JAN-133500
SQL Queries Interview Questions and Answers on "SQL Select" - Examples
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
Oracle Equivalent of SQL Server SUBSTRING is SUBSTR, Query : select substr(FIRST_NAME,0,3) from employee
SQL Server Equivalent of Oracle SUBSTR is SUBSTRING, Query : select substring(FIRST_NAME,0,3) from employee
MySQL Server Equivalent of Oracle SUBSTR is SUBSTRING. In MySQL start position is 1, Query : select substring(FIRST_NAME,1,3) from employee
8. Get position of 'o' in name 'John' from employee table
Oracle Equivalent of SQL Server CHARINDEX is INSTR, Query : Select instr(FIRST_NAME,'o') from employee where first_name = 'John'
SQL Server Equivalent of Oracle INSTR is CHARINDEX, Query: Select CHARINDEX('o',FIRST_NAME,0) from employee where first_name = 'John'
MySQL Server Equivalent of Oracle INSTR is LOCATE, Query: Select LOCATE('o',FIRST_NAME) 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
Oracle,MYSQL Equivalent of SQL Server Len is Length , Query :select length(FIRST_NAME) from employee
SQL Server Equivalent of Oracle,MYSQL Length is Len, Query :select len(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 '_'
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, Query : Select concat(FIRST_NAME,'_',LAST_NAME) from EMPLOYEE
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
Database SQL Queries Interview Questions and answers on "SQL Order By"
15. Get all employee details from the employee table order by First_Name Ascending
 Select * from employee order by FIRST_NAME asc
6. 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
SQL Queries Interview Questions and Answers on "SQL Where Condition" - Examples
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')
SQL Queries Interview Questions and Answers on "SQL Wild Card Search" - Examples
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 Queries Interview Questions and Answers on "SQL Pattern Matching" - Examples
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')
SQL Queries Interview Questions and Answers on "SQL DATE Functions" - Examples
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 * from EMPLOYEE where SUBSTRING(convert(varchar,joining_date,103),7,4) = '2013'
SQL Queries in MySQL, Select * from EMPLOYEE where year(joining_date) = '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' or Select * from EMPLOYEE where to_char(joining_date,'Mon') = 'Jan'
SQL Queries in SQL Server, Select * from EMPLOYEE where SUBSTRING(convert(varchar,joining_date,100),1,3) = 'Jan'
SQL Queries in MySQL, Select * from EMPLOYEE where month(joining_date) = '01'
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'
SQL Queries in MySQL (Format - “YYYY-DD-MM”), Select * from EMPLOYEE where 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 EMPLOYEE where joining_date >'01/31/2013'
SQL Queries in MySQL (Format - “YYYY-DD-MM”), Select * from EMPLOYEE where joining_date > '2013-01-31'
35. Get Joining Date and Time from employee table
SQL Queries in Oracle, select to_char(JOINING_DATE,'dd/mm/yyyy hh:mi:ss') from EMPLOYEE
SQL Queries in SQL Server, Select convert(varchar(19),joining_date,121) from EMPLOYEE
SQL Queries in MySQL, Select CONVERT(DATE_FORMAT(joining_date,'%Y-%m-%d-%H:%i:00'),DATETIME) from EMPLOYEE
36. Get Joining Date,Time including milliseconds from employee table
SQL Queries in Oracle, select to_char(JOINING_DATE,'dd/mm/yyyy HH:mi:ss.ff') from EMPLOYEE . Column Data Type should be “TimeStamp”
SQL Queries in SQL Server, select convert(varchar,joining_date,121) from EMPLOYEE
SQL Queries in MySQL, Select MICROSECOND(joining_date) 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

38. Get database date
SQL Queries in Oracle, select sysdate from dual
SQL Queries in SQL Server, select getdate()
SQL Query in MySQL, select now()

For Interview Questions on Advanced SQL Queries go to Complex Queries Section  of this article.

No comments:

Post a Comment