How to exclude certain rows from sql select












-1














How do I exclude certain rows?
For example, I have the following table:



+------+------+------+
| Col1 | Col2 | Col3 |
+------+------+------+
| 1 | 1 | R |
| 1 | 2 | D |
| 2 | 3 | R |
| 2 | 4 | R |
| 3 | 5 | R |
| 4 | 6 | D |
+------+------+------+


I need to select only:



|    2 | 3    |    R |  
| 2 | 4 | R |
| 3 | 5 | R |


My select that does not work properly:



with t (c1,c2,c3) as(
select 1 , 1 , 'R' from dual union all
select 1 , 2 , 'D' from dual union all
select 2 , 3 , 'R' from dual union all
select 2 , 4 , 'R' from dual union all
select 3 , 5 , 'R' from dual union all
select 4 , 6 , 'D' from dual),
tt as (select t.*,count(*) over (partition by c1) cc from t ) select * from tt where cc=1 and c3='R';


Thanks in advance!










share|improve this question




















  • 3




    can you post tables but not in pictures
    – nikhil sugandh
    Nov 23 '18 at 10:00












  • Are you using MySQL or Oracle?
    – jarlh
    Nov 23 '18 at 10:01










  • Instead of me trying to guess what you are trying to do on the basis of non working code you could tell me.
    – P.Salmon
    Nov 23 '18 at 10:12










  • I'm using PL/SQL developer v.10
    – Karina Nogaibayeva
    Nov 23 '18 at 10:25






  • 2




    What is the logic behind why you only want to select those two rows? Is it that you're trying to select rows where the col3 values are all 'R' across each col1 value? Or is it rows where the col3 values are all the same for each col1 value? Or something else? Please edit your question to add in why you want to select those rows and not any others.
    – Boneist
    Nov 23 '18 at 10:44
















-1














How do I exclude certain rows?
For example, I have the following table:



+------+------+------+
| Col1 | Col2 | Col3 |
+------+------+------+
| 1 | 1 | R |
| 1 | 2 | D |
| 2 | 3 | R |
| 2 | 4 | R |
| 3 | 5 | R |
| 4 | 6 | D |
+------+------+------+


I need to select only:



|    2 | 3    |    R |  
| 2 | 4 | R |
| 3 | 5 | R |


My select that does not work properly:



with t (c1,c2,c3) as(
select 1 , 1 , 'R' from dual union all
select 1 , 2 , 'D' from dual union all
select 2 , 3 , 'R' from dual union all
select 2 , 4 , 'R' from dual union all
select 3 , 5 , 'R' from dual union all
select 4 , 6 , 'D' from dual),
tt as (select t.*,count(*) over (partition by c1) cc from t ) select * from tt where cc=1 and c3='R';


Thanks in advance!










share|improve this question




















  • 3




    can you post tables but not in pictures
    – nikhil sugandh
    Nov 23 '18 at 10:00












  • Are you using MySQL or Oracle?
    – jarlh
    Nov 23 '18 at 10:01










  • Instead of me trying to guess what you are trying to do on the basis of non working code you could tell me.
    – P.Salmon
    Nov 23 '18 at 10:12










  • I'm using PL/SQL developer v.10
    – Karina Nogaibayeva
    Nov 23 '18 at 10:25






  • 2




    What is the logic behind why you only want to select those two rows? Is it that you're trying to select rows where the col3 values are all 'R' across each col1 value? Or is it rows where the col3 values are all the same for each col1 value? Or something else? Please edit your question to add in why you want to select those rows and not any others.
    – Boneist
    Nov 23 '18 at 10:44














-1












-1








-1







How do I exclude certain rows?
For example, I have the following table:



+------+------+------+
| Col1 | Col2 | Col3 |
+------+------+------+
| 1 | 1 | R |
| 1 | 2 | D |
| 2 | 3 | R |
| 2 | 4 | R |
| 3 | 5 | R |
| 4 | 6 | D |
+------+------+------+


I need to select only:



|    2 | 3    |    R |  
| 2 | 4 | R |
| 3 | 5 | R |


My select that does not work properly:



with t (c1,c2,c3) as(
select 1 , 1 , 'R' from dual union all
select 1 , 2 , 'D' from dual union all
select 2 , 3 , 'R' from dual union all
select 2 , 4 , 'R' from dual union all
select 3 , 5 , 'R' from dual union all
select 4 , 6 , 'D' from dual),
tt as (select t.*,count(*) over (partition by c1) cc from t ) select * from tt where cc=1 and c3='R';


Thanks in advance!










share|improve this question















