Ans. We can use the EmployeeDetails table to fetch the employee details with a where clause for the manager-
SELECT EmpId, FullName FROM EmployeeDetails WHERE ManagerId = 986;
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.
SELECT DISTINCT(Project) FROM EmployeeSalary;
Ans. SQL Server – Using MINUS- operator-
SELECT * FROM EmployeeSalary MINUS SELECT * FROM ManagerSalary;
MySQL – Since MySQL doesn’t have MINUS operator so we can use LEFT join-
SELECT EmployeeSalary. * FROM EmployeeSalary LEFT JOIN ManagerSalary USING (EmpId) WHERE ManagerSalary.EmpId IS NULL;
Ans. Using sub query-
SELECT EmpId FROM EmployeeDetails where EmpId IN (SELECT EmpId FROM EmployeeSalary);
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.
SELECT EmpId FROM EmployeeSalary WHERE Project IS NULL;
Ans. Here, we will use BETWEEN in the ‘where’ clause to return the EmpId of the employees with salary satisfying the required criteria and then use it as subquery to find the fullName of the employee from EmployeeDetails table.
SELECT FullName FROM EmployeeDetails WHERE EmpId IN (SELECT EmpId FROM EmployeeSalary WHERE Salary BETWEEN 5000 AND 10000);
Ans. Using BETWEEN for the date range ’01-01-2020′ AND ’31-12-2020′-
SELECT * FROM EmployeeDetails WHERE DateOfJoining BETWEEN '2020/01/01' AND '2020/12/31';
Also, we can extract year part from the joining date (using YEAR in mySQL)-
SELECT * FROM EmployeeDetails WHERE YEAR(DateOfJoining) = '2020';
Ans. Using ‘Exists’-
SELECT * FROM EmployeeDetails E WHERE EXISTS (SELECT * FROM EmployeeSalary S WHERE E.EmpId = S.EmpId);
Ans. The query has two requirements – first to fetch the project-wise count and then to sort the result by that count.
For project-wise count, we will be using the GROUP BY clause and for sorting, we will use the ORDER BY clause on the alias of the project-count.
SELECT Project, count (EmpId) EmpProjectCount FROM EmployeeSalary GROUP BY Project ORDER BY EmpProjectCount DESC;
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.
SELECT E.FullName, S.Salary FROM EmployeeDetails E LEFT JOIN EmployeeSalary S ON E.EmpId = S.EmpId;
To validate emails you can use the regular expressions function (REGEXP_LIKE). Consider the below query.
SELECT
Email
FROM
Employee
where NOT REGEXP_LIKE(Email, ‘[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}’, ‘i’);
To check the procedures, you can consider the following query.
SELECT * FROM SampleSource
WHERE Type=’PROCEDURE’
AND NAME IN (‘SP_CONNECTED_AGG’,’SP_UNCONNECTED_AGG’);
To find the procedures columns information, you can consider the following query.
|
StudentID | FirstName | MiddleName | LastName |
1 | Rohit | Kumar | NULL |
2 | Sakshi | Chowdhary | NULL |
3 | NULL | Yash | Singhania |
4 | Akash | NULL | Kumar |
5 | Avinash | NULL | Daksh |
You can use the COALESCE function to return the first non-null value from the table. Consider the below query.
1 | SELECT StudentID, COALESCE(FirstName, MiddleName, LastName) as Name FROM Student |
The answer is quite simple. You cannot use the WHERE clause to restrict the groups. Instead of this, you have to use the HAVING clause.
Your query should be as follows:
1 2 3 4 | SELECT EmployeeID, AVG(Salary) FROM EmployeeDetails HAVING AVG(Salary) > 75 GROUP BY EmployeeID |
Consider the table named “Employee”.
Now, to find the Nth salary consider the below statement.
1 | SELECT DISTINCT(salary) FROM employee ORDER BY salary DESC LIMIT n-1,1 |
So, if you want to find out the 7th largest salary, consider the below query.
1 | SELECT DISTINCT(salary) FROM employee ORDER BY salary DESC LIMIT 6,1 |