GeekInterview.com
Series: Subject: Topic:

Oracle SQL Interview Questions

Showing Questions 1 - 20 of 189 Questions
First | Prev | | Next | Last Page
Sort by: 
 | 

Write a query to display alternate records from the employee table?

Asked By: kowmudiswarna | Asked On: Sep 14th, 2007

Answered by: sukanta on: May 26th, 2012

select r, ename from(select rownum r,ename from emp) where mod(r,2)=1;

Answered by: naveen reddy on: Aug 17th, 2011

for even rows:-

select ename,empno, job, rownum from emp group by ename ,rownum ,empno,job having mod(rownum,2)=0


for odd rows please replace '0' with 1 then u get odd rows:-

How to find the two minimum salaries ?

Asked By: fareed | Asked On: Oct 14th, 2005

Answered by: SUDHEER on: May 24th, 2012

First arrange the student ages from student table in the descending order then query with row number condition:

select * from (select rownum as a ,age from student order by age desc)
where a=2;

Answered by: Haresh kumar on: Jan 3rd, 2012

To find out any(1st,2d,3d,....) minimum/maximum salary
you can use either sub query or co-related sub query..
using sub query

select min(sal) from emp where sal <(select min(sal) from emp);(gives 2nd minimum salary).
select min(sal) from emp where sal <(select min(sal) from emp where sal < (select min(sal) from emp)); (3rd)
(similar is true for maxsal)
using co-related sub query
select distinct a.sal from emp a where &n = (select distinct(count b.sal) from emp b where a.sal <=b.sal);
in co-related sub query you are actually comparing values of your main query i.e(a.sal) with (b.sal)
so ,
your sub query has a relationship whit main query .(sub query in not independent)

Display the emploee records who joins the department before their manager?

Asked By: kowmudiswarna | Asked On: Sep 25th, 2007

Answered by: ngoudjou albert on: May 22nd, 2012

We use the selfjoin on the table employees to retrieve the job before thier managers

Code
  1. SELECT * FROM employees e , employees m
  2. WHERE e.manager_id=m.employee_id
  3. AND e.hire_date < m.hire_date;

Answered by: janardhan g on: Aug 7th, 2011

select x.ename from emp x,emp y where x.mgr=y.empno and x.hiredate

Truncate and delete concept

Asked By: NehaBindaas | Asked On: Feb 1st, 2012

Hi, let us take a scenario such that I issue a delete on table emp and truncate on table emp1 which is exact replica (in terms of both structure and data) of emp table.Now , I issue a commit on both sessions. if I do a select * from emp and select * from emp1 now, which one should execute faster and...

Answered by: Johny on: May 18th, 2012

Truncate will not delete the structure of the table. That retains the table, but delete every row. The "DROP" command, will delete everything including the structure

Answered by: Ravinder Lathwal on: May 4th, 2012

1.We can filter data in delete but in truncate we cant
2.Truncate faster than delete;
3.We can rollback delete but truncate is auto commit.
4.Delete is DML but truncate is DDL.

Find out nth highest salary from emp table

Asked By: Interview Candidate | Asked On: Aug 29th, 2004

Select distinct (a.Sal) from emp a where &n = (select count (distinct (b.Sal)) from emp b where a.Sal < = b.Sal);for eg:-enter value for n: 2sal---------3700

Answered by: rupesh on: May 4th, 2012

Code
  1. SELECT rownum,salary,last_name
  2. FROM (SELECT salary,last_name FROM employees ORDER BY salary DESC)
  3. WHERE rownum<=n;

where n is the number of person you want to show. eg: if n=3,itll show first three values.

Answered by: saketp on: Apr 29th, 2012

this is the simplest of all :)

Code
  1. SELECT * FROM(SELECT ROWNUM AS a,salary FROM employees ORDER BY salary DESC) WHERE a = "enter your nth value here";

Dispaly employee records who gets more salary than the average salary in their department?

Asked By: kowmudiswarna | Asked On: Sep 14th, 2007

Answered by: farhan on: Apr 20th, 2012

Code
  1. SELECT * FROM emp  WHERE sal >(SELECT avg(sal) FROM emp)

Answered by: uma on: Mar 5th, 2012

select * from emp a
where salary >(select avg(salary) from emp b where a.dept_no = b.dept_no);

How can you retrieve the numeric values from varchar2 type column?

Asked By: Balapradeep Reddy | Asked On: Feb 10th, 2012

Hi friends, one table is there. It is having varchar2 type column and this column having both numeric and alphanumeric values but I want to retrieve only numeric related values from that table. If it is possible then say the answer plz... I will wait for ur tremendous answer...

Answered by: Nisa on: Apr 17th, 2012

Using regexp_replace function

