-
Notifications
You must be signed in to change notification settings - Fork 0
/
subqueries.sql
46 lines (31 loc) · 891 Bytes
/
subqueries.sql
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
use mydatabase;
select * from products;
select categoryid from categories
where category_name='phones'; -- 101
select * from products
where categoryid=101; -- full row
-- or
select * from products
where categoryid =(
select categoryid from categories -- subqueries
where category_name='phones' );
-- derived tables
select category_name from
( select categoryid,category_name from categories ) as der -- derived table
where categoryid=101;
-- exists
select * from products
where exists(select *
from categories
where products.categoryid=categories.categoryid);
-- null and 500 is not coming
select * from products;
select * from categories;
select * from products
where not exists(select *
from categories
where products.categoryid=categories.categoryid);
-- in
use mydatabase;
select * from products
where product_name in ('realme xt','redmi');