Download as pdf or txt
Download as pdf or txt
You are on page 1of 57

alssMAte

Date
Page

SQL Interview a/A


Ahat ae ell the diffexent novmalfzations
Data base noxmalízations can be easily
undexstood uith the help of a_case study. The
noxmatoxms can be divided into 6 foxms, And
heu aYe explained below:

1st nd 3rd Boyce4th 5h 6th


hoxma normalnoxmalcoddnfnoimal nofmal noxma
Fom form foxm form Poym.form
Data base Normal foxmsS
zite an SOL QUeru to fetch the count of
emplouees NOYknO in 2 rolect R1
Heie, l e nlbuld beusinq aggxegate unction count
) mith the SaL Wlhere clause

SELECT CouNT (K)_ PTULKmaL UNKEDI)


NOTES CMGRY CTEERANA).
FROMEmplouee SalaYu
WHERe Poroject P1;

e an SaL QUesy to fnd the emplouee ld lhose_


calaru les în range o 600 and 15000
Here, wle can use the Betueen 'opexatox i th a
ahese clause
SELECTEmp [dd SalasyL
FROM EMPloyee Sajayuy
lNERE SalaryBETEENo00 ISooo
1
elassMate
Date
Page

Aite an SaL query to fetch all those eMplouee


LAhose on_2rolecE_otheY thanP1
Hexe Jle can use the OT operatox to Fetch
the roS nihich are not satiskging the given
Condibon MuL KomRLINKGDIn)
NOTES GLERYTELE M).

SELECT EMP rD
FRoM Employee Salavy.
InlERE NOT PoJect = PL";
Ox using he not equal o Operaox
SELECT EMP ID
FROM Employee Sal ar)
W ERE Projec<>P1

e an saL dueu to dis play he total Salary


Of each emplouee aAding -the Salau leith
Vasioble value
Hese, e can simply use the E'opeya kor in SQL.
sELECT EMP Id
Salary t yariable as ToralSalaryy
FRoM EmPloyee Salary

zite an seLquexu_o Perch the eMplouee full


name and yeplace he S12Qce uith
Dsing Keplace unction=
SELECT REPLACE Full Name, ')
EROM EmPloyee Detaîls
clAsSMAte
Date
Page

ite an s&L aueru to fetch the EmpldS that aye


2xerent in both the tables-_
Emplouee Detalls Qnd Employee Salaxuu
using Subguery
SELECT EmpTd FROMM
EmPlouyee Detalls
here Empld IN
(SELECT EmPId FRoM EmPloyeesalQry);

slte a saL quesu to fetch he Emolds that


a e p7e sent in_Emplouee Detalls but not in
meloUee selayu.
Using Subuexy eTuL IomeR LiNKEDIn).
NDTES GMLERY CTELCYRN)

SELECT Emprd FRoM


EmPloyee Detal|s
Whese Emp Id NOT IN
(sELECr Emprd FRoM Employee S al@yy);

Wite an saL Queru to display hoth the Empld


and angaeY Td oa ethex.
Here ale can use the coNCAT Command.

sELELT CONCAT CEmprd,Manages Id) as Neo rd


FRoM Employee Details

3
sSMAte
Date
Page

i t e an SQL auexu to upp eycasethe name o9F


he Emplouee and louseYcase he city Yalues.
l e can use sQL Uppe1 and Lower ancHonso
achieve the intended results.
SELECT vPPER CFull lame), LonlERCiy_
ERoM Cmployee Details

ite an saL quexU o u12date_he enMployee


names bu emoving leadina an d alling Seace
Using_he 'Dpdare Command 9lth the 'LTRIM
and RTRTM function.

UPDATE5 EmployeeDetalls
SCT FullName LTRIM (RTRIM
= CfulU Name)) >

ite an saL query to ind the CuYent


dare- Rme
My SaL PMUL KomRULINKGP N):
NOTES aMLERY TEEUR).

SELECT NoLO C 2

SQL SEWes-

SELECT Cet dat );

OYacle
SELECT SySDATE FROM DUAL
elAsSMAte
Date
Page

etch_q the eMplouees Aho arenot 0Yking


