10 SQL Commands You Must Know
10 SQL Commands You Must Know
(must know)
Case when:
Tt allows you to wrtte compleL Conditioral stolcirents
f you want to allocote a cotain vaue or clas depen
-dino' on othet vara bles. Less Comnonly krown, 2t also allows
-ding
you to pirot data.
Select Disiodt:
SeLecT Dp1STINCT isiS Some thino that you hould aluwoy
haye at the back ot e rcbtemuly Common
Use SeECT DICTI stalements with arcfate
functions (wohich is #3)
fumple
SeLECT
CoUNT (order_ id)/ couNT (DISINCT Custona_id) as
0rders Pes cust
ROM
customes_orders
AQÙreÙate tunctions:
Related to Point #tR, you should have strong
wndercBard imÙ of arefate functiong like min, max,
Sum, count, etc., Thií o mean thal you have
Srong undexstanding of the GROUP 'By ard HAvINOY
ClQuse.
Example : Answer
Id Email SeLecT
Email
ab.om FROM
R cod.com Person
a @b.com GtRoUP By
Enail
HAVING
CoUNT Cenail) > 1
Left jont v, îmer Joint
tor those oho aYe velatiely new to SQL or
hove not wsed it in a while, Dt can be ealy o
mix up Left jount and inner jouns. Make &ur you
clearly undert tond, tow each jont derives difel
Tesulfs.
Self qains :
A $aD Self-join jons a table with itsd.
you riht thimk that Serves no purpose, Buf youd
be Swprised at how coron his is. In many real he
Seltnß, Data is storc d in one lare tade rthes than
many snallet tables. In Such Oses, selfjoins may be
Teayikad to Solve uniqu problems,
Eranple:
Name Marojer Jd
Salary
1 Joe foDDD
Hemy Booo0
Som G00DO NULL
Max 40000 NULL
Ansuer
SeLecT
as Employee
a. Name as
PRoM
Employee a a
TOIN Employee as b on o. Maraa TD= b.TA
WHCRE a.Salary > b.Sdlay
Sub querie3:
A Sub qury ,is also known as an iner quany or
a nested ury, is oa query with in a qury and is
embedded in The WHeRe clouse. this is a
to solve unigue problemt that vequiÝte multiple
quois n aequine in oder pradu a fiven out
Come. Gub qyies and wiTH AS Statamerz ave both
hemdy útin ten quorjing so you choud abolt
ely make Bwe that you know how to use -hem.
thern.
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.