GeekInterview.com
Series: Subject: Topic:

SQL Interview Questions

Showing Questions 21 - 40 of 214 Questions
First | Prev | | Next | Last Page
Sort by: 
 | 

How do you print the last n rows or the first n rows of a table ?

Asked By: bharaniprasanth | Asked On: Mar 3rd, 2008

Star Read Best Answer

Editorial / Best Answer

Answered by: nirmal1in

View all questions by nirmal1in   View all answers by nirmal1in

Member Since Jan-2010 | Answered On : Jan 16th, 2010

This can be accomplished in following way:

Example: table - emp

For First n rows:

SELECT * FROM
(SELECT empno,ename,job,row_number() over (order by ename desc) a
FROM emp) x
WHERE x.a < 5 --- say n is 5 display first 5 records

For last n rows:

SELECT * FROM
(SELECT empno,ename,job,row_number() over (order by ename) a
FROM emp) x
WHERE x.a < 5

FROM )WHERE .a<

This query can be used for finding nth row also say n = 5

SELECT * FROM
(SELECT empno,ename,job,row_number() over (order by ename desc) a
FROM emp) x
WHERE x.a = 5

Answered by: deepthi on: May 17th, 2012

Code
  1. SELECT rownum,a.*FROM(SELECT rownum,b.*FROM emp b ORDER BY rownum DESC) a WHERE rownum <&n;

Answered by: Vasavi Katakam on: Jul 18th, 2011

For First N Rows:

Code
  1.  
  2. SELECT top 5 * FROM table_name
  3.  

For Last N Rows:

Code
  1.  
  2. SELECT * FROM
  3. (SELECT top 5 * FROM
  4. table_name
  5. ORDER BY Columnname DESC)
  6. ORDER BY Columnname ASC
  7.  

Find second lowest salary from emp table

Asked By: lalit.eng.kumar | Asked On: May 1st, 2008

Display the second lowest salary from emp table without using min & all

Answered by: Gunjan David on: May 13th, 2012

Ypu need to know MR. SQL that you cant use = operator with rownum ....

Answered by: ram on: Apr 16th, 2012

Code
  1. SELECT * FROM emp a WHERE (&n-1) =(SELECT count(UNIQUE sal) FROM emp b WHERE a.sal>b.sal) FOR nth minimum salary
  2. SELECT * FROM emp a WHERE (&n-1) =(SELECT count(UNIQUE sal) FROM emp b WHERE a.sal<b.sal) FOR nth maximum salary

Query to find the list of employees age > 30

Asked By: rekssql | Asked On: Jan 11th, 2007

Write a query to find the list of employees whos age is greater than 30 or so given a date of birth column

Answered by: maninder singh on: Apr 9th, 2012

Please tell me,
How to get age of all employee by using query

Answered by: Santhoshkandula on: May 18th, 2011



      SELECT *,DATEDIFF(YY,,GETDATE()) AS AGE FROM WHERE DATEDIFF(YY,,GETDATE()) > 30




  Thanks & Regards
  K.santhosh

Deleting duplicate records in a table without using rowid

Asked By: varadarajan | Asked On: Aug 29th, 2006

1. How to delete a duplicate records in a table without using rowid? 2. What is the use of connect by clause? 3. What is the use of connect by clause? 4. How to display "experience of employee"?E.G. 3 years 4 months 19 days? 5. What is select statement to spe?

Answered by: Muralidhar on: Apr 6th, 2012

We delete the duplicate records using level and rownum. The query is

Code
  1. DELETE FROM emp WHERE ROWNUM IN(SELECT MAX(ROWNUM) FROM emp WHERE LEVEL=nth CONNECT BY PRIOR empno>empno GROUP BY LEVEL);

Answered by: pravesh28 on: Nov 28th, 2010

Delete from table_name where ROWID not in ( select

max(rowid) from table group by duplicate_values_field_name);

     

3 way join

Asked By: vital_parlsey | Asked On: Mar 1st, 2012