On einu20iec
his is one ofthe vey basic inteniew
auestlons in which the înteyyiewey Alants to
see if_ the 2erson Knos about the commenlu
used -TS NULL opexatoy
SELECT EmPtd
FRoM EmPloyee Salary
WMERE Project [S NOLL,

Write an oL queYu to fetch allemplouee yeoYds


om emploueeDerailk trable lNho havea alaru_
recoyd in Emelouee alaru able.
Usina Exists' eTuL KeompR LNICEDIN)
NOTES qLERYCTELEYKAM)

SELGCT& FRoM EmPloy ee Oetails E


HERE Ex1STs
CsELGCT FRom Employee Salary
WyERE E. Emprd =S. Emptd)

5
alAsSMALe
Date
Page

SQL DATABASEPART-4
1 What is Database ?_
A database is an Orqanized collection Of data,
Stored and xetxieved digitally fyom remote
Ox lOcal computex sustem. Databases can be

vast and complex, and such databases are


developed using flxed design and modeling
approaches
TUL LUmar LinKEDIN)
NOTES qLLEZY CTE LEGRAM).
Nhat is DBMS ?
DBMS stunds fov Database Management Sustem.
DBMS is a system software xesponsible fox the
creation, etrieval, updationand management
of the database It ensuxes that ouy data is
Consistent osqanized and easily accessible
by seyving as an interface between th e data basel|
andits end-useYs ox appücation so Ftuaxe
3.What is RDBMS? How is it diffexent fyom DBMS?
RDBMSStands for Relationgl Data base Management|
Sustem The key difference heye Compare d to
DBMS is that RDBMS Stoxes data in the fovm
o f 9 collection of tables , n d xelations can be
defined betueen the common elds of these
tables Most Modeyn database management Sustem
Ke MyS0L, Micyosoft sQL SeYueY, OyacleIBM
DB2, and Amazon Redshift aye bQsed on RDBMS

ATuL HumR (uNKEDIN)


elASSMAte
Dafe
Page

STUL AUmaR CLINKEDIn).


NOTES ymLERy (TELEURAM).
4 Ahat is SaL 2
SOL stands fox Styuctuxed ueyy Lanqugge It is
the Stand@xd lanquaae used to maintain the
xelationall database and pex form_many diffexent
data manipulation Operations on the data- SGL-
was initiatly invented in 191O It is a dgtabase
tanguage used foy database CYeation deletion
Fetching and modifying xows,etc Sometimes, it
is pxonounced asSequel We can also use it
to hand le oyqanized datq comprised of entities
Vayiables) and yelations between different entities
of the data

5lhat is difference between SQL and MySGL?


SCL MySOL
SOL is G Standayd MySQL is a database
languaqe which stands manaqement Sustem.
For Structured Quexy
Languaqe)based on the
Engiish Language
SOL fs the core Of the MysQL is an_RDBMS
elational datQbase LRelational Database
INhich is used for Management System)
accessing and managing Such As SQL Seyvey,
darabases Infoxmix etc.

ATULHOYmar urKEDIn)
clASSMAte
Date
Page

6 Alhen SOL appeared?


SCLExst appeayed in 1974 is one of the most
used lanqu ages for maintaining the relationa
database In_986, SQL became the standayd
Of the American National Stan dayds nstitute
ANSI) and ISo (Inteynational Oxgan'zation for
Standardization) in 19831
ATUL KUmeR LinKEDIN)
NOTES CGALLERY (TELELRAM).
1What îs q Self Join?
A Seif-oin iS q tupe of join that can be used
fo connect fo0 fableS As Q esult, if is Q UnaXy
xelationshîp. Each xOw of the table is a attach ed
to itself and al1 other ons of the same table
in a self-join As q esult a self-join is mostly
used to combine Qnd Compaxe_OWS_fYom the
Same data basetable.
8 What Qre the usaQes of SOLY
SOLiS_Yesponsible_for maintaining the yelational dqta
and the datg styuctu res present in the database
Some of Common usages are aiven belowN:
To execure queries aoajnst Q database
Toxetrieve data fyom a doatabase
To insexts yecords fn a database
To updgtes ecords fyom a database
To delete yecords fom a_data bas e.
To Cveate new databases
To cyegte neu tables în a database
To Create views in databa se
T6 pexfoYm conmplex operation s on the database
ATUL naaLunKEPIN) 3
elAsSMAte
Date
Page

