
















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
SQL short notes handwritten, information technology
Typology: Study notes
1 / 24
This page cannot be seen from the preview
Don't miss anything!
Becauge it^ Can be (^) used to (^) access
amounts of data dineetly uhene tts slored,
aplicat'ons. Compared to speread shet tools, ata
meanS no more
Ihe lolouing^ sqL^ statement^ seleete^ all^ !he Column.s from^ the^ alestable:
"select (^) columhs (^) uise
Example - (^) SELECI year, month
SELECT cuest (^) AS (^) "west (^) Region" FRoM Sales
west.
Ihe LIMIT elause is used to spect'y the number o (^) records to (^) petun
LIMIT 100
WHERE Clause
Example- SELECT * FROM Sales
operators you.^ cuould in (^) Exeel: + (^) -)*, (^) ouever in (^) saL you (^) Can (^) ony (^) perform (^) anithmetie (^) acogg Columons on^ values in (^) agiven (^) row. To
Con (^) only (^) add (^) values in (^) multiple columns (^) Arorn (^) the Same row (^) together asing (^) +-iyou want to (^) odad
9Pegate Panetins.
Exemple -^ SELECT (^) yeor
Example. SELECT (^) Jear, months twest s South,
FROM Sales ;
FROM Sales; Example -^ SELECT (^) year, month
months
west +south- 4year AS new.column
west Southy
west South,
FROM Sales j
wegt +south As (^) South.hsc
CREATE TABLE
anew toble in a.databage.
Example- (^) CREATE (^) TABLE person.l Person ID ints LastName Vanchar (255), FristNome varchar (255),
city varchor (255)
" INSERT INTO
The INSERT INTO SBatement is used to insert neau peconds^ in^ atable.
INSERT INTO (^) table-name (Colunnd, (^) colam2, (^) eolumns,..) VALUES (value 1, value2, Value3, ...);
INSERT INTO table_hame VALUES (value1, \alue2,value3,...);
" UPDATE Statement
The UPDATE stotement is used to madik he existing peconds in atobel.
UPDATE Sales
UPDATE Maltiple Records
SET ContactNane =" Alan", city=" Goa" WAERE Customer ID= 1;
Hig the WHERE clausge that detepmines how many
UPDATE Sales
Notesi
SET Postal Code = 00000 WHERE Cotry =India" ;
Be Carelall eken. upateing recopds. T you omit
Te DELETE statemernt is used to delete exisling pecords in a toble.
Delete Al Recopdg I is (^) possible to (^) delete all (^) roue in (^) atable uthout
deleteing the^ table. (^) This (^) meang that (^) the (^) table StnacBure ,^ attribates , (^) and (^) inderes adl (^) be (^) itact:
Exmgle -
" Altases
DELETE FROM toble_pame
Aliases are used togve atabe, on a colun in atable, atempoary Aliases are ol en ased to make coumn. Tames more peadable.
name.
An (^) alias (^) ony exists Kor (^) the (^) duration (^) olat queny. An aliag is created tuth the hs keyuOPd.
Al as Calumn Fxampe SELECT Column_name AS alias _hame
" Alias Table Fxample. SELECT column.name (s) FROM table_name AS alias-name ;
"IN Operatop SELECT *
FROM Songs WHERE (^) artist IN (^) (Taylor (^) Suif.'Usher)
" BETWEEN Operator SELECT* FROM Songs WHERE year_Pank BETWEEN 5 AND 10
" AND Operator SELECT *
FROM Songs WHERE year = 2012 AND yean Pank <= 10;
OR OPeratop
WHERE yeaR_Pank =5oR artist ="Soru" ;
" NOT OPerator
WHERE NOT Country ="Jopan'
" Combining AND,OR and NOT
WHERE Counny-'Tapan' AND (CiBy- Go'oR cdy='ruri)
" QRDER By
SELECT
ORDER By Country, Customer Name j
SELECT* FROM Sales
"SELECT -- This is select command FROM Sales
WHERE year =2020;
"/"^ Here's^ a^ Comment^ So^ long^ and^ deseniptive that it could^ only^ ft^ on^ mulliple^ lines.^ fortanately it, too, will not aleet how thig code runs. */ SELECT *
WHERE year^ =^2015 ;
Example - SELECT MIN CColumn -name)
FROM toble_name WHERE Condition;
FROM toble.name WHERE Condition
Example :- SELECT Ava Ccolumn-hame) FROM able.name WHERE Con.dition ;
The SQL GiROUP BY clause GROUP 8S ollows you to Separate aata into groaps.,
SELECT year, COUNT (*)^ AS^ Count FROM Sales GAOUP BS year 3
Multile colamn SELECT year, month, COUNT()^ As^ Count FROM Saleg
GROUP BY^ Column (^) numbens
SELECT year, morth, COUNTO AS count FROM Sales
SELECT yeap, month, COUNT)AS Count FROM &ales GiROUP BN yean, month
SELECT Column hames FROM table.name WHERE Condetien GhROUP By column hame
The HAVING clause was added to SQL becuse the WHERE keyuord cannot be used aith aggregate
Syntax CASE
END ;
WHEN Cohdition1 THEN pesult 1 WHEN Condition 2 THENpesult WHEN Condition N THEN pesultN ELSE resul!
Example (^) SELECT (^) OrderID, (^) Quondty CASE
ELSE "The quantity is under 20
FROM 8oes;
Jou'll occasionally^ want^ to^ look^ at^ nly^ the^ tunigue values ina^ particaar^ colunn.^ You^ Can^ do^ this^ using SELECT DISTINCT yntax.
Exomple:- SELECT^ DIS^ TINCT^ month FROM Sales ;
"SELECT DISTINCT year, month
Using (^) DISTINCT in (^) agqreqations. SELECT COUNT (DISTINCT month) AS Ungue-months
MySQL JOINS
FROM Sales;
A (^) TOIN (^) clause ts (^) used to (^) combine rows (^) rom two oron more tables, based ona nelated column between them.
Fxampe: SELECT * FROM benn.calege- lotbal Playens playens JOIN benm. college. Poolball teams team ON teamg. 3ehool_namne =plasyers .sehol
Suppocted Iypes o ToTNS in My SQL "INNER JOIN : Retunns records that have matching Values in. both tables.
INNER
ond the matched recopds rom he night table.
" CROSS^ JoIN:^ Returns^ all^ records^ Brom^ bolh^ tables.
Tges
RIGHT JOIN
JOIN (ables table^ table
LEFT JOTN
CROSS 51Etno.e J0IN
RIGHT JOIN Ihe RIGHT JOIN keyuond petunns all peconds from
the night iable (Aables), and !he nalehig necras ti? any)rom the lelf table Cloble 1).
Example:
(tablei
SELECT Column_name (s) FROM Aable 1 RTGHT JOIN table 2
CROSS JOIN
ON table1. Colunnname =toble 2.columnane ;
The CROSs JOTN kejuord betarns all necovds rom
CROSS JOIN
SELECT colum_name (9) FROM table 1
Asel! Join is a reaular join, bat the table is Joined uith dsel.
SELECTaol umn-name (s) FROM table1 T1 , table1.T
UNTON Operator sQL joins allow yo¡ to cornoine tu catageta Side-by. side, but UNTON allQwS you to staek one dataset on top oPthe (^) other. (^) Put (^) differenlly, UNION (^) allowsyou to
the nesults o ene statemerd digplay in the Same table
Example i C SELECT Qolumn-name (s) FROM table 1 UNION
" SELECT olumm_name (s) FROM Lable 1 UNTON ALL SELECT coumn_nane Cs) FROM table 2;
IN Operator The INopenaBor allous youto SpeciBy meltiple Values in a wHERE ause.
The TNopenaAor^ is^ a^ shorthond^ 4or^ mulliple^ oA Condetions.