Query with || operator in IN clause

Hi All,
I am having two table emp_demo,dept_demo , below are the structure of the table :
create table emp_demo(
empno number primary key,
ename varchar2(20),
deptno number,
dept_loc varchar2(20),
dname varchar2(20)
create table dept_demo(
deptno number,
dept_loc varchar2(20),
dname varchar2(20)
Query 1 :
select * from emp_demo e where e.deptno||e.dept_loc||e.dname IN ( select d.deptno||d.dept_loc||d.dname from dept_demo d)
Query 2 : 
select * from emp_demo e where e.deptno  IN ( select d.deptno from dept_demo d)
and e.dept_loc  IN ( select d.dept_loc from dept_demo d)
and e.dname  IN ( select d.dname from dept_demo d)
Could anyone please confirm that both the query will returns the same results in any condition.
Thanks,
Rajendra

I agree with Karthick.
Your query with || cannot be guaranteed to return the correct results, as it's possible that the concatenation of different strings could result in the same overall string
As an illustrative example...
if you have two strings... forename "FRED" and surname "SAMUEL BLOGGS" (very posh double barrelled surname)
The concatenating these together can give us "FRED SAMUEL BLOGGS".
Now what if the two strings we had were: forenames "FRED SAMUEL" and surname "BLOGGS"
concatenating those together will also give us "FRED SAMUEL BLOGGS"
So we had two lots of data, with different starting strings that resulted in the same overall string.
In terms of your query, you should ideally join the data as Karthick showed, but just so you know, if you're wanting to do an IN clause on multiple columns, you do not concatenate the data together (it get's even more messy if the datatypes are different), but you actually treat all the columns as a set of data.  You do that using syntax like this...
select *
from emp_demo e
where (e.deptno,e.dept_loc,e.dname) IN (select d.deptno,d.dept_loc,d.dname
                                        from   dept_demo d)
Your second query is also completely different from the first query as it removes the relationship between the individual columns.  So, just because the dept_no is in the dept_demo table, and the dept_loc is also in the dept_demo table and the dname is also in the dept_demo table, doesn't mean that they all exist together on the same row of the dept_demo table.  I very much doubt that is what you wanted.
SQL is set based.  That's a key thing to remember. 

Similar Messages

  • Query with date in where clause

    hi,
    i have build a view with join conditions from 8 tables. the data from this view is more then 100,000
    when i run the query with different clause its work with some seconds. but when i put date column in where cluase it sleeps.
    eg..
    where unit_id = 4 and t_id = 's09' and vb like '%amb%'
    works fine.
    where unit_id = 4 and t_id = 's09' and vb like '%amb%' and date between dt1 and dt2
    now sleep
    please ......give me some suggestions

    hi i have done the explain plan
    the result is
    Operation Object
    SELECT STATEMENT ()
    NESTED LOOPS ()
    NESTED LOOPS ()
    HASH JOIN ()
    HASH JOIN ()
    TABLE ACCESS (FULL) PR_PO_MST
    TABLE ACCESS (FULL) PR_SUPPLIER
    TABLE ACCESS (FULL) PR_PO_DTL
    TABLE ACCESS (BY INDEX ROWID) INDENT_MST
    INDEX (RANGE SCAN) ST_IND_MST_IDX
    TABLE ACCESS (BY INDEX ROWID) ST_ITEM
    Operation Object
    INDEX (UNIQUE SCAN) PK_ST_ITEM
    now wot do do????

  • Visual Web - How to 'setObject' for an SQL query with an IN  ? clause

    Hello, I am relatively new to visual web pack. I am stuck at writing the java code for passing parameters to the SQL query. In a normal scenario, where we need to check if some variable is present in a column (= ?), we would pass the parameter as :
    setObject(1, whatevervalue);
    How do we pass parameters if we need to check for more than one variable? The query editor in VWP has the option IN, which allows us to check for more than one match in a column. The IN clause is supposed to allow the format ('ex1', 'ex2' ...). I tried using an array as a parameter, like, setObject(1, somearray). It gives me a null exception. I even tried sending a variable in ('ex1', 'ex2' ...) format; doesn't like it either.
    Any help is greatly appreciated.

    Check the following docs:
    http://www.netbeans.org/kb/60/web/databoundcomponents.html
    http://www.netbeans.org/kb/60/web/inserts-updates-deletes.html
    See also:
    http://developers.sun.com/jscreator/reference/techart/2/query-editor.html

  • SQL QUERY How to write a sql query with a complex where clause.

    I would like to get a list of all my invoices from the past year plus any open invoices that are more than a year old.
    I don't want any overlapping rows.
    Debra has a question

    Debra,
    Sorry but you have posted to a forum that deals exclusively with questions/issues about customizing and programming Microsoft Project, a planning and scheduling application. I suggest you delete this post and find a more appropriate forum.
    John

  • Simple Select query with 'where', 'and', 'between' clauses takes time

    Hi,
    I have a select query as below
    SELECT * FROM (SELECT a.*,ROWNUM currentStartRecord From (select ai_inbound.ai_inb_seq tableseq,'AI_INBOUND' tablename,'INBOUND' direction,ai_inbound.appl,ai_inbound.ai_date datetime,ai_inbound.ic_receiver_id pg_id,ai_inbound.ic_sender_id tp_id,ai_inbound.session_no,ai_inbound.ic_ctl_no,ai_inbound.msg_set_id msg_type,ai_inbound.appl_msg_ctl_no reference_no,ai_inbound.fg_version version,ai_inbound.msg_status status,ai_inbound.input_file_name,ai_inbound.output_file_name,ai_inbound.ack_file_name from ai_inbound where ai_inbound.appl = ? and ai_inbound.ai_date between ? and ? )a where ROWNUM <= 49)where currentStartRecord >= 0
    The above query takes longer time through application than expected when the date fields are passed whereas it works fine when no date fields are passed. We are using oracle9.2 version of the database. All the indexed columns and partitioned indexed columns are rebuild.
    Kindly let me know how can i tune up the query to improve the performance.
    Thanks

    Hi,
    I have a select query as below
    SELECT * FROM (SELECT a.*,ROWNUM currentStartRecord From (select ai_inbound.ai_inb_seq tableseq,'AI_INBOUND' tablename,'INBOUND' direction,ai_inbound.appl,ai_inbound.ai_date datetime,ai_inbound.ic_receiver_id pg_id,ai_inbound.ic_sender_id tp_id,ai_inbound.session_no,ai_inbound.ic_ctl_no,ai_inbound.msg_set_id msg_type,ai_inbound.appl_msg_ctl_no reference_no,ai_inbound.fg_version version,ai_inbound.msg_status status,ai_inbound.input_file_name,ai_inbound.output_file_name,ai_inbound.ack_file_name from ai_inbound where ai_inbound.appl = ? and ai_inbound.ai_date between ? and ? )a where ROWNUM <= 49)where currentStartRecord >= 0
    The above query takes longer time through application than expected when the date fields are passed whereas it works fine when no date fields are passed. We are using oracle9.2 version of the database. All the indexed columns and partitioned indexed columns are rebuild.
    Kindly let me know how can i tune up the query to improve the performance.
    Thanks

  • Query with subquery containing group clause doesn't return any rows - WHY ?

    Hi,
    My query doesn't return any values :
    select g1.NTRX from gtrx g1
    where exists
    (SELECT b.cfunctrx, b.cpro1trx, b.nmsgitrx, b.nmrc, b.ncrd, b.namtstrx,
    b.dltimtrx, b.nrtrftrx,count(*)
    FROM gtrxacq a, gtrx b
    WHERE a.ntrx = b.ntrx AND a.acq_bus_date = (SELECT curr_bus_date -1
    FROM gmbr
    WHERE nmbr = 0)
    and g1.NTRX=b.NTRX
    GROUP BY b.cfunctrx,
    b.cpro1trx,
    b.nmsgitrx,
    b.nmrc,
    b.ncrd,
    b.namtstrx,
    b.dltimtrx,
    b.nrtrftrx
    HAVING COUNT (*) > 1);
    but such query returns some number of rows :
    SELECT b.cfunctrx, b.cpro1trx, b.nmsgitrx, b.nmrc, b.ncrd, b.namtstrx,
    b.dltimtrx, b.nrtrftrx,count(*)
    FROM gtrxacq a, gtrx b
    WHERE a.ntrx = b.ntrx AND a.acq_bus_date = (SELECT curr_bus_date -1
    FROM gmbr
    WHERE nmbr = 0)
    /*and g1.NTRX=b.NTRX*/
    GROUP BY b.cfunctrx,
    b.cpro1trx,
    b.nmsgitrx,
    b.nmrc,
    b.ncrd,
    b.namtstrx,
    b.dltimtrx,
    b.nrtrftrx
    HAVING COUNT (*) > 1
    AND when i put results from query above into query :
    select g1.NTRX from gtrx g1
    where
    g1.CFUNCTRX= 200 and g1.CPRO1TRX= 000 and g1.NMSGITRX= 1240 and
    g1.NMRC= '000000000000675' and g1.NCRD= 405671**********
    and g1.NAMTSTRX=14.26 and g1.DLTIMTRX=to_date('07/08/2008 15:07:02','MM/DD/YYYY HH24:MI:SS')
    and g1.NRTRFTRX= '000414598393';
    it returns values.
    what is wrong ?
    Best Regards Arkadiusz Masny

    but such query returns some number of rows :
    /*and g1.NTRX=b.NTRX*/Add b.NTRX into group by and recheck.

  • Query with 'different from' clause in Sqlite

    My question is very simple:
    How can I submit a query with a 'different from' clause into a table.
    In AIR, I would like to query :
    "select * from myTable where myField <> 'value' ";
    but it does'nt work !
    Nor
    "select * from myTable where myField != 'value' ";
    nor
    "select * from myTable where myField is not 'value' ";
    is it impossible to query with a 'different from' clause in Sqlite wih Adobe Air

    Hello,
    SQLite in AIR does support 'different from' and '<>' is the correct operator. Take the following emp table for example:
    id
    name
    age
    1
    A
    26
    2
    B
    30
    3
    C
    28
    If you want to query the records whose name isn't A. The following SQL is correct:
    select * from emp where name <> 'A'
    Thanks,
    Yang

  • A query with "insert..select" run with dqp?

    I would like to know if a query with "insert cluase select clause" is operated with the function of the dqp.
    I think that a query portion with "select clause" is possible to run with dqp.
    if anybody knows about this case, please let me know.
    Thanks in advance,
    Dokon Kim

    There is no such thing as pl/sql 5.1.4, at that time pl/sql didn't even exist.
    You also state it works in Sql*plus but it doesn't work in PL/SQL as PL/SQL is using OCI7. Which isn't true.
    Please stop playing games and withhelding information.
    Post the real info. Not what you think is important.
    Why are you using a (desupported) 9.0.1 client against a 10GR2 database?}
    Sybrand Bakker
    Senior Oracle DBA

  • Having with more than one clause

    Hi
    Is posssible to have a query with more than one clause in having condition
    Example In my query I have Count , Sum and AVG , I need to use 3 conditions in having, Is It possible ?
    Thank you in advance

    Hi,
    yes, in Having you can also use AND and OR.
    with x as (select 1 nr from dual)
    select nr
    from x
    group by nr
    having count(*) = 1
    and sum(nr) = 1Herald ten Dam
    http://htendam.wordpress.com

  • Query with order by clause

    Hi,
    I found a query with order by clause in procedure which is taking long time.
    Stats are upto date.
    Total Rows :650000.
    It is ordered by primary key column.
    select * from table_name order by col1;
    col1 is a primary key.No of cpu's used is 4.
    can anyone suggest me a better solution to improve the performance of a query.
    Is it better to use parallel hint for above scenario.
    Any help really apprecaited.
    Thanks in advance.

    Hi,
    Thanks for ur immediate reply.
    It doesn't have where clause.
    below is the plan
    SQL> select * from table(dbms_xplan.display);
    PLAN_TABLE_OUTPUT
    Plan hash value: 619071987
    | Id  | Operation                   | Name                 | Rows  | Bytes | Cos
    |   0 | SELECT STATEMENT            |                      |   671K|   255M| 125
    |   1 |  TABLE ACCESS BY INDEX ROWID| FULL_ITEM_FACILITIES |   671K|   255M| 125
    |   2 |   INDEX FULL SCAN           | FIF_PK               |   671K|       |
    9 rows selected
    Executed in 0.094 secondsThanks in advance
    Edited by: unique on Jun 22, 2009 8:26 AM

  • Abap MDM API query with OR operation

    Hi experts,
    not sure this is the proper section for the thread, anyway; dealing with ABAP MDM API, is there a way to build up a query which involves several clause in OR conjunction?
    This is the scenario: I've got to extract several items on the basis of exact product codes. Tipically, I've got huge vectors of hundreds of product codes; actually, I could easily retrieve data by looping over them and, for each code, perform a query on MDM via QUERY method building the clause as follows:
          ls_query-parameter_code  = 'MDMSRM_PART_NO'. "Field code
           ls_query-operator        = 'EQ'.   "Contains
           ls_query-dimension_type  = mdmif_search_dim_field.      "Fieldsearch
           ls_query-constraint_type = mdmif_search_constr_text.    "Text search 
           lv_text = wa_codes-product_code.
           GET REFERENCE OF lv_text INTO ls_query-value_low.
           APPEND ls_query TO lt_query.
    This will however result in hundreds of query in the LOOP itself.
    Is there any way to build a query with OR operators ?
    Thanks,
    M.

    Hi,
    I see you are not clearing your local structure "ls_query".  This could be reason of problem,  try this and let us know the result:
    DATA lt_query                  TYPE mdm_query_table.
    DATA ls_query                 TYPE mdm_query.
    DATA lv_search_text       TYPE string.
    DATA lt_result_set            TYPE mdm_search_result_table.
    DATA ls_result_set           LIKE LINE OF lt_result_set.
    Fill query structure with FIRST parameter
        ls_query-parameter_code  = 'Name'.
        ls_query-operator        = 'CS'. 
        ls_query-dimension_type  = mdmif_search_dim_field.    
        ls_query-constraint_type = mdmif_search_constr_text.
        lv_search_text = 'BMW'.
        GET REFERENCE OF lv_search_text INTO ls_query-value_low.
        APPEND ls_query TO lt_query.
    CLEAR ls_query.
    Fill query structure with SECOND parameter
        ls_query-parameter_code  = 'Model'.
        ls_query-operator        = 'CS'. 
        ls_query-dimension_type  = mdmif_search_dim_field.    
        ls_query-constraint_type = mdmif_search_constr_text.
        lv_search_text = '2009'.
        GET REFERENCE OF lv_search_text INTO ls_query-value_low.
        APPEND ls_query TO lt_query.
    CLEAR ls_query.
    Query on records (search for value 'BMW' model '2009' in table Products)
        CALL METHOD lr_api->mo_core_service->query
          EXPORTING
            iv_object_type_code = 'Products'                  
            it_query            = lt_query
          IMPORTING
            et_result_set       = lt_result_set.

  • Problem with SQL query region source containing OLAP clauses

    Hi team,
    I believe I found a bug when HTMLDB validates the SQL Query in a report region.
    My query includes an ORDER BY clause within a windowing function and HTMLDB refuses to accept the source owing to the presence of the ORDER BY and the column heading sort preference.
    Clearly the order-by in a window function has little to do with the column heading sort, but this error prevents me from updating the conditional display item in the page definition.
    Note also that the page was imported smoothly from the 1.5 version, so probably the region source is not checked at that time, but only when you update it "manually".
    So, in the end, if I don't change anything the page works because the sql query is assumed to be correct but unfortunately I need to change the condition and I cannot.
    The problem shows up in page 126 of app 21670, SQL query region.
    Bye,
    Flavio
    PS: may be I can work around this by using the pl/sql function returning the sql query.

    Flavio,
    We're aware of this problem. For now, your workaround is described in this thread:
    HTMLDB 1.6 and "order by" in analytic functions
    Sergio

  • Problem with performance of a query having order by, distinct clause

    Hi,
    I have a problem with queries having order by, distinct clause.
    While its executing its taking lot of time. With DBMS_PROFILER identified the queries taking long time.
    The table is having approximately 70 million rows.
    Problem -1
    select * from table_name order by col1;
    select distinct col1,col2 from table_name;
    Here i am having 2 solutions request to let me know whether i am right if not suggest me right solution.
    Solution1:
    Max parallel servers is 8.
    select /* + parallel(table_name,8) */ * from table_name order by col1;
    select /* + parallel(table_name,8) */ distinct col1, col2 from table_name ;
    Solution-2:
    select /* + first_rows */ * from table_name order by col1;
    select /* + first_rows */ distinct col1, col2 from table_name ;
    Problem-2
    I am having a query with where condition on columns.
    Select * from table_name where col1='value1' and col2!='value2';
    Index created on col1 and col2.
    As we no that not equal won't use index as it is a composite index it should use the lead column. but its not using the index.
    Should i forcibly use index with hint or suggest me better solution.
    Any help really appreciated.
    Thanks in advance

    unique wrote:
    The table is having approximately 70 million rows.
    select * from table_name order by col1;Do you really want 70,000,000 rows from your table without any restrictions ? And furthermore ordered output ? I honestly understand the slowness of that query.
    Here i am having 2 solutions request to let me know whether i am right if not suggest me right solution.Who knows if you choosed the right solution. I would suggest to reconsider your query and the need of 70,000,000 returned rows.
    Problem-2
    I am having a query with where condition on columns.
    Select * from table_name where col1='value1' and col2!='value2';Please, provide the explain plan, eventually the tkprof output could also help. And tables descirption AND indexes description.
    And OS and Oracle version.
    Nicolas.

  • 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

Maybe you are looking for