Sie sind auf Seite 1von 23

SQL Queries

This PPT includes

Creating a database

Creating a table

Constraints

Inserting values in the table

Retrieving information from table

Working with multiple table



Introduction

Database Name: Institute

Tables In database:
1) at!h"detail : bat!h"no# bat!h"name# trainer# duration
$) fa!ult%"detail : trainer"id# trainer"name# &ualifi!ation#
e'perien!e# !onta!t"no
() student"detail: student"id# student"name#!onta!t"no#
bat!h"no# bat!h"name# address

Creating a database

)%nta':
Create database database"name

*+g+
!reate database Institute,

Creating a table

)%nta':
!reate table table"name
-!ol1"name#!ol$"name#.#!oln"name)

*+g+
create table batch_detail
(
batch_no integer PRIMARY KEY
batch_na!e "archar #$IQ#E
trainer "archar
batch%duration integer
&


Constraints

Constraints are used to limit the values entered into !olumns of


a table+

T%pes of !onstraints
1) N/T N011: 2revent a !olumn from
a!!epting N011 values+
$) 0NI30*: *nsures uni&ueness of the
values in a !olumn+
()2RI45R6 7*6: )ame as 0NI30*# but onl% one
table per !olumn is allowed+
8)C9*C7: Controls the value of a !olumn being inserted+
:) D*;501T: 5ssign a default value for the !olumn+
<) R*;*R*NC*): 5ssign a ;oreign 7e% !onstraint+

Inserting data in to a table

)%nta':
insert into table table"name
=alues-!ol1"val#!ol$"val#.++!oln"val)
*+g+
insert into table batch_detail
"alues( '( )Testing_'(**Ra+i" Arora*,&
-to insert !ulti.le ro/s use this 0uer1 2or an1 no o2 ti!es3

Retrie"ing the in2or!ation

)%nta':
)*1*CT >!olumn"list?
;R/4 >table"name
@W9*R* >!ondition?A
@BR/02 6 >!olumn"name-s)?A
@95=INB>!ondition?A
@/RD*R 6 >e'pression?A

45ERE Clause

W9*R* Clause is used to spe!if% the


!ondition+

W9*R* !lause is !ase sensitive+

/perators are used to spe!if% the !ondition+


1)Relational /perators: C# ?#>#>C#>C#>?#DC
$)1ogi!al /perators: 5ND#/R# N/T
())pe!ial /perators: IN# *TW**N#1I7*#I)

Continue6

)%nta':
sele!t E from table"name,
sele!t !ol1"name#++ Coln"name from table"name,
W9*R* >!ondition?

e+g+
select - 2ro! batch_detail
45ERE Trainer 7)Ra+i" Arora* 8

Result9
( testing_'( Ra+i" Arora ,

:R;ER <Y Clause

/RD*R 6 !lause is used to impose an


order on the result of a &uer%+

It is used with )*1*CT stmt+

/ne or more !olums andFor e'pression !an


be spe!ified in /RD*R 6 !lause+

Continue6

)%nta':
)*1*CT >!olumn"list?
;R/4 >table?
W9*R* >!ondition?
/RD*R 6>!olumns? @5)CGD*)CA ,

=R:#P <Y Clause

BR/02 6 !lause is used to divide the rows


in a table into smaller groups+

)31 groups the &uer% after it retrives the


rows from a table+

Conditional Retrieval of rows from a grouped


result is possible with the 95=INB !lause

Continue6

)%nta':
)*1*CT >!olumn"list?
;R/4 >table"name?
BR/02 6 >!olumn-s)
95=INB>!ondition?

*+g+
)*1*CT E ;R/4 bat!h"detail
BR/02 6 bat!h"name
95=INB trainerCH4r+ RaIiv 5roraJ ,

Quer1ing Multi.le Tables

Collating Information-Ioins): 5re used to


!ombine !olumns from different table+
T%pes of Koins:
1) *&ui Koins
$) Cartesian Koins
() /uter Koins
8) )elf Koins
:) NonL*&ui Koins

Continue63

)et /perators
1) 0nion
$) Interse!t
() 4inus

E0ui >oins

When two tables are Ioined together using e&ualit% of


values in one or more !olums+

)%nta':
)*1*CT !olums"list ;R/4 tab1#tab$
W9*R* tab1+!ol"name C tab$+!ol"name ,

*+g+ :
SELECT student_id student_na!es3batch_no
batch_na!e trainer
?R:M student_detail s batch_detail b
45ERE s3batch_no 7 b3batch_no8

Cartesian >oins

When no Ioin !ondition !lause is spe!ifed in W9*R*


!lause# ea!h row of one table mat!hes ever% row of
the another table+This results in a !artesion produ!t+

)%nta':
)*1*CT !olumn"list ;R/4 tab1#tab$,

*+g+
SELECT student"id# student"name# bat!h"no# bat!h"name#
trainer ?R:M student"detail # bat!h"detail ,

:uter >oins

If there are an% values in one table that do not have


!orresponding values in the other table# in an e&ui Ioin that row
will not be sele!ted+)u!h rows !an be sele!ted for!efull% b%
using the /uter Ioin s%mbol-M)+

)%nta':
)*1*CT !olums"list ;R/4 tab1#tab$
W9*R* tab1+!ol"name C tab$+!ol"name ,

*+g+ :
SELECT student_idstudent_na!es3batch_no
batch_na!etrainer
?R:M student_detail sbatch_detail b
45ERE s3batch_no (@& 7 b3batch_no8

Sel2 >oins

To Ioin a table to itself means that ea!h row


of the table is !ombined with itself and with
ever% other row of a table+

This !an be viewed as a Ioin of two !opies of


the same table+

)%nta':
)*1*CT !olumn"list ;R/4 tab1#tab$,,

SET :.erators

#$I:$9 This !lause merges the outputs of two or more


&uaries into a single set of rows N !olumns+

)%nta':
)*1*CT >stmt1?
0NI/N
)*1*CT >stmt$?

*+g+
)*1*CT student"name ;R/4 student"detail
W9*R* bat!h"no C O1
0NI/N
)*1*CT student"name ;R/4 student"detail
W9*R* bat!h"no C O$

Continue6

I$TERSECT9 This operator returns the rows that are


!ommon between two sets of rows+

)%nta':
)*1*CT >stmt1?
INT*R)*CT
)*1*CT >stmt$?

*+g+
)*1*CT student"name ;R/4 student"detail
W9*R* bat!h"no C O1
0NI/N
)*1*CT student"name ;R/4 student"detail
W9*R* trainer C HRaIiv 5roraJ

Das könnte Ihnen auch gefallen