You are on page 1of 2

1.How to Display Odd rows(Employee id is should be odd number) in Employee table?

ans select * from employee where mod(employee_id,2)!=0 ;

2.Display last 50% records from Employee table?


ans: select * from emp where employee_id < (select count(emp_id)/2 from emp);
It can be done by using analytical function row_number

3.how to write sql query for the below scenario


I/p:ORACLE
O/p:
O
R
A
C
L
E
i.e, splitting into multiple columns a string using sql

ans : select substring('oracle',emp_id,1) from emp1 order by emp_id asc ;


select unnest(array['o','r','a','c','l','e']) ;

4.If there are two tables emp1 and emp2, and both have common record. How can I
fetch all the
records but common records only once
ans : by using union we can achieve
select * from emp1 union select * from emp2;

5.Write an SQL Query to check whether date passed to Query is in the specified
format

ans : select * from emp where date like '__-__-__'

6.For any given date, get the Last Date of: Previous Month & Current Month & Next
Month

ans : select date_trunc('month',now()) - interval '1 day' as prev_mon


,date_trunc('month',now()) + interval '1 month - 1 day' as cur_mon,
date_trunc('month',now()) + interval '2 month - 1 day' as next_mon;

7.Write a query to calculate number of A in string 'VIKASAAA

ans : select (length(replace('VIKASAAA','A',' ')) - length('vikasaaa')) ;


select array_length(string_to_array('vikasaaa','a'),1)-1 ;

8.What is the main difference between RANK and DENSE_RANK function

ans: rank gives you the ranking within your ordered partition mentioned in 'rank()
over(partioned by m_id) rank_mid' and same values are assigned the same rank with
eliminating next sequent ranks while
dense rank again gives you the ranking within your ordered partition, but the
ranks are consecutive without skipping any values

9.Write a SQL query to maximum salary from a table without using MAX or MIN
aggregate functions
Salar
7500
7525
7550
7600
757
ans : select * from emp order by desc limit 1;

10.What is the result of following query with detailed explanation?


SELECT CASE WHEN nul null THEN �True� ELSE �False' END AS Result;

ans : false will be returned as a column result

eg: select case


when length(first_name)>5 then first_name
else last_name
end as result from emp;

You might also like