---
title: "How to add queries within queries"
canonical: "https://support.appfire.com/space/SUPPORT/89138219/How%20to%20add%20queries%20within%20queries"
format: markdown
---
> Macro (div)
> 
> ### On this page
> 
> > Macro (toc)

# <span style="color: #000000">Description</span>

<span style="color: #000000">To improve how you show data to viewers, you can employ some advanced techniques by including queries within other queries. This also allows you to incorporate data from multiple data sources into a single table.</span>

<span style="color: #000000">The example on this page demonstrates a number of advanced techniques using this approach. The example uses two inner</span> SQL Query macros with<span style="color: #000000"> queries that depends on the data provided by the outer query. Although not shown here, this technique can drive a query from another data source just as easily as from the same data source. Those queries can produce tables, as illustrated here, or a single value by using </span>*<span style="color: #000000">table=false</span>*<span style="color: #000000"> in the wiki markup for the inner query.</span>

> ℹ️ <span style="color: #000000">To know more about </span>*<span style="color: #000000">SQL Query</span>*<span style="color: #000000"> macros, click the relevant version:</span>
> ℹ️ 
> ℹ️ - [10.x version](https://appfire.atlassian.net/wiki/spaces/SQL/pages/74321526/SQL-query+-+10.x)
> ℹ️ - [8.x version](https://appfire.atlassian.net/wiki/spaces/SQL/pages/74320969/SQL-query+Macro+-+8.x)

# <span style="color: #000000">Example</span>

## <span style="color: #000000">Result view</span>

![image](media://b3a94b57-2617-4a8c-8a3f-a1a3384bbbf6)

## <span style="color: #000000">Edit view</span>

![image](media://bb2afff1-ac21-474d-9399-219c4b49b8e3)

## <span style="color: #000000">SQL text</span>

##### **SQL Query**

```sql
select 
   '{sql-query:dataSource=exampleDataDS|output=wiki|rowOrientation=horizontal} 
        select 
            "Customer Number" as "Number" 
          , "Customer Name" as "Name" 
          , "Country" 
          , "Credit Limit"
          , (select employees."First Name" || '' '' || employees."Last Name" from employees where customers."Sales Rep Employee Number" = employees."Employee Number") as "Sales Rep" 
        from customers  
        where customers."Customer Number" = ' || orders."Customer Number" || ' {sql-query}'
    as "Customer"
  , date("Order Date") as "Order Date"
  , date("Required Date") as "Required Date"
  , date("Shipped Date") as "Shipped Date"
  , case 
        when "Status" = 'Cancelled' then '{status:color=red|title=' || "Status" || '}'
        when "Status" = 'Shipped' then '{status:color=green|title=' || "Status" || '}'    
        else '{status:color=yellow|title=' || "Status" || '}'  
    end as "Status"
  , "Order Number"
  , '{sql-query:dataSource=exampleDataDS|autoTotal=true|autoNumber=true|columnTypes=S,I,S,F|columnAttributes=,,style=text-align: right} 
        select 
            "Product Code" as "Product"
          , "Quantity Ordered" as "Quantity"
          , "Unit Price"
          , round("Quantity Ordered" * "Unit Price", 2) as "Total Price"
        from orderdetails where orderdetails."Order Number" = ' || orders."Order Number" || ' order by orderdetails."Order Line Number"{sql-query}' as "Order Details"
from orders
order by "Order Date" desc
limit 75
```