Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 39871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:55:59+00:00 2026-05-10T14:55:59+00:00

Consider the Oracle emp table. I’d like to get the employees with the top

  • 0

Consider the Oracle emp table. I’d like to get the employees with the top salary with department = 20 and job = clerk. Also assume that there is no ’empno’ column, and that the primary key involves a number of columns. You can do this with:

select * from scott.emp where deptno = 20 and job = 'CLERK' and sal =  (select max(sal) from scott.emp             where deptno = 20 and job = 'CLERK') 

This works, but I have to duplicate the test deptno = 20 and job = ‘CLERK’, which I would like to avoid. Is there a more elegant way to write this, maybe using a group by? BTW, if this matters, I am using Oracle.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T14:56:00+00:00Added an answer on May 10, 2026 at 2:56 pm

    The following is slightly over-engineered, but is a good SQL pattern for ‘top x’ queries.

    SELECT   *  FROM   scott.emp WHERE   (deptno,job,sal) IN  (SELECT     deptno,    job,    max(sal)    FROM     scott.emp   WHERE     deptno = 20     and job = 'CLERK'   GROUP BY     deptno,    job   ) 

    Also note that this will work in Oracle and Postgress (i think) but not MS SQL. For something similar in MS SQL see question SQL Query to get latest price

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider a table with 3 columns: ID (unique, taken from an Oracle sequence), CATEGORY
Please consider the following table structure on Oracle: create table DOCS ( DOC_NO NUMBER
Lets consider a simple table say products in Oracle (I tried on Oracle 9i).
I need to combine different row's values in table in oracle. For example, consider
Consider the following URL: http://www.iamsomeweiredurl.fromsomewhere/andIwant#thisfragment Is there a chance to get thisfragment by @RequestMapping
I am creating a new custom SQL folder in Oracle discoverer administrator. Consider my
Consider the following (simplistic) situation: CREATE TABLE PARENT ( PARENT_ID INT PRIMARY KEY );
I need to copy some data from one table to another in Oracle, while
Consider the query (it runs on both Oracle and MySQL) UPDATE table1 SET something_id
Consider this code segment Class.forName (oracle.jdbc.OracleDriver); connection = DriverManager.getConnection (jdbc:oracle:thin:@//XXX.XXX.XXX.XXX:1521/xe, abc, def); DatabaseMetaData metaData=connection.getMetaData();

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.