Interview Questions
Interview Questions
Ques.2. Write an SQL query to fetch the different projects available from the
EmployeeSalary table.
Ans. While referring to the EmployeeSalary table, we can see that this table
contains project values corresponding to each employee, or we can say that we
will have duplicate project values while selecting Project values from this table.
So, we will use the distinct clause to get the unique values of the Project.
Ques.4. Write an SQL query to find the maximum, minimum, and average of
the employees.
Ans. We can use the aggregate function of SQL to fetch the max, min and
average values-
Ques.5. Write an SQL query to find the employee id whose salary lies in the
range of 9000 and 15000.
Ans. Here, we can use the ‘Between’ operator with a where clause.
Select empid,salary from employeesalary where salary between 9000 AND 15000;
Ques.6. Write an SQL query to fetch those employees who live in Toronto
and work under manager with ManagerId – 321.
Ans. Since we have to satisfy both the conditions – employees living in ‘Toronto’
and working in Project ‘P2’. So, we will use AND operator here-
Ques.7. Write an SQL query to fetch all the employees who either live in
California or work under a manager with ManagerId – 321.
Ans. This interview question requires us to satisfy either of the conditions –
employees living in ‘California’ and working under Manager with ManagerId
‘321’. So, we will use the OR operator here-
Ques.8. Write an SQL query to fetch all those employees who work on
Project other than P1.
Ans. Here, we can use the NOT operator to fetch the rows which are not
satisfying the given condition.
Ques.9. Write an SQL query to display the total salary of each employee
adding the Salary with Variable value.
Ans. Here, we can simply use the ‘+’ operator in SQL.
Ques.10. Write an SQL query to fetch the employees whose name begins
with any two characters, followed by a text “hn” and ending with any
sequence of characters.
Ans. For this question, we can create an SQL query using like operator with ‘_’ and
‘%’ wild card characters, where ‘_’ matches a single character and ‘%’ matches ‘0
or multiple characters’.
Ques.11. Write an SQL query to fetch all the EmpIds which are present in
either of the tables – ‘EmployeeDetails’ and ‘EmployeeSalary’.
Ans. In order to get unique employee ids from both the tables, we can use Union
clause which can combine the results of the two SQL queries and return unique
rows.
UNION
INTERSECT
Ques.13. Write an SQL query to fetch records that are present in one table
but not in another table.
Ans. SQL Server – Using MINUS- operator-
MINUS
Ques.14. Write an SQL query to fetch the EmpIds that are present in both
the tables – ‘EmployeeDetails’ and ‘EmployeeSalary.
Ans. Using sub query-
Ques.23. Fetch all the employees who are not working on any project.
Ans. This is one of the very basic interview questions in which the interviewer
wants to see if the person knows about the commonly used – Is NULL operator.
Ques.29. Write a query to fetch employee names and salary records. Display
the employee details even if the salary record is not present for the
employee.
Ans. This is again one of the very common interview questions in which the
interviewer just wants to check the basic knowledge of SQL JOINS.
Here, we can use left join with EmployeeDetail table on the left side of the
EmployeeSalary table.
FROM EmployeeSalary