Вы находитесь на странице: 1из 5

1) Find second higest salary

SELECT MAX(SALARY) FROM EMP WHERE SALARY NOT IN (SELECT MAX(SALARY) FROM EMP);
2)
Max salary from each dept then
SELECT DEPID, MAX(SALARY) FROM EMP GROUPBY DEPID;

3) if depnm is asked against depid then we need joinning ..


SELECT DEPNAME, MAX(SALARY) FROM EMP e RIGHT JOIN DEPT d ON e.EMPID = d.DEPID G
ROUPBY DEPNAME;

4)Display current date


GETDATE()
5)Write an SQL Query to check whether date passed to Query is the date of given
format or not.
select EXTRACT (Year from date 'dd/mm/yyyy') from DUAL;
check it out in oracle ... there is isdate() is their but maynot work in oracle
.. so check in internet..
6)write query to check distinct emp whose dob is between dd/mm/yyyy to dd/mm/yyy
y
SELECT DISTINCT EMPNAME FROM EMP WHERE DOB BETWEEN 'dd/mm/yyyy' AND 'dd/mm/yyyy
' ;

7)find number of emp according to gender whose dob id between two given dates,
SELECT COUNT(*), SEX FROM EMP WHERE DOB BETWEEN 'dd/mm/yyyy' AND 'dd/mm/yyyy' G
ROUPBY SEX ;

8) find emp whose salary is greater than or equal to 10000,


SELECT EMPNAME FROM EMP WHERE SALARY >= 10000 ;

9) find emp whose name start with m


SELECT * FROM EMP WHERE EMPNAME LIKE 'M%' ;

10) find all emp record where name is like JOE or joe or Joe
SELECT * FROM EMP WHERE UPPER(EMPNAME) LIKE %JOE% ;

11) Write SQL Query to find Duplicate records then delete them in Oracle DB ! Im
portant check it out in internet
********************************************************************************
*******
AnonymousNovember 13, 2013 at 11:25 PM
for selecting the distinct records:
select * from emp a where rowid = (select max(rowid) from emp b where a.empno=b.
empno)
SELECT * FROM EMP a EHERE rowid = ( SELECT MAX(rowid) FROM EMP b where a.empno
= b.empno );

to Delete:
delete from emp a where rowid != (select max(rowid) from emp b where a.empno=b.e
mpno);
DELETE from EMP a WHERE rowid != ( SELECT MAX(rowid) FROM EMP b WHERE a.empno =
b.empno );

-----
I know to ways one way use max and another way use distinct
one way :
DELETE
FROM TestTable
WHERE ID NOT IN
(
SELECT MAX(ID)
FROM TestTable
GROUP BY NameCol)
GO

and

another way

Select Distinct id, name into #temp from emp;


Truncate table emp;
Insert into emp(id,name) select id,name from #temp;
----------

Nandakumar Govindarajulu EthirajuluJanuary 14, 2016 at 11:47 AM


Ans:
Select Key column --> Group by Key column --> Having count(*) > 1
Will give Duplicate records
Replace select with Delete statement

Nandakumar Govindarajulu EthirajuluJanuary 14, 2016 at 11:54 AM


eg., Delete from customer where cus_id in (select cus_id from customer
group by cus_id
having count(*) > 1)

Nandakumar Govindarajulu EthirajuluJanuary 14, 2016 at 12:02 PM


For performance (Millions of records) Use exists instead of in and Constant for
Existence Checking
eg.,
delete from customer a where exists (select 1 from customer b where a.cus_id = b
.cus_id
group by cus_id
having count(*) > 1)

Jamal AhmedMarch 13, 2017 at 5:02 AM


You can use the RowNumber() to delete duplicate records -
DELETE E
FROM Employee E
INNER JOIN (Select RowNumber () over (Partitioned by ID group by ID) R
From Employee) Emp
ON E.ID=Emp.ID
Where Emp.R>1
Where R>1

********************************************************************************
*******
12) Find students mark greater then the average !
select student name, marks from table stu where mark > (select avg(mark) from t
able stu) ;
13)
find all emps who are also managers ;
here we may use a self join .. like example
select e.empname, m.empname from emp e, emp m where e.mgrid = m.empid ;
14) you have composite index of three columns and you may provide value of two
columns in where clause of select query
Will index be used for this kind of operaions?
check the internet please !!!!
-----------------
Q1. What is the use of GRANT command?
Ans. GRANT command is used to grant specific user to perform specific task.
Q2. What is the use of SQL check constraint?
Ans. CHECK constraint limits the value range that can be placed in a column.
Difference between rank() and dense_rank() in SQL?
Difference between where and having clause in SQL?
Difference between correlated vs non-correlated subquery?
primary key vs foreign key
truncate vs delete etc.
What is difference between Clustered Index and No-Clustered Index??
SELECT EXTRACT(YEAR FROM DATE '1998-03-07') FROM DUAL;

EXTRACT(YEARFROMDATE'1998-03-07')
---------------------------------
1998

Вам также может понравиться