SQL Notes ?
SQL Notes ?
Page-
SaL
SSL, Stuustusad Quty Longunge3 aegtatming
Longuanedorignedto maanage stoteal lata in
Solatienal ntalosos.
SQL_ohaaatis thaauah Simple, dec lazatúe statemets.
h Reubs data accukati and Secuh and halps
naimtain the imtegaityo atalea.s03, 20gaadl03s.oS1
KELATIONAL DATABASE
A selational datalns_is a dataleasa that_etganizes
ionhoimaiainte ene mok talks.
TABLE
A Table ib a cellection o data cigamitl iuto heue
eheked te 28
ano columns laloles ahe Sometm
colals.
_elatioub Hoz taloke is
COLOUMN:
patticulatr type
A colouman ib a te_data alus of 2
th_colgunns
HOO idL, namlamo aag a
BoW:
talole
A Jeu b a Singlo jicshd n a
Justin Biolhe;
Eihat sow in celolss Aasid ok 1 ane e
04e 2 2
DATATYPES:
Tmtlgph
Tene
Dato
Ranl Dwima
STATE MENTS
ba
A Statmentt i a ttt that he dalaknsa
tlcognizes as a alid mmamd
satknints alunyá Wneina
cLAUSE NAME
CoMPONENTS OF A STHTEMENT
Pago-
*CREATE STATEMENTS
CREATE statemmts allou ub t uae a nu taloa
im the datalsase. You Cam use the_cREATE stalememt
Omtime uou uuont te_CuoR a meu ta/ole fhan SchatA
NAME OE
TABLE_
CLAUSE
CREATE TABLE ( s
id INTEGER, Ftat colaumn stous_intagR
MAme TEXT Secend codoumm stotas tet
INTEGER Thisd celoumm stoias imtegen
INSERT STATEMENT
INSER stoumant imsots _a heur ou imte a talalo.
Cam o usod to ado neu ushols
NAME
OF TABLE
cLAUSE
0 INSERI INTOD collosi, name 0q
CLAUSE
NALUES 4Tst
THAT
Bibe 22),
INDICATES VALUE BENG FED PARAMEIEB
IDENTIFYINGG TnE
VALUES BEING
VALVINSERUED
SELECT STATEMENT
SELECTskaemih aho uied tpdh lata an a dotaloos
CLAVSE
eTo QVERY DATA
columns to o talole
reLAUSE
ALTER TAB LE cololsb
ADD CcOLUMNtuitta-hamdde TEXT
h statememt addsa n colwmmtuiten-hamello to.
the caloos taloke.
UP DATE STATEMENT
UPDATe Statemkat aditS a uowy in a teleo. You com
heUPDATE statamomt hom yeu wadb tehamgencistisg
ecols..
CLAUSET
UPDATE calalbs
SET tuitte? hamdlko taylsuilt13
wHEREE
T updoteg the Secod with am il alue of l te haui
the twittnnhende @tauleikuifEL3
DELETE STATEMEN
DELETE FROM statommant dolotes oha moe_ous
aem a talolo aa to dalep Leistimg ecsLals.
DELETE FROMcolalos
WHERE uitten-hamelaib NULL
Tdolctes l Srocodls icolos talbla
tibtenKamello
)Oate
Paga.
LONSTRAINTS
caluma
Consttaits that aldl infohma~ion abeut hauw b
cam beuszd ate iwekeokaltbh Abeciuingthe dataa
type foh_a columm
a be ustdl te te the datakaso to ypet te
Snseatod data that dohn' t oohere to a_cztain zosbeadta..
PRIMARY KEY
Coeunb Lonn be useo to uniguely _identily
the sous tttenpts_
toimeet a oo with_am islemtical alue to a u
uinletion
_ltendy im he taloh uill_Bult_i a cnsteait
uih ll t allou to insat4 O
UNIQUE
NOT NULL
mwst oue_c yaul ADw5 to_a imsot ne yalu Ca
Theb
NoT NULL q Lonts10it ualrtio
DEFAULT
Thy toke am allitionnk asaumtt thot u bo_aAumer
QUEKIES
Quwhyimg i6 ulheiusiong inoimatien stetul in4
dataloase allsus ub to _canmuniat._uith the
datalasi by asking qyutiorb
SELECT TYPE
od to solest all
Foh selectimg 2_cokeumnb, we Saled_inoliunidlual
Colns theinames 80beatiid y a_lma
As
As ib kuyuol m SQL thait alleusS yau toinomg_
a uawwmn o tabl ubing am alias. The heu
name cam b Cythimg isbuti SimglL apotos
Nails
SELECT tools > Nals
GalVES
Nails
FROM msetoyi
Hawma
SELECTDSTINT teols GIVES
Nails
FROM inmentioy
LWHERE
SELEC *
oils
FROM
wHERE inde L
sratimg
CoNDITION
thaSbult Gøt
wth ony taue
LKE -1_
LKE Cam be a usek_hato uhim ye uamt te Compe
Siniln values
SELECT
FROM Condition valuati
WHERE name LIKE Se en t h e name_celi
LIKE -II
IheZsigmibOmethatildcahd charaso that
LIKE
Con ae ubid uith
7is a uildcond cholacto that motcheb zihe e
nuissimq JoziteLb._2
al moias wh stat A
ASwatcheB
omee
%omatohas_alU thot emdls im a
SELECT
FROM
wHERE name
LIKE %man i
abts will inclueleMan
his ismt_Case Sembitie
1S NULL
Umkmeun alues_ae imduicoteo by NULL
NULL aueb camlt he tested
bu compaison optatos
u e usR 1S NULL amo 15
NOT NULL
Suc aS 2 amo
6 L E T mamp
FROM Ymewils
wWHEREimd nadimg 16 NO NULLL-
Grives hames o those ous whic koue Aata ia l e aatig
BETWEEN
in a wHEBE caus0 te tee
BETWEEN Co b ube
the r0sult 6ct withia couum amgl Valueh lom be tet
ouunoehs_eh dates
*IMP BETWEe N 2LETTES ib not2 imcusinto 2lotte
inclusine 2no
BETWEEN 2 NO'S
SeLECT SELECT *
FROM meiab
FRoM wovils
wHERE BETWEEN
WHERE name BETwEENN D AND G; 970 AND 1979;
Guies mpo alooudt all
aimes o ' dout almoikes hi ou
ffhm DEF woies o1970S
AND
multisl Condlitianbm a luHERE
UsRA t e conbine
SELLCT*
OR
AND all_coolikionb ake tel
tuna
OR any colition is
SeLECTX
FRoM
wHERe ea 2o14
OR
ORDER BY
o cam Set the ozults ubinq ODER BY, oithe alphaletily
umaticall
SELECT
FROM
OBDER BY,
CLAUSE
hamj
FROM meviS
wHERE ml Aatg 2
ORDER BY uear DESC,-
LIMIT
IJots ub ebeeiy the mazismum wmoe a teus the
SHesult st wilL hane
LIMIT aluaus gotat the eey enol o the_quueky
Also, t shot Subpoated_in alLSaL dataloase3-
SELECT
FROM mi22
ORDER BY imdl snting DESC
LIMIT 3
CASE
It is
I t allsuus usto cruato olikMoAnt otpuits
ua ohamdLing i-thnid
SELECTT name
CASE
SUMMARY
SELECI i the lauso e us0 d time we a 4k
imematien JLom a dotalbak
AGGGREGATES
aleulatiaxs korfeimtolon multiklo oub oa tabl
ahe calla aggaegalts
COUNT
The fastest_way te oleulat_hau vnany ous _ah a
SELECT cOUNT*
FROM ak-apha
WHERE pice 0-0 Gis o a
appbwkickasafan
SUM
sUM _ib a functien tkat takos the mama o coum
os am osLgme cnd ctuemb the sLm o all tho aluos
a that celwnan
MAXIMIN
The MAx() and MIN wnctiond otukn tho iohert
and omeat alus in clouana
S ESELEC MAXlpice
FROM fako-apka Gines doica
AVERAGE
AVG mcion ib used ino the asnoage walue oa
pabdLiculat clumm
SELECT
AVGLpaice
EROMfakl-a
RoUND
malk0b the esult tabla Lasion to uadL
RoUND kes 2 aguoments insiolo th_hazlnthoz3
olumm nam Cokumm te h2 omdlad e
2 An_Ihtopon The uns tlluwdich i pas to he
roundeel o Intagen iaoAks he
dlocimall placob.
SELECT RoUNDuick,O)
FROMtake-ap
GROUP BY -I
ndutal byuotaml,iu a te knoueaehna
inadle satimg fall maids Jack yonhue use wHERE
SEL ORDER BY
We Jaun to byha
T unonl tnisy we Kove
Date
Pege
GRoUPBY - II
SQL dats_ub uA0 Columa aleunces in ouGRoUP BY
that udL make u inea laetten
2 SELE CTcateno
ice AVA (deulaar) BETTER
AS
ERoMfaka-appS
GROUP BY catapey phuite GRaUP 6Y 1,2-
HAVING
HANING i u SimiIn to WHEREADL t o
wHERE claub COLm be ubed with HAVING
HAVING meb a GRoUe BY ut halee_ORDERAY
amd LIMIT
MULTIPLE TABLES:
The need to uwk_en mlte talolos amd how iE
done
JOIN
stlione salhots_all colums lsam ou nbrned table.
u uant te soluct ctaim celeummá we cam sheciky uich
nR ue um
Socgnd Line sheipwe tallo we utA te lodk
Thinl e use8 TOIN to Say that ue want to
Lmine imeimatien lam odlos with ustem2ih
Founth ime tells_u Kou- to Comsine e tuw talolos
Lwe hae to wate talole 1s_Custome-ia colunm
uth Customehs taloRo 15
custoneh.id cakumman
As coumn ames1s might be cejeatpol _e use the
Sutnn talola-name coumn-nane
SELECT ¥
FROM dAB
JoNCsdomors
ON dets:cusiemk-id=_anm8auston