-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13. Subqueries.sql
More file actions
34 lines (29 loc) · 850 Bytes
/
Copy path13. Subqueries.sql
File metadata and controls
34 lines (29 loc) · 850 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
use ecom;
select * from dim_product;
-- SUBQUERY : Query within query
-- Sub queries are queries written inside another query. They're used when we need to get a result from one query and use it inside another.
-- Example, in dim_product table, filter out those records which are greater than the average price.
select
*
from
dim_product
where
unit_price > (select
avg(unit_price)
from
dim_product); -- inner query will be excecuted first
-- We can also use above subquery as a table
select
*
from (select
*
from
dim_product
where
unit_price > (select
avg(unit_price)
from
dim_product)) as subquery_table -- Treated as in memory table
where
product_name = 'Figure Method';
-- We use subqueries when we want to use multi step transformation.