Friday, January 28, 2011

Relation Operator in SQL Server

In this chapter we will learn about relation operator. Realtion Operator have a utility to make some condition in query syntax. At bellow is a little relation operator:
1. '=' -> Equal
2. '<>' -> Not Equal
3. '<' -> Less
4. '<=' -> Less than or equal to
5. '>' -> Bigger
6. '>=' -> Bigger than or equal to
7. 'Like' -> Usually use symbol '%'
8. 'IN' ->Search by parameter more than 1
9. 'Between' -> Search for 2 condition
10. 'Not Between' -> Not between 2 condition.
11. 'Null' -> Empty Value
12. 'Not Null' -> Not Empty Value

Example
Create database dbStudent

Create table dataStudent(
NRP varchar(100) primary key not null, Name varchar(100), Grade int
)

Insert into dataStudent value('0772186','Hendry Apryanto',40)
Insert into dataStudent value('0772097','Yohan Andreas',60)
Insert into dataStudent value('0772232','Tommy Gunawan',90)
Insert into dataStudent value('0772185','Junius',40)
Insert into dataStudent value('0772111','Tana El San',50)

1. Using Equal Symbol ('=')
Select * from dataStudent where NRP='0772186'
Result:
NRP = 0772186
Name = Hendry Apryanto
Grade = 40

2.Using Not Equal Symbol ('<>')
Select * from dataStudent where NRP <> '0772186'
Result :
NRP = 0772097
Name = Yohan Andreas
Grade = 60

NRP = 0772185
Name = Tommy Gunawan
Grade = 90

NRP = 0772185
Name = Junius
Grade = 40

NRP = 0772111
Name = Tana El San
Grade = 50

3. Using Less and Less Than or Equal to('<' and '<=')
Select * from dataStudent where Grade < 50
Result:
NRP = 0772186
Name = Hendry Apryanto
Grade = 40

NRP = 0772185
Name = Junius
Grade = 40

Select * from dataStudent where Grade <= 50
Result:
NRP = 0772186
Name = Hendry Apryanto
Grade = 40

NRP = 0772185
Name = Junius
Grade = 40

NRP = 0772111
Name = Tana El San
Grade = 50

4.Using Bigger and Bigger than or equal to ('>' and '>=')

Select * from dataStudent where Grade > 60
Result :
NRP = 0772185
Name = Tommy Gunawan
Grade = 90

Select * from dataStudent where Grade >= 60
Result :
NRP = 0772097
Name = Yohan Andreas
Grade = 60

NRP = 0772185
Name = Tommy Gunawan
Grade = 90

5. Using Like

Select * from dataStudent where Name LIKE '%h%' (Check at column name containt 'h' word)
Result:
NRP = 0772186
Name = Hendry Apryanto
Grade = 40

NRP = 0772097
Name = Yohan Andreas
Grade = 60

6. Using IN
Select * from dataStudent where Name IN ('Yohan Andreas','Hendry Apryanto')
Result:
NRP = 0772186
Name = Hendry Apryanto
Grade = 40

NRP = 0772097
Name = Yohan Andreas
Grade = 60

7. Using Between
Select * from dataStudent where Grade between 50 and 80
Result:
NRP = 0772097
Name = Yohan Andreas
Grade = 60

NRP = 0772111
Name = Tana El San
Grade = 50

8. Using Not Between
select * from dataStudent where Grade not between 40 and 70
Result:
NRP = 0772185
Name = Tommy Gunawan
Grade = 90

9. Using Null and Not Null
Select * from dataStudent where Name is null
Result:
(No data)

Select * from dataStudent where Name is not null
Result:
NRP = 0772186
Name = Hendry Apryanto
Grade = 40

NRP = 0772097
Name = Yohan Andreas
Grade = 60

NRP = 0772185
Name = Tommy Gunawan
Grade = 90

NRP = 0772185
Name = Junius
Grade = 40

NRP = 0772111
Name = Tana El San
Grade = 50

Okay that's is it and finally congratulation you know a little relation operator in sql server. Thank you for see this tutorial, I hope with this tutorial can help you..^^

No comments:

Post a Comment