# IV. Writing - 09- Subqueries in the Select clause

**URL:** <https://forum.codewithmosh.com/t/iv-writing-09-subqueries-in-the-select-clause/25969>\
**Category:** SQL\
**Created:** [April 4, 2024, 8:45am UTC](https://forum.codewithmosh.com/t/iv-writing-09-subqueries-in-the-select-clause/25969 "2024-04-04T08:45:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ritu](https://avatars.discourse-cdn.com/v4/letter/r/7993a0/32.png) [@Ritu](https://forum.codewithmosh.com/u/Ritu)\
**Post date:** [April 4, 2024, 8:45am UTC](https://forum.codewithmosh.com/t/iv-writing-09-subqueries-in-the-select-clause/25969/1 "2024-04-04T08:45:06Z")

</div>

USE SQL\_INVOICING;

SELECT  
Invoice\_ID,  
Invoice\_Total,  
(SELECT AVG(Invoice\_Total) FROM Invoices) AS Invoice\_Average,  
Invoice\_Total - (SELECT AVG(Invoice\_Total) FROM Invoices) AS Difference  
FROM Invoices;

| Invoice\_ID | Invoice\_Total | Invoice\_Average | Difference |
| --- | --- | --- | --- |
| 1 | 101.79 | 152.388235 | -50.598235 |
| 2 | 175.32 | 152.388235 | 22.931765 |
| 3 | 147.99 | 152.388235 | -4.398235 |
| 4 | 152.21 | 152.388235 | -0.178235 |
| 5 | 169.36 | 152.388235 | 16.971765 |
| 6 | 157.78 | 152.388235 | 5.391765 |
| 7 | 133.87 | 152.388235 | -18.518235 |
| 8 | 189.12 | 152.388235 | 36.731765 |
| 9 | 172.17 | 152.388235 | 19.781765 |
| 10 | 159.50 | 152.388235 | 7.111765 |
| 11 | 126.15 | 152.388235 | -26.238235 |
| 12 | 135.01 | 152.388235 | -17.378235 |
| 13 | 167.29 | 152.388235 | 14.901765 |
| 14 | 162.02 | 152.388235 | 9.631765 |
| 15 | 126.38 | 152.388235 | -26.008235 |
| 16 | 180.17 | 152.388235 | 27.781765 |
| 17 | 134.47 | 152.388235 | -17.918235 |

* * *

**Aggregate**  
SELECT  
I.Client\_ID,  
C.Client,  
SUM(Invoice\_Total) Client\_Invoice\_Total,  
Avg(Invoice\_Total) Client\_Average\_Invoice  
FROM Invoices I  
INNER JOIN Clients C ON  
I.Client\_ID = C.Client\_ID  
GROUP BY I.Client\_ID, C.Client;

| Client\_ID | Client | Client\_Invoice\_Total | Client\_Average\_Invoice |
| --- | --- | --- | --- |
| 1 | Vinte | 802.89 | 160.578000 |
| 2 | Myworks | 101.79 | 101.790000 |
| 3 | Yadel | 705.90 | 141.180000 |
| 5 | Topiclounge | 980.02 | 163.336666 |

---

<div class="post-metadata">

**Author:** ![UniqueNospaceShort](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/uniquenospaceshort/32/1814_2.png) [@UniqueNospaceShort](https://forum.codewithmosh.com/u/UniqueNospaceShort)\
**Post date:** [April 4, 2024, 7:22pm UTC](https://forum.codewithmosh.com/t/iv-writing-09-subqueries-in-the-select-clause/25969/2 "2024-04-04T19:22:05Z")

</div>

Hi,  
What’s the point ?  
Is it a question or a tip/demo ?

Cheers
