Saturday, May 25, 2013

SQL SELECT Example





The "Persons" table:
P_Id LastName FirstName Address City
1 Mahabub Khan Dream Street Meherpur
2 Farhana Sharmin Lake Drive Uttara
3 Mohammad Ullah Mount Street Kushtia
Now we want to select the content of the columns named "LastName" and "FirstName" from the table above.
We use the following SELECT statement:
SELECT LastName,FirstName FROM Persons
The result-set will look like this:
LastName FirstName
Mahabub Khan
Farhana Sharmin
Mohammad Ullah
________________________________________
SELECT * Example
Now we want to select all the columns from the "Persons" table.
We use the following SELECT statement:
SELECT * FROM Persons
Tip: The asterisk (*) is a quick way of selecting all columns!
The result-set will look like this:
P_Id LastName FirstName Address City
1 Mahabub Khan Dream Street Meherpur
2 Farhana Sharmin Lake Drive Uttara
3 Mohammad Ullah Mount Street Kushtia


The 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.
SQL SELECT DISTINCT Syntax
SELECT DISTINCT column_name(s)
FROM table_name

________________________________________
SELECT DISTINCT Example
The "Persons" table:
P_Id LastName FirstName Address City
1 Mahabub Khan Dream Street Meherpur
2 Farhana Sharmin Lake Drive Uttara
3 Mohammad Ullah Mount Street Kushtia
Now we want to select only the distinct values from the column named "City" from the table above.
We use the following SELECT statement:
SELECT DISTINCT City FROM Persons
The result-set will look like this:
City
Meherpur
Kushtia


The WHERE clause is used to filter records.
________________________________________
The WHERE Clause
The WHERE clause is used to extract only those records that fulfill a specified criterion.
SQL WHERE Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name operator value

________________________________________


WHERE Clause Example
The "Persons" table:
P_Id LastName FirstName Address City
1 Mahabub Khan Dream Street Meherpur
2 Farhana Sharmin Lake Drive Uttara
3 Mohammad Ullah Mount Street Kushtia
Now we want to select only the persons living in the city "Sandnes" from the table above.
We use the following SELECT statement:
SELECT * FROM Persons
WHERE City='Sandnes'
The result-set will look like this:
P_Id LastName FirstName Address City
1 Mahabub Khan Dream Street Meherpur
2 Farhana Sharmin Lake Drive Uttara

No comments:

Post a Comment

USA Mirror: The Comparison of Higher Education: Finland, Germa...

USA Mirror: The Comparison of Higher Education: Finland, Germa... : - Jakir Hossin Contents: Introduction 3,4 Chapter 1 Globalization of Edu...