Monday, October 22, 2012

Important SQL Interview Questions



Important SQL Interview Questions

To get nth highest salary

SELECT SALARY FROM(SELECT SALARY ,DENSE_RANK() OVER(ORDER BY SALARY desc) AS R FROM EMP) WHERE R=2;



To Get duplicate Rows

SELECT COLUMN NAMES FROM TABLE NAME GROUP BY COUMNS NAME HAVING COUNT (*)>1


To Delete Duplicate Rows

DELETE FROM TABLE WHERE ROWID NOT IN (SELECT MAX(ROWID) FROM TABLE GROUP BY COLUMN NAME

Top 5 having highest salary first.
Select ename, sal from( select * from emp order by sal desc) where rownum<=5



Bottom 5 .
SELECT COLUMNNAME FROM (SELECT COLUMN NAME FROM TABLENAME ORDER BY SAL) WHERE ROWNUM<=-5

Max Sal in their Respective Dept No
SELECT COLUMNS NAME, MAX(SAL) OVER (PARTITION BY DEPTNO) AS M_SAL FROM EMP


Display in first row
SELECT COLUMN NAME FROM TABLE NAME ORDER BY DECODE (COLUMN NAME, 'WHAT U NEED IN FIRST ROW','1','2'),COLUMN NAME


Display rows which is multiple of 3
SELECT * FROM (SELECT ROWNUM R, ENAME FROM TABLENAME) WHERE MOD(R,3)=0

Get 3 max sal
select distinct sal from emp a where 3 >= (select count(distinct sal) from emp b where a.sal <= b.sal) order by a.sal desc;


Get 3 min Sal
select distinct sal from emp a where 3 >= (select count(distinct sal) from emp b where a.sal >= b.sal);



1 comment:

  1. Hello Nilima,

    Great info! I recently came across your blog and have been reading along.
    I thought I would leave my first comment. I don’t know what to say except that I have

    When using SSDT for VS2015 (17.2), I'm unable to preview any OLE DB data sources that use the Oracle OLE DB provider installed as part of the Oracle client.
    I've tested this on a few different machines, on both Windows Server 2012 R2, as well as Windows 10 x64 1703.
    To recreate it, install SSDT for VS2015 (17.2) on a system, as well as the Oracle client 32-bit. Create a new SSIS package, and add a new OLE DB connection, using the Oracle Provider for OLE DB. Then create a new data flow task in a package and add an OLE DB source. Use the Oracle connection, enter a SQL query, and then try to preview the results. The following error occurs:
    The system cannot find message text for message number 0x80040e51 in the message file for OraOLEDB. (OraOLEDB)
    Even though the error occurs, you can still get the column meta data and connect the OLE DB source to other components. As well, the package will run successfully both from the SSIS designer/debugger, as well as when deployed to an SSIS server.
    On one of my systems that was showing the issue, I also installed the original VS2010 BIDS, as well SSDT-BI for VS2012. In both 2010 and 2012, the designer was able to successfully preview the results from my OLE DB source.

    Great effort, I wish I saw it earlier. Would have saved my day.

    Thank you,
    Kevin

    ReplyDelete