Query that uses another Query within itself

Posted on 2011-05-11
Last Modified: 2012-05-11
Hello All;

OK, this is a weird question, and hopefully I will be able to explain it well enough to get some good advice.

This was originally an Access Database
#1 There was a Query that was made and SAVED with name: Query1
#2 There was another Query that was made, that used the Query1 within it.


select some, thing from table

(2nd Query)
Select some.table2, thing.Query1 from Query1, table2


I need to know how to make this type of Query work inside of SQL Server.
Since in Access, the Queries are saved within the database itself, and can then be used when needed.
SQL Server does not have this luxery (Or does it?)

Any help on this is greatly appreciated.
Question by:Wayne Barron
    LVL 51

    Accepted Solution

    If I'm understanding correctly an Access saved query is the same as a SQL Server "view".  So just create a view.  Then do a normal select blah, blah from "Query1".  

    create view Query1
    select some, thing from table

    Assuming that's even needed in sql server ;-)
    LVL 32

    Expert Comment


    here is simplified example

    select field1, field2, field3
    from (select field1, field2, field3
             from table2) A
    where > 1000
    LVL 32

    Expert Comment


      select field1, field2, (select field3 from table2 where = as field3
      from table1
    LVL 51

    Expert Comment

    >> Assuming that's even needed in sql server ;-)

    In other words, make sure there's a reason you actually need the "view".  For example, if you had special formatting or filters that you wanted to reuse in multiple places,  that might be a good to create a "view".  It would avoid duplication.  Otherwise, you might just combine the sql and skip the "view" altogether.

    LVL 30

    Author Closing Comment

    by:Wayne Barron
    In this case, it is needed.
    It is not mine.
    It belongs to a right big company, and the manager creates the Queries (Query1)
    So, the only thing that I do is create the Query to works with the (Query1), so they need to be able to change this Query anytime they need to.

    And, once I created the VIEW, I did not have to use word VIEW in the Query, I named the VIEW the same as Query1 and it worked without issue.

    Thank you
    LVL 51

    Expert Comment

    Yep, that's a good reason to create a VIEW.  Glad I could help!

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
    Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
    Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
    Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now