Day 10
SELECT statement
While the SELECT statement offers a plethora of arguments that can make it very complex, in its simplest form it consists of two keywords: a list of columns and a table name.
USE Thigalla;
SELECT
FROM
Click and drag the Columns folder underneath the table in Object Explorer to the right of the keyword SELECT. Ensure that there is a space after the SELECT keyword.
Click and drag the Flowers. Department table from Object Explorer to the right of the keyword FROM. Ensure that there is a space after the FROM keyword.
--Use this query to select data from the Flowers. Department table in the database USE Thigalla; SELECT DepartmentID, Name, GroupName, ModifiedDate FROM [Flowers]. [Department]
8. Execute the query and review the results.
Filtering data with the WHERE clause
So far, you have simply returned all the rows in a table.In the real world, this is probably not what you will do—most times, and you will need to return only subsets of data.
You can create aliases, which can be a shorter or more understandable name, for table and column names, making it easier to work with aggregations, expressions, and queries that involve multiple tables.In addition, your database may contain very cryptic column names, and you may want to provide names that are more meaningful to applications and end users.Using aliases allows you to rename or shorten the names of tables and columns.
Alias a table and column
--Use this query to alias a table and columnUSE THIGALLA;
SELECT DepartmentID, Name AS DepartmentName, GroupName AS DepartmentGroupNameFROM HumanResources.Department AS d
In the preceding query, the Name column is renamed DepartmentName, the GroupName column is renamed DepartmentGroupName, and the table has been aliased as simply d. Now you can reference the table as d instead of the entire table name throughout the query.The use of table aliases is explained in the next section.
SELECT statement
While the SELECT statement offers a plethora of arguments that can make it very complex, in its simplest form it consists of two keywords: a list of columns and a table name.
- Write a SELECT statement.
- Sort your results.
- Filter a SELECT statement with the WHERE clause.
- Use comparison operators.
- Use the BETWEEN operator.
- Use the WHERE clause with multiple conditions.
- Search for a list of values.
- Use a wildcard search.
- Create aliases.
- Use the JOIN operator.
- Limit the data returned in your result set.
- Use the UNION keyword to combine result sets.
- Open Microsoft SQL Server Management Studio (SSMS) and connect to a server.
- In Object Explorer, expand the Databases folder.
- Expand the AdventureWorks2012 database.
- Expand the Tables folder.
- Expand the Flowers. Department table.
- Open the query editor in SSMS.
- In the query editor, enter and execute the following T-SQL code:
USE Thigalla;
SELECT
FROM
Click and drag the Columns folder underneath the table in Object Explorer to the right of the keyword SELECT. Ensure that there is a space after the SELECT keyword.
Click and drag the Flowers. Department table from Object Explorer to the right of the keyword FROM. Ensure that there is a space after the FROM keyword.
--Use this query to select data from the Flowers. Department table in the database USE Thigalla; SELECT DepartmentID, Name, GroupName, ModifiedDate FROM [Flowers]. [Department]
8. Execute the query and review the results.
Filtering data with the WHERE clause
So far, you have simply returned all the rows in a table.In the real world, this is probably not what you will do—most times, and you will need to return only subsets of data.
- Comparison operators
- The BETWEEN operator
- A WHERE clause with multiple conditions
- A search for a list of values
- A wildcard search
You can create aliases, which can be a shorter or more understandable name, for table and column names, making it easier to work with aggregations, expressions, and queries that involve multiple tables.In addition, your database may contain very cryptic column names, and you may want to provide names that are more meaningful to applications and end users.Using aliases allows you to rename or shorten the names of tables and columns.
Alias a table and column
- Open the query editor in SSMS.
- In the query editor, enter and execute the following T-SQL code:
--Use this query to alias a table and columnUSE THIGALLA;
SELECT DepartmentID, Name AS DepartmentName, GroupName AS DepartmentGroupNameFROM HumanResources.Department AS d
In the preceding query, the Name column is renamed DepartmentName, the GroupName column is renamed DepartmentGroupName, and the table has been aliased as simply d. Now you can reference the table as d instead of the entire table name throughout the query.The use of table aliases is explained in the next section.
Thanks & Regards
Srikanth Goud- Virtualization Administrator