How do I exclude certain rows?
For example, I have the following table:



+------+------+------+
| Col1 | Col2 | Col3 |
+------+------+------+
| 1 | 1 | R |
| 1 | 2 | D |
| 2 | 3 | R |
| 2 | 4 | R |
| 3 | 5 | R |
| 4 | 6 | D |
+------+------+------+


I need to select only:



|    2 | 3    |    R |  
| 2 | 4 | R |
| 3 | 5 | R |


My select that does not work properly:



with t (c1,c2,c3) as(
select 1 , 1 , 'R' from dual union all
select 1 , 2 , 'D' from dual union all
select 2 , 3 , 'R' from dual union all
select 2 , 4 , 'R' from dual union all
select 3 , 5 , 'R' from dual union all
select 4 , 6 , 'D' from dual),
tt as (select t.*,count(*) over (partition by c1) cc from t ) select * from tt where cc=1 and c3='R';


Thanks in advance!







sql oracle






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 11:51









Salman A

176k66336424




176k66336424










asked Nov 23 '18 at 9:59









Karina NogaibayevaKarina Nogaibayeva

73




73








  • 3




    can you post tables but not in pictures
    – nikhil sugandh
    Nov 23 '18 at 10:00












  • Are you using MySQL or Oracle?
    – jarlh
    Nov 23 '18 at 10:01










  • Instead of me trying to guess what you are trying to do on the basis of non working code you could tell me.
    – P.Salmon
    Nov 23 '18 at 10:12










  • I'm using PL/SQL developer v.10
    – Karina Nogaibayeva
    Nov 23 '18 at 10:25






  • 2




    What is the logic behind why you only want to select those two rows? Is it that you're trying to select rows where the col3 values are all 'R' across each col1 value? Or is it rows where the col3 values are all the same for each col1 value? Or something else? Please edit your question to add in why you want to select those rows and not any others.
    – Boneist
    Nov 23 '18 at 10:44














  • 3




    can you post tables but not in pictures
    – nikhil sugandh
    Nov 23 '18 at 10:00












  • Are you using MySQL or Oracle?
    – jarlh
    Nov 23 '18 at 10:01










  • Instead of me trying to guess what you are trying to do on the basis of non working code you could tell me.
    – P.Salmon
    Nov 23 '18 at 10:12










  • I'm using PL/SQL developer v.10
    – Karina Nogaibayeva
    Nov 23 '18 at 10:25






  • 2




    What is the logic behind why you only want to select those two rows? Is it that you're trying to select rows where the col3 values are all 'R' across each col1 value? Or is it rows where the col3 values are all the same for each col1 value? Or something else? Please edit your question to add in why you want to select those rows and not any others.
    – Boneist
    Nov 23 '18 at 10:44








3




3




can you post tables but not in pictures
– nikhil sugandh
Nov 23 '18 at 10:00






can you post tables but not in pictures
– nikhil sugandh
Nov 23 '18 at 10:00














Are you using MySQL or Oracle?
– jarlh
Nov 23 '18 at 10:01




Are you using MySQL or Oracle?
– jarlh
Nov 23 '18 at 10:01












Instead of me trying to guess what you are trying to do on the basis of non working code you could tell me.
– P.Salmon
Nov 23 '18 at 10:12




Instead of me trying to guess what you are trying to do on the basis of non working code you could tell me.
– P.Salmon
Nov 23 '18 at 10:12












I'm using PL/SQL developer v.10
– Karina Nogaibayeva
Nov 23 '18 at 10:25




I'm using PL/SQL developer v.10
– Karina Nogaibayeva
Nov 23 '18 at 10:25




2




2




What is the logic behind why you only want to select those two rows? Is it that you're trying to select rows where the col3 values are all 'R' across each col1 value? Or is it rows where the col3 values are all the same for each col1 value? Or something else? Please edit your question to add in why you want to select those rows and not any others.
– Boneist
Nov 23 '18 at 10:44




What is the logic behind why you only want to select those two rows? Is it that you're trying to select rows where the col3 values are all 'R' across each col1 value? Or is it rows where the col3 values are all the same for each col1 value? Or something else? Please edit your question to add in why you want to select those rows and not any others.
– Boneist
Nov 23 '18 at 10:44












7 Answers
7






active

oldest

votes


















0














select * from table where col2 = 'R'


or if you want to exclude rows with D value just



select * from table where col2 != 'D'






