Walk Lightly on this PLANET and yet leave such a FOOTPRINT that cannot be erased for thousands of Years..!!!
Visit Codstech for Cyber Security related Posts !

Visitors

Tuesday, April 16, 2013

8 : SQL " AND & OR "Operators

The AND & OR operators are used to filter records based on more than one condition.


The AND operator displays a record if both the first condition and the second condition are true.
The OR operator displays a record if either the first condition or the second condition is true.

The created Table is : 


Create a Database named "Studysql" and a table named "tbl_biodata"
Right click on the database Studysql and select new query.Then we can write queries as per the requirements.
( Click here to know how created this table )

ID
NAME
AGE
CITY
PHONE
1
Priyada
25
Cochin
1000000001
2
Mohan
30
Cochin
2000000002
3
Jaimon Jacob
40
Vytila
3000000003
4
Sumith
24
Kottayam
4000000004
5
Shaiju
36
Thrissur
5000000005



1 . AND Operator Example


Now we want to select only the persons with the name equal to "Priyada" AND the city equal to "Cochin":

To do so, write the command like this in SQL.

select * from tbl_biodata where names='Priyada' and city='Cochin'



The result-set will look like this:


2 . OR Operator Example

Now we want to select only the persons with the first name equal to "Priyada" OR the first name equal to "Mohan":


To do so, write the command like this in SQL.

SELECT * FROM tbl_biodata WHERE names='Priyada' OR names='Mohan'



The result-set will look like this:




NEXT CHAPTER

PREVIOUS CHAPTER


7 : SQL "WHERE" Clause

The WHERE clause is used to filter records.
That is , The WHERE clause is used to extract only those records that fulfill a specified criterion.


The created Table is : 


Create a Database named "Studysql" and a table named "tbl_biodata"
Right click on the database Studysql and select new query.Then we can write queries as per the requirements.
( Click here to know how created this table )

ID
NAME
AGE
CITY
PHONE
1
Priyada
25
Cochin
1000000001
2
Mohan
30
Cochin
2000000002
3
Jaimon Jacob
40
Vytila
3000000003
4
Sumith
24
Kottayam
4000000004
5
Shaiju
36
Thrissur
5000000005


Now we want to select only the persons living in the city "Thrissur" from the table above.


To do so, write the command like this in SQL.


SELECT * FROM tbl_biodata WHERE city='Thrissur'



The result-set will look like this:





SQL SELECT Syntax of the same is : 

SELECT column_name(s)
FROM table_name
WHERE column_name operator value
============================================================================================

Note : Operators Allowed in the WHERE Clause



With the WHERE clause, the following operators can be used:
OperatorDescription
=Equal
<>Not equal
>Greater than
<Less than
>=Greater than or equal
<=Less than or equal
BETWEENBetween an inclusive range
LIKESearch for a pattern
INTo specify multiple possible values for a column
Note: In some versions of SQL the <> operator may be written as !=

=================================================================================

6 : SQL " SELECT DISTINCT " STATEMENT


In a table, some of the columns may contain duplicate values. This is not a problem, however, sometimes you will want to list only the different (distinct) values in a table.

The DISTINCT keyword can be used to return only distinct (different) values.

The created Table is : 


Create a Database named "Studysql" and a table named "tbl_biodata"
Right click on the database Studysql and select new query.Then we can write queries as per the requirements.
( Click here to know how created this table )

ID
NAME
AGE
CITY
PHONE
1
Priyada
25
Cochin
1000000001
2
Mohan
30
Cochin
2000000002
3
Jaimon Jacob
40
Vytila
3000000003
4
Sumith
24
Kottayam
4000000004
5
Shaiju
36
Thrissur
5000000005


Here, refer the field CITY . The column COCHIN is repeated 2 times.

Now we want to select only the distinct values from the column named "City" from the table above.

To do so, write the command like this in SQL.

SELECT DISTINCT City FROM tbl_biodata


The result-set will look like this:


SQL SELECT Syntax of the same is : 


SELECT DISTINCT column_name(s)
FROM table_name




5 : SQL "SELECT" STATEMENT


The SELECT statement is used to select data from a database.
The result is stored in a result table, called the result-set.


The created Table is : 


Create a Database named "Studysql" and a table named "tbl_biodata"
Right click on the database Studysql and select new query.Then we can write queries as per the requirements.
( Click here to know how created this table )

ID
NAME
AGE
CITY
PHONE
1
Priyada
25
Cochin
1000000001
2
Mohan
30
Cochin
2000000002
3
Jaimon Jacob
40
Vytila
3000000003
4
Sumith
24
Kottayam
4000000004
5
Shaiju
36
Thrissur
5000000005

We can select the table in 2 methods. First , we can select a part of the table , and second , we can select the whole table.

1. The SQL SELECT Statement(Selecting a part of the table).


Now , we want to SELECT the content of the column named "NAME" & " CITY" from the above table.
To do so, write the command like this in SQL.


SELECT names,city FROM tbl_biodata


Then ,Select EXECUTE (you can select the EXECUTE option from the menu " QUERY DESIGNER ").

The result-set will look like this:




SQL SELECT Syntax of the same is : 

SELECT column_name(s)
FROM table_name


2 . SELECT * Example

Now we want to select all the columns from the "tbl_biodata" table.

To do so, write the command like this in SQL.

select * from tbl_biodata







The result-set will look like this:



SQL SELECT Syntax of the same is : 

SELECT * FROM table_name



Tip: The asterisk (*) is a quick way of selecting all columns!
That is , this command is used to select all the fields from the table.




NEXT CHAPTER