GeekInterview.com
Series: Subject: Topic:

SQL Interview Questions

Showing Questions 1 - 20 of 204 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

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

How to rename or change table name in SQL?

Asked By: shojol | Asked On: Mar 30th, 2012

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

SQL >> rename table_name1 to new_table_name;

Answered by: ravinder on: May 4th, 2012

alter table table_name_old rename to table_name_new;

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);

     

Get numeric values only using SQL ?

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

I have table with one column. Type varchar.. phone --------------- 99samn44423m44 8034skdf3453a2 909090abc4567d i want to get numeric values.. I mean my output will be phone --------------- 994442344 80343453a2 9090904567 thanks in advance.. sateesh

Answered by: naga on: Mar 12th, 2012

Code
  1. SELECT REPLACE(TRANSLATE(LOWER(99sAmn44423mz44),abcdefghijklmnopqrstuvwxyz, ), ,) FROM dual

Answered by: sriram on: Feb 24th, 2012

Code
  1. SELECT columnname FROM tablename WHERE regexp_like(columnname,^[[:digit:]]+$) ;

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...

State true or false. !=, <>, ^= all denote the same operation.

Asked By: Interview Candidate | Asked On: Mar 6th, 2005

True

Answered by: Durgesh Kumar Singore on: Feb 7th, 2012

In SQL 2005 only != and <> operator perform same operation

^= does not work

Answered by: Aashu on: Feb 3rd, 2012

^= operator is a Bit wise Exclusive OR operater in MS SQL.
It stands into category of compound operators and performs a bitwise exclusive OR and sets the original value to the result.

Difference between varchar and varchar2?

Asked By: Sreyas | Asked On: Feb 23rd, 2006

Star Read Best Answer

Editorial / Best Answer

Answered by: muralidhar_batta

Member Since Feb-2006 | Answered On : Feb 26th, 2006

varchar means fixed length character data(size) ie., min size-1 and max-2000

where as varchar2 means variable length character data ie., min-1 to max-4000

Answered by: pooja manoja on: Feb 4th, 2012

Varchar - If we declare datatype as varchar then it will occupy space for null values. It can store upto 2000 bytes of character.

Varchar2 - If we declare datatype as varchar2, it will not occupy any space. It can store 4000 bytes of character.

Thanx,

Answered by: gangi reddy on: Dec 21st, 2011

Difference b/w varchar and varchar2 is varchar is ANSI standard and varchar2 is a Oracle data type
and also varchar is fixed length record, varchar2 is variable length.

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...

What is difference between DBMS and RDBMS?

Asked By: krishnamoorthy | Asked On: May 15th, 2006

Answered by: what is sql on: Jan 26th, 2012

SQL is command line argument. You can access data into database to write procedure.

Answered by: Ammu on: Oct 23rd, 2011

what are the 12 rules of CODD

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

What is the difference between SQL and SQL server?

Asked By: priyank singh pathania | Asked On: Sep 26th, 2005

Not known

Answered by: durgaprasad on: Oct 23rd, 2011

ORACLE organises the database of collection data and this data retrieve from sql .So sql is a language but sql-server is a product of Microsoft which support sql.

Answered by: sandeep on: Jul 13th, 2011

SQL is a language which is used to retrieve and manipulate data in database .... SQL Server is the relational data base management system, a product from Microsoft.

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".