File tree Expand file tree Collapse file tree 4 files changed +36
-6
lines changed Expand file tree Collapse file tree 4 files changed +36
-6
lines changed Original file line number Diff line number Diff line change
1
+ -- The SQL EXISTS operator tests the existence of any value in a subquery
2
+ -- i.e. it executes the outer SQL query only if the subquery is not NULL (empty result-set).
3
+
4
+ -- Syntax:
5
+ /*
6
+ SELECT column1, column2, ...
7
+ FROM table_name
8
+ WHERE EXISTS (subquery);
9
+ */
10
+
11
+ -- Example: select all customers from the customers table who have placed an order
12
+
13
+ SELECT id, name
14
+ FROM customers
15
+ WHERE EXISTS(SELECT order_id
16
+ FROM order_details
17
+ WHERE customers .id = order_details .customer_id );
18
+
19
+ -- Example: -- select customer id , name AND country of customers whose order amount is less than 12000
20
+
21
+ SELECT id, name, country
22
+ FROM customers
23
+ WHERE EXISTS(SELECT order_id
24
+ FROM order_details
25
+ WHERE customers .id = order_details .customer_id
26
+ AND price < 12000 );
Original file line number Diff line number Diff line change
1
+ -- We can also use the NOT operator to inverse the working of the EXISTS clause.
2
+ -- The SQL command executes if the subquery returns an empty result-set.
3
+
4
+ -- -- Example: select all customers from the customers table who have not placed an order
5
+
6
+ SELECT id, name
7
+ FROM customers
8
+ WHERE NOT EXISTS(SELECT order_id
9
+ FROM order_details
10
+ WHERE customers .id = order_details .customer_id );
Original file line number Diff line number Diff line change 1
- select * ,
2
- case
3
- when age > any (select age from teachers) then ' Declined'
4
- else ' Approved'
5
- end as Status
6
- from students;
You can’t perform that action at this time.
0 commit comments