Welcome to our guide on “SQL Interview Questions”! If you’re preparing for a job interview that involves SQL, you’re in the right place. This article covers some of the most essential and frequently asked questions in SQL interviews. Whether you’re a beginner or an experienced professional, understanding these key queries can give you a competitive edge and help you confidently showcase your SQL knowledge.
Essential SQL Interview Questions and Answers
SQL Query to find second highest salary of Employee ?
Answer : There are many ways to find second highest salary of Employee in SQL, you can either use SQL Join or Subquery to solve this problem. Here is SQL query using Subquery : select MAX(Salary) from Employee WHERE Salary NOT IN (select MAX(Salary) from Employee );
SQL Query to find Max Salary from each department. ?
Answer : SELECT DeptID, MAX(Salary) FROM Employee GROUP BY DeptID.
Write SQL Query to display current date. ?
Answer:SQL has built in function called GetDate() which returns current timestamp.
Write an SQL Query to check whether date passed to Query is date of given format or not. ?
Answer: SQL has IsDate() function which is used to check passed value is date or not of specified format ,it returns 1(true) or 0(false) accordingly. SELECT ISDATE(‘1/08/13’) AS “MM/DD/YY”; It will return 0 because passed date is not in correct format.
Write a SQL Query to print the name of distinct employee whose DOB is between 01/01/1960 to 31/12/1975. ?
Answer: SELECT DISTINCT EmpName FROM Employees WHERE DOB BETWEEN ‘01/01/1960’
AND ‘31/12/1975’;
Write an SQL Query find number of employees according to gender whose DOB is between 01/01/1960 to 31/12/1975.?
Answer : SELECT COUNT(*), sex from Employees WHERE DOB BETWEEN ‘01/01/1960 ‘AND ‘31/12/1975’ GROUP BY sex;
Write an SQL Query to find employee whose Salary is equal or greater than 10000.?
Answer : SELECT EmpName FROM Employees WHERE Salary>=10000;
Write an SQL Query to find name of employee whose name Start with ‘M’ ?
Answer : SELECT * FROM Employees WHERE EmpName like ‘M%’;
Find all Employee records containing the word “Joe”, regardless of whether it was stored as JOE, Joe, or joe. ?
Answer : SELECT * from Employees WHERE upper(EmpName) like upper(‘joe%’);
Write a SQL Query to find year from date.?
Answer : SELECT YEAR(GETDATE()) as “Year”;
Fetch ALTERNATE records from a table. (EVEN NUMBERED) ?
Answe : select * from emp where rowid in (select decode(mod(rownum,2),0,rowid, null) from emp);
To select ALTERNATE records from a table. (ODD NUMBERED) ?
Answer : select * from emp where rowid in (select decode(mod(rownum,2),0,null ,rowid) from emp);
Find the 3rd MAX salary in the emp table. ?
select distinct sal from emp e1 where 3 = (select count(distinct sal) from emp e2 where e1.sal <= e2.sal);
Find the 3rd MIN salary in the emp table. ?
Answer : select distinct sal from emp e1 where 3 = (select count(distinct sal) from emp e2where e1.sal >= e2.sal);
Select FIRST n records from a table. ?
Answer : select * from emp where rownum <= &n;
Select LAST n records from a table ?
Answer : select * from emp minus select * from emp where rownum <= (select count(*) – &n from emp);
List dept no., Dept name for all the departments in which there are no employees in the department. ?
Answer : select * from dept where deptno not in (select deptno from emp);
alternate solution: select * from dept a where not exists (select * from emp b where a.deptno = b.deptno); altertnate solution: select empno,ename,b.deptno,dname from emp a, dept b where
a.deptno(+) = b.deptno and empno is null;
How to get 3 Max salaries ?
select distinct sal from emp a where 3 >= (select count(distinct sal) from emp b where a.sal
<= b.sal) order by a.sal desc;
https://youtu.be/yqqnKfms3Pg?si=9nMcJRPIugQ_fCls