SQL Zero To Hero
SQL Zero To Hero
Gtroup
tte1'1in« J
·Ol2Da? BY e!_!j~ l]
d,t DcSC
nt
SQ
ALIAS AS
fxomRle=:
An Alicu is cna.tcd wtfh ·m · l'11e.1onf,
S€LEcT tolu.mn_n4me f\S aliClS-narne
FROM table .. ncimc~
GtROUP Sy
=) (GttooP sy cotuM tJI
E"Kcirnf?!t:
set~CT column_ nomc CS)
FROM tablt_name
WHERe condih·oo
Gtl?.OUP BY Column_~ls)
ORoei BY colamn_na111c cs>;
~ (H4Vin~ J SELECT' colitmn narwirC.I)
- - · FtoM table name
.£-.a.mple. WHera condition
GcRoUP BY col-."'n-namt 00
HA\'l ll<h Condition
OiD!R 8V tolumn...naM&tl);
➔ fORDeR BY (Ase~ or @RDefl By De~
rht order b~ ~wonl i, UJtd to Sort -f"1.
,.,!tllt -Se~ i1'1 o.sttriid in§ o.- dtsetnt!linJ order
E~nmp.k:
Se LECT colwnni, Columna, •••..
F~oM tciblt-no.mt
ORDE~ B'I coluwnni, tolu.rr,,at,, ..... Asc/DtS~
JOINS
:!J [rtJNeR 'J"OIN ]
'this kc~c.uord Selecfl -that- ho.Ve ma•chin§ VAl.w,
i"' boffi ta.Ya
€-ntmple: Setecr columl\_namces>
-== __. ~RoM hb\ei
:tNN£f101N t'Cll>lct
OM table .column_name= blbtcz. column_ narn,,;
_ _ _ _-.
~ ILeFT 101 NJ "
f-.omp le: SE l ecT t.olllmn_namc C.s)
- = fROM ta.blt.1
LEFT'JOIN tal,k '-
➔ ~ IGtHT!OIN]
ftamp,le : S~lecr cola111n-"6tw1e(s)
... ,=i- =-- FRoM ta~le i
RI Gt MT -JOIN ta.l>le ~
ON taliet.coluthn n4mt =- t:able2. cofumn_rnn,;
-
~ 1-
J: U-L.L_ TI>
_I N]
_____
Example:
Table: customers. Table. orders Answe
Id Name Id Customo SeLecT
Joe
Name as cuttomers
fROM
3
Heny
Sam
Custo mer
WHeRE
Id NoTINC
SeLecT
Customy Id
ROM Orders
String ormmatting:
string fundtiöns are impotant espeoaly wohen
woskin with data that isnt clearn. Thuk, compani e,
may test yau on Stino fomattinß and mani pula ltön
to make `use that you krow how to
data. manipulate
Strin formatting incdudea thine ike:
"EFT, RIGHT CONCAT
" TRIM " UPPER, LOWER
"PosiTION
COA LESCe
" SUBSTeR
Ansuere
SeLecT
a.Id
FROM
Weather a,
Weather b
WHERe
a.Temperatote> b.Tenperátote
AND DATEDIFF (0.Recovd Dale, b. Recoxd Dat)=1
Window Functions:
Windouw functioo dllouw ybu to perform an aiedte
Value on all 700s, mst eod of relkn ory ne (owCuhicl
is what o GROUP By statenert does). Tts euhremay use a
you want to rank os,caleulte cumulatue Sums,,
and more.
Example
depname empno Salary
develop 5R0D
develop 4200
develo p 4500
develop 8
develop 5 RO0
Personnel 3500
Personnel 3900
Sales 3 H800
Sales
Cales 5000
A80D
Answe
WITH Sal_ rank AS
(SeLECT
empno,
RANKC)oveR (ORDER BY salary Des) Tnk
continuss
FROM
Salaies)
SerecT
empno
FROM
Sal_vank
WHeRe
Union:
As a bonus, ttlo s UNIONI ohile it doecnt Come
up aften, you'l be atked abot this the odd tine
and is Sood to knau) in Qenetal. ff you have to0
tables uwith the Lame coturons and you wat to
Con bine tfom, this is ohen youd ue UNIDN.