Code
  1. SELECT to_number(regexp_replace(abc123efg456kjhdf,[[:alpha:]]))
  2. FROM dual;

Answered by: Sudipto on: Apr 17th, 2012

Code
  1. SELECT * FROM table_name WHERE regexp_like (column_name, ^[0 - 9] + $);

Session 1delete from emp; session 2 select * from emp;what will be the result?

Asked By: chiranjeevi reddy | Asked On: Feb 8th, 2006

Answered by: giriraj gupta on: Apr 9th, 2012

Only data will be deleted for first session and for second session data will be available because there is no commit in first session.

Answered by: mfasiur on: Oct 13th, 2009

Yes,Abhishek is right.I already opened 2 sessions & did this exercise.In the 1st window,i issued delete from emp & went to 2nd window.I was able to see all the rows in the 2nd session.THis is ...

Reversing string in Oracle without using reverse function

Asked By: amit88 | Asked On: Apr 5th, 2012

How do I reverse a string using only Oracle SQL without using the reverse function??

Answered by: Siva Kumar S on: Apr 7th, 2012

I just took employees tables last_name. See the code.

Code
  1. SELECT substr(last_name,4,1)||substr(last_name,4,2)||substr(last_name,2,1)||substr(last_name,1,1)
  2. "Reversing Name" FROM employees
  3. WHERE employee_id=100

Set operator

Asked By: jagadeesh9 | Asked On: Mar 25th, 2008

What is a set operator? What are the types what is the difference between set operators and joins?

Answered by: Aparna on: Apr 5th, 2012

Union --> All rows/cols from 1st select and 2nd select with no duplicates Union All -->All rows/cols from 1st Select and 2nd Select with duplicates Intersect --> Rows common in both select statements...

Answered by: dajjaal on: Sep 20th, 2010

The UNION operator is used to combine the result-set of two or more SELECT statements.   SQL joins are used to query data from two or more tables, based on a relationship between certain columns in these tables.

Difference between nested query,sub query and nested query?

Asked By: pvsp.indian | Asked On: Jun 15th, 2008

What is difference between nested query,sub query and nested query? Explain clearly with an example?

Star Read Best Answer

Editorial / Best Answer

Answered by: arpitapatel

Member Since Sep-2008 | Answered On : Sep 24th, 2008

Query inside the query is nested query.
It is also called as sub query.
Correlated subquery runs once for each row selected by the outer query. It contains a reference to a value from the row selected by the outer query.

Nested subquery runs only once for the entire nesting (outer) query. It does not contain any reference to the outer query row.

Answered by: rams on: Apr 3rd, 2012

Both are same

Answered by: katgeektalk on: Aug 22nd, 2010

The subquerry is often reffered to as a nested SELECT, sub-SELECT, or inner SELECT statement.
The subquery generally excuted first, and its output is used to complete the query condition for the main (or outer) query.

Get 10 and 11th records in the output using SQL in 5 ways ?

Asked By: kalyan.sam | Asked On: Dec 5th, 2011

I have to tables a and b. My records in this table are in such a way that table a table b 10 15 11 16 12 10 13 11 question :- I want only the 10 and 11 records as my output. can...

Answered by: hussain on: Mar 29th, 2012

Code
  1. SELECT * FROM emp WHERE (rowed,0) IN (SELECT rowid,mod(rownum,5) FROM emp);

Answered by: SivaKumar on: Mar 23rd, 2012

Method 1

Code
  1. SELECT a,employee_id,last_name,first_name,salary FROM
  2. (SELECT ROWNUM AS a,employee_id,last_name,first_name,salary FROM employees)
  3. WHERE a BETWEEN 10 AND 11

Display the number value in words?

Asked By: Interview Candidate | Asked On: Aug 30th, 2005

SQL> select sal, (to_char(to_date(sal,'j'), 'jsp'))from emp;the output like,sal (to_char(to_date(sal,'j'),'jsp'))--------- -----------------------------------------------------800 EIght hundred1600 one thousand six hundred1250 one thousand two hundred fiftyif you want to add some text like, rs. Three...

Answered by: boobal on: Mar 21st, 2012

This answer is no different to
select to_char(to_date(1290,j),jsp) from dual;

Answered by: sweetytrivedi on: Jun 25th, 2008

In order to display the number values , the best it can be done is with the julian i.e. j and jsp.

however, using the translate also returns the same value for whatever value to display.

select to_char(to_date(1920,'j'),'jsp') from dual;

Which is more faster - in or exists?

Asked By: Interview Candidate | Asked On: Aug 29th, 2004

Exists is more faster than in because exists returns a boolean value whereas in returns a value.

Answered by: rohitosu on: Mar 9th, 2012

