[Solved] PL\\SQL select first row from column 1 where column 2 is unique
Looking to automatically optimize YOUR SQL query? Start for free.

EverSQL Database Performance Knowledge Base

PL\\SQL select first row from column 1 where column 2 is unique

Database type:

I just started learning SQL. I'm using an Oracle DB.

Table emp with column empno, ename, job, mgr, hiredate, sal, comm, deptno.

I want to get the first record from ename column for each deptno and ename have to be sort by asc.

The following SQL works:

Select ename
from (Select d.ename
From emp d
Where d.deptno = 10
Order by ename asc)
where rownum <= 1
Union
Select ename
from (Select d.ename
From emp d
Where d.deptno = 20
Order by ename asc)
where rownum <= 1
Union
Select ename
from (Select d.ename
From emp d
Where d.deptno = 30
Order by ename asc)
where rownum <= 1
Union
Select ename
from (Select d.ename
From emp d
Where d.deptno = 40
Order by ename asc)
where rownum <= 1

I want to optimise it so I dont need to rewrite it if I add some record to deptno column.

I tried with SELECT distinct like

SELECT ename FROM emp WHERE (SELECT distinct deptno FROM emp)

How to optimize this SQL query?

The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Avoid Subqueries (query line: 4): We advise against using subqueries as they are not optimized well by the optimizer. Therefore, it's recommended to join a newly created temporary table that holds the data, which also includes the relevant search index.
  2. Avoid Subqueries (query line: 18): We advise against using subqueries as they are not optimized well by the optimizer. Therefore, it's recommended to join a newly created temporary table that holds the data, which also includes the relevant search index.
  3. Avoid Subqueries (query line: 32): We advise against using subqueries as they are not optimized well by the optimizer. Therefore, it's recommended to join a newly created temporary table that holds the data, which also includes the relevant search index.
  4. Avoid Subqueries (query line: 46): We advise against using subqueries as they are not optimized well by the optimizer. Therefore, it's recommended to join a newly created temporary table that holds the data, which also includes the relevant search index.
  5. Use UNION ALL instead of UNION (query line: 43): Always use UNION ALL unless you need to eliminate duplicate records. By using UNION ALL, you'll avoid the expensive distinct operation the database applies when using a UNION clause.
The optimized query:
SELECT
        ename 
    FROM
        (SELECT
            d.ename 
        FROM
            emp d 
        WHERE
            d.deptno = 10 
        ORDER BY
            d.ename ASC) 
    WHERE
        rownum <= 1 
    UNION
    SELECT
        ename 
    FROM
        (SELECT
            d.ename 
        FROM
            emp d 
        WHERE
            d.deptno = 20 
        ORDER BY
            ename ASC) 
    WHERE
        rownum <= 1 
    UNION
    SELECT
        ename 
    FROM
        (SELECT
            d.ename 
        FROM
            emp d 
        WHERE
            d.deptno = 30 
        ORDER BY
            ename ASC) 
    WHERE
        rownum <= 1 
    UNION
    SELECT
        ename 
    FROM
        (SELECT
            d.ename 
        FROM
            emp d 
        WHERE
            d.deptno = 40 
        ORDER BY
            ename ASC) 
    WHERE
        rownum <= 1

Related Articles



* original question posted on StackOverflow here.