Require a query with all clauses

Hi All,
I have been asked for a query to write by using all sql clauses like group by, where, having, order by in a single query. I just wrote the below query but I did not get out put. Please do correct where I am missing.
select deptno, max(sal) from emp group by deptno having  deptno > ( select distinct deptno from emp where deptno >= 20)
Thanks for your time and info.

1007912 wrote:
select deptno, max(sal) from emp group by deptno having  deptno > ( select distinct deptno from emp where deptno >= 20)
Logically it was wrong; as your sub-query will return more then one record for comparison. By the way if you are querying the same data from same table then why you need a sub query in having clause! No need of it. Even if you are not using any aggregate predicates then use it with where clause.
This is how your query and output should come out!
SQL> select
  2   deptno, max(sal)
  3  from emp
  4  where deptno >= 20
  5  group by deptno
  6  order by 1
  7  /
    DEPTNO   MAX(SAL)
        20       3000
        30       2850
SQL>

Similar Messages

  • Outer join on query with OR clause

    hi all, i am having problem outerjoining a query with or clause
    here is my data
    WITH table1 AS
    SELECT  'test' txt1, 'pak' txt2, 'ced' txt3, 'su' txt4 FROM dual UNION ALL
    SELECT  null txt1, 'pak' txt2, 'ced2' txt3, 'su2' txt4 FROM dual UNION ALL
    SELECT  null txt1, NULL txt2, 'ced3' txt3, 'su3' txt4 FROM dual UNION ALL
    SELECT  null txt1, NULL txt2, null txt3, 'su3' txt4 FROM dual UNION ALL
    SELECT  'text5' txt1, NULL txt2, null txt3, 'su3' txt4 FROM dual UNION ALL
    SELECT  null txt1, NULL txt2, null txt3, null txt4 FROM dual
    ,table2 AS
    SELECT 111 pid, 'test' txt1, 'pak4' txt2, 'ced' txt3, 'su' txt4 FROM dual UNION ALL
    SELECT 222 pid, 'test1' txt1, 'pak' txt2, 'ced2' txt3, 'su2' txt4 FROM dual UNION ALL
    SELECT 333 pid, 'test2' txt1, 'pak3' txt2, 'ced3' txt3, 'su4' txt4 FROM dual UNION ALL
      SELECT 444 pid, 'test2' txt1, 'pak3' txt2, 'ced4' txt3, 'su3' txt4 FROM dual
    SELECT b.pid, a.*
    from table1 a, table2 b
    WHERE (a.txt1 = b.txt1 OR
           a.txt1 IS NULL AND a.txt2=b.txt2 OR
           Nvl(a.txt2, a.txt1) IS NULL AND a.txt3 = b.txt3 OR
           Nvl(a.txt2, a.txt1) IS NULL  AND a.txt3 IS NULL AND a.txt4 = b.txt4
           ) as you can see i am joining table1 and table 2. i am joining with txt1, if txt1 is null then join by txt2, if null then join by txt3 and so on.
    the code above product this output
    PID     TXT1     TXT2     TXT3     TXT4
    ===     ====     ===   ==== ====
    111     test     pak      ced     su
    222             pak      ced2     su2
    333                     ced3     su3
    444                          su3this output is partially correct. only 4 rows were display and two was left out
    SELECT  'text5' txt1, NULL txt2, null txt3, 'su3' txt4 FROM dual UNION ALL
    SELECT  null txt1, NULL txt2, null txt3, null txt4 FROM dual i tried using outer join but oracle complain that i cannot use outerjoin with OR clause.
    can someone help modify my query to display the output below ?
    PID     TXT1     TXT2     TXT3     TXT4
    ===    ====      ===   ====  ====
    111     test     pak      ced     su
    222             pak      ced2     su2
    333                     ced3     su3
    444                          su3
    NULL  NULL   NULL    NULL   NULL
         test5

    Not sure you can do it with the Oracle style outer joins, but wioth ANSI style joins it is simple, actually exactly as you had it.
    SQL> set null null;
    SQL> WITH table1 AS (
      2   SELECT  'test' txt1, 'pak' txt2, 'ced' txt3, 'su' txt4 FROM dual UNION ALL
      3   SELECT  null txt1, 'pak' txt2, 'ced2' txt3, 'su2' txt4 FROM dual UNION ALL
      4   SELECT  null txt1, NULL txt2, 'ced3' txt3, 'su3' txt4 FROM dual UNION ALL
      5   SELECT  null txt1, NULL txt2, null txt3, 'su3' txt4 FROM dual UNION ALL
      6   SELECT  'text5' txt1, NULL txt2, null txt3, 'su3' txt4 FROM dual UNION ALL
      7   SELECT  null txt1, NULL txt2, null txt3, null txt4 FROM dual),
      8  table2 AS (
      9   SELECT 111 pid, 'test' txt1, 'pak4' txt2, 'ced' txt3, 'su' txt4 FROM dual UNION ALL
    10   SELECT 222 pid, 'test1' txt1, 'pak' txt2, 'ced2' txt3, 'su2' txt4 FROM dual UNION ALL
    11   SELECT 333 pid, 'test2' txt1, 'pak3' txt2, 'ced3' txt3, 'su4' txt4 FROM dual UNION ALL
    12   SELECT 444 pid, 'test2' txt1, 'pak3' txt2, 'ced4' txt3, 'su3' txt4 FROM dual)
    13  SELECT b.pid, a.*
    14  from table1 a
    15     LEFT JOIN table2 b
    16        ON (a.txt1 = b.txt1 OR
    17            a.txt1 IS NULL AND a.txt2=b.txt2 OR
    18            Nvl(a.txt2, a.txt1) IS NULL AND a.txt3 = b.txt3 OR
    19            Nvl(a.txt2, a.txt1) IS NULL  AND a.txt3 IS NULL AND a.txt4 = b.txt4);
           PID TXT1   TXT2   TXT3   TXT4
           111 test   pak    ced    su
           222 null   pak    ced2   su2
           333 null   null   ced3   su3
           444 null   null   null   su3
    null       text5  null   null   su3
    null       null   null   null   nullJohn

  • Hierarchical query with where clause

    Hi,
    How can I query hierarchically a query with WHERE clause? I have a table with three fields session_id,id and root_id.
    When I try with the following query,
    select id, level from relation
    where session_id = 79977
    connect by prior id = root_id start with id = 5042;
    It gets duplicate values.
    I want the query to show in the hierarchical manner with a filter condition using WHERE clause. Please help me how can I achieve this. If you know any link that describes more about this, please send it.
    Thanks in Advance.
    Regards,
    -Parmy

    Hi Sridhar Murthy an others,
    Thanks a lot for your/the answer. It's working for me. It saved a lot of other work around without the proper knowledge of hierarchical query. Please send me any link that describes these issues in detail and also I hope as I have mentioned in the other message, same cannot be achieved on views or ( on two different tables ???)
    Any way thanks for your reply,
    It's working for me.
    With happiness,
    -Parmy

  • Hosting company does not support SQL query with OUTFILE clause

    From my mysql database, I want to allow the user to run a query and produce a csv / text file of our membership database.   Unfortunately,  I just found out my hosting company does not support the SQL query with OUTFILE clause for MySQL database.
    Are there any other options available to produce a file besides me running the query in phpadmin and making the file available to users.
    Thanks.  George

    Maybe this external Export Mysql data to CSV - PHP tutorial will be of help
    Cheers,
    Günter

  • Tunning the Query with Distinct Clause

    Hi All,
    I have the below query that returns 28113657 records
    select src_Wc_id, osp_id, src_osp_id
    from osp_complements o1
    where exists (select 1 from wc_crossref wc
                        where wc.src_wc_id = o1.SRC_WC_ID
                        and wc.state = 'CA')
    This query executes within a second...
    But when i include a DISTINCT clause in the select statement, it takes more time ... (more than 20 mins)
    I am trying to get it tunned. Please advice me with your knowledge to get it done
    Thanks for your time
    Kannan.

    Retrieving distinct rows requires a sort of all returned rows. 20 - 3 = ~17 mins for sorting 28 mln rows looks too much. You need to tune your instance in order to speed up sort operation. The amount of memory dedicated to sorts is controlled by PGA_AGGREGATE_TARGET parameter. If it's set to 0 (not recommended) then SORT_AREA_SIZE is used. The process of PGA tuning is quite complex and described in PGA Memory Management chapter of Performance Tuning Guide.
    There is a workaround which allows to bypass sort operation, but it requires proper index and proper access by that index. The idea is that rows rertrieved via index are automatically ordered by indexed columns. If that and only that columns (possibly - in the same order as in the index, I don't know) are selected using DISTINCT then sort is not actually performed. Rows are already sorted due to access via index.
    Hope this will help you.
    Regards,
    Dima

  • Issue in select query with where clause

    Hi guys,
    I'm facing an issue while using select query with the where clause. When I'm selecting all the data from the table it returns the correct result. But when I'm using the where clause to get the specific rows from the table it returns no rows. But the data I'm trying to fetch using the where condition exists in the table.
    Here is my query which causing the issue,
    select * from mytable where myfield = 'myvalue'
    But if I use the following query it returns the result correctly.
    select * from mytable
    Also the myfield value 'myvalue' exists in the table.
    I have tried by running this query in both SQL Developer and SQL Plus. I have tried this query in mssql as well. It works perfectly and returns correct result sets for both the queries I have mentioned above. I'm unable to predict the issue as I'm new to ORACLE. Please help.
    Thanks,
    Ram.

    Hi Ram,
    I experienced an issue similar to this with a varchar2 field. Some of our records had a hidden newline character at the end of them, which was making queries like the one below fail:
    select * from employees
    where email = '[email protected]'The best way I found to detect this was to use
    select 'XX'||email||'XX' from employeesTo make sure that there were no newlines. But that is just a guess. If you could provide some example table data and the outputs of your selects, it would be helpful.
    Jeff

  • Select query with UNION clause in database adapter

    Friends,
    I have got a SQL query with two UNION clause withing it. like
    select a,b,c
    from a
    union
    select a,b,c
    from b
    The schema generated is like below in sequence
    <element>a</element>
    <element>b</element>
    <element>c</element>
    <element>a</element>
    <element>b</element>
    <element>c</element>
    So, the columns from different select queries joined with UNION clause are all appeared in schema instead of the distinct columns.
    Is there any way around to solve this issue ? or will need to with DB function/procedure.

    I think I know what you are saying but your example doesn't make sense, your SQL should produce something like
    I had to change a, b, c with elementA, elementB, elementC as a and b are reserved html tags.
    <elementA>DateA</elementA>
    <elementB>DataB</elementB>
    <elementC>DataC</elementC>
    ...What is the result of the query when you run it in SQLPlus? Is it what you expect?
    cheers
    James

  • Populate list by executing query with where clause

    Hi,
    I m populating my list from database table using following command.
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("learning", new HashMap());
    EntityManager em = emf.createEntityManager();
    String query = "select d from Dept d";
    List list = em.createQuery(query).getResultList();
    My list is getting populated with all of rows in my database table(dept). The problem is, I cant use where clause in my query string.
    String query = "select d from Dept d where d.dept_name='ANYNAME'";
    Moreover I cant select even indivisual columns by using following line
    String query = "select d.dept_code,d.dept_name from Dept d";
    It looks like that my query is getting all rows from table as Object.
    What to do to
    1. put some where clause
    2. get some particular colums instead of all columns
    3. apply some table joins in query
    plz help.

    Hi Alex,
    I misunderstood your question. You had mentioned "My list is getting populated with all of rows in my database table(dept). The problem is, I cant use where clause in my query string. "
    I thought you might not have permissions to modify the code in the class, and that's why I suggested that you could write new classes.
    1) write new classes,? means if I need just two columns from a table >(instead of all columns), do I need to write new class with just two >columns (getters and setters)?You can write a SQL statement anyway you want , there are no restrictions in JDBC , so if you need just 2 columns you can write a custom query for that.
    "select column1 as a, column2 as be from table_name where column3 = xyz" etc.
    2) Is not anyway to write SQL statement like
    select dept_code,dept_name from dept where dept_name='anything'The above SQL is correct, it should work.
    3) Stored procedures? can I get some example(code) to get an idea >how to implement complex queries with where clause and joinings >using stored procedures?Stored procedures are specific to the database you are using, Oracle has a particular syntax for store procdures.
    You can call stored procedures from JDBC also, search on Google for
    "Calling stored procedures from JDBC"
    4) I m using hibernate. can I get any example of using hibernate own >query?Tutorial on Hibernate Query Language:
    http://www.hibernate.org/hib_docs/v3/reference/en/html/queryhql.html

  • Query with 'unique' clause

    Hi folks,
    A simple question (8.0.5 on WinNT):
    Is there any way to speed up the execution time for
    a query with a 'unique' clause? I have a table with 1.6 million
    records, and I want to get unique values for a
    particular column which is not the primary key.
    The result of the query -
    select unique(col1) from table1;
    typically returns between 1 to 100 unique values for col1.
    As such, this query takes upto 18 secs on my test machine in
    SQL Window itself (for 1.6 million records in the table). Any
    ways to speed it up?
    Thanks,
    Cheers,
    Sanchayan

    Having an index on the column should speed it up, but only
    where the column is defined as NOT NULL I think. If you
    can't put a NOT NULL constraint on the column consider
    whether you need to know about NULL values.
    If the index was of the bitmap type that would also speed it
    up further, probably to take the query time down to one or
    two seconds, but they come with a big list of warnings.
    Actually, I can't remember now if they're available in your
    version.

  • For Update Query with Wait Clause from ORACLE Forms

    Hi
    We are using Oracle Forms 10g running with 10g database 10.2.0.1.0. We happend to see a query which is getting generated in AWR report like Select rowid, all_columns from TableName for Update of C1 Nowait. But no such query is really written in forms and we are aware that, Any query prefixed with rowid is definitely executing from Forms. But how the ForUpdate and Nowait clause is appended to the query.
    We have checked the following properties in the database Block
    *1) Locking Mode is set to Automatic*
    *2) Update Changed Columns only is set to YES*
    *3) Query all records is set to No (Though this particular property may not be relevant to the issue)*
    What is the property or setting which might trigger such a query from ORACLE Forms with ForUpdate and Nowait clause.
    Any ideas/suggestions on why such behaviour. Please have a healthy discussion on this. Thanks in advance.

    Why have you started another thread on the same thing?
    FOR UPDATE cursor is causing Blocking/ Dead Locking issues
    Just use one thread to avoid confusion.
    The fact that nobody has answered on the other thread for a couple of days could be to do with it being the weekend and most people are not at work.
    Either be patient or, if it's more "urgent" than that, raise a SR/Tar with Oracle metalink.

  • Parameters in query with in clause ?

    Hi all,
    I use Oracle ADF Business components to query my database.
    I run the query below with parameters
    SELECT *
    FROM MyTABLE
    WHERE columName IN (:nameOfParameters)
    where nameOfParameter = 'Value1','Value2','ValueN'
    But the query doesn't work. How can I solve this problem ?
    Tkanks in advance for your help,
    Carole

    SELECT  *
      FROM  MyTABLE
      WHERE ',' || :nameOfParameters || ',%' like '%,''' || columName || ''',%'
    /SY.

  • Copy query with all language-dependend settings

    Dear all,
    this is my first post message in SDN.
    I want to copy a query ("query_a" => "query_b") without loosing my translations of the key-figures, structures and cell definitions.
    If I use the query designer, these texts will be only copied in my logon-language.
    If I use the transaction "rszc", all language-dependend texts will be copied - very good - but I have no influence on the technical name (the new name would be "query_a_1", but I want to have "query_b"!).
    We use BW 3.5
    Best regards,
    Christoph Riesel

    Dear Venky V,
    thank you very much for your quick reply.
    If I use "save as..." in the query designer,
    the language-dependend texts especially the cell definitions will only be saved in the language that I'm logged on; but I don't want to loose my translations.
    Regards,
    Christoph Riesel

  • Help for a query with minus clause

    I want to write a query like this:
    select a.field
    from table_1 a
    minus
    select b.field
    from table_2 b
    where not exists (select 1
    from table_3 c
    where c.field = a.field);
    Unfortunately I get:
    ERROR at line 8:
    ORA-00904: invalid column name
    Can you help me?
    Thanks!

    Hi,
    Mark1970 wrote:
    I want to write a query like this:
    select a.field
    from table_1 a
    minus
    select b.field
    from table_2 b
    where not exists (select 1
    from table_3 c
    where c.field = a.field);
    Unfortunately I get:
    ERROR at line 8:
    ORA-00904: invalid column name
    Can you help me?
    Thanks!Table_1 and its alias, a, are only in scope in the first branch of the query, before the keyword MINUS. The second branch, after MINUS, must be a complete self-contained query.
    There are many ways to re-write the query, including:
    select     a.field
    from      table_1          a1
        minus
    select     b.field
    from     table_2          b
    where      not exists      (
                           select     1
                       from      table_3     c
                       JOIN     table_1     a     ON     c.field = a.field
                   );It's suspicious that the EXISTS sub-query does not depend on anything in table_2. Are you sure this is what you want to do?
    If you'd like help, post a little sample data (CREATE TABLE and INSERT statements for all three tables) and the results you want from that data.

  • Tunning query with LIKE clause

    Hi, is there any chance to improve execution plan for SQL query which is using LIKE clause?
    Query:
    SELECT * FROM [TABLE_NAME] WHERE ADDRESS LIKE :1 ESCAPE '\';
    | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
    | 0 | SELECT STATEMENT | | 112K| 18M| 11361 (1)| 00:02:17 |
    |* 1 | TABLE ACCESS FULL| [TABLE_NAME] | 112K| 18M| 11361 (1)| 00:02:17 |
    Execution plan is far from ideal. Table has several millions of records.
    This query is used by application to seach using patterns...
    Any ideas?
    Thx in advance!

    This example isn't entirely realistic. Your table T has only 1 column which is also indexed. Apparently, for small enough tables of one column it will search the entire index for the wildcard value. But if I add a second column, or base the single column version on a larger table, the optimizer uses a full table scan:
    SQL> drop   table t;
    Table dropped.
    SQL> CREATE TABLE t AS SELECT DBMS_RANDOM.STRING('a',100) a
      2                          ,DBMS_RANDOM.STRING('a',100) b
      3                    FROM user_objects;
    Table created.
    SQL>
    SQL> CREATE INDEX t_idx ON t (a) COMPUTE STATISTICS;
    Index created.
    SQL>
    SQL> SET autotrace traceonly explain
    SQL>
    SQL> SELECT *
      2  FROM t
      3  WHERE a LIKE '%acb%';
    Execution Plan
       0      SELECT STATEMENT Optimizer=CHOOSE (Cost=2 Card=1 Bytes=100)
       1    0   TABLE ACCESS (FULL) OF 'T' (Cost=2 Card=1 Bytes=100)
    SQL> drop   table t;
    Table dropped.
    SQL> CREATE TABLE t AS SELECT DBMS_RANDOM.STRING('a',100) a
      2                       --   ,DBMS_RANDOM.STRING('a',100) b
      3                    FROM all_objects;
    Table created.
    SQL>
    SQL> CREATE INDEX t_idx ON t (a) COMPUTE STATISTICS;
    Index created.
    SQL>
    SQL> SET autotrace traceonly explain
    SQL>
    SQL> SELECT *
      2  FROM t
      3  WHERE a LIKE '%acb%';
    Execution Plan
       0      SELECT STATEMENT Optimizer=CHOOSE (Cost=20 Card=399 Bytes=39900)
       1    0   TABLE ACCESS (FULL) OF 'T' (Cost=20 Card=399 Bytes=39900)

  • Query With BETWEEN Clause Slows Down

    hi,
    I am experiencing slow down query by using BETWEEN clause. Is there any solution for it?

    Here is the difference if I use equal not between.
    SQL> select to_char(sysdate,'MM-DD-YYYY HH24:MI:SS') from dual;
    TO_CHAR(SYSDATE,'MM
    11-14-2005 15:44:03
    SQL> SELECT COUNT(*) /*+ USE_NL(al2), USE_NL(al3), USE_NL(al4),
    2 USE_NL(al5), USE_NL(al6) */
    3 FROM acct.TRANSACTION al1,
    4 acct.account_balance_history al2,
    5 acct.ACCOUNT al3,
    6 acct.journal al4,
    7 acct.TIME al5,
    8 acct.object_code al6
    9 WHERE ( al1.reference_num = al4.reference_num(+)
    10 AND al1.timekey = al5.timekey
    11 AND al5.timekey = al2.timekey
    12 AND al3.surrogate_acct_key = al2.surrogate_acct_key
    13 AND al3.surrogate_acct_key = al1.surrogate_acct_key
    14 AND al1.report_fy = al3.rpt_fy
    15 AND al6.object_code = al1.object_adj
    16 )
    17 AND ((al1.timekey = 20040701
    18 or al1.timekey = 20040801
    19 or al1.timekey = 20040901
    20 or al1.timekey = 20041001
    21 or al1.timekey = 20041101
    22 or al1.timekey = 20041201
    23 or al1.timekey = 20050101
    24 or al1.timekey = 20050201
    25 or al1.timekey = 20050301
    26 or al1.timekey = 20050401
    27 or al1.timekey = 20050501
    28 or al1.timekey = 20050601
    29 or al1.timekey = 20050701
    30 or al1.timekey = 20050801
    31 or al1.timekey = 20050901)
    32 AND al3.dept = '480');
    COUNT(*)/*+USE_NL(AL2),USE_NL(AL3),USE_NL(AL4),USE_NL(AL5),USE_NL(AL6)*/
    34245
    SQL> select to_char(sysdate,'MM-DD-YYYY HH24:MI:SS') from dual;
    TO_CHAR(SYSDATE,'MM
    11-14-2005 15:44:24

Maybe you are looking for

  • Multiple foreign keys to a single table

    Hi, I need to write an SQL sentence to bring a unique row formed from multiple foreign keys which are dependent on the same table. The two tables as follow: CREATE TABLE UNIDADMEDIDA ( IDUNIDADMEDIDA NUMERIC(3) NOT NULL, DESCRIPCION VARCHAR2(128) NOT

  • Smart Form - operator not followed by value

    Hi all, in smart form --> Global definition --> initialization i have written this code.... select belnr gjahr bukrs blart bldat budat waers into corresponding fields of table tbkpf from bkpf where bukrs = ubukrs and gjahr = ugjahr and budat in ubuda

  • Matching an "Edit Text" in a form with a Field in a Table

    Hi Every Body, I've succeed to add a new "Edit Text" in the invoice Form (133) with c# : oItem = oInvoiceForm.Items.Item("103");                     oNewItem = oInvoiceForm.Items.Add("EditText1", SAPbouiCOM.BoFormItemTypes.it_EDIT);                  

  • Error: The file "iTunes Library.itl" or "iTunes LIbrary.xml" cannot be.....

    I'm trying to recover my playlist I spent a week on sorting music and after updating to the latest iTunes update it all vanished and all my music and playlist was gone!!!! ***????? Anyhow I read a couple of solutions on here to try to fix the problem

  • Tree Issue--Data out of order?

    All, I suspect this is more of a generic SQL question, but, anyhow, I'm trying to create a tree report of the items in our bills of materials. When I run the page, the tree is empty (not even a root). Trying to figure out what the problem is, I've co