Tuesday 24 February 2009

Explain COUNT () function in SQL Server.

Using COUNT (Column_Name) function

The COUNT (Column_Name) function is used to view the number of specified rows without displaying the null values.

Ex :
Select count(EmployeeName) as EmployeeNameRamesh from Employees
Where EmployeeName

This Query will display the total numbers who has name “Ramesh”. For ex. if there were eight people in the name of “Ramesh” in the table, then the output will show as “8”.

Using COUNT (*) function

The COUNT (*) function is used to view the total number of rows in a particular table.

Ex :
Select count(*) as NumberofEmployees from Employees

This Query will display the total number of rows in the table “Employees”, the output will be displayed in the column name of “NumberofEmployees”.

Using COUNT (DISTINCT Column_Name) function

The COUNT (DISTINCT Column_Name) function is used to view the distinct number of records.

Ex :
Select count(distinct Employees) as NumberofEmployees from Employees

This Query will display the total number of employees distinctly with the new table name “NumberofEmployees”. i.e employees records with the same name is not shown repeatedly.

Labels:


Bookmark this Blog in your Favorites

Friday 6 February 2009

Explain ALIASES clause in SQL Server.

The ALIASES clause is used to give another name for a column or a table. This is mostly used to show the output meaningful.

Ex :
Using ALIAS in a Column

Select EmployeeName as FullName, EmployeeID as ID from Employees

This Query will display the column ‘Employee Name’ as ‘Full Name’ and ‘Employee ID’ as ‘ID’ from the table “Employees”.

Using ALIAS in a Table

Select EmployeeName, EmployeeID from Employees as Resources

This Query will select the columns ‘Employee Name’ and ‘Employee ID’ from the table “Employees” and display as a “Resource” table.

Using WHERE clause with SQL ALIAS

Select EmployeeName as FullName, EmployeeID as ID from Employees where EmployeeID > 250

This Query will select the ‘Employee Name’ and ‘Employee ID’ from “Employees” table and display as ‘Full Name’ and ‘ID’ with a condition of ‘Employees ID’ greater than 250.

Labels:


Bookmark this Blog in your Favorites