share|improve this answer





























    0














    It depends on your requirements but you can do in this way:



    SELECT * FROM `table` WHERE col1 = 2 AND col3 = "R"


    if you want to exclude just do it like WHERE col1 != 1



    You ca also use IN clause also e.g.



    SELECT column_name(s)
    FROM table_name
    WHERE column_name IN (value1, value2, ...);


    This syntax is for MySql, but you can modify it as per your requirement or database you are using.






    share|improve this answer































      0














      this will work :



      select * from (select * from table_name) where rownum<=4
      minus
      select * from ( select * from table_name) where rownum<=2





      share|improve this answer























      • How should that work? There is no row named rownum in the example
        – Nico Haase
        Nov 23 '18 at 10:35










      • @NicoHaase its called pseudo column bro rownum is a pseudo column
        – nikhil sugandh
        Nov 23 '18 at 10:43












      • @Karina Nogaibayeva its working right??
        – nikhil sugandh
        Nov 23 '18 at 10:44










      • What kind of "pseudo column" is that? Does PostgreSQL support it?
        – Nico Haase
        Nov 23 '18 at 10:46










      • @NicoHaase its tagged oracle thats why
        – nikhil sugandh
        Nov 23 '18 at 10:47



















      0














      My guess is that you want all rows for a col1 where no row for a col1 = D and at least 1 row for a col1 = R. @ where [not] exists may do



      DROP TABLE T;
      CREATE TABLE T
      (Col1 NUMBER, Col2 NUMBER, Col3 VARCHAR(1));
      INSERT INTO T VALUES ( 1 , 1 , 'R');
      INSERT INTO T VALUES ( 1 , 2 , 'D');
      INSERT INTO T VALUES ( 2 , 3 , 'R');
      INSERT INTO T VALUES ( 2 , 4 , 'R');
      INSERT INTO T VALUES ( 3 , 5 , 'R');
      INSERT INTO T VALUES ( 3 , 6 , 'D');
      INSERT INTO T VALUES ( 4 , 5 , 'X');
      INSERT INTO T VALUES ( 4 , 6 , 'Y');
      INSERT INTO T VALUES ( 5 , 6 , 'X');
      INSERT INTO T VALUES ( 5 , 5 , 'R');
      INSERT INTO T VALUES ( 5 , 6 , 'Y');

      SELECT *
      FROM T
      WHERE NOT EXISTS(SELECT 1 FROM T T1 WHERE T1.COL1 = T.COL1 AND COL3 = 'D') AND
      EXISTS(SELECT 1 FROM T T1 WHERE T1.COL1 = T.COL1 AND COL3 = 'R');


      Result



            COL1       COL2 COL3
      ---------- ---------- ----
      5 6 X
      5 5 R
      5 6 Y
      2 3 R
      2 4 R





      share|improve this answer





























        0














        use row_number() window function



            with t (c1,c2,c3) as(
        select 1 , 1 , 'R' from dual union all
        select 1 , 2 , 'D' from dual union all
        select 2 , 3 , 'R' from dual union all
        select 2 , 4 , 'R' from dual union all
        select 3 , 5 , 'R' from dual union all
        select 4 , 6 , 'D' from dual
        ),
        t1 as
        (
        select c1,c2,c3,row_number() over(order by c2) rn from t
        ) select * from t1 where t1.rn>=3 and t1.rn<=5


        demo link



        C1  C2  C3
        2 3 R
        2 4 R
        3 5 R





        share|improve this answer



















        • 1




          Why downvote for this answer. It looks correct to me. As per given data this query should result in the expected output.
          – XING
          Nov 23 '18 at 10:12










        • @XING don't know why they have given downvote but thanks to you
          – Zaynul Abadin Tuhin
          Nov 23 '18 at 10:21



















        0














        You can try using correlated subquery



        select * from tablename a
        from
        where exists (select 1 tablename b where a.col1=b.col1 having count(*)>1)





        share|improve this answer































          0














          Based on what you have provided I can only surmise that the only requirement is for COL1 to be equal to 2 or 3 in that case all you have to do is (assuming that you actually have table);



          SELECT * FROM <table_name>
          WHERE col1 IN (2,3);


          This will give you the desired output for the particular example provided in the question. If there is a selection requirement that goes beyond retrieving data where column 1 is either 2 or 3 than a more specific or precise answer can be provided.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53444409%2fhow-to-exclude-certain-rows-from-sql-select%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            7 Answers
            7






            active

            oldest

            votes








            7 Answers
            7






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            select * from table where col2 = 'R'


            or if you want to exclude rows with D value just



            select * from table where col2 != 'D'






            share|improve this answer


























              0














              select * from table where col2 = 'R'


              or if you want to exclude rows with D value just



              select * from table where col2 != 'D'






              share|improve this answer
























                0












                0








                0






                select * from table where col2 = 'R'


                or if you want to exclude rows with D value just



                select * from table where col2 != 'D'






                share|improve this answer












                select * from table where col2 = 'R'


                or if you want to exclude rows with D value just



                select * from table where col2 != 'D'







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 23 '18 at 10:06









                Arga PutraArga Putra

                1




                1

























                    0














                    It depends on your requirements but you can do in this way:



                    SELECT * FROM `table` WHERE col1 = 2 AND col3 = "R"


                    if you want to exclude just do it like WHERE col1 != 1



                    You ca also use IN clause also e.g.



                    SELECT column_name(s)
                    FROM table_name
                    WHERE column_name IN (value1, value2, ...);


                    This syntax is for MySql, but you can modify it as per your requirement or database you are using.






                    share|improve this answer




























                      0














                      It depends on your requirements but you can do in this way:



                      SELECT * FROM `table` WHERE col1 = 2 AND col3 = "R"


                      if you want to exclude just do it like WHERE col1 != 1



                      You ca also use IN clause also e.g.



                      SELECT column_name(s)
                      FROM table_name
                      WHERE column_name IN (value1, value2, ...);


                      This syntax is for MySql, but you can modify it as per your requirement or database you are using.






                      share|improve this answer


























                        0












                        0








                        0






                        It depends on your requirements but you can do in this way:



                        SELECT * FROM `table` WHERE col1 = 2 AND col3 = "R"


                        if you want to exclude just do it like WHERE col1 != 1



                        You ca also use IN clause also e.g.



                        SELECT column_name(s)
                        FROM table_name
                        WHERE column_name IN (value1, value2, ...);


                        This syntax is for MySql, but you can modify it as per your requirement or database you are using.






                        share|improve this answer














                        It depends on your requirements but you can do in this way:



                        SELECT * FROM `table` WHERE col1 = 2 AND col3 = "R"


                        if you want to exclude just do it like WHERE col1 != 1



                        You ca also use IN clause also e.g.



                        SELECT column_name(s)
                        FROM table_name
                        WHERE column_name IN (value1, value2, ...);


                        This syntax is for MySql, but you can modify it as per your requirement or database you are using.







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Nov 23 '18 at 10:47









                        a_horse_with_no_name

                        293k46447541




                        293k46447541










                        answered Nov 23 '18 at 10:21









                        Hemant KumarHemant Kumar

                        415317




                        415317























                            0














                            this will work :



                            select * from (select * from table_name) where rownum<=4
                            minus
                            select * from ( select * from table_name) where rownum<=2





                            share|improve this answer























                            • How should that work? There is no row named rownum in the example
                              – Nico Haase
                              Nov 23 '18 at 10:35










                            • @NicoHaase its called pseudo column bro rownum is a pseudo column
                              – nikhil sugandh
                              Nov 23 '18 at 10:43












                            • @Karina Nogaibayeva its working right??
                              – nikhil sugandh
                              Nov 23 '18 at 10:44










                            • What kind of "pseudo column" is that? Does PostgreSQL support it?
                              – Nico Haase
                              Nov 23 '18 at 10:46










                            • @NicoHaase its tagged oracle thats why
                              – nikhil sugandh
                              Nov 23 '18 at 10:47
















                            0














                            this will work :



                            select * from (select * from table_name) where rownum<=4
                            minus
                            select * from ( select * from table_name) where rownum<=2





                            share|improve this answer























                            • How should that work? There is no row named rownum in the example
                              – Nico Haase
                              Nov 23 '18 at 10:35










                            • @NicoHaase its called pseudo column bro rownum is a pseudo column
                              – nikhil sugandh
                              Nov 23 '18 at 10:43












                            • @Karina Nogaibayeva its working right??
                              – nikhil sugandh
                              Nov 23 '18 at 10:44










                            • What kind of "pseudo column" is that? Does PostgreSQL support it?
                              – Nico Haase
                              Nov 23 '18 at 10:46










                            • @NicoHaase its tagged oracle thats why
                              – nikhil sugandh
                              Nov 23 '18 at 10:47














                            0












                            0








                            0






                            this will work :



                            select * from (select * from table_name) where rownum<=4
                            minus
                            select * from ( select * from table_name) where rownum<=2





                            share|improve this answer














                            this will work :



                            select * from (select * from table_name) where rownum<=4
                            minus
                            select * from ( select * from table_name) where rownum<=2






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 23 '18 at 10:52

























                            answered Nov 23 '18 at 10:02









                            nikhil sugandhnikhil sugandh

                            1,2562719




                            1,2562719












                            • How should that work? There is no row named rownum in the example
                              – Nico Haase
                              Nov 23 '18 at 10:35










                            • @NicoHaase its called pseudo column bro rownum is a pseudo column
                              – nikhil sugandh
                              Nov 23 '18 at 10:43












                            • @Karina Nogaibayeva its working right??
                              – nikhil sugandh
                              Nov 23 '18 at 10:44










                            • What kind of "pseudo column" is that? Does PostgreSQL support it?
                              – Nico Haase
                              Nov 23 '18 at 10:46










                            • @NicoHaase its tagged oracle thats why
                              – nikhil sugandh
                              Nov 23 '18 at 10:47


















                            • How should that work? There is no row named rownum in the example
                              – Nico Haase
                              Nov 23 '18 at 10:35










                            • @NicoHaase its called pseudo column bro rownum is a pseudo column
                              – nikhil sugandh
                              Nov 23 '18 at 10:43












                            • @Karina Nogaibayeva its working right??
                              – nikhil sugandh
                              Nov 23 '18 at 10:44










                            • What kind of "pseudo column" is that? Does PostgreSQL support it?
                              – Nico Haase
                              Nov 23 '18 at 10:46










                            • @NicoHaase its tagged oracle thats why
                              – nikhil sugandh
                              Nov 23 '18 at 10:47
















                            How should that work? There is no row named rownum in the example
                            – Nico Haase
                            Nov 23 '18 at 10:35




                            How should that work? There is no row named rownum in the example
                            – Nico Haase
                            Nov 23 '18 at 10:35












                            @NicoHaase its called pseudo column bro rownum is a pseudo column
                            – nikhil sugandh
                            Nov 23 '18 at 10:43






                            @NicoHaase its called pseudo column bro rownum is a pseudo column
                            – nikhil sugandh
                            Nov 23 '18 at 10:43














                            @Karina Nogaibayeva its working right??
                            – nikhil sugandh
                            Nov 23 '18 at 10:44




                            @Karina Nogaibayeva its working right??
                            – nikhil sugandh
                            Nov 23 '18 at 10:44












                            What kind of "pseudo column" is that? Does PostgreSQL support it?
                            – Nico Haase
                            Nov 23 '18 at 10:46




                            What kind of "pseudo column" is that? Does PostgreSQL support it?
                            – Nico Haase
                            Nov 23 '18 at 10:46












                            @NicoHaase its tagged oracle thats why
                            – nikhil sugandh
                            Nov 23 '18 at 10:47




                            @NicoHaase its tagged oracle thats why
                            – nikhil sugandh
                            Nov 23 '18 at 10:47











                            0














                            My guess is that you want all rows for a col1 where no row for a col1 = D and at least 1 row for a col1 = R. @ where [not] exists may do



                            DROP TABLE T;
                            CREATE TABLE T
                            (Col1 NUMBER, Col2 NUMBER, Col3 VARCHAR(1));
                            INSERT INTO T VALUES ( 1 , 1 , 'R');
                            INSERT INTO T VALUES ( 1 , 2 , 'D');
                            INSERT INTO T VALUES ( 2 , 3 , 'R');
                            INSERT INTO T VALUES ( 2 , 4 , 'R');
                            INSERT INTO T VALUES ( 3 , 5 , 'R');
                            INSERT INTO T VALUES ( 3 , 6 , 'D');
                            INSERT INTO T VALUES ( 4 , 5 , 'X');
                            INSERT INTO T VALUES ( 4 , 6 , 'Y');
                            INSERT INTO T VALUES ( 5 , 6 , 'X');
                            INSERT INTO T VALUES ( 5 , 5 , 'R');
                            INSERT INTO T VALUES ( 5 , 6 , 'Y');

                            SELECT *
                            FROM T
                            WHERE NOT EXISTS(SELECT 1 FROM T T1 WHERE T1.COL1 = T.COL1 AND COL3 = 'D') AND
                            EXISTS(SELECT 1 FROM T T1 WHERE T1.COL1 = T.COL1 AND COL3 = 'R');


                            Result



                                  COL1       COL2 COL3
                            ---------- ---------- ----
                            5 6 X
                            5 5 R
                            5 6 Y
                            2 3 R
                            2 4 R





                            share|improve this answer


























                              0














                              My guess is that you want all rows for a col1 where no row for a col1 = D and at least 1 row for a col1 = R. @ where [not] exists may do



                              DROP TABLE T;
                              CREATE TABLE T
                              (Col1 NUMBER, Col2 NUMBER, Col3 VARCHAR(1));
                              INSERT INTO T VALUES ( 1 , 1 , 'R');
                              INSERT INTO T VALUES ( 1 , 2 , 'D');
                              INSERT INTO T VALUES ( 2 , 3 , 'R');
                              INSERT INTO T VALUES ( 2 , 4 , 'R');
                              INSERT INTO T VALUES ( 3 , 5 , 'R');
                              INSERT INTO T VALUES ( 3 , 6 , 'D');
                              INSERT INTO T VALUES ( 4 , 5 , 'X');
                              INSERT INTO T VALUES ( 4 , 6 , 'Y');
                              INSERT INTO T VALUES ( 5 , 6 , 'X');
                              INSERT INTO T VALUES ( 5 , 5 , 'R');
                              INSERT INTO T VALUES ( 5 , 6 , 'Y');

                              SELECT *
                              FROM T
                              WHERE NOT EXISTS(SELECT 1 FROM T T1 WHERE T1.COL1 = T.COL1 AND COL3 = 'D') AND
                              EXISTS(SELECT 1 FROM T T1 WHERE T1.COL1 = T.COL1 AND COL3 = 'R');


                              Result



                                    COL1       COL2 COL3
                              ---------- ---------- ----
                              5 6 X
                              5 5 R
                              5 6 Y
                              2 3 R
                              2 4 R





                              share|improve this answer
























                                0












                                0








                                0






                                My guess is that you want all rows for a col1 where no row for a col1 = D and at least 1 row for a col1 = R. @ where [not] exists may do



                                DROP TABLE T;
                                CREATE TABLE T
                                (Col1 NUMBER, Col2 NUMBER, Col3 VARCHAR(1));
                                INSERT INTO T VALUES ( 1 , 1 , 'R');
                                INSERT INTO T VALUES ( 1 , 2 , 'D');
                                INSERT INTO T VALUES ( 2 , 3 , 'R');
                                INSERT INTO T VALUES ( 2 , 4 , 'R');
                                INSERT INTO T VALUES ( 3 , 5 , 'R');
                                INSERT INTO T VALUES ( 3 , 6 , 'D');
                                INSERT INTO T VALUES ( 4 , 5 , 'X');
                                INSERT INTO T VALUES ( 4 , 6 , 'Y');
                                INSERT INTO T VALUES ( 5 , 6 , 'X');
                                INSERT INTO T VALUES ( 5 , 5 , 'R');
                                INSERT INTO T VALUES ( 5 , 6 , 'Y');

                                SELECT *
                                FROM T
                                WHERE NOT EXISTS(SELECT 1 FROM T T1 WHERE T1.COL1 = T.COL1 AND COL3 = 'D') AND
                                EXISTS(SELECT 1 FROM T T1 WHERE T1.COL1 = T.COL1 AND COL3 = 'R');


                                Result



                                      COL1       COL2 COL3
                                ---------- ---------- ----
                                5 6 X
                                5 5 R
                                5 6 Y
                                2 3 R
                                2 4 R





                                share|improve this answer












                                My guess is that you want all rows for a col1 where no row for a col1 = D and at least 1 row for a col1 = R. @ where [not] exists may do



                                DROP TABLE T;
                                CREATE TABLE T
                                (Col1 NUMBER, Col2 NUMBER, Col3 VARCHAR(1));
                                INSERT INTO T VALUES ( 1 , 1 , 'R');
                                INSERT INTO T VALUES ( 1 , 2 , 'D');
                                INSERT INTO T VALUES ( 2 , 3 , 'R');
                                INSERT INTO T VALUES ( 2 , 4 , 'R');
                                INSERT INTO T VALUES ( 3 , 5 , 'R');
                                INSERT INTO T VALUES ( 3 , 6 , 'D');
                                INSERT INTO T VALUES ( 4 , 5 , 'X');
                                INSERT INTO T VALUES ( 4 , 6 , 'Y');
                                INSERT INTO T VALUES ( 5 , 6 , 'X');
                                INSERT INTO T VALUES ( 5 , 5 , 'R');
                                INSERT INTO T VALUES ( 5 , 6 , 'Y');

                                SELECT *
                                FROM T
                                WHERE NOT EXISTS(SELECT 1 FROM T T1 WHERE T1.COL1 = T.COL1 AND COL3 = 'D') AND
                                EXISTS(SELECT 1 FROM T T1 WHERE T1.COL1 = T.COL1 AND COL3 = 'R');


                                Result



                                      COL1       COL2 COL3
                                ---------- ---------- ----
                                5 6 X
                                5 5 R
                                5 6 Y
                                2 3 R
                                2 4 R






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Nov 23 '18 at 10:54









                                P.SalmonP.Salmon

                                7,5972415




                                7,5972415























                                    0














                                    use row_number() window function



                                        with t (c1,c2,c3) as(
                                    select 1 , 1 , 'R' from dual union all
                                    select 1 , 2 , 'D' from dual union all
                                    select 2 , 3 , 'R' from dual union all
                                    select 2 , 4 , 'R' from dual union all
                                    select 3 , 5 , 'R' from dual union all
                                    select 4 , 6 , 'D' from dual
                                    ),
                                    t1 as
                                    (
                                    select c1,c2,c3,row_number() over(order by c2) rn from t
                                    ) select * from t1 where t1.rn>=3 and t1.rn<=5


                                    demo link



                                    C1  C2  C3
                                    2 3 R
                                    2 4 R
                                    3 5 R





                                    share|improve this answer



















                                    • 1




                                      Why downvote for this answer. It looks correct to me. As per given data this query should result in the expected output.
                                      – XING
                                      Nov 23 '18 at 10:12










                                    • @XING don't know why they have given downvote but thanks to you
                                      – Zaynul Abadin Tuhin
                                      Nov 23 '18 at 10:21
















                                    0














                                    use row_number() window function



                                        with t (c1,c2,c3) as(
                                    select 1 , 1 , 'R' from dual union all
                                    select 1 , 2 , 'D' from dual union all
                                    select 2 , 3 , 'R' from dual union all
                                    select 2 , 4 , 'R' from dual union all
                                    select 3 , 5 , 'R' from dual union all
                                    select 4 , 6 , 'D' from dual
                                    ),
                                    t1 as
                                    (
                                    select c1,c2,c3,row_number() over(order by c2) rn from t
                                    ) select * from t1 where t1.rn>=3 and t1.rn<=5


                                    demo link



                                    C1  C2  C3
                                    2 3 R
                                    2 4 R
                                    3 5 R





                                    share|improve this answer



















                                    • 1




                                      Why downvote for this answer. It looks correct to me. As per given data this query should result in the expected output.
                                      – XING
                                      Nov 23 '18 at 10:12










                                    • @XING don't know why they have given downvote but thanks to you
                                      – Zaynul Abadin Tuhin
                                      Nov 23 '18 at 10:21














                                    0












                                    0








                                    0






                                    use row_number() window function



                                        with t (c1,c2,c3) as(
                                    select 1 , 1 , 'R' from dual union all
                                    select 1 , 2 , 'D' from dual union all
                                    select 2 , 3 , 'R' from dual union all
                                    select 2 , 4 , 'R' from dual union all
                                    select 3 , 5 , 'R' from dual union all
                                    select 4 , 6 , 'D' from dual
                                    ),
                                    t1 as
                                    (
                                    select c1,c2,c3,row_number() over(order by c2) rn from t
                                    ) select * from t1 where t1.rn>=3 and t1.rn<=5


                                    demo link



                                    C1  C2  C3
                                    2 3 R
                                    2 4 R
                                    3 5 R





                                    share|improve this answer














                                    use row_number() window function



                                        with t (c1,c2,c3) as(
                                    select 1 , 1 , 'R' from dual union all
                                    select 1 , 2 , 'D' from dual union all
                                    select 2 , 3 , 'R' from dual union all
                                    select 2 , 4 , 'R' from dual union all
                                    select 3 , 5 , 'R' from dual union all
                                    select 4 , 6 , 'D' from dual
                                    ),
                                    t1 as
                                    (
                                    select c1,c2,c3,row_number() over(order by c2) rn from t
                                    ) select * from t1 where t1.rn>=3 and t1.rn<=5


                                    demo link



                                    C1  C2  C3
                                    2 3 R
                                    2 4 R
                                    3 5 R






                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Nov 23 '18 at 11:29

























                                    answered Nov 23 '18 at 10:00









                                    Zaynul Abadin TuhinZaynul Abadin Tuhin

                                    11.5k2831




                                    11.5k2831








                                    • 1




                                      Why downvote for this answer. It looks correct to me. As per given data this query should result in the expected output.
                                      – XING
                                      Nov 23 '18 at 10:12










                                    • @XING don't know why they have given downvote but thanks to you
                                      – Zaynul Abadin Tuhin
                                      Nov 23 '18 at 10:21














                                    • 1




                                      Why downvote for this answer. It looks correct to me. As per given data this query should result in the expected output.
                                      – XING
                                      Nov 23 '18 at 10:12










                                    • @XING don't know why they have given downvote but thanks to you
                                      – Zaynul Abadin Tuhin
                                      Nov 23 '18 at 10:21








                                    1




                                    1




                                    Why downvote for this answer. It looks correct to me. As per given data this query should result in the expected output.
                                    – XING
                                    Nov 23 '18 at 10:12




                                    Why downvote for this answer. It looks correct to me. As per given data this query should result in the expected output.
                                    – XING
                                    Nov 23 '18 at 10:12












                                    @XING don't know why they have given downvote but thanks to you
                                    – Zaynul Abadin Tuhin
                                    Nov 23 '18 at 10:21




                                    @XING don't know why they have given downvote but thanks to you
                                    – Zaynul Abadin Tuhin
                                    Nov 23 '18 at 10:21











                                    0














                                    You can try using correlated subquery



                                    select * from tablename a
                                    from
                                    where exists (select 1 tablename b where a.col1=b.col1 having count(*)>1)





                                    share|improve this answer




























                                      0














                                      You can try using correlated subquery



                                      select * from tablename a
                                      from
                                      where exists (select 1 tablename b where a.col1=b.col1 having count(*)>1)





                                      share|improve this answer


























                                        0












                                        0








                                        0






                                        You can try using correlated subquery



                                        select * from tablename a
                                        from
                                        where exists (select 1 tablename b where a.col1=b.col1 having count(*)>1)





                                        share|improve this answer














                                        You can try using correlated subquery



                                        select * from tablename a
                                        from
                                        where exists (select 1 tablename b where a.col1=b.col1 having count(*)>1)






                                        share|improve this answer














                                        share|improve this answer



                                        share|improve this answer








                                        edited Nov 23 '18 at 11:39

























                                        answered Nov 23 '18 at 10:06









                                        fa06fa06

                                        11.6k2917




                                        11.6k2917























                                            0














                                            Based on what you have provided I can only surmise that the only requirement is for COL1 to be equal to 2 or 3 in that case all you have to do is (assuming that you actually have table);



                                            SELECT * FROM <table_name>
                                            WHERE col1 IN (2,3);


                                            This will give you the desired output for the particular example provided in the question. If there is a selection requirement that goes beyond retrieving data where column 1 is either 2 or 3 than a more specific or precise answer can be provided.






                                            share|improve this answer


























                                              0














                                              Based on what you have provided I can only surmise that the only requirement is for COL1 to be equal to 2 or 3 in that case all you have to do is (assuming that you actually have table);



                                              SELECT * FROM <table_name>
                                              WHERE col1 IN (2,3);


                                              This will give you the desired output for the particular example provided in the question. If there is a selection requirement that goes beyond retrieving data where column 1 is either 2 or 3 than a more specific or precise answer can be provided.






                                              share|improve this answer
























                                                0












                                                0








                                                0






                                                Based on what you have provided I can only surmise that the only requirement is for COL1 to be equal to 2 or 3 in that case all you have to do is (assuming that you actually have table);



                                                SELECT * FROM <table_name>
                                                WHERE col1 IN (2,3);


                                                This will give you the desired output for the particular example provided in the question. If there is a selection requirement that goes beyond retrieving data where column 1 is either 2 or 3 than a more specific or precise answer can be provided.






                                                share|improve this answer












                                                Based on what you have provided I can only surmise that the only requirement is for COL1 to be equal to 2 or 3 in that case all you have to do is (assuming that you actually have table);



                                                SELECT * FROM <table_name>
                                                WHERE col1 IN (2,3);


                                                This will give you the desired output for the particular example provided in the question. If there is a selection requirement that goes beyond retrieving data where column 1 is either 2 or 3 than a more specific or precise answer can be provided.







                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Nov 23 '18 at 12:07









                                                m_e_sm_e_s

                                                11




                                                11






























                                                    draft saved

                                                    draft discarded




















































                                                    Thanks for contributing an answer to Stack Overflow!


                                                    • Please be sure to answer the question. Provide details and share your research!

                                                    But avoid



                                                    • Asking for help, clarification, or responding to other answers.

                                                    • Making statements based on opinion; back them up with references or personal experience.


                                                    To learn more, see our tips on writing great answers.




                                                    draft saved


                                                    draft discarded














                                                    StackExchange.ready(
                                                    function () {
                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53444409%2fhow-to-exclude-certain-rows-from-sql-select%23new-answer', 'question_page');
                                                    }
                                                    );

                                                    Post as a guest















                                                    Required, but never shown





















































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown

































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown







                                                    Popular posts from this blog

                                                    Berounka

                                                    Sphinx de Gizeh

                                                    Different font size/position of beamer's navigation symbols template's content depending on regular/plain...