-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsub_query_with_example.sql
More file actions
52 lines (40 loc) · 1.25 KB
/
sub_query_with_example.sql
File metadata and controls
52 lines (40 loc) · 1.25 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use techysanoj;
show tables;
-- you need to create fake entries etc in a new table
select * from ecommerce_sales_large limit 10;
-- 1. find all the customer who are from the same region of customer_id = CUST015796
-- firstly make subquery like
select max(region) from ecommerce_sales_large where customer_id = 'CUST015796';
-- now use this query for outer query
select
customer_id
from ecommerce_sales_large
where region = (select max(region)
from ecommerce_sales_large
where customer_id = 'CUST015796');
-- 2. finding the second maximum total_price and its product_cateogry
-- inner query to find the maximum price
select max(total_price) from ecommerce_sales_large;
-- here we get the second maximum price now to find the product_category
select
max(total_price)
from ecommerce_sales_large
where total_price
< (select
max(total_price)
from ecommerce_sales_large);
-- it can be multiple rows depend on the data
select product_category from
ecommerce_sales_large
where total_price =
(
select
max(total_price)
from ecommerce_sales_large
where total_price
< (select
max(total_price)
from ecommerce_sales_large)
);
-- 3. similarly you can do this for the third maximum price
-- not for multiple row sub query we have to use in, any, all and exits operator