G.hat axe Tables andFields?


lables A toble is an Orqanized cO11ection of
data stOxed in the foYm of yoNs and
Columns Colunmns can be cateQorized as
vextica1 and 0 S gs horizontal

Eields he columns in G table are called FieldSs


hile the xons can b e yefermed to 9 s
xecoyds.
ATUt KUTMAEtUNKEDx
NOTES CaALLEKYTELELR ANM).
10.List the diffevent tupes of yelationships in SOLY
Theye are different types of yelations In he
database
One-to-One t his is q Connection between two
tables in which egch xecoyd in one table
COYresponds to the maximum of one
Yecord in the othey

One-fo-Many and Many to:0ne This is the most


fyequent connection n hich a ecoyd in_one
toble is tinked to several yecoyds in another

Many to-Many-This is used when_definino a


Yelationship that yequires seveyai instances
On each side

Seif-RefeYencIng Relarionshlgslhena table has to


declare a connection ulth its elP this is
the method tO employ

AruL ki0m (LINKEDIN).


elASSMATe
Date
Page

SQL- DATABASE.
PART-2

Nhat is the SELECT statement?


A SELECI command aets zexo 0Y more xowsS
fxom one ox more databa.se tables oz views
The most freguent data Manipulation langugge
OML command is SELECT in_most applications.4
SELECTqueries define a yesult set but not ho Fo
calculate it, becquse SOL is a declayative
pyoqygmming 1anguage
What Qxe some common clause used with
SELECT Queyuin SQL
The follotoing axe Some frequent SCL clauses used
inconiuction with a SELECT Gueyy
WHERE clause i In SOL, the WHERE clause is
used to filter yecoxds hat aye xequixed
dependino on ceytain _cxiteyiq

ORDER BY clauseThe ORDER BY clause in SQL


isused to Sovt da ta in ascending ASC) oY
descending DESC) ordey dependinq on specific
fteld()(DEsC). eTUL KUmaRLINKEDIN)
NOTES qmLLERy CTELEqRANM),

OROUP By clauSe CROUP BY clause in SQL is used


to ayoup entxies with identical data and may be
used l t h aggreqation methods to obtaîn
Summaise.d datgbgse Yesults
Continue
ATUL UmRunKEDI), 14
clASSMALe
Date
Page

HAVING clause_i- The HAVING clause in SQL 1s used


to fltey yecoxds in_combination uith the GRODP 15y_
clauseIt is different fYom WHERE since the nlHERE
Clause cannot filtey aa qreqate d yec0 Yds

3 Does SQL Suppo7t pyoayamminea lanauage


Featuyes?_
SSL efers to th e Standard uery Lanquage
Iherefoxe,it is true that SQL i a language hut-
does not actually support the proqxamm ing
lanqua ge It is 9 common language that doesn't a
lo.Dp Conditiona statements, and 10gical Opexations
It cannot be used roY anuthing Other than data
manipulation It is a command language to pexfoxm
database opeyations. he pximary puypose of SaL.
is 4o xetrieye, maniputete, update, delete, and
pex Poxm_complex operations liKejoins on the dara
present in fhe database.
ATUL 4Umar lLNKEDIN),.
NOTES GALLERY (TELEQRAM)|
4 Ahat are_Constraints in SOL?
ConstyQints are used to specify the ules
Concexning data in the table If can be applted
for single or Multiple fieldsin an SQL tQble
during the creation of the table or Qftey
Creating usinq the ALTER TABLE Command
he consraintS are

NOT NOLL Restxic.ts NULL value from being


fnsested into column

ATUL Um LiNIKEDIN)
Continue
elASSMAte
Date
Page

CHECK- Vavifies that au values In a field Satisfy


a Condition

DEPAULT Automatically assigns default value


Ifno value has been specified for the field
UNIQUEEnsues unigue values fo be inserted
into the field

