Employee - Id First - Name Last - Name Salary Joining - Date Department
Employee - Id First - Name Last - Name Salary Joining - Date Department
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
SQL Queries Interview Questions and Answers on "SQL Select" - Examples
1. Get all employee details from the employee table
3. Get First_Name from employee table using alias name “Employee Name”
10. Get FIRST_NAME from employee table after removing white spaces from left
side
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
15. Get all employee details from the employee table order by First_Name
Ascending
19. Get employee details from employee table whose employee name are “John”
and “Roy”
20. Get employee details from employee table whose employee name are not
“John” and “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'
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'
25. Get employee details from employee table whose first name starts with 'J' and
name contains 4 letters
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'
31. Get employee details from employee table whose joining month is “January”
32. Get employee details from employee table who joined before January 1st 2013
33. Get employee details from employee table who joined after January 31st
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
42. Get department,total salary with respect to a department from employee table
order by total salary descending
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
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
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.
53. Select first_name, incentive amount from employee and incentives table for all
employees who got incentives using left join
54. Select max incentive with respect to employee from employee and incentives
table using sub query
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 MySQL, select * from employee order by salary desc limit N
SQL Queries in SQL Server, select min(SALARY) from (select top N * from
employee) a
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 ?
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.
62. How to fetch data that are common in two query results ?
63. Get Employee ID's of those employees who didn't receive incentives without
using sub query ?
select EMPLOYEE_ID from EMPLOYEE
MINUS
64. Select 20 % of salary from John , 10% of Salary for Roy and for other 15 % of
salary from employee table
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
68. Select Last Name from employee table which contain only numbers
Explanation : Here 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.
Oracle -
CREATE TABLE EMPLOYEE (
EMPLOYEE_ID NUMBER,
SALARY FLOAT(126),
SQL Server -
DECLARE
seq_no number(12);
BEGIN
END;
SHOW ERRORS;
REFRESH COMPLETE
NEXT SYSDATE + 1 AS
BUILD IMMEDIATE