Suggested Topic

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

1.

How to find dulpicate record from given table

1.Using the GROUP BY clause to group all rows by the target column(s) –
i.e. the column(s) you want to check for duplicate values on.
2.Using the COUNT function in the HAVING clause to check if any of the groups have
more
than 1 entry; those would be the duplicate values.
eg:-

SELECT name, COUNT(email)


FROM users
GROUP BY email
HAVING COUNT(email) > 1;

SELECT OrderID, COUNT(OrderID)


FROM Orders
GROUP BY OrderID
HAVING COUNT(OrderID) > 1;

2.In a table
Update "Male" into "Female" and "Female" into "Male" in Gender Column of a
table in single line query.?

UPDATE tableA
SET Gender = CASE
WHEN Gender = 'Male' THEN 'Female'
ELSE 'Male'
END

UPDATE table SET gender =


(CASE WHEN gender ='male' THEN 'female' else 'male' END);

3.2nd highest salary of employee

select max(salary) from employee where salary < (select max(salary) from employee);

4.How to get 3 Highest salaries records from Employee table?

select min(salary) from (select distinct salary from emp orderby salary desc)
where rownum<=3;

===================================

https://www.java67.com/2013/04/10-frequently-asked-sql-query-interview-questions-
answers-database.html

1.SQL Query to find Max Salary from each department.

select deptid,max(salary)
from employee
group by deptid

## printing deptname using JOIN

SELECT DeptName, MAX(Salary) FROM Employee e RIGHT JOIN Department d ON e.DeptId =


d.DeptID GROUP BY DeptName;

2.Write SQL Query to display the current date?


select GetDate();

3.Write an SQL Query to check whether the date passed to Query is the date of the
given format or not?

select isdate('1/05/21') as ('MM/DD/YY') --- the query will return false because it
is not in expected format

4. Write an SQL Query to print the name of the distinct employee whose DOB is
between 01/01/1960 to 31/12/1975.

select distinct(empname)
from employee
where DOB between ('01/01/1960' AND '31/12/1975');

5.Write an SQL Query to find the number of employees according to gender whose DOB
is between 01/01/1960 to 31/12/1975

-- SELECT COUNT(*), sex FROM Employees WHERE DOB BETWEEN '01/01/1960' AND
'31/12/1975' GROUP BY sex;

6. Write an SQL Query to find an employee whose salary is equal to or greater than
10000

SELECT EmpName FROM Employees WHERE Salary >= 10000;

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

SELECT * FROM Employees WHERE EmpName like 'M%';

8.find all Employee records containing the word "Joe", regardless of whether it was
stored as JOE, Joe, or joe.

SELECT * from Employees WHERE UPPER(EmpName) like '%JOE%';

9.How do you find all employees who are also managers?

SELECT e.name, m.name FROM Employee e, Employee m WHERE e.mgr_id = m.emp_id;

You might also like