MOST FREQUENTLY ASKED SQL QUERY

1.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 : 1. select MAX(Salary) from Employee WHERE Salary NOT IN (select MAX(Salary) from Employee );
2. SQL Query to find Max Salary from each department.
Answer : SELECT DeptID, MAX(Salary) FROM Employee GROUP BY DeptID.
3.Write SQL Query to display current date.
Ans:SQL has built in function called GetDate() which returns current timestamp. SELECT GetDate();
4.Write an SQL Query to check whether date passed to Query is date of given format or not.
Ans: 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.
5. Write a SQL Query to print the name of distinct employee whose DOB is between 01/01/1960 to 31/12/1975.
Ans: SELECT DISTINCT EmpName FROM Employees WHERE DOB BETWEEN ‘01/01/1960’ AND ‘31/12/1975’;

6.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;

7.Write an SQL Query to find employee whose Salary is equal or greater than 10000.

Answer : SELECT EmpName FROM Employees WHERE Salary>=10000;

8.Write an SQL Query to find name of employee whose name Start with ‘M’

Ans: SELECT * FROM Employees WHERE EmpName like ‘M%’;

9.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%’);

  1. Write a SQL Query to find year from date.

Answer : SELECT YEAR(GETDATE()) as “Year”;

11.To fetch ALTERNATE records from a table. (EVEN NUMBERED) select * from emp where rowid in (select decode(mod(rownum,2),0,rowid, null) from emp);

12.To select ALTERNATE records from a table. (ODD NUMBERED) select * from emp where rowid in (select decode(mod(rownum,2),0,null ,rowid) from emp);

13.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);

14.Find the 3rd MIN salary in the emp table. select distinct sal from emp e1 where 3 = (select count(distinct sal) from emp e2where e1.sal >= e2.sal);

15.Select FIRST n records from a table. select * from emp where rownum <= &n;

16.Select LAST n records from a table select * from emp minus select * from emp where rownum <= (select count(*) – &n from emp);

17.List dept no., Dept name for all the departments in which there are no employees in the department. 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);

>= (select count(distinct sal) from emp b where a.sal >= b.sal);

20.How to get nth max salaries ? select distinct hiredate from emp a where &n = (select count(distinct sal) from emp b where a.sal >= b.sal);

21.Select DISTINCT RECORDS from emp table. select * from emp a where rowid = (select max(rowid) from emp b where a.empno=b.empno);

22.How to delete duplicate rows in a table? delete from emp a where rowid != (select max(rowid) from emp b where a.empno=b.empno);

23.Count of number of employees in department wise. select count(EMPNO), b.deptno, dname from emp a, dept b where a.deptno(+)=b.deptno group by b.deptno,dname;

  1. Suppose there is annual salary information provided by emp table. How to fetch monthly salary of each and every employee?

select ename,sal/12 as monthlysal from emp;

25.Select all record from emp table where deptno =10 or 40.

select * from emp where deptno=30 or deptno=10;

26.Select all record from emp table where deptno=30 and sal>1500.

select * from emp where deptno=30 and sal>1500;

27.Select all record from emp where job not in SALESMAN or CLERK.

select * from emp where job not in (‘SALESMAN’,’CLERK’);

28.Select all record from emp where ename in ‘BLAKE’,’SCOTT’,’KING’and’FORD’.records where ename may be any no of character but it should end with ‘R’.

select * from emp where ename like’%R’;

31.Count MGR and their salary in emp table.

select count(MGR),count(sal) from emp;

32.In emp table add comm+sal as total sal .

select ename,(sal+nvl(comm,0)) as totalsal from emp;

33.Select any salary <3000 from emp table.

select * from emp where sal> any(select sal from emp where sal<3000);

34.Select all salary <3000 from emp table.

select * from emp where sal> all(select sal from emp where sal<3000);

35.Select all the employee group by deptno and sal in descending order.

select ename,deptno,sal from emp order by deptno,sal desc;

36.How can I create an empty table emp1 with same structure as emp?

Create table emp1 as select * from emp where 1=2;

37.How to retrive record where sal between 1000 to 2000? Select * from emp where sal>=1000 And sal<2000

38.Select all records where dept no of both emp and dept table matches. select * from emp where exists(select * from dept where emp.deptno=dept.deptno)

39.If there are two tables emp1 and emp2, and both have common record. How can I fetch all the recods but common records only once? (Select * from emp) Union (Select * from emp1)

40.How to fetch only common records from two tables emp and emp1? (Select * from emp) Intersect (Select * from emp1)

  1. How can I retrive all records of emp1 those should not present in emp2? (Select * from emp) Minus (Select * from emp1)

42.Count the totalsa deptno wise where more than 2 employees exist. SELECT deptno, sum(sal) As totalsal FROM emp GROUP BY deptno HAVING COUNT(empno) > 2

43.Display the names of employees who are working in the company for the past 5 years.

select ename from emp where sysdate-hiredate>5*365;

44.Display the list of employees who have joined the company before 30th June 90 or after 31st dec 90.

select * from emp where hiredate between ‘30-jun-1990’ and ‘31-dec-1990’;

45.Display the names of employees working in department number 10 or 20 or 40 or employees working as clerks, salesman or analyst.

select ename from emp where deptno in (10,20,40) or job in (‘CLERK’,’SALESMAN’,’ANALYST’);

46.Display the names of employees whose name starts with alphabet S.

select ename from emp where ename like ‘S%’;

47.Display employee names for employees whose name ends with alphabet.

select ename from emp where ename like ‘%S’;

 

Leave a Comment

Your email address will not be published. Required fields are marked *