Hi, i have 3 files each having 2 cols e.G file1 empid deptid 1 100 2 200 3 200 4 100 5 300 file2 empid salary 1 10 2 20 3 30 4 40 5 ...

Answered by: Mark Haynes on: Mar 2nd, 2012

I believe what you are looking for is a query like this:

select f3.deptname dept_name, sum(isnull(f2.salary,0)) amt_spent_each_dept
from file3 f3 left join file1 f1 on (f3.deptid = f1.deptid)
left join file2 f2 on (f1.empid = f2.empid)
group by f3.deptname
order by f3.deptname

What is the advantage in creating force view?

Asked By: samyuvi | Asked On: Feb 18th, 2012

Answered by: Lokesh M on: Feb 18th, 2012

Force view creates the view even if the underlying tables / referenced objects are not present. Force View are used while executing a script that has create tables and views statements or while creating a view for remote table.

What is the advantage to use trigger in your pl?

Asked By: Atmaram Pradhan | Asked On: Jun 21st, 2006

Answered by: bhagya on: Feb 10th, 2012

1.Auditing the tables
2.Online transaction
3.Securing from another table
4.Automatic calculations
5.Replicate the tables

Answered by: chennoju on: Aug 3rd, 2006

The main advantages of the triggers are

* To Audit the table

* To check the complex conditions which are not possible by the constraints.

Explain normalization with examples?

Asked By: Beena | Asked On: Sep 19th, 2005

Answered by: Rakesh on: Feb 7th, 2012

Normalization--To Reduce or to remove the redundancy in table we used Normalization and the normalization we can achieve by breaking of tables by this table is in Normalized.

Answered by: prabu on: Jul 13th, 2011

0NF "sql CUSTOMER ORDER (CustName, OrderNo, ProdNo, ProdDesc, Qty, CustAddress, DateOrdered) 1NF - remove multivalued attributes "sql CUSTO...

What is a sub query and co-related sub query?

Asked By: jayababuhrd | Asked On: Jan 26th, 2012

Answered by: indira on: Feb 1st, 2012

Sub query: A sub query is executed once for the parent statement. Correlated Sub query: Correlated sub query runs once for each row selected by the outer query. It contains a reference to a value fr...

How to get number of days using SQL ?

Asked By: sateesh44 | Asked On: Nov 24th, 2011

I have table .. Like empno ename dob ------------------------------------ 1 you 1-jan-2011 2 me 28-jan-2011 tel me the query for no.Of days different from.. Two dates of column dob.? thanks in advance. sateesh

Answered by: kirathakudu on: Dec 13th, 2011

select date1-date2 from tablename;

If the above value is negative it means date 1 is older than date 2

How to insert a column in middle order?

Asked By: thoufik | Asked On: Oct 28th, 2011

Hi,i have 1 table contain 2 column if I add a new column it default stored in last column but my recruitment adding the new column in middle of the order so can any one explain

Answered by: preethi on: Dec 7th, 2011

Mr. Chandu can you give some example code. I am not clear with the code you have posted.

Answered by: Chandu on: Dec 6th, 2011

You can use BEFORE < col name >
or
AFTER < col name >
or
< col name > 2

Here 2 is the position that the new col is about to place.

How to retrieve the 2nd record from a table ?

Asked By: anabarai | Asked On: Jul 5th, 2011

How to retrieve the 2nd record from a table which has say 1000 records in it. Please answer this without using the row-id? I tried to say a query "select * from table where rowcount=1" which worked well and also "select * from table where rowcount=3" which gave me the result but not when I did the same...

Answered by: rima on: Nov 24th, 2011

Code
  1. SELECT * FROM `table_name` WHERE rownum <> 1 ORDER BY rownum  ASC LIMIT 1;

Answered by: smonroy on: Sep 27th, 2011

the best way to issue is

select top 1 * from cars where id_car in
(select top 2 id_car from cars order by id_car)
order by id_car DESC

and with that
obtain the second record (This way is for the PK)

Retrieve all columns except 1 in a table.

Asked By: Ahilan Carpenter | Asked On: Sep 28th, 2011

