-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQLWithMosh.sql
1364 lines (1210 loc) · 32.1 KB
/
SQLWithMosh.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- NULL(null value)
-- don;t forgate to line end with ";"
-- USE "*" to take all value
-- -- Use single quotes for "LIKE" Clause
## Database is a collection of data stored in a format that can easily be accessed
## Rational DBMS (we stored data in table ) MySQL,oracle
## Non-Relational DBMS ( we dont have table or relation) they have hteir own querry language
######################################################
### THE SELECT Statement(How to retrieve data from single data)
USE sql_store;
SELECT * FROM customers
;
################################
USE sql_store;
SELECT *
FROM customers
WHERE customer_id = 1
ORDER BY first_name
;
#################################
USE sql_store;
SELECT 1, 2
FROM customers
;
##################################################
############################## The SELECT Clause
USE sql_store;
SELECT first_name, last_name, points # Here order is sensitive chainging coloumn order effect coloumn
FROM customers
;
##################################
USE sql_store;
SELECT first_name, last_name, points + 10
FROM customers
;
##################################
USE sql_store;
SELECT
first_name,
last_name,
points,
(points + 10) * 100 AS "discount factor"
FROM customers
;
############ We can use astrisk to select all coloumn ############
SELECT state
FROM customers
;
#############################################
######################################
################## DISTINCT keyword
# With this querry we can retireve uniqness
# By this we can remove duplicate
SELECT state
FROM customers
;
######################################## Exercize
# Return all the products
# name
# unit price
# new price(unit price * 1.1)
SELECT
products.name,
products.unit_price,
unit_price * 1.1 AS new_price
FROM products
;
################################ OR
SELECT name, unit_price, unit_price * 1.1 AS new_price
FROM products
;
##################################################
################################ THE WHERE CLAUSE
SELECT *
FROM Customers
WHERE points > 3000
;
##### Compare items (>, >=, <, <=,=, !=, <>
SELECT *
FROM Cusotmers
WHERE state = 'VA'
;
####################################
SELECT *
FROM Customers
WHERE birthdate > '1990-01-01'
;
################################################ EXERCIZE
------ Get the orders placed this year
SELECT *
FROM orders
WHERE order_date >= '2019-01-01'
;
#####################################################
############### The AND, OR and NOT Operators
# AND (When both condition are true)
SELECT *
FROM Customers
WHERE birth_date > '1990-01-01' AND points > 1000
;
#####################################
# OR(When one of the both condition are met)
SELECT *
FROM Customers
WHERE birth_date > '1990-01-01' OR points > 1000
;
#####################################
USE sql_store;
SELECT *
FROM Customers
WHERE birth_date > '1990-01-01' OR
(points > 1000 AND state = 'VA')
;
########################################## Another loical oprater NOT
# we can use NOT operater negate
USE sql_store;
SELECT *
FROM Customers
WHERE NOT birth_date > '1990-01-01' OR points > 1000
;
########################################### EXERCIZE
# From the order items table, get the items
# for order #6
# where the total price is greater than 30
USE sql_store;
SELECT *
FROM order_items
WHERE order_id = 6 AND unit_price * quantity > 30
;
####################################################
################################### The IN Operator
# we can not add boolean expressionn in SQL
SELECT *
FROM Customers
WHERE state = 'VA' OR 'GA' OR 'FA' #its wrong
;
################################ right here
SELECT *
FROM Customers
WHERE state = 'VA' OR state ='GA' OR state = 'FA'
;
################################ more easier ways
SELECT *
FROM Customers
WHERE state IN ('VA','GA','FA' )
;
############################### One more example
SELECT * FROM customers
WHERE STATE NOT IN ('VA','GA','FA' )
;
############################################ EXAMPLE
### Return products with
# quantity in stock eqal to 49,38,72
SELECT *
FROM products
WHERE quantity_in_stock IN (49,38,72)
;
################################################################
########################################## BETWEEN OPERATOR
SELECT *
FROM customers
WHERE points >= 1000 AND points <=3000
;
#################################
SELECT *
FROM customers
WHERE points BETWEEN 1000 AND 3000
;
############################################## Exercize
SELECT *
FROM customers
WHERE birth_date BETWEEN '1990-01-01' AND '2000-01-01'
;
#######################################################
############################## THE LIKE OPERATOR
###########( % any number of characters)
#######( "_" single character)
SELECT * FROM customers
WHERE last_name LIKE 'b%' #last_name start with B
;
################################
SELECT * FROM customers
WHERE last_name LIKE 'brush%' #last_name start with 'brush'
;
################################
SELECT * FROM customers
WHERE last_name LIKE '%b%' #last_name that means we can have any number of character before or after b
;
################################
SELECT * FROM customers
WHERE last_name LIKE '%y' #In last_name end with "y"
;
################################
SELECT * FROM customers
WHERE last_name LIKE '_y' #In last_name matches single character
;
################################
SELECT * FROM customers
WHERE last_name LIKE '_____y' #In last_name matches single character
;
################################
SELECT * FROM customers
WHERE last_name LIKE 'b____y' #that means last_name start with b after b 4character follow by y
;
####################################################### EXAMPLE
########## GET THE CUSTOMERS WHOSE
##### ADDRESSES CONTAIN TRAIL OR AVENUE
##### PHONE NUMBERS END WITH 9
SELECT * FROM
customers
WHERE address LIKE "%trail%" OR
address LIKE "%avenue%" OR
phone LIKE "%9"
;
###################################
SELECT * FROM
customers
WHERE phone NOT LIKE "%9"
;
#####################################################
##################################################
#############################The REGEXP Operator
## ("|"means or)
SELECT *
FROM customers
WHERE last_name REGEXP '^field'
;
##################################
SELECT *
FROM customers
WHERE last_name REGEXP 'field$'
;
##################################
SELECT *
FROM customers
WHERE last_name REGEXP 'field|mac'
;
##################################
SELECT *
FROM customers
WHERE last_name REGEXP 'field|mac|rose'
;
##################################
SELECT *
FROM customers
WHERE last_name REGEXP '^field|mac|rose' # it means last_name should be start with field or have contain mac or rose
;
##################################
SELECT *
FROM customers
WHERE last_name REGEXP 'field$|mac|rose' # it means last_name should be end with field or have contain mac or rose
;
##################################
SELECT *
FROM customers
WHERE last_name REGEXP '[gim]e' # that meatches any customer who have ge, ie,me in last_name
;
##################################
SELECT *
FROM customers
WHERE last_name REGEXP 'e[fmq]' # any customer who have "e" followed by f , m and q
;
##################################
SELECT *
FROM customers
WHERE last_name REGEXP '[a-h]e' #[a-h] means (a,b,c,d,e,f,g,h)
;
#################################
-- When we put (^) before characters (a,b,c....)
-- we put ($) after characters(a,b,c....)
-- ^ beginning
-- $ end
-- | logical or
-- [abcd] match any single character listed in bracket
-- [a-f] means represent range a to f
#####################################
############################################## EXERCIZE
-- Get the customers whose
-- first names are ELKA or AMBUR
-- last names end with EY or ON
-- last names start with MY or contains SE
-- last names contain B followed by R or U
SELECT * FROM customers
WHERE first_name REGEXP "ELKA|AMBUR"
;
###############################
SELECT * FROM customers
WHERE last_name REGEXP "EY$|ON$"
;
##############################
SELECT * FROM customers
WHERE last_name REGEXP "^MY|SE"
;
###############################
SELECT * FROM customers
WHERE last_name REGEXP "B[RU]" or(br|bu)
;
####################################################################
############################################## THE "IS NULL" Operator
SELECT * FROM
customers
WHERE phone IS NULL
;
###############################
SELECT * FROM
customers
WHERE phone IS NOT NULL
;
################################################ Example
# Get the orders that are not shipped
SELECT * FROM orders
WHERE shipper_id IS NULL
;
##################################################################
############################### ORDER BY Clause
SELECT * FROM customers
ORDER BY first_name ASC
;
################################
SELECT * FROM customers
ORDER BY first_name DESC
;
##################################
SELECT * FROM customers
ORDER BY first_name, state
;
################################################## NOTE
# IN MySQL we can short by any coloumns whether that coloumn in select clause or not
SELECT first_name, last_name
FROM customers
ORDER BY birth_date
;
###################################
SELECT first_name, last_name, 10 AS points
FROM customers
ORDER BY points, first_name
;
###################################
SELECT first_name, last_name, 10 AS points
FROM customers
ORDER BY 1,2 #you should avoid bcz may be in future you add new coloumn in front of first_name
# - So shorting data by coloumn position produced unexpected result something you shoud avoid
# instead by coloumn name
;
####################################################### Example
SELECT
order_items.order_id,
order_items.product_id,
order_items.quantity,
order_items.unit_price,
order_items.unit_price * quantity AS toatal_price
FROM order_items
WHERE order_id = 2
ORDER BY quantity * unit_price DESC
;
############################################
############################## Second method
SELECT * ,quantity * unit_price AS total_price
FROM order_items
WHERE order_id = 2
ORDER BY total_price DESC
;
#######################################################
################################# The LIMIT CLAUSE
SELECT * FROM customers
LIMIT 3
;
##################################### Important
SELECT *
FROM customers
LIMIT 6, 3 # It tells skip first 6 records then picked 3 records
-- page 1: 1-3
-- page 2: 4-6
-- page 3: 7-9
;
########################################################### Example
-- GET the top three loyal customers --
SELECT * FROM customers
ORDER BY points DESC
LIMIT 3
;
#############################################################
#######################################################
########################################## JOINS
# there is two types of joins 1st (INNER JOIN ) 2nd (Outer Join)
# by convention Inner JOIN as can represent as JOIN
# So INNER JOIN is optional
SELECT * FROM orders
JOIN customers
ON orders.customer_id = customers.customer_id
;
##################################
SELECT order_id, customer_id, first_name, last_name
FROM orders
JOIN customers
ON orders.customer_id = customers.customer_id
# When executing this we get error bcz Column 'customer_id' in field list is ambiguous(more than one)
# bcz we have customer_id coloumn in both orders and customers table so mysql
#-- is not sure which table do wanna select this column from that why it saying ambigious coloumn
# so we need to quantify this coloumn by prefixing it with table name we can either pick from cusrtomers table or orders table
;
######### So here is solution
SELECT order_id, orders.customer_id, first_name, last_name
FROM orders
JOIN customers
ON orders.customer_id = customers.customer_id
;
############################################################
### Note:- By Using ALLIAS we can make our code even simpler
SELECT order_id, o.customer_id, first_name, last_name
FROM orders o
JOIN customers c
ON o.customer_id = c.customer_id
;
########################################################### Example
SELECT *
FROM order_items
JOIN products
ON products.product_id = order_items.product_id
;
#######################################
SELECT
order_id,
order_items.product_id,
order_items.quantity,
order_items.unit_price
FROM order_items
JOIN products
ON products.product_id = order_items.product_id
;
##########################################
SELECT
order_id,
oi.product_id,
oi.quantity,
oi.unit_price
FROM order_items oi
JOIN products p
ON p.product_id = oi.product_id
#USING allias it would be
# remember why unit_price coloumn has 2 places the reason for this is that price of product can change
# so for each order_items we wanna have price at the time user or customers placed the order
# so this is snapshot of price at the given point in time the unit_price we have in products table is the current price right now
# we have s unit_price in order_items this is price bcz this is the price at the time of placed order
;
####################################################################
#################################################################
######################################## Joining Across Databases
# How to combine coloumn tables from multiple databases
SELECT *
FROM order_items oi
JOIN sql_inventory.products p
ON oi.product_id = p.product_id
;
#################################### (2ND WAY) or
USE sql_inventory;
SELECT *
FROM sql_store.order_items oi
JOIN products p
ON oi.product_id = p.product_id
;
###################################
USE sql_inventory;
SELECT *
FROM sql_store.order_items
JOIN products
ON sql_store.order_items.product_id = products.product_id
;
####################################
SELECT *
FROM order_items oi
JOIN sql_inventory.products p
on oi.product_id = p.product_id
;
##########################################################
########################################### (SELF JOINS)
#################################
# Note:- So joining a table with itself is pretty much the same as joining the table with an another table
# --- the only diffrence we have to use diffrent allias we have to prefix each coloumn with allias
USE sql_hr;
SELECT
e.employee_id,
e.first_name,
m.first_name as Manager
FROM employees e
JOIN employees m
ON e.reports_to = m.employee_id
;
###############################################################
###########################################################
########################### Joining Multiple Tables
USE sql_store;
SELECT
o.order_id,
o.order_date,
c.first_name,
c.last_name,
os.name AS status
FROM orders o
JOIN customers c
ON o.customer_id = c.customer_id
########################################################### noted
JOIN order_statuses os
ON o.status = os.order_status_id
;
########################################################## Exercize
--
USE sql_invoicing;
SELECT *
FROM payments
JOIN clients
ON payments.client_id = clients.client_id
JOIN payment_methods
ON payments.payment_method = payment_methods.payment_method_id
;
-- ################
USE sql_invoicing;
SELECT
payments.date,
payments.invoice_id,
payments.amount,
clients.name,
payment_methods.name
FROM payments
JOIN clients
ON payments.client_id = clients.client_id
JOIN payment_methods
ON payments.payment_method = payment_methods.payment_method_id
;
#####################################################
###########################################
-- -- (COMPOUND JOIN CONDITION) -- --
SELECT *
FROM order_items
JOIN order_item_notes
ON order_items.order_id = order_item_notes.order_id
AND order_items.product_id = order_item_notes.product_id
;
#################################################
########################################
-- -- -- (IMPLICIT JOIN SYNTAX)-- -- --)
----
SELECT *
FROM orders
JOIN customers
ON orders.customer_id = customers.customer_id
;
-- Below is also Same But this is type of implicit join
-- Implicit Join TYPE Syntax By using
SELECT *
FROM orders, customers
WHERE orders.customer_id = customers.customer_id
;
###############OR###############
SELECT *
FROM orders, customers
;
##########################################
##################################
#########################
-- -- OUTER JOINS --
USE sql_store;
SELECT
customers.customer_id,
customers.first_name,
orders.order_id
FROM customers
JOIN orders
ON customers.customer_id = orders.customer_id
ORDER BY customers.customer_id
;
###############################
#Customer who do not have order or not
-- when we use left join all the record from left table are return whether condition are true or not
SELECT
customers.customer_id,
customers.first_name,
orders.order_id
FROM customers
LEFT JOIN orders
ON customers.customer_id = orders.customer_id
ORDER BY customers.customer_id
;
############################### correct order
-- Note :- you will get same results
SELECT
customers.customer_id,
customers.first_name,
orders.order_id
FROM customers
RIGHT JOIN orders
ON customers.customer_id = orders.customer_id
ORDER BY customers.customer_id
;
-- Noted Point :- you will get all the customers whther they have ordered or not
SELECT
customers.customer_id,
customers.first_name,
orders.order_id
FROM orders
RIGHT JOIN Customers
ON customers.customer_id = orders.customer_id
ORDER BY customers.customer_id
;
############################### Left join
USE sql_store;
SELECT
customers.customer_id,
customers.first_name,
orders.order_id
FROM customers
LEFT JOIN orders
ON customers.customer_id = orders.customer_id
ORDER BY customers.customer_id
;
############################### Right Join
USE sql_store;
SELECT
customers.customer_id,
customers.first_name,
orders.order_id
FROM customers
RIGHT JOIN orders
ON customers.customer_id = orders.customer_id
ORDER BY customers.customer_id
;
################################################## Exercize
SELECT
products.product_id,
products.name,
order_items.quantity
FROM products
LEFT JOIN order_items
ON products.product_id = order_items.product_id
;
###
SELECT
products.product_id,
products.name,
order_items.quantity
FROM products
RIGHT JOIN order_items
ON products.product_id = order_items.product_id
;
###########################################################
################################################
## (OUTER JOINS BETWEEN MULTIPLE TABLE)
SELECT
customers.customer_id,
customers.first_name,
orders.order_id
FROM customers
LEFT JOIN orders
ON customers.customer_id = orders.customer_id
ORDER BY customers.customer_id
;
#############################################
#######################################
####Outer Joins B/t Multiple Tables
SELECT
customers.customer_id,
customers.first_name,
orders.order_id
FROM customers
LEFT JOIN orders
ON customers.customer_id = orders.customer_id
JOIN shippers
ON orders.shipper_id = shippers.shipper_id
ORDER BY customers.customer_id
;
######################################
####
SELECT
customers.customer_id,
customers.first_name,
orders.order_id,
shippers.name as shipper
FROM customers
LEFT JOIN orders
ON customers.customer_id = orders.customer_id
LEFT JOIN shippers
ON orders.shipper_id = shippers.shipper_id
ORDER BY customers.customer_id
;
###########################################
-- its not shhowwing all orders
SELECT
customers.customer_id,
customers.first_name,
orders.order_id,
shippers.name as shipper
FROM customers
LEFT JOIN orders
ON customers.customer_id = orders.customer_id
LEFT JOIN shippers
ON orders.shipper_id = shippers.shipper_id
ORDER BY customers.customer_id
;
#####################################
SELECT
customers.customer_id,
customers.first_name,
orders.order_id,
shippers.name as shipper
FROM customers
JOIN orders
ON customers.customer_id = orders.customer_id
LEFT JOIN shippers
ON orders.shipper_id = shippers.shipper_id
ORDER BY customers.customer_id
;
######################################################### Exercize
SELECT
orders.order_id,
orders.order_date,
customers.first_name,
shippers.name
FROM orders
JOIN customers
ON orders.customer_id = customers.customer_id
JOIN Shippers
ON orders.shipper_id = shippers.shipper_id
;
#######################################
SELECT
orders.order_id,
orders.order_date,
customers.first_name,
shippers.name
FROM orders
JOIN customers
ON orders.customer_id = customers.customer_id
LEFT JOIN Shippers
ON orders.shipper_id = shippers.shipper_id
;
################ IMPORTANT NOTED ###############
##################
SELECT
orders.order_id,
orders.order_date,
customers.first_name,
shippers.name,
order_statuses.name AS status
FROM orders
JOIN customers
ON orders.customer_id = customers.customer_id
LEFT JOIN Shippers
ON orders.shipper_id = shippers.shipper_id
JOIN order_statuses
ON orders.status = order_statuses.order_status_id
;
################################################
################################################
############ (SELF OUTER JOIN)
USE sql_hr;
SELECT
e.employee_id,
e.first_name,
m.first_name AS manager
FROM employees e
JOIN employees m
ON e.reports_to = m.employee_id
-- it shows only manager
;
##################################################
-- so we can solve this problem by using left join
USE sql_hr;
SELECT
e.employee_id,
e.first_name,
m.first_name AS manager
FROM employees e
LEFT JOIN employees m
ON e.reports_to = m.employee_id
;
##############################################
##################### (THE USING CLAUSE)
# USING Keyword only work if the coloumn name exactly same across diffrent table
USE sql_store;
SELECT
orders.order_id,
customers.first_name
FROM orders
JOIN customers
ON orders.customer_id = customers.customer_id ## Is same as USING(customer_id)
;
########### IS SAME AS #############
###
USE sql_store;
SELECT
orders.order_id,
customers.first_name
FROM orders
JOIN customers
USING (customer_id)
;
#####################################
####
SELECT
orders.order_id,
customers.first_name
FROM orders
JOIN customers
USING(customer_id)
JOIN shippers
USING(shipper_id)
;
#####################################
####
SELECT
orders.order_id,
customers.first_name,
shippers.name AS shipper
FROM orders
JOIN customers
USING(customer_id)
LEFT JOIN shippers
USING(shipper_id)
;
####################################
SELECT *
FROM order_items
JOIN order_item_notes
ON order_items.order_id = order_item_notes.order_Id AND
order_items.product_id = order_item_notes.product_id
;
#####################################
SELECT *
FROM order_items
JOIN order_item_notes
USING (order_id, product_id)
;
###################################
USE sql_invoicing;
SELECT
payments.date,
clients.name AS client,
payments.amount,
payment_methods.name
FROM payments
JOIN clients
USING(client_id)
JOIN payment_methods
ON payments.payment_method = payment_methods.payment_method_id
;
#######################################
########## Natural Joins(Do not have control and figure out or guess the join)
SELECT
orders.order_id,
customers.first_name
FROM orders
NATURAL JOIN customers
;
###################################################
#################################### (CROSS JOIN)
#Cross Joins (we use cross join combine or join every record from 1st table with every record in second table)
SELECT
customers.first_name AS customer,
products.name AS product
FROM customers
CROSS JOIN products
ORDER BY customers.first_name
;
###################################
SELECT
customers.first_name AS customer,
products.name AS product
FROM customers, products
ORDER BY customers.first_name
;
##################################################### EXERCIZE
###### Implicit way######
SELECT
products.name,
products.quantity_in_stock as Quantity,
shippers.name
FROM products, shippers
ORDER BY products.name
;
####################################
# Explicit way