Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
156 views

SQL Notes ?

SQL is a language used to manage relational data in databases. It uses simple declarative statements to insert, query, update and delete data. SQL ensures data accuracy and integrity and helps maintain the structure of tables, rows, and columns. Key elements of SQL include tables, rows, columns, data types, statements and clauses. Statements like CREATE, INSERT, SELECT, UPDATE and DELETE are used to structure and manipulate data in a database.

Uploaded by

Lokesh Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
156 views

SQL Notes ?

SQL is a language used to manage relational data in databases. It uses simple declarative statements to insert, query, update and delete data. SQL ensures data accuracy and integrity and helps maintain the structure of tables, rows, and columns. Key elements of SQL include tables, rows, columns, data types, statements and clauses. Statements like CREATE, INSERT, SELECT, UPDATE and DELETE are used to structure and manipulate data in a database.

Uploaded by

Lokesh Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Date

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

CREATE TABLE talola-rame


oelumn-1 data-typ
lolumm-2 dataLpey
pApta3 dote type
A A AA

CoMPONENTS OF A STHTEMENT

CLAUSE lauss btohen spaalic tasksiS


by rwentio clauso5 ae utlOn m
capital JtRA CUauseb_anialbo k selleta
t 25 Commanels

NAME Mae f fabe prtmod jb be adplie

3) PARAMETER A akaanaten ba list o oumn, datatya


yau that oL passac to a clousl as _an ahgumenat
Dats -

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

SELECT hame FROM_colles


satuns all data in the name alsnon of tha
calokos talola

ib a shocial wildcas.o chasactot_we'ye been ubisngp


o l l o u you te Soleet we comm im a taloL
utheul honing te homi ach inoliwiclualy
ALTERTABLE STATEMENT

adde a mew ceumm te


ALTERTABLE statomanat
whem we am to aly
e Vo com use thib Lommamd

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

Hane a_AKote aluo to e uou


canMane
Similar t kz PRIMARY KE Y_wrenpt a tab
mamy d i o r t UNIQUE Colsummb.

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

doebot sheiy a oluo f thak Celummm


DaD.
Pago-

QUEKIES
Quwhyimg i6 ulheiusiong inoimatien stetul in4
dataloase allsus ub to _canmuniat._uith the
datalasi by asking qyutiorb

SQL was imitialy SEQUEL


Staucuaud QunnguAg StAucted Eno Quehyg

SELECT TYPE
od to solest all
Foh selectimg 2_cokeumnb, we Saled_inoliunidlual
Colns theinames 80beatiid y a_lma

SELECI coluomn-1 columm-2


FROM talaoMOmD

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

SELECTL nome AS TUlo


FROM

Nome tho calumn is o Titla


DISTINCT im Hho . t
DISTINCCI h used to e t u n
miajue values autput
Hawme

Nails
SELECT tools > Nals
GalVES
Nails

FROM msetoyi
Hawma
SELECTDSTINT teols GIVES
Nails

FROM inmentioy

LWHERE

Used to Sustaict auwu ALIS.

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

Opato wlh uHERE


to Spastck e spackfie
pobeun in celumm
Dato
Pog

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*

FRoM meviLb AND1999 1


BETWEEN 1990
WHERE uenh cOND
AND
qam Amamco 2
2COND

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

DESCE Keyued used in dor BY b SA in dozctrdirg trla


ASC uueolusao in ORDER BY to art axindig or

talunya gots akker wHERE.


SELECT*

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

wHE omde ationg 3 THEAFantosti "


nakjq>6 THEL Foo
Rees
wHEN inn
ELSP
END
RoOM

SUMMARY
SELECI i the lauso e us0 d time we a 4k
imematien JLom a dotalbak

A S mam a Csum o talok

DISTINCI rotum U e aluOS

wHERE ib pebula_coano thak ots you flh the


abults o the aswiy babed en (enditioak that yL Spii

LiKE amd BETWEEN shacinl epekato

A ND am OR Comdnn3 mutiblo Conditiers

RDER BY soAks tho_ usult.

LIMIT speciios he mau eus tihet 4ui


Deto-
Page-

AGGGREGATES
aleulatiaxs korfeimtolon multiklo oub oa tabl
ahe calla aggaegalts

COUNT
The fastest_way te oleulat_hau vnany ous _ah a

table a te e the coUNI) £unctien

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

SELECTSUM douunlend) Cuves


FROM faki-apbsj T numloer
Deunleads

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

SeLELT AVGmd satin)


FRoM esilb BETERAS AVGimd saimn
wHERE yah 1999, FROMmevies
GRoUP BY

SEL ORDER BY

wMGRE yeah 2000

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

SeLECT bice, RoUNDALdowlndi) LounT).


FROM fake-app
GRovPB
BY1
MAVING cOVNT 10 ddl a cleuso
SreRtucE e
te
queey t o
)Deto
Pago-

MULTIPLE TABLES:
The need to uwk_en mlte talolos amd how iE
done

CoMBINING TABLES MANUALLY


Netumgthe data ef a talele and then checku
another talele fo lothespending data ib_La
jsining ue- taloles

Cemleinionq taloles mamually is timc_ ca&lumin


w USe a G0q mcL collled JOIN

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

You might also like