Thursday 3 November 2011

Retrive records from database


Retrieves rows from the database and enables the selection of one or many rows or columns from one or many tables in SQL
Syntax of the SELECT statement
SELECT select_list [ INTO new_table ]
[ FROM table_source ] [ WHERE search_condition ]
[ GROUP BY group_by_expression ]
[ HAVING search_condition ]
[ ORDER BY order_expression [ ASC | DESC ] ]
·  Table listing (FROM clause)
·  Filter logic (WHERE clause)
·  Summarize values (GROUP BY)
·  Filters the GROUP BY values (HAVING)
·  Sorting logic (ORDER BY)

A single column can be accessed from a SELECT statement

USE Employee;
GO
SELECT EmpID FROM Employee
GO

Multiple columns can be accessed from a single SELECT statement
USE Employee;
GO
SELECT EmpID, Firstname, Lastname, Address FROM Employee
GO

Add a WHERE clause to limit the records values from the  table only when the WHERE criteria is met

USE Employee;
GO
SELECT EmpID, Firstname, Lastname, Address FROM Employee where EmpID=1005
GO

Add ORDER BY clauses to show how data can be sorted in both ascending and descending. Default is ascending.

USE Employee;
GO
SELECT EmpID, Firstname, Lastname, Address FROM Employee orderby Firstname Desc
GO

Add Group BY clauses to show data in a Group which is used with aggregate function.

USE Employee;
GO
SELECT sum(Salary) FROM Employee Group By Department
GO

No comments:

Post a Comment