-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQL subqueries.sql
More file actions
147 lines (107 loc) · 3.21 KB
/
Copy pathSQL subqueries.sql
File metadata and controls
147 lines (107 loc) · 3.21 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use salesdb;
-- Join tables - Filtering - Transformations - Aggregations
-- Dependency: Non-correlated subquery & correlated query
-- Result types: Scalar, Row, Table subqueries
-- Location/clauses: select, from, join, where(comparision operators & Logical operators)
-- Find the products that have a price higher than the avg price of the products
select
productid,
product,
price
from(
select
productid,
product,
price,
round(avg(price) over(), 0) avgprice
from products)t where price>avgprice;
-- Rank customers based on their total sales
select
*,
dense_rank() over(order by totalsales desc) cusrank
from(
select
customerid,
sum(sales) totalsales
from orders
group by customerid)t;
-- Select subquery: used to aggregate data side by side with the main query's data
-- allowing for direct comparision
-- only scalar subqueries are allowed
-- show the productids, names, prices and total number of orders
select
productid,
product,
price,
(select count(orderid) from orders) totalorders
from products;
-- join subquery: Used to prepare the data(filtering or aggregation)
-- before joining it with other tables
-- Show all customer details and find the total orders of each customer
select
c.*,
o.totalorders
from customers c
left join(
select
customerid,
count(*) totalorders
from orders
group by customerid) o on
c.customerid = o.customerid;
-- Where subquery: Used for complex filtering logic & makes query more flexible & dynamic
-- to filter the data we use comparosion and logical operators
-- When using comparision operators only scalar subquery is allowed
-- Find the price that have a price higher than the average price of all products
select
productid,
price
from products
where price > (
select
avg(price) avgprice
from products
);
-- LOGICAL OPERATOR: IN
-- Show the detais of orders made by customers in germany
select
c.country,
o.*
from orders o
left join customers c on o.customerid = c.customerid
where country in (select country from customers where country = 'Germany');
select
*
from orders
where customerid in(select customerid from customers where country='Germany');
-- ANY operator: checks if any value within a list
-- used tocheck if a value is true for atleast one of the values in a list
-- Find female employees whose salaries are greater than the salaries of any male employees
select
employeeid,
gender,
salary
from employees
where gender = 'F' and salary > any(select salary from employees where gender = 'M');
-- Find female employees whose salaries are greater than the salaries of all male employees
select
employeeid,
gender,
salary
from employees
where gender = 'F' and salary > all(select salary from employees where gender = 'M');
-- Non - correlated subquery: A subquery that can run independently from the main query
-- correlated subquery: A subquery that that relays on values from the main query
select
*,
(select count(*) from orders o where o.customerid = c.customerid) totalorders
from customers c;
-- EXISTS operator
-- Show the details of orders made by customers in germany
select
*
from orders o
where exists(select
1
from customers c
where country='Germany' and o.customerid = c.customerid);