-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubquery and its operator.sql
More file actions
40 lines (31 loc) · 1.65 KB
/
subquery and its operator.sql
File metadata and controls
40 lines (31 loc) · 1.65 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
-- single row subquery
-- only 1 row return
use sakila;
select amount from payment where payment_id = 2;
select * from payment where amount = 0.99;
select * from payment where amount = (select amount from payment where payment_id = 2);
-- multi row subquery: don't use directly greater than , less than or equals to
-- in operator
select * from payment where amount in (select amount from payment where payment_id = 3 or payment_id = 2);
-- not in operator
select * from payment where amount not in (select amount from payment where payment_id = 3 or payment_id = 2);
-- any operator [use operator : >, <, =]
-- 0.99 , 5.99
select * from payment where amount =any (select amount from payment where payment_id = 3 or payment_id = 2);
select * from payment where amount >any (select amount from payment where payment_id = 3 or payment_id = 2);
-- all operator
select * from payment where amount <=all (select amount from payment where payment_id = 3 or payment_id = 2);
select * from payment where amount >all (select amount from payment where payment_id = 3 or payment_id = 2);
-- ques1. get all the payment information where the month of payment should be same as of payment_id 2 or 3
select month(payment_date) from payment where payment_id =2 or payment_id = 3;
select * from payment where month(payment_date) in (5, 6);
select * from payment where month(payment_date) in( select month(payment_date)
from payment where payment_id in (2, 3));
-- ques2. get all the payment information whose amount is larger than among all the amount of payment_id 2 to 8
SELECT *
FROM payment
WHERE amount > ALL (
SELECT amount
FROM payment
WHERE payment_id BETWEEN 2 AND 8
);