20 most popular SQL Query Interview Questions and Answers:
SQL or Structured Query Language is a standard language for dealing with relational databases. it is very important for us to understand how to use queries to retrieve the required data.I will discuss a few queries which you must practice to become a Database Administrator.I will be discussing the top SQL queries which could be asked in your interviews.
SQL or Structured Query Language is a standard language for dealing with relational databases. it is very important for us to understand how to use queries to retrieve the required data.I will discuss a few queries which you must practice to become a Database Administrator.I will be discussing the top SQL queries which could be asked in your interviews.
Q1. Write a query to create a new table which consists of data and structure copied from the other table.
Ans:- SELECT * INTO NewTable FROM EmployeeInfo WHERE 1 = 0;
Q2. Write a query to fetch the number of employees working in the department ‘HR’.
Ans:- SELECT COUNT(*) FROM EmployeeInfo WHERE Department = 'HR';
Q3. Write a query to get the current date.
Ans:- SELECT GETDATE();
Q4. Write a query to find the names of employees that begin with ‘S’.
Ans:- SELECT * FROM EmployeeInfo WHERE EmpFname ='S%';
Q5. Write a query to fetch top N records.
Ans:- SELECT TOP N * FROM EmployeePosition ORDER BY Salary DESC;
Q6. Write a query to retrieve the EmpFname and EmpLname in a single column as “FullName”. The first name and the last name must be separated with space.
Ans:- SELECT CONCAT(EmpFname, ' ', EmpLname) AS 'FullName' FROM EmployeeInfo;
Q7. Write a query to fetch the EmpFname from the EmployeeInfo table in upper case and use the ALIAS name as EmpName.
Ans:- SELECT EmpFname AS UPPER(EmpName) FROM EmployeeInfo;
Q8. Write a query find number of employees whose DOB is between 02/05/1970 to 31/12/1975 and are grouped according to gender.
Ans:- SELECT COUNT(*), Gender FROM EmployeeInfo
WHERE DOB BETWEEN '02/05/1970 ' AND '31/12/1975' GROUP BY Gender;
Q9. Write a query to retrieve the first four characters of EmpLname from the EmployeeInfo table.
Ans:- SELECT SUBSTRING(EmpLname, 1, 4) FROM EmployeeInfo;
Q10. Write a query to fetch all the records from the EmployeeInfo table ordered by EmpLname in descending order and Department in the ascending order.
Ans:- SELECT * FROM EmployeeInfo ORDER BY EmpLname desc, Department asc;
Q11. Write a query to fetch department-wise count of employees sorted by department’s count in ascending order.
Ans:- SELECT Department, count(EmpID) AS EmpDeptCount
FROM EmployeeInfo GROUP BY Department
ORDER BY EmpDeptCount ASC;
Q12. Write a SQL query to retrieve employee details from EmployeeInfo table who have a date of joining in the EmployeePosition table.
Ans:- SELECT * FROM EmployeeInfo E
WHERE EXISTS
(SELECT * FROM EmployeePosition P WHERE E.EmpId = P.EmpId);
Q13. Write a query to fetch all employees who also hold the managerial position.
Ans:- SELECT E.EmpFname, E.EmpLname, P.EmpPosition
FROM EmployeeInfo E INNER JOIN EmployeePosition P ON
E.EmpID = P.EmpID AndAP.EmpPosition ('Manager');
Q14. Write a query to fetch details of all employees excluding the employees with first names, “Sanjay” and “Sonia” from the EmployeeInfo table.
Ans:- SELECT * FROM EmployeeInfo WHERE EmpFname NOT IN ('Sanjay','Sonia');
Q15. Write a query to add email validation to your database.
Ans:- SELECT Email FROM EmployeeInfo
WHERE NOT REGEXP_LIKE(Email, ‘[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}’, ‘i’);
Q16. Write a query to retrieve Departments who have less than 2 employees working in it.
Ans:- SELECT DEPARTMENT, COUNT(EmpID) as 'EmpNo' FROM EmployeeInfo
GROUP BY DEPARTMENT HAVING COUNT(EmpD) < 2;
Q17. Write a query to find the third-highest salary from the EmpPosition table.
Ans:- SELECT TOP 1 salary
FROM(
SELECT TOP 3 salary
FROM employee_table
ORDER BY salary DESC) AS emp
ORDER BY salary ASC;
Q18. Write a query to retrieve the last 3 records from the EmployeeInfo table.
Ans:- SELECT * FROM EmployeeInfo WHERE
EmpID <=3 UNION SELECT * FROM
(SELECT * FROM EmployeeInfo E ORDER BY E.EmpID DESC)
AS E1 WHERE E1.EmpID <=3;
Q19. Write a query to retrieve the list of employees working in the same department.
Ans:- Select DISTINCT E.EmpID, E.EmpFname, E.Department
FROM EmployeeInfo E, Employee E1
WHERE E.Department = E1.Department AndAE.EmpID != E1.EmpID;
Q20. Write a query to retrieve EmpPostion along with total salaries paid for each of them.
Ans:- SELECT EmpPosition, SUM(Salary) from EmployeePosition GROUP BY EmpPosition;
So this brings us to the end of the SQL Query Interview Questions article. I hope this set of SQL Query Interview Questions will help you ace your job interview. All the best for your interview!
Got a question for us? Please mention it in the comments section of this “SQL Query Interview Questions” article and we will get back to you as soon as possible.
No comments:
Post a Comment