knowledgediarybd.com

Your Virtual Knowledge Campus







SQL AND QUERY,SQL OR QUERY AND SQL DISTINCT QUERY

SQL AND OR AND DISTINICT QUERYSQL and, or, & distinct   are three most important query. These have a lot of application in structural query language(sql).IN sql AND & OR operators are normally used to filter records from a table depend on more than one condition. On the other hands some times we need to separate unique value from the number of duplicate values or the number of values from a table.Then we can use sql select distinct command  to separate unique value.

SQL AND QUERY:

The SQL AND operator show a record when both the first and the second condition are true

For Example:

The ‘teacher info2’ Table

after drop

Now if you want to select only those teacher with the first name equal to “HEDAYETUL” AND  the last name equal to “SHOVON”

Then we use the following sql command:

SELECT * FROM `teacher info2`
WHERE FIRST_NAME = 'HEDAYETUL'
AND LAST_NAME = 'SHOVON'

The output of the above query:

sql and

The SQL OR Operators:

The SQL OR operator shows a record if only one condition is true from two condition.

SQL OR Operator Example:

Now If we want to select only those teacher with the first name equal to “FIROZ” OR the first name equal to “HEDAYETUL”:

For that we use the following select stament:

SELECT * FROM `teacher info2` WHERE
FIRST_NAME= 'HEDAYETUL'
OR FIRST_NAME = 'FIROZ'

The output of the above statement looks like below:

sal or statement

AND & OR Combining :

For combining AND & OR we use the following select statement:

SELECT * FROM `teacher info2` WHERE

LAST_NAME = 'SHOVON'
AND (FIRST_NAME = 'HEDAYETUL' OR  FIRST_NAME = 'FIROZ')

The output of the above statement looks like below:

COMBIND AND & OR]

SQL SELECT DISTINCT Statement

Generally in a table, some of the columns may hold similar values. This is not a problem, however, sometimes we want to list only the different (distinct) values from a table.

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

Syntax of SQL SELECT DISTINCT :

SELECT DISTINCT Column_Name
FROM Table_Name

SELECT DISTINCT Example:

The ‘teacher info2’ Table

after drop

Now if we want to select only the distinct values from the column named “MIDDLE_NAME” from the table above.

For this We use the following SELECT Distinct statement:

SELECT DISTINCT MIDDLE_NAME
FROM `teacher info2`

The result-set will look like this:

middle_name

Related Post or you may like:

  1. Introduction of Database & SQL

  2. Create Database & Table

  3. SQL Insert,Update & Delete

  4. SQL Select and Where

  5. SQL Primary,Unique & Foreign Key

  6. SQL Drop truncate and Alter

  7. SQL Order By& Group By

  8. SQL Like & Check Constrain

  9. Database Connection

knowledgediarybd.com © 2017
shares