Page 1 of 34
SQL Trntaoduction
DataBase i Tt_is_an Ohganized collection of deta
Bo that it Zan be easily acceSšed.
Manage. these database8, D8MS (Dataßase
Hanagemnt Syetem) Ahe used.
DBMS DataBase
ypes DBMS -
of
@pmagramnen-gul
RelationalDBNS
Non- Relational DBNS.
Relational DBNsi- Ih this D8MS,data Stored in.
table FOmat.
RollNO Name cla&s
Jai 5th
2 Aman th
3
Anuj 5th
Ram 8+h
CRelational DBNS)
|Fon Examþle- NysqL,Oacle.
Page 2 of 34
on- Relational DBNS;- Tn this DBNS_data is
Btoned in key-value þai.
RollNo": 1,
class":"sth",
name: Ta"
CNon-RelationalDBNS)
Fos Examþle- NongoDB., Redš.
SQL- I stand for Gtauctused Quey anguage
SQL s used o
ubdate, delete, insent data in
table oh Relational DataBaáe.
SQL
RDBMS
Relationoal
DataBase
@psogammeh qinl.
Page 3 of 34
Date :
|Tage Na :
SQL CREATE Coimandi
I is used jos CrentingTables.
Syatax
CREATE TABLE tobleName(
Column1 datatype,
Column 2 dataube,
SAL KeywOhds_athe Case -insensitive.
Tn NysQL, case-ingehsitiueis an option 4o
CAn
tusn oh and D
Fos Examhle
CRERTE TABLE UBes
FiAstName vohchaây
TNLastNameuahcho,'i
EmoilidNascho;
passiosd Naschah
EisstNomellastNamelEmail idLpoAswOhd
-@pogammen-gin.
Page 4 of 34
SQL TNSERT
INTO Cormmand
It i8 Used to data
inset into tables.
Syatax
INSERT INTO_ tableName Ccolumn
column2
VALWES(value14Value2..
A O o
database table. is knouon as
hecoad
OA a tuhle.
A
column oß database table i8 knoah06 an
attaibute.
@paograminehgini
Eoh Exomple
TNSERT TNTO uSER (Ein&t Name
LOstName,
Emaild,PaBsOhd)
NALUES (Tai, Shanma,
[email protected],
abc #12g)
FinstNamellastName Email.id PassJObd
LTai Shasma labc@gnai, abe n3
Hox to Thsest Nultiple Recohd (hOhijtuble )
(Uaya, Shanma, gagnail.com, abc);
Page 5 of 34
|Fage Na :
-Ay Ghampior
sQL SELECT Command
t is uBed to retieves data trom the table.
SELECI columni, Column2
Erom tableName
To Select comþlete tabhle,JBe. k
(sta)
SELECT*
e
Fom tableName
Examble @pogsammes-qinl
IEinstName LastNannePassNohd
Tai kumas I22i
Singh abc
Arnit shasma
Table '- USER
Command -
SELEC EinatName
Fhom USER
Duthut
FinstName
Arott
Page 6 of 34
Tage Na :
Hou Das SQL
AORK
SQLQuen
Queay Lanaunge Process0n
Pansertobtimize
D&NS Engine&
Eile Nanages t
Thansaction Hanogeh
Physical Database.
Passeng Th this paocessQuesy statement is.
tokenized.
Dhtimisin # ohtimize the best algohithm
fos the byte code.
@pagrammea ginß..
:
Page 7 of 34
|J'age Wa
ERON- H
is Used to sloeciy the tables aom.
Which data etched.
WHERE_ ¥
to iltes secohds based
is Used on.
the givan Condition.
JoIN' 14 is used to combine data fnom
tables based_on a non
Cammon ield.
LGROUP BY E
is U8ed to grou hecobds based
sequinemernt.
s UBed to
|HANTNG H
tten ghoup
-
ORDER BY i6 uBed to Bot the data in.
ascending o descending ondes.
ISELECT : Tt i8 UBed to hetaieves data fhom
4he table.
LTMIT IA ia used to &hecig hoa iany
hoD8ane setuaned.
[email protected]..
Page 8 of 34
SQL
Constsaints
Constsajhts
cHECK UNTQUE DEEAULI TNDEA
NOT NULL PRIMARY. FoRETGN
KEY KEY
@phagammeaaginl
These Constnaints also knouon as Thitugaity
Constaaints..
Page 9 of 34
sQL Constaaints'- Constnaints ahe the ules
and Aesthictionsalbplied on the doata in
lla.
table.
NOT NULL:- Value cannot be Null in
Calumn.
oa
DNTAUE- Value Cannot be &oame in
column..
Qpaaqramnmes ginl
a
pRTNARY KEY:- Uaed to niquely identiy
FOREIGN KEY- Kefe.ences
table.
CHECK- Satispies 0 &hecic condition
DEFADLT Set default alue
CREATE INDEX:- D&ed to sheedup the head
Phocess
Page 10 of 34
Date :
|Tage Na :
Ay Chabion
EROM
AHFRE
HAVENG
SELEGE
-ORDER BYE
@pragammesgal..
Page 11 of 34
|Date :
|Tage Na :
Mly Ghambion
Doanca Beleun LTER
And UPDAE
ALTER
UPDATE
addingdeleting ,.and updating the data
modipying attaibutes in the
existingtable.
the table.
Changes. ane made changes.ahe made
to the table. to the data.
&thuctuse.
By degault, all the Tt sets the apecijed
Values in the tuble Nalue to the tuble
ahe as
initialized Ubdate Command
p
null AlTER
the
Command is úagd.
@pagtamneaginl
Page 12 of 34
Tage Na :
Mly Ghambion
SQL SELECT DISTINCT i
It is used to wetusn only
a
unique valueshom a
&heciied calumn in table.
Systas
SELECT DISTINCT
coumn.hame
FROM_ tablename ,
Examhle
FisstNameLastName Passhiosd
Tai Kumas 123
Singh d23
Amit Shaam0
Table i USER
Command PasgDAd
SELECT Distinct
FRON USER
Outhut
Posshtosd
12.3
-@pagpammea-ginl
Page 13 of 34
Jage Na :
M hambior
SQL WHERE CLAUSE -
H
s used to iltes row8 in a table based
&hecified Condition.
Syatax
SELECTcolumn-hame
FRON table-name
iHERE conditich,
Example'
FisstNome LastName
Jai Kumas
Singh 20
21
Amit Shaama
Table'- USER
-
Command
SELECT EinstName, Laat Name
FROM USER
WHERE
Output EinstName LastName
Amit Shabma
@psagammes-gil
Page 14 of 34
|Date :
Tage Na :
-M Ghampion
SQL
AND -
The AND_oþeratoh
Aetunnstaue i both.
Condition ahe
taueand false otheawise.
Syntax WHERE
Condition1 AND_Condition25
SQL
OR
t hetuhns thue ip eitheh Conditon
is.
tnue,and false i bth conditions ase
false..
Syntog WHERE Conditioni
oRCondition2
Tt hetunns the ophosite of a
Condition.
hHERE N6T Condition?
@phagrammeh-ginl
AND,OR NOT Qhehatohs ahe Used to Combine
Conditions ina whehe clau&e to Cheate mObe
Comþlex Filtesing Conditions.
Page 15 of 34
5QL ORDER BY
4is used to sozt the hesult
in ascanding oh descending ondes.
SELECT Columnl,column2.,
FRON tablename
ORDER By Calumn4 CASCJLDESC]ycolumn2
CAscIfDESCJ,
ASCi- t i8 used to &obt the hesult n ascending, Ohdes
lDESC
-Tt z Used to Bont the aesult in descending
Ohdes.
Exagple
Fi2stNanne Lastame
Jai Kumas 44
Sihgh 20
Amit Shabmo 2.1
Table Uaea
Command'
SELECT k
ERON VBes
ORDER BY
Outhut
FiastName lAstName
Singh 20
Amit Shaàma 21
Kumas
Page 16 of 34
INSERT TNTO -
1+ is L8ed to insest dota into a
table.
Sytaxe
Thseat into tablename(column1,column2,)
Values C valued,value 2,;
Note These must be the &ame numbe o} Nalues.
as the Bame humbeh b columna shecified.
Examble
Eia&tNome lastame
Jai Kumas
Singh
Aait Shasma 20
Table'- Uses
Command
Tnseat into uses CÉiRstName,lastName, lgc)
Nalues ( abe, y2, 25)3
@psagamma-gil.
outhut
EnstName LastNome Age
Jai Kuman
Singh 15
Amit shaahma 20
abe 25
Page 17 of 34
:
|Page Na
SQL NDLL Nalues i
His used to nephesent missing Oh unkhoon
dat.
Note:- Null
is diRRerent rom zer0 0A embtystaing
Tnseat Null walue -
INSERT INIO tablehame((Columndcolumn2,)
VALUES
Cvalue1,Null,.
1o Check fos Noll Values
SELECT e
Calumni,Column2
Fh.om tablename
HERE column2 TS NULL
TS NDT NULL
SELECT Column4column 2
FRON tablename
iWHERE Calumh1 TS NOT NÚLL;
@paogammesgil
Page 18 of 34
Page Na
SQL UPDATE -
Tt is
used to modiy existing
data in table.
Syntax:
UPDATE table-hame
SET Column1= ValuelColümn2=Value2,
WHERE
8ome-Column: Some-Nalue 3
SET Tt to &hbeciy the column and
is used
values toubdate..
Examlale
FiastNanmeLastName Age
Kumas
Stngh 15
Amit Shasma 20
Table'- USeAg
LCommand
DPDATE USERS
outhut EtastNameLastName
Tai kumah
Singh. l6
Amit Shasmg 21
Page 19 of 34
|Tage Na :
My Ghanbion
SQL DELETE
H
is used to_emove existingkecohd fham
a a
tablein SQL Database..
DELETE EROM tablename hHERE_ Condition
Note This obehation is. not sevenBible
, So be
Caheful when using DELETE Statements!
@psogiamardsgi.
Page 20 of 34
|Date :
|Page Ne:
-My Ghampion
5QL
Wildcasds i
wildcands ase
SQL
8hecial chaactess used
in
LIKE' chenato, to Beanch
patteah in a column foaa speciyc
ofa table.
The percent sign (l) heþresents zeso, Ohe
Ot multEiple
chahactes..
The undehscohe sign (-)_aelþaesents one,
Single chahactet.
SQL LIKE
It is used to
q
&eahch fon a
a
sþeciic
pattesn in column of table
Syptax -
SELECT Column1,Column2,
FRON tablename
WHERE colunn-nameLIKE pattesn;
@paagames-gial
Page 21 of 34
SQL TN -
t is
WHERE
used to sþeciy multiple walues
ina
Clause for Rilteaing
data.
SELECT
columnicolunmn2,
FROM table-hame
WHERE Columnname IN CValue1,Value2);
6QL Betneen i
His used to ilter datoa cbased con a
o a wHERE
hange values in clause.
Syntax
SELECT
Columhi, column2;
ERON table-hame
WHERE Column-hame Between
Value1_AND Value 2
SQL Alias' @pAogrammeh gial
T+ is used to qiue a temhonaAy name to
a
table os aColumn_ in a queag
Syuta
SELECT column-hame AS
alaß hame
Ehom table.aname
Page 22 of 34
A
5QL UNTON Oþesatoz
is usedto combinethe hesult 8ets
H
two oh mone SELECT&tatements into a
of
single hesult 8et..
NoTE Tt hetunns only distinct aows.
Syntaxje
SELECT Columnn.hame
FRON table-hame-1.
UNION
SELECT column-hame
ERON table-name-23
Examble SELECTA b SELECTB
3
2 Union
3
@paogsammen-ginl
Page 23 of 34
SQL GROUP BY
H is u8ed to grouhhOJA that
haue the Bamevalues into Summahy
hoWs,Like Find the numben of customehsin
each ciby
SQL HAVING Lt to
is used
the aesult& o ilte
GRoUP BY' queny Based _on the
0.
Jalues o an aggagate quncion.
SQL EXTSTS TAis U6ed to check iga Bubqueny
retuans any houws.
T a
hetuang boolean value.
a
SQL ALL Used
Fishesult to Combane ualue usith
the of
subquesy a
H
retusns taue ip the value is thue oh all elenmenta
SQL ANN- Tt ib also 4 comþanison oþesatok.
T+ hetuàns taue iR the Value ia t,ue
on at Reast one element
@pAogsammea-gial-
Page 24 of 34
:
Tage Na
Sytax EaaOnsi= These occuh uhen SQL&tatements
o the Janguage.the
o
not folloyo Conaect Syntax and 8tauctuse.
Semantic EaAo8i- These ocCuA when the SQL
Statement is ghammatically coaAect, But does nat
paoduce the Hesined sesut due to inconsect
sesult due Lagi.
Con&taaint voilationgi- These occus when the SL
Violotes One oà mose consta aints on the database,.
Data tybe. EAho8- hese 0ccus data ia
when
insested in a ay that does nat mach the
espected data type.
Fos Fxamkle i- Thseat a
sting- into a numenic ield.
a
Taonsaction EOAL i- These occCus ushen
taansaction qails due to pablena ith Iaoking
@pagamme-gial.
Page 25 of 34
SQL JOTNS
SQL JOTINS= Theso gtatements allos us to
access inonmation om ton oh mohe table
ot phce
They
They also keeh Duh database nohmalized
INNER JoIN
LEET JoTN
RTGHT JoTN
FuLL ToIN
@agzamnegial
Page 26 of 34
INNER JOIN
INNERJOTN-T etunns_dataset that ha
matching ualues in both tables.
table1< table2
SELECT
columnhame
ERON table1
INNER ToIN table 2.
ON table1. Column-hame=table 2.. Column-hame
Page 27 of 34
LEET TOIN
LEFT TOTN
ept
-Aetusnsiall Aecords faom th
table and matched econds from the
ight
AZ 2
table
tables t
SELECT
column-hame
ERDN table4
2.
LeET ToIN table
ON
table4. colunmn-name= table2.Column-name
Soibe
Page 28 of 34
KIGHT JOIN
RTGHT
ToIN- TH
hetuxns all secohds thom.
the aight table and the matched hecohds
hom the ept.
table1< table 2
SELECT Column- hame
FRON table1.
ToIN _tabl
RrGHT 2
ON
tablei.column-hame =table 2.column-narme;
Soiþe...
Page 29 of 34
FULL
5/5
TOIN
EDLL ToTN - TH
2etuans all Aecondá when
a
thehe is match in eitheh the left table
ight table.
<
tablei 4able 2
SELECT Column-name
4
IERON table
Full ouTER TOTN table 2..
LON table1.column-hame =table 2.Column-hame
HERE condition..
@paganmegisl
Page 30 of 34
bimag kyi nque. walue
hat is U&ed to idendijya nod.
a
table.
hinking- abaut. hique Constant,
Hhen you must te Knous the iRerence
Unique"constant can Sose. nd0 value alu
Cnnot gtohe null value.
SVlax'
Cheate table tablenamel
clumnl dotatye
columnz datotye
Bumany kesltolumn name).
A
> tableCan cohtain only Ohe
Page 31 of 34
Foruigin Koy- It isa key whichis used to
inkto table toguthes
aluts.
>Foreigh key Can haue multible
Al
@progiamineh.gisl-.
C
Cheate tabletablename
columni datatype
column 2 datatypes
- J.
konagh keylcolumn
name]
Re} ežnces lpumasykoydable
Page 32 of 34
TAKE ExAMPLE
LET'S
Student
RollNDName Addhess.
Prninas RAM DElhi
2 Lalit Mumbal
3 RAM Delhi
Name and AddaeS8 ase hbt uniqul,
Cund not null uaDe
u Rosultt
table
10'/o
90 o Golo 20/o.
10b So
Page 33 of 34
Nthslscince bnglish
20/o.
So21.
You want tocheckRolND 3nasks jn.
maths,FohLign key Ccme into pictuse.
Stuclent table kesut table.
Ne have to add phimag K
Result table which act
fo reign
Mathd Sjence EngliahRallNp
2)%
Page 34 of 34
Commit and Rollback
> Commd and Rollbackane dsansaction.
Statements.
Commit -
T1 is used to
pesfesmedbyo Jansdctin.
Loimit ytas:=
CorMIT
Rollbacki
txansaction.
Rollback synta
Roll Backj