As a rule of thumb IN should be used when the sub query will return small result set. It should be kept in mind that the sub query should not return null values. EXISTS should be used when the result set returns large result set. Other factors that might affect query performance is the indexes

Answered by: Vineet Arya on: Sep 17th, 2011

Which is more faster - IN or EXISTS? It totally depends upon the query. For some queries Exist is efficient and for some IN is efficient Exits is correlated sub query, in which the outer query run f...

How can you retrieve the random records from the table?

Asked By: Balapradeep Reddy | Asked On: Feb 10th, 2012

Hi friends, one table having some many records but I want to retrieve under little-bit records randomly. If it is possible in SQL then send the reply ? So, I will wait for ur valuable answer...

Answered by: Balapradeep Reddy on: Feb 14th, 2012

Code
  1. SELECT *FROM ( SELECT * FROM table_name ORDER BY dbms_random.value ) WHERE rownum <=5;

Suppose there are two fields in table(employee) say name and salary and there are in total 100 records in table. Now my query gives the name of an employee whose salary is 10th among 100 salaries....

Asked By: Interview Candidate | Asked On: Aug 30th, 2005

Answered by: GeekQA123 on: Feb 3rd, 2012

It will work--No error
select salary,num from (select salary,rownum num from employees order by salary desc)
where num=2;

Answered by: sheel kumar handa on: Feb 13th, 2007

Hiii to userSELECT DISTINCT (a.sal) FROM EMP A WHERE &N = (SELECT COUNT (DISTINCT (b.sal)) FROM EMP B WHERE a.sal<=b.sal); WITH THIS RESULT U GOT ANY NUMBER OF SALARY IN A GIVEN TABLE PLZ TRY IT;S BEST WAY TO FIND ANY NUMBER OF SALARY IN GIVEN TABLE....................... ONLY SIMPLY ENTER THE VALUE OF '" N " &GET ANY POSTION OF SALARY ,....EXAM... IF U WANT 5th SALARY THEN PUT .........N=5... OR U WANT 7th SALARY FROM TABLE PUT ONLY N=7....................and so on .............

To get second highest age from the student table

Asked By: technofreek | Asked On: May 18th, 2011

I am trying to get second highest age from the student table.Student table:sql> desc student; name null? Type ----------------------------------------- -------- ----------------- rollno number name ...

Answered by: Santhosh on: Jan 17th, 2012

Code
  1. SELECT min(age) FROM student WHERE age IN (SELECT DISTINCT top 2 age FROM student orderby age DESC)

Answered by: nagaraj on: Jan 10th, 2012


select rollno,name,age from
(select rollno, name,age, dense_rank() over ( partition by null order by age desc ) rnk
from student)
where rnk = 2

How to filter the null records in filter by expression?

Asked By: madhu922.m | Asked On: Dec 20th, 2011

For example I have 50 columns I filter the nulls in fbe.In filter by exp a parameter select_exp .What function is used .How to write the condition .I take a example on emp file(empno,ename,sal......).How to write the condition in filter by expression?

Answered by: G on: Jan 13th, 2012

U can use : " <>" to filter null values using code.
Eg:
with worksheet.range("A1:B10")
.autofilter 2, criteria1: "<>"
end with

Answered by: shiv on: Jan 2nd, 2012

IS NOTNULL(salary)

Find manager for employee

Asked By: rajaramanv | Asked On: May 24th, 2008

Id employee department manager----------------------------------------------1 suresh c++ null 2 suresh c++ null3 suresh c++ 25 sarathy testing 26 rajaraman c# 17 joe flash ...

Answered by: OracleLearner on: Jan 11th, 2012

The code below retrieves the manager name. Note that employees who do not have manger is also listed. Select Emp1.Employee_Id, Emp1.first_name, Emp1.last_name, Emp1.Manager_id, Emp2.first_name...

Answered by: shiiva on: Sep 22nd, 2008

SELECT e.ename as Manager, m.ename as Employee FROM emp e, emp m WHERE e.MGR = m.EMPNO

How to get first 5 records then next 5 records till end of row count in SQL -Oracle

Asked By: Dev | Asked On: Feb 6th, 2006

Answered by: ananymous on: Jan 6th, 2012

The best ans given above is

Code
  1.  
  2. SELECT * FROM (SELECT ROWNUM R ,CUST_NO FROM S1_ETL.CA_STTC_DETS_1) WHERE R BETWEEN &F AND &L

Answered by: SIVA KARTHIK on: Dec 13th, 2011

SELECT * FROM (SELECT ROWNUM R ,ENO FROM EMP) WHERE R BETWEEN &F AND &L
/

First | Prev | | Next | Last Page

 

 

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Career Counselling

 Have Career Question?

 Ask Chandra

 Ask Only Career questions.

Follow us:
 

Latest Questions

Ads

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, click "Subscribe".