0% found this document useful (0 votes)
45 views

SQL آموزش سریع

This document provides an overview of SQL commands and clauses in 3 sentences or less. It discusses basic SELECT statements and various clauses like WHERE, ORDER BY, GROUP BY, HAVING. It also covers JOINs and subqueries. Examples are provided for most concepts to illustrate how they can be used.

Uploaded by

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

SQL آموزش سریع

This document provides an overview of SQL commands and clauses in 3 sentences or less. It discusses basic SELECT statements and various clauses like WHERE, ORDER BY, GROUP BY, HAVING. It also covers JOINs and subqueries. Examples are provided for most concepts to illustrate how they can be used.

Uploaded by

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

SQL

:
:
:
85


SQL
.
.
[email protected]

o cI_gxa SQL


..l ei. e>ls..l , ll,l l lIl.o ,I >

Teachers Courses Students Term
* TeacherCode * CourseCode * StudentCode

* StudentCode
TeacherName CourseName

StudentName

* CourseCode
Phone Dep

Adrs

* TeacherCode
Salary Unit

Sex

Grade


City



el.. liI, l> ( * ) i.. _o _>.o l l i,I


1 { olSjlj Oj _I ll,8 _aSj ul>x:I

Select Field1 [, Field2 , ..| From <Table Name>

Example :
Select TeacherCode,TeacherName
From Teachers

Select StudentName
From Students
ll,8 o ul>x:I
Select *
From Courses

T { b_a l; ll,8 _aSj ul>x:I

Select Field1 [, Field2 , ..| From <Table Name> Where <Condition>

Example :

Select TeacherCode,TeacherName
From Teachers
Where TeacherCode>100


1
[email protected]


_o Where Claues _I OIg: qo And,Or,Not o_S oolaxaI
Select StudentName
From Students
Where StudentCode>10 Or StudentName='Ali'

Select * From Courses Where Not (Unit=+)

l,8 Oj o_go Oj _o oog>o ol>jI I_;

Select Field1 [, Field2 , ..| From <Table Name>
Where Field Between StartRange And EndRange

Example
Select StudentName
From Students
Where StudentCode Between 11 And 19
Select *
From Teachers
Where TeacherName Between 'Ali' And 'Sasan'

;lao clolS o_SI,j I_;

Select Field1 [, Field2 , ..| From <Table Name>
Where Field Like value

Example

Select StudentName
From Students
Where StudentName Like 'Ali'
_IL . .l.o .loI ,,oa. l.
Select *
From Courses
Where CourseName Like 'Nath%'
_I>o . l,. >o oI ,,oa. l.
Select StudentName
From Students
Where StudentName Like '%Ali%'


_l> clolS _aSj Oo_SI,j I_;

Select Field1 [, Field2 , ..| From <Table Name>
Where Field In {valu1, valu2, valu3,..)
2
[email protected]


Example

Select StudentName
From Students
Where StudentCode !n (101,950,3+2)
oa:_j lo_gS_ o_S I,j I_; ) NULL {

Select Field1 [, Field2 , ..| From <Table Name>
Where Field Is Null
o _S: : NULL l. Blank ..l .ls.o Vol . iI, > .l. Space .>,
iI, cl >. NULL >l lol ..,. >.. .>, , _, iI, NULL >. il>

T { _la u:_o I_;

Select Field1 [, Field2 , ..| From <Table Name>
Order By <Field List> [Desc Asc]

Example

Select StudentName
From Students
Order By SudentName
liI, elo. ..>. l. ..o _.l>..l
Select StudentName,StudentCode
From Students
Order By 2
>a _I. l. ..o
Select StudentName,StudentCode
From Students
Order By StudentName Asc ,StudentCode Desc

o _S: : > _,liI, Order By > _..,l. _o lo.> i.. _o i,
Select i..l. ei. i,

L { _o qlj_ cI_lxc _I oolaxaI Select

L,1 { qlj_ ooloI g;Ig: _I oolaxaI

Select FunctionName (Field1) [ As AliasName | From <Table Name>


3
[email protected]


qlj_ g;Ig: ua, ) FunctionName {

l,8 Oj _xSI> Max
l,8 Oj @8I> Min
l,8 go> @l> Sun
oIn: __loa Count
l,8 _,S:l,o Avg



Example

Select Nax(StudentCode)
From Students
s> L..o _li> . .li> c>li, cloIao
Select Nax(Salary) As NaxS, Nin(Salary) As NinS, Avg(Salary)
From Teachers

Select Nax (Salary) From Teachers Where TeacherCode Between [1000,1500|
_> >lia.
Select Count(*) From Cources


L,T { qlj_ clxal>o _I oolaxaI

Select AliasName = <Calculate Expretion> From <Table Name>
Select <Calculate Expretion> As AliasName From <Table Name>

Example

Select Salary , NewSal = Salary +1000
From Teachers

Select TeacherName , Salary*1.2 As Pay!t , Phone
From Teachers


o { _la V> ) ;lao lo_gS_ _jlo: c {

Select Distinct Field From <Table Name>

4
[email protected]

Example
l. ci. l. ,I ,lo.
Select Dictinct City
From Students
l. l. l. ,I ,lo.
Select City
From Students
l . >lia. lo. l. ci.
Select Count (Dictinct City )
From Students

A { x; og_S ) x; xao {

Select Field1 [,Field2,..| From <Table Name> Group By <Field List>

Example

Select StudentName
From Students
Group By StudentCity , StudentName
. ,s. . clol .l> >lia.
Select City , Count(*)
From Students
Group By City
>lia. clol .l> ,. cl,o ol .l> ei. l. . _>
Select StudentCode, Count(*) As TerCourse , Count(*) * +5000 As NustPay
From Term
Group By StudentCode

o _S: : _,liI, ) .l.l>o l, .l. . ( > Select lo.> i.. _o i,
> _..,l. _o Group By i..l. ei. i, ,. .


V { x; og_S g_ _IS b_a ) x; xao {

Select Field1 [,Field2,..| From <Table Name> Group By <Field List>
aving <Condition>

Example

. ,s. . >o clol .l> >lia.
Select City , Count(*)
From Students
Group By City
aving Sex = 'N'
5
[email protected]


l ,. _,l . l. i.l>ol .l>
Select City , Count (*)
From Students
Group By City
aving Count(*) >3

o _S: : Where aving i.li. _.ls. _, . lol having _.lVLl
i. _o _o i.l ei. i.. ..> .


{ llSjlj u,S_:

Select Table1.Field1 ,Table2.Field2 [ ,..| From <Table1,Table2,.>

Example
. > >l..l _>

Select Teachers.TeacherName, Teachers.TeacherCode , Term.CourseCode
From Teachers, Term

o _S: : > ,l >li. >.l >lo . el,l i,. Term ll>
el,l i.l. > Teachers > . >lia. . l. >> _o l. ..>
l > Il> ) ( ..l LI i,. li, l > . . l,. >lo > lol
l. _o , clol .l> _olo. ,lo. i.l. i,so ..l oo .i,. .

. > >l..l _>

Select Teachers.TeacherName, Teachers.TeacherCode , Term.CourseCode
From Teachers, Term
Where Teachers.TeacherCode= Term.TeacherCode
clol .l> _> .lo. ) (

Select Students.StudentName, Term.CourseCode , Term.Grade
From Students,Term
Where Students.StudentCode= Term.StudentCode

o _S: : l el,l l. i,.l. _o i...,. ..o ll,l > iI, .l.
i,.. .

clol .l> _> .lo. ) (
Select StudentName, Term.CourseCode , Grade
From Students,Term
Where Students.StudentCode= Term.StudentCode

6
[email protected]


o _S: : >> ll,l l. .,o >. _o ,. Alias ) lo l. ( ,a.
i,.

clol .l> _> .lo. ) (
Select StudentName, DCourseCode , Grade
From Students D ,Term D
Where DStudentCode= D.StudentCode

l ..,. _> , > l .l. ,.l> clol .l> eo. l. i.l .
Select StudentName, CourseName , Grade
From Students D1, Courses D2 , Term D3
Where D1.Sex='F' And D3.Grade>=15 And
D1.StudentCode=D3.StudentCode And
D3.CourseCode=D2.CourseCode


.,.. . clol .l> _> .lo. ) (
Select StudentName, CourseName , Grade
From Students D1 ,Term D3, Courses D2
Where D1.StudentCode=D3.StudentCode And
D3.CourseCode=D2.CourseCode
Order By CourseName , StudentName

{ llSjlj blx:_I

Select Field1 ,Field2 [ ,..| From <Table1> Inner oin <Table2> On
Table1.Field = Table2.Field

clol .l> _> .lo. ) (

Select StudentName, D3.CouseCode, Grade
From Students D1 !nner oin Term D3 On
D1.StudentCode=D3.StudentCode


{ Select g:_og: l

Select Field1 ,Field2 [ ,..| From <Table1,Table2,.>
Where Field Oprand (Select Field1 ,Field2 [ ,..| From
<Table1,Table2,.>)





7
[email protected]


l_Sloc ua, ) Oprand {

ua cgo>o_o IN Not IN
o_Ioog>g _SI Exists Not Exists
qSj @8I> Any
o lox> All
l,8 _,S:l,o
> , < , = , <= , >= ,
<>

Example
eo. _.lol .l> l. i.l .
Select StudentName
From Students
Where StudentCode !N
(Select StudentCode
From Term
Where Grade>15)
o xS: : l l Select ,,. _o e>ls..l . >. l . ,ll l. l.,l l,
.l..> ... ci.l> _.>l .. SQL >> _o . i,l> lia. cl. eV
>. il> i,so l,.. . l..> ,> l. .,I.l ,l .,. .

eo. _.lol .l> l. i.l .
Select StudentName
From Students
Where StudentCode =
(Select StudentCode
From Term
Where Grade>15)
o _S: : i,. .,. >> l. l el,l , i,.l. _o lo.
..o i> l .o _.loIao l. i.. _o .l,> s> L
Select TeacherName
From Teachers
Where Salary < (Select Avg(Salary)
From Teachers)
_> , > _li> _.lol .l> l. i.l .
Select StudentName
From Students
Where StudentCode = Any (Select StudentCode
From Term
Where Grade=16)




8
[email protected]


..l Uiao L..o l ,. _Iiao ll> _.lol .l> l.
Select StudentName , Avg(Grade)
From Students D1, Term D2
Where D1.StudentCode = D2.StudentCode
Group By StudentName
aving Avg(Grade) > Any (Select Avg(Grade)
From Term
Group By StudentName)
i.l e>. l. . _.> _, > _.lol .l> l.
Select StudentCode, StudentName
From Students D1
Where Not Exists ( Select *
From Term
Where StudentCode = D1.StudentCode)

i.l> >o >l. Ls _,l.V l.
Select Distinct CourseName
From Course D1
Where 'N' = All ( Select Sex
From Term D2 , Students D3
Where D2.StudentCode = D3.StudentCode
And
D2.CourseCode = D1.CourseCode)

l ,. _.l>l. l. i.l . _>
Select StudentName
From Students S
Where Exists (Select Count(*)
From Term
Where StudentCode= S.StudentCode
aving Count(*)>2)

o _S: : ..l oo aving l e>ls..l ci. l Group By > Select l
i,. l. _I>l> . l. >. _o l. l.,l aving l l C. e , ..
i. Ulol l L. _,. i. el. .
l ,. _,l ei..l> l. i.l> ol .l>
Select Dep
From Courses C
Where Exists (Select Count(*)
From Term
Where CourseCode= C.CourseCode
aving Count(*)>+)




9
[email protected]


_gxao qlS _8 Oljlj _o Select

Select <Field List >
From <Table List >
Where Condition
Group By <Field List >
aving Condition
Order By <Field List >





1 { g> ol>jI

Create Tale <Table Name> (Columns)

Example
clol .l> el,l >l,l
Create Table Students ( StudentCode Small!nt Not Null Unique ,
StudentName Char (20) ,
Adrs Char (50) ,
City Char (10) ,
.....
)

11 { g> >

Drop Tale <Table Name>

Example
clol .l> el,l .>
Drop Table Students









10
[email protected]


1T { o_gS_ Oo_S 8lI

Insert Into Tale <Table Name> [(Columns Name)| alues <values>

Insert Into Tale <Table Name>
Select <Fields>
From <Table Name>
Where <Condition>

Example
_> el,l > i,i > ) (
!nsert !nto Courses (CourseCode,CourseName,Dep,Unit)
values (1005 , 'Nath 2' , 'Nt' , +)
_> el,l > i,i > ) (
!nsert !nto Courses
values(1005,'Nath 2','Nt',+)

1T { o_gS_ _Io _,,:

Update <Table Name> Set <Column Name> = <value> Where
<Condition>

Example
_l, _> i>l ,,a.
Update Course
Set Unit = 3
Where CourseCode=1005
L..o liso , cloIao s> ,,a.
Update Teachers
Set Salary=1.2 * Salary -100
Where Salary < (Select Avg(Salary)
From Teachers)

1L { o_gS_ >

Delete From <Table Name> Where <Condition>

Example
_l, _> .>
Delete From Course
Where CourseCode=1005


11
[email protected]



1L { lo: Oj ol>jI ) iew {

Create iew <view Name> (<Column Names>) As
Select <Column Names>
From <Table Names>
Where <Condition>

Example
s> cloIao l .o Ui , >l,l s> .l.
Create view TeachLook (TCode , TName , NainSal , Sal2)
Select (TeacherCode , TeacherName , Salary , Salary *2)
From Teachers

Create view Resome (StName , Ccode , Grade )
Select StudentName, D3.CouseCode, Grade
From Students D1 !nner oin Term D3 On
D1.StudentCode=D3.StudentCode

o _S: : , .>l. l _ view , co cl l. cl. _o Table > > l.
cl !nsert, Update Delete >l> l.l .

1o { lo: >

Drop eiw <view Name>

Example
clol .l> lo. .>
Drop view Students
1A { SxjI ol>jI

Create [Uniue] Index <!ndex Name> ON <Table Name> (<Field
Names>)

Example
cloIao el,l . ,I. l _i.,l , >l,l ) (
Create !ndex TName!DX ON Teachers (TeacherName)
cloIao el,l . ,I. l _i.,l , >l,l ) (
Create !ndex TName!DX ON Teachers (TeacherCode,TeacherName)




12
[email protected]





1V { SxjI >

Drop Index <!ndex Name>

Example

Drop !ndex TName!DX
1 { Union ) loI T ;lao ll,8 l; olSjlj {

Select <Fields> From <Table Name>
Union
Select <Fields> From <Table Name>

Example

Select StudentCode , StudentName
Form Students
Union
Select TeacherCode , TeacherName
Form Teachers





















13
[email protected]



_o l ooIo Ig:I g> SQL Tale

l,8 g: ,g:
Bigint -2'63 ~ 2'63 _,>
!nt -2'31 ~ 2'31 _,>
Smallint -2'15 ~ 2'15 _,>
Tinyint 0 ~ 255
Bit 0 ~ 1
Float -1.79'308 ~ 1.79'308 l.l
Real -3.+'38 ~ 3.+'38 l.l
DateTime 1f1f1753 ~ 31f12f9999
SmallDateTime 1f1f1900 ~ 6f6f2079
Char .li> . ..l UL A .l
varChar .li> . ,a.o UL A .l
Text .l 2'31 .li> . ,,a.o UL
Nchar Unicode .li> . ..l UL .l
NvarChar Unicode .li> . ,,a.o UL .l
Ntext Unicode .l 2'30 .li> . ,,a.o UL
Binary .,l. .li> ..l UL A
varBinary .li> ,,a.o UL A
!mage 2'31 .li> ,,a.o UL
Uniqueidentifier _.l > . >.o .l..







14
[email protected]

You might also like