aboutsummaryrefslogtreecommitdiff
path: root/content/posts/postgres-aggregates/subqueries.sql
diff options
context:
space:
mode:
Diffstat (limited to 'content/posts/postgres-aggregates/subqueries.sql')
-rw-r--r--content/posts/postgres-aggregates/subqueries.sql28
1 files changed, 28 insertions, 0 deletions
diff --git a/content/posts/postgres-aggregates/subqueries.sql b/content/posts/postgres-aggregates/subqueries.sql
new file mode 100644
index 0000000..38efccd
--- /dev/null
+++ b/content/posts/postgres-aggregates/subqueries.sql
@@ -0,0 +1,28 @@
+EXPLAIN (
+ ANALYZE,
+ COSTS,
+ VERBOSE,
+ BUFFERS
+)
+SELECT
+ products.id,
+ products.name,
+(
+ SELECT
+ COUNT(id)
+ FROM
+ reviews
+ WHERE
+ product_id = products.id) AS num_reviews,
+(
+ SELECT
+ COUNT(id)
+ FROM
+ orders
+ WHERE
+ product_id = products.id) AS num_orders
+FROM
+ products;
+
+-- WHERE
+-- products.id % 10 = 0;