TNDEX-Indexes a field providing faster fetrieva|


ofxecOYds. ATuL AumaR uNKEDin).
NOTES qmiGRY TELERM).

PRIMARY KEY Uniquely identifies each recoids in


a table.

FOREIGN KEY Ensuyes xe fesential integsity for a


ecoid in another table
5 What axe the Subsets of SOL
he foll0wing are the fouy sianiftcant subsets
Of the SQL

Data de finition anguage (DDL)-It define.s the


data tyuctuye that Consists Of commands like
CREATEALTER DROP etc.

Data Manipulatfon langugge(DML It is used to


Manipulate existinq data in database. The comman d3s
n thls categ ozu axe SELECT OPDATE, INSERT erc
Continue
ATuL KUmR (LINKEDIn) 3
clASSMALte
Date
Page

Data Conttrol LanQuage DCL)-1E ContYols accesS


fo the data stOsed o the database The commands
i othis categosy include GRANT and REVOKE.

xan saction Control Langugge TCL) Tt is used to


deal nlth the tyansaction_operarlons in the dafabase.
he commands In this CategOXy Qre COMML ROLLBACK
SET TRANSACTION, SAVE POTNT ete
6 Alhat aye UNION MINUS and INTERSECT comman ds
The ONIoN Ope1atoy is used to combine the results
of tw0 table5 hile also emoving duplicate entsies

TheMENUS opexator Is used to 1etuin YOWS from


the elrst Query but not from the second ueYy
The NTERSECT operator is used to combine the
results Of both queries into a Singie yOwis.

Before 1unning elther of the above SOL SBatements


Certain yequl1ements must be sqtisfied

Ithinthe clause each SELECT qUeyy must have


the sa me amount of Columns

The data types in the columns mus also be


CompaYable. ATutkum RLINKEDIN)
NOTES GALLERY CTELERAM),

In egch SELECT Statement the columnS must


be in the same ordey.

ATUL maRLLINKEDIN)
elAsSMALe
Date
( Page

hat Is a Primary keu


h e PRIMARY KEY constralnt uniquely identifles eqch
YON in a tablel t must contatn UNIQUE Values
and ha.s gn implicit NOT NULL Constyaint. A table
in saLis stxictly yestyicted to have one and only
one primary Key, which is comprised of single
OY multiple felds Ccolumns)

CREATE TABLE Students Create table vwith a


Singie Aetd as_primary keu
IDINT NOT NULL
Name VARCHAR (255)
PRIMARY KEy (ID ATuL kwmR UNKEDI)
lOTES qALLERy CTELEURAM).

CREATE TABLE Students C/* Create table i t h


multiple flelds as
primayu key
ID INT NOT NOLL
Lastnlame VARCHAR (255)
FirstName VARCHAR (255) NOT NULL
CONSTRAJNT PK-Student
PRIMARy KEY (ID Firstaame)

ALTER TA BLE Stud ents/* Set a column as primavy


key
ADDPRIMARY KEy (ID);
ALTER TABLE SFudents Set multlpte columns as
primary Key
ATUL HUmk LnKEDIn) Continue 5
alasSMAte
Date
Page

ADDCONSTRAINT PKStudent/Alaming o Primaryy


Key
PRIMARYy KEy (ID, FiystnName)_
Ialrite a Sql statement to add the primary key
t-id to the table teqcheys'
Nxite SQL tatement to gdd the pyimayy Key
constyaint pk-a foy table 'table-a and fleids
col-b col-C'

Ahat iS the purpose of DDL Langugge


DDL Stands fox Data Definition Language I is
the subset of g datobgse that defines the data
stucture of the databa.se uhen the database 1s
CYeated Fox exgmpie, We can use the DDL Command
to add xemove OY modify tables.It consists of the
followinq commands: CREATE ALTER and DELETE
database objects such qs schema, tables, indexes,
view sequence erc

Example
ATUL 4umaR (LNKEDIn)
NOTES cULERY(TELEURAM),|
CREATE TABLG Students

Roll-no LNT
Name vARCHAR (45)
Branch VARCHAR (30),

TUL mt(LnKEpIN)

You might also like