You are on page 1of 2

COMPLEX SQL QUERIES:

To fetch ALTERNATE records from a table. (EVENNUMBERED)


select * from emp where rowid in (selectdecode(mod(rownum,2),0,rowid, null) from emp);2.
To select ALTERNATE records from a table. (ODDNUMBERED)
select * from emp where rowid in (selectdecode(mod(rownum,2),0,null ,rowid) from emp);3.
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);4.
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);5.
How do I get a top ten?
select a.ordered_column, a.other_stuf
from table_name a
where 10 > (
select count(1)
from table_name b
where b.ordered_column
< a.ordered_column )
order by a.ordered_columnl;

How to get the First name, second name, Third name?


SELECT SUBSTR('first second third',0,INSTR('first second third',' ', 1, 1))
FROM dual;
output: 'first
'
SELECT SUBSTR('first second third',
INSTR('first second third',' ', 1, 1)+1,
INSTR('first second third',' ',1,2)-INSTR('first second third',' ',1,1)-1)
FROM dual;
output: ' Second'
SELECT SUBSTR('first second third',
INSTR('first second third',' ', 1, 2)+1,
INSTR('first second third',' ',1,2)-INSTR('first second third',' ',1,1)-1)
FROM dual;
Output:'third '
Combine above 3 queries into a select statement as follows to get desired output of 3 names in 3
seperate columns
Select substr query1, substr query2, substr query3 from dual;

How write a SQL statement to query the result set and display row as
columns and columns as row?
Paul |
Red
Green
Blue

John | Tim | Eric


1
5
1
3
8
4
3
5
2
2
9
1

Red | Green | Blue


Paul 1
8
2
John 5
4
2
Tim
1
3
9
Eric 3
5
1

Union All, Aggregate and CASE Version:


select name,
sum(case when color = 'Red' then value else 0 end) Red,
sum(case when color = 'Green' then value else 0 end) Green,
sum(case when color = 'Blue' then value else 0 end) Blue
from
(
select color, Paul value, 'Paul' name
from yourTable
union all
select color, John value, 'John' name
from yourTable
union all
select color, Tim value, 'Tim' name
from yourTable
union all
select color, Eric value, 'Eric' name
from yourTable
) src
group by name

How to delete identical row from a table?


How to delete identical row from a table?
if we have 4 row and two of them having same data then how we delete identical row using delete query

You might also like