For Data engineer Interview you should practice some basics Dataset SQL Queries.
Here I have added Top 30 Dataset Question for SQL Queries
Use this Online Editor for Practice.
Also Checkout this Article for Local Environment Setup.
Here we have given two Data SQL Tables and we need to execute the queries to get end result.
Question: 1 Write an SQL query to fetch the Employee ID and Full Name of all the employees working under Manager with id – ‘10098’.
Answer: SELECT EmpId, FullName FROM EmployeeDetails WHERE ManagerId = 10098;
Question: 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.
SELECT DISTINCT(Project) FROM EmployeeSalary;
Question: 3 Write an SQL query to fetch the count of employees working in project ‘P1’.
Ans. Here, we would be using aggregate function count() with the SQL where clause-
SELECT COUNT(*) FROM EmployeeSalary WHERE Project = 'P1';
Question: 4 Write an SQL query to find the maximum, minimum, and average salary of the employees.
Ans. We can use the aggregate function of SQL to fetch the max, min, and average values-
SELECT Max(Salary), Min(Salary), AVG(Salary) FROM EmployeeSalary;
Question: 5 Write an SQL query to find the employee id whose salary lies in the range of 90,000 and 150,000.
Ans. Here, we can use the ‘Between’ operator with a where clause.
SELECT EmpId, Salary FROM EmployeeSalary WHERE Salary BETWEEN 90000 AND 150000;
Question: 6 Write an SQL query to fetch those employees who live in Boston and work under manager with ManagerId – 10035.
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-
SELECT EmpId, City, ManagerId FROM EmployeeDetails WHERE City='Toronto' AND ManagerId='321';
Question:7 Write an SQL query to fetch common records between two tables.
Ans. SQL Server – Using INTERSECT operator-
SELECT * FROM EmployeeSalary INTERSECT SELECT * FROM ManagerSalary;
Question: 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.
SELECT EmpId FROM EmployeeSalary WHERE NOT Project='P1';
Or using the not equal to operator-
SELECT EmpId FROM EmployeeSalary WHERE Project <> 'P1';
For the difference between NOT and <> SQL operators, check this link – Difference between the NOT and != operators.
Question: 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.
SELECT EmpId, Salary+Variable as TotalSalary FROM EmployeeSalary;
Question: 10 Write an SQL query to fetch all the employees who either live in SFO or work under a manager with ManagerId – 10098.
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-
SELECT EmpId, City, ManagerId FROM EmployeeDetails WHERE City='California' OR ManagerId='321';
Question: 11 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’.
SELECT FullName FROM EmployeeDetails WHERE FullName LIKE ‘__hn%’;
Question: 12 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.
SELECT EmpId FROM EmployeeDetails UNION SELECT EmpId FROM EmployeeSalary;
MySQL – Since MySQL doesn’t have INTERSECT operator so we can use the sub query-
SELECT * FROM EmployeeSalary WHERE EmpId IN (SELECT EmpId from ManagerSalary);
Question: 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-
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;
Question: 14 Write an SQL query to fetch the EmpIds that are present in both the tables – ‘EmployeeDetails’ and ‘EmployeeSalary.
Ans. Using sub query-
SELECT EmpId FROM EmployeeDetails where EmpId IN (SELECT EmpId FROM EmployeeSalary);
Question: 15 Write an SQL query to fetch the EmpIds that are present in EmployeeDetails but not in EmployeeSalary.
Ans. Using sub query-
SELECT EmpId FROM EmployeeDetails where EmpId Not IN (SELECT EmpId FROM EmployeeSalary);
Question: 16 Write an SQL query to fetch the employee full names and replace the space with ‘-’.
Ans. Using ‘Replace’ function-
SELECT REPLACE(FullName, ' ', '-') FROM EmployeeDetails;
Question: 17 Write an SQL query to fetch the position of a given character(s) in a field.
Ans. Using ‘Instr’ function-
SELECT INSTR(FullName, 'Snow') FROM EmployeeDetails;
Question: 18 Write an SQL query to display both the EmpId and ManagerId together.
Ans. Here we can use the CONCAT command.
SELECT CONCAT(EmpId, ManagerId) as NewId FROM EmployeeDetails;
Question: 19 Write a query to fetch only the first name(string before space) from the FullName column of the EmployeeDetails table.
Ans. In this question, we are required to first fetch the location of the space character in the FullName field and then extract the first name out of the FullName field.
For finding the location we will use the LOCATE method in MySQL and CHARINDEX in SQL SERVER and for fetching the string before space, we will use the SUBSTRING OR MID method.
MySQL – using MID
SELECT MID(FullName, 1, LOCATE(' ',FullName)) FROM EmployeeDetails;
SQL Server – using SUBSTRING
SELECT SUBSTRING(FullName, 1, CHARINDEX(' ',FullName)) FROM EmployeeDetails;
Question: 20 Write an SQL query to upper case the name of the employee and lower case the city values.
Ans. We can use SQL Upper and Lower functions to achieve the intended results.
SELECT UPPER(FullName), LOWER(City) FROM EmployeeDetails;
Question: 21 Write an SQL query to find the count of the total occurrences of a particular character – ‘n’ in the FullName field.
Ans. Here, we can use the ‘Length’ function. We can subtract the total length of the FullName field with a length of the FullName after replacing the character – ‘n’.
SELECT FullName, LENGTH(FullName) - LENGTH(REPLACE(FullName, 'n', '')) FROM EmployeeDetails;
Question: 22 Write an SQL query to update the employee names by removing leading and trailing spaces.
Ans. Using the ‘Update’ command with the ‘LTRIM’ and ‘RTRIM’ function.
UPDATE EmployeeDetails SET FullName = LTRIM(RTRIM(FullName));
Question: 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.
SELECT EmpId FROM EmployeeSalary WHERE Project IS NULL;
Question: 24 Write an SQL query to fetch employee names having a salary greater than or equal to 5000 and less than or equal to 10000.
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);
Question: 25 Write an SQL query to find the current date-time.
SELECT SYSDATE FROM DUAL;
Question: 26 Write an SQL query to fetch all the Employees details from EmployeeDetails table who joined in the Year 2020.
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';
Question: 27 Write an SQL query to fetch all employee records from EmployeeDetails table who have a salary record in EmployeeSalary table.
Ans. Using ‘Exists’-
SELECT * FROM EmployeeDetails E WHERE EXISTS (SELECT * FROM EmployeeSalary S WHERE E.EmpId = S.EmpId);
Question: 28 Write an SQL query to fetch project-wise count of employees sorted by project’s count in descending order.
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;
Question: 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.
SELECT E.FullName, S.Salary FROM EmployeeDetails E LEFT JOIN EmployeeSalary S ON E.EmpId = S.EmpId;
Question: 30 Write an SQL query to join 3 tables.
Ans. Considering 3 tables TableA, TableB, and TableC, we can use 2 joins clauses like below-
SELECT column1, column2 FROM TableA JOIN TableB ON TableA.Column3 = TableB.Column3 JOIN TableC ON TableA.Column4 = TableC.Column4;