Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Nsted SELECT statements

Nsted SELECT statements

Scheduled Pinned Locked Moved Database
databasesql-serversysadmintutorialquestion
13 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Den2Fly

    Hello is it possible in SQL (and specially SQL Server 2000) to perform a SELECT on result of another SELECT statement? something like this just as an example of what I am talking about: SELECT * FROM (SELECT a.Name, a.EMail FROM Users a WHERE a.Gendre = 0) WHERE Name = 'Danielle' Thanks for any note, -Den --- "Art happens when you least expect it."

    C Offline
    C Offline
    Colin Angus Mackay
    wrote on last edited by
    #3

    As Christian said, why do you want to do this? Surely it would be easier to write SELECT Name, Email FROM Users WHERE Gendre = 0 AND Name='Danielle' Of course it is a very useful thing if you want to perform inner joins on subqueries, but I found recently that the query optimiser can sometimes get itself in a bit of a fankle over that if the subquery operates on too much data: The Stored Procedure runs how fast?[^]


    Do you want to know more?

    D 1 Reply Last reply
    0
    • C Christian Graus

      SELECT myTable.* FROM (SELECT a.Name, a.EMail FROM Users a WHERE a.Gendre = 0) myTable WHERE Name = 'Danielle' You need to provide an alias. Why do you want to do this ? It means the data needs to be processed twice. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

      D Offline
      D Offline
      Den2Fly
      wrote on last edited by
      #4

      HEllo Christian, Thank you so much for your reply The example I used was not my real requirement and as Colin said it could be easily relpaiced by an "AND". I have a rather complicated query in which I really need such an approach Thank you again --- "Art happens when you least expect it."

      C 1 Reply Last reply
      0
      • D Den2Fly

        HEllo Christian, Thank you so much for your reply The example I used was not my real requirement and as Colin said it could be easily relpaiced by an "AND". I have a rather complicated query in which I really need such an approach Thank you again --- "Art happens when you least expect it."

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #5

        Yeah, I guessed it was an example :-) I've sometimes felt I needed to do something similar, but I've always found a better performance alternative. In fact, we're ot allowed to write queries like this at work, and we write some complex queries Perhaps you could provide an example closer to what you're doing ? Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

        C M 2 Replies Last reply
        0
        • C Colin Angus Mackay

          As Christian said, why do you want to do this? Surely it would be easier to write SELECT Name, Email FROM Users WHERE Gendre = 0 AND Name='Danielle' Of course it is a very useful thing if you want to perform inner joins on subqueries, but I found recently that the query optimiser can sometimes get itself in a bit of a fankle over that if the subquery operates on too much data: The Stored Procedure runs how fast?[^]


          Do you want to know more?

          D Offline
          D Offline
          Den2Fly
          wrote on last edited by
          #6

          Hello Colin Thanks for your note, you are right about that query but that was just an example. ( not a good one I think :-) ) and again you are right my real project is some INNER JOINs that I have found this a good way to achive the result --- "Art happens when you least expect it."

          C 1 Reply Last reply
          0
          • C Christian Graus

            Yeah, I guessed it was an example :-) I've sometimes felt I needed to do something similar, but I've always found a better performance alternative. In fact, we're ot allowed to write queries like this at work, and we write some complex queries Perhaps you could provide an example closer to what you're doing ? Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

            C Offline
            C Offline
            Colin Angus Mackay
            wrote on last edited by
            #7

            Christian Graus wrote: In fact, we're ot allowed to write queries like this at work Do you mean as per the example? Or using a subquery in an inner join? Either way, isn't it a bit restrictive. Surely a better approach would be to write the query which ever way makes most sense and then if it is too slow profile it to see where the bottle necks are and rewrite those sections.


            Do you want to know more?

            C 1 Reply Last reply
            0
            • C Colin Angus Mackay

              Christian Graus wrote: In fact, we're ot allowed to write queries like this at work Do you mean as per the example? Or using a subquery in an inner join? Either way, isn't it a bit restrictive. Surely a better approach would be to write the query which ever way makes most sense and then if it is too slow profile it to see where the bottle necks are and rewrite those sections.


              Do you want to know more?

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #8

              Colin Angus Mackay wrote: using a subquery in an inner join? Bingo Colin Angus Mackay wrote: Either way, isn't it a bit restrictive. The actual rule is, if you think you need to do it, ask and they'll show you why you don't :-) It works, I always find for myself why I don't. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

              1 Reply Last reply
              0
              • D Den2Fly

                Hello Colin Thanks for your note, you are right about that query but that was just an example. ( not a good one I think :-) ) and again you are right my real project is some INNER JOINs that I have found this a good way to achive the result --- "Art happens when you least expect it."

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #9

                Yeah, joins are generally the answer rather than what you were doing before :-) Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

                1 Reply Last reply
                0
                • C Christian Graus

                  Yeah, I guessed it was an example :-) I've sometimes felt I needed to do something similar, but I've always found a better performance alternative. In fact, we're ot allowed to write queries like this at work, and we write some complex queries Perhaps you could provide an example closer to what you're doing ? Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

                  M Offline
                  M Offline
                  munawarhussain
                  wrote on last edited by
                  #10

                  hi nice to see ur provided info anbout Select Subquries... but u alos mentioned that u are not allowed to use such queries at work..then what u do ...where subquries are needed...and u don't have an alternate???? is there any more accurae and efficient way as an alternate to these sub-quries. ................................ munawar

                  C 1 Reply Last reply
                  0
                  • M munawarhussain

                    hi nice to see ur provided info anbout Select Subquries... but u alos mentioned that u are not allowed to use such queries at work..then what u do ...where subquries are needed...and u don't have an alternate???? is there any more accurae and efficient way as an alternate to these sub-quries. ................................ munawar

                    C Offline
                    C Offline
                    Christian Graus
                    wrote on last edited by
                    #11

                    munawarhussain wrote: then what u do ...where subquries are needed They generally are not needed, that's the point. munawarhussain wrote: is there any more accurae and efficient way as an alternate to these sub-quries. I usually find a way to use joins rather than subqueries. Obviously, if there physically is no other way, we'd use a subquery, but as a rule we're able to eliminate them. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

                    M 1 Reply Last reply
                    0
                    • C Christian Graus

                      munawarhussain wrote: then what u do ...where subquries are needed They generally are not needed, that's the point. munawarhussain wrote: is there any more accurae and efficient way as an alternate to these sub-quries. I usually find a way to use joins rather than subqueries. Obviously, if there physically is no other way, we'd use a subquery, but as a rule we're able to eliminate them. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

                      M Offline
                      M Offline
                      munawarhussain
                      wrote on last edited by
                      #12

                      Christian..thanks for reply.. may i know, in case, when u want to delete records is there any other way to delete data from more than one table with out using subquries?? in selecting data from multiple tables its possible to use joins.. okkkk

                      C 1 Reply Last reply
                      0
                      • M munawarhussain

                        Christian..thanks for reply.. may i know, in case, when u want to delete records is there any other way to delete data from more than one table with out using subquries?? in selecting data from multiple tables its possible to use joins.. okkkk

                        C Offline
                        C Offline
                        Christian Graus
                        wrote on last edited by
                        #13

                        You can use joins to delete as well. You can also use them to update. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups