sql

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

Upload: kitchaa

Post on 11-Oct-2015

5 views

Category:

Documents


0 download

DESCRIPTION

fgf

TRANSCRIPT

Interview Questions on SQL are based on following two tables, Employee Table and Employee Incentive Table.Table Name : EmployeeEMPLOYEE_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 : IncentivesEMPLOYEE_REF_IDINCENTIVE_DATEINCENTIVE_AMOUNT

101-FEB-135000

201-FEB-133000

301-FEB-134000

101-JAN-134500

201-JAN-133500

1.Get First_Name from employee table using alias name Employee NameA: Select first_name Employee Name from employee4. Get First_Name from employee table in upper case Select upper(FIRST_NAME) from EMPLOYEE5. Get First_Name from employee table in lower caseSelect lower(FIRST_NAME) from EMPLOYEE6. Get unique DEPARTMENT from employee tableselect distinct DEPARTMENT from EMPLOYEESelect first 3 characters of FIRST_NAME from EMPLOYEEselect substring(FIRST_NAME,0,3) from employeeGet position of 'o' in name 'John' from employee tableSelect CHARINDEX('o',FIRST_NAME,0) from employee where first_name='John'9. Get FIRST_NAME from employee table after removing white spaces from right sideselect RTRIM(FIRST_NAME) from employee10. Get FIRST_NAME from employee table after removing white spaces from left sideselect LTRIM(FIRST_NAME) from employee11. Get length of FIRST_NAME from employee tableselect len(FIRST_NAME) from employee2. Get First_Name from employee table after replacing 'o' with '$'select REPLACE(FIRST_NAME,'o','$') from employee13. Get First_Name and Last_Name as single column from employee table separated by a '_'Select FIRST_NAME + '_' +LAST_NAME from EMPLOYEEGet FIRST_NAME ,Joining year,Joining Month and Joining Date from employee tableselect year(joining_date),month(joining_date), DAY(joining_date) from EMPLOYEEGet all employee details from the employee table order by First_Name AscendingGet all employee details from the employee table order by First_Name descendingSelect * from employee order by FIRST_NAME descGet all employee details from the employee table order by First_Name Ascending and Salary descendingSelect * from employee order by FIRST_NAME asc,SALARY descGet employee details from employee table whose employee name is JohnSelect * from EMPLOYEE where FIRST_NAME='John'Get employee details from employee table whose employee name are John and RoySelect * from EMPLOYEE where FIRST_NAME in ('John','Roy')Get employee details from employee table whose employee name are not John and RoySelect * 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%'Get employee details from employee table whose first name ends with 'n'Select * from EMPLOYEE where FIRST_NAME like '%n'24. Get employee details from employee table whose first name ends with 'n' and name contains 4 lettersSelect * 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 lettersSelect * from EMPLOYEE where FIRST_NAME like 'J___' (Underscores)26. Get employee details from employee table whose Salary greater than 600000Select * from EMPLOYEE where Salary >60000027. Get employee details from employee table whose Salary less than 800000Select * from EMPLOYEE where Salary 3000Select first_name, incentive amount from employee and incentives table for all employes even if they didn't get incentivesSelect FIRST_NAME,INCENTIVE_AMOUNT from employee a left join incentives B on A.EMPLOYEE_ID=B.EMPLOYEE_REF_IDSelect 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.Select FIRST_NAME, ISNULL(INCENTIVE_AMOUNT,0) from employee a left join incentives B on A.EMPLOYEE_ID=B.EMPLOYEE_REF_IDSelect first_name, incentive amount from employee and incentives table for all employees who got incentives using left joinSelect FIRST_NAME, isnull(INCENTIVE_AMOUNT,0) from employee a right join incentives B on A.EMPLOYEE_ID=B.EMPLOYEE_REF_IDSelect max incentive with respect to employee from employee and incentives table using sub queryselect DEPARTMENT,(select ISNULL(max(INCENTIVE_AMOUNT),0) from INCENTIVES where EMPLOYEE_REF_ID=EMPLOYEE_ID) Max_incentive from EMPLOYEESelect TOP 2 salary from employee tableselect top 2 * from employee order by salary descSelect TOP N salary from employee tableSelect 2nd Highest salary from employee tableselect min(SALARY) from (select top 2 * from employee) aSelect Nth Highest salary from employee tableselect min(SALARY) from (select top N * from employee) aSelect First_Name,LAST_NAME from employee table as separate rowsselect FIRST_NAME from EMPLOYEE union select LAST_NAME from EMPLOYEESelect employee details from employee table if data exists in incentive table ?select * from EMPLOYEE where exists (select * from INCENTIVES)How to fetch data that are common in two query results ?select * from EMPLOYEE where EMPLOYEE_ID INTERSECT select * from EMPLOYEE where EMPLOYEE_ID