I have a table with 20 columns and I have to retrieve 19 except one. is there any way easier then giving select col1,col2,col3,..........Col19 from table1.

Answered by: bhanu on: Nov 23rd, 2011

if we are dong select top 33* from table, we will get topmost 33 records but not the all 33 columns except 1 column in a table.

Answered by: dilbagh on: Oct 9th, 2011

select top 19 * from table1

How to delete identical row from a table?

Asked By: rahulshukla_01 | Asked On: Oct 12th, 2011

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

Answered by: Ashok Aaruu on: Oct 22nd, 2011

its not clear...... it will be execute correctly?

Answered by: Siraj on: Oct 15th, 2011

delete from table_name where rowid not in (SELECT max(rowid) FROM table_name group by (column_name))

What is difference between co-related sub query and nested sub query?

Asked By: Chirag | Asked On: Oct 7th, 2005

Co-related sub query is one in which inner query is evaluated only once and from that result outer query is evaluated.Nested query is one in which inner query is evaluated for multiple times for gatting one row of that outer query.Ex. Query used with in() clause is co-related query. Query used with...

Answered by: V.CHIRU on: Oct 13th, 2011

If the evaluation of an inner query(a sub query) depends on a variable which gets its value in an outer query,such a subquery has to be evaluated for each value of the variable.Such a query is a corre...

Answered by: rangunagaraj on: Jul 5th, 2007

If the evaluation of an inner querry(a sub querry) depends on a variable which gets its value in an outer querry,such a subquerry has to be evaluated for each value of the variable.Such a querry is a ...

How many tables are needed to create and their relationships ?

Asked By: sharath | Asked On: Aug 1st, 2011

Supplier name supplier site supplier street supplier contact person supplier city supplier state supplier phone number business requirement is : one supplier can have multiple ( n number of sites ), one supplier site can have multiple contacts persons ( n number of contact persons ), one supplier contact...

Answered by: ashadevi21 on: Oct 5th, 2011

Supplier Table Columns : Supplier id , supplier name (Supplier ID, supplier name as Primary Key) Supplier Site Details Columns : Supplier name , site, street, city, state (Supplier name as foreign Ke...

Inner join and outer join

Asked By: priyadarshini | Asked On: Sep 29th, 2011

What is the difference between inner join and outer join?

Answered by: Mark Haynes on: Sep 30th, 2011

First to define what Inner Join and Outer Join mean. Inner Join: A join between two tables were only those records in both tables that meet the conditions of the join expression are returned. In most...

Is it possible to create user without using (create user) command ?

Asked By: dineshkrishnamca | Asked On: May 9th, 2011

Answered by: smonroy on: Sep 27th, 2011

If you want is not created a user with command line, you should create it with the interface

Answered by: Amit on: Sep 6th, 2011

Use Grant command to create user and giving permission.

Employee query question

Asked By: Shashipal Reddy | Asked On: May 9th, 2011

Suppose im having employee table with fields, eno, ename, dept, address1, address2, address3. In address field employee can fill only address1 or address2 or address3... At a time he can fill three address fields. Now I want all employee names who filled only one address field..

Answered by: smonroy on: Sep 27th, 2011

SELECT ename FROM employee WHERE (address1<>'' OR address2<>'' OR address3<>'' )

Answered by: ashok2909 on: Aug 1st, 2011

As far as i know use concatenation operator...

Code
  1. SELECT ename AS name,address1 || ',' ||address2||','||address3 AS address FROM employee

Having clause

Asked By: madhulatha_madduri | Asked On: Feb 1st, 2010

Given a select statement that has a group by clause.The having clause uses the same syntax as which other clause?A. Whereb. Unionc. Subqueryd. Order by

Answered by: smonroy on: Sep 28th, 2011

It's like WHERE clause but "HAVING" It is more commun to use with GROUP BY

Answered by: Ksudhir_Kumar on: Sep 5th, 2010

Having clause is used filter data which is retrieve by group by clause.for example: if you try to use having to filter data instead of where clause then it fires error.the style of query is:- 1...

First | Prev | | Next | Last Page

 

 

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Ads

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.