As a database developer, you perform queries by passing instructions to the database engine. This is done using some special reserved words. In Microsoft SQL Server, data analysis can be performed using a query window in Microsoft SQL Server Management Studio, at the Command Prompt, or in PowerSheel. To prepare a window that assists you with data selection, in the Object Explorer, you can right-click a table and click Select Edit Top 200 rows. When you do this, the interface becomes equipped with the Query Designer toolbar. The Query Designer window can be made of four sections:
If you don't want a particular section or you want to hide some sections, you can right-click anywhere in the table, position the mouse on Pane and click the name of the section:
As an alternative to these techniques , to prepare a window for data selection:
This would display a window made of three sections:
A SQL statement is primarily built by selecting one or more columns whose data you want to view. To select a column, if you are working in the Query Designer, in the Diagram section, you can click the check box on the left side of the name:
After clicking the check box of a column, it becomes selected in the Criteria section also and its name appears in the SQL section. Another technique used to select a column consists of clicking a box under the Column header of the Criteria section. This would reveal that it is a combo box:
You can then click the item desired in the list to select it. In the Criteria section, if you click a combo box that already contains a column but select another, the previous one would be replaced by the new one. Also, after selecting a column in the Criteria section, its check box becomes selected in the Diagram section and its name gets added to the SQL section. If you know the name of a column that you want to add, which you can see in the Diagram section, you can directly enter it in the SQL statement. Any of the above three techniques allows you to select one or more columns to build the desired SQL statement. After making your selections in the Query Designer, click OK. This would display a Query with a SQL statement. Also, the SQL Editor toolbar would be added under the Standard toolbar. To display the SQL Editor toolbar:
After creating a SQL statement, you can view its result, which you can get by executing the statement. To do this:
After executing the statement, the bottom section gets filled with data from only the selected column(s) of the table. Here is an example:
Data selection is actually performed using SQL code that contains one or more columns. To start, proceed as we saw previously to display a Query window.
|
|
||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||
After entering the SQL statement in a Query window, you can execute it to see the result. The result would be displayed in the bottom section. There are two ways you can display the result. To have access to these options, you can first display the SQL Editor toolbar. To specify how you want to show the results of your SQL statement, if you are using the Query window, you have two options:
In the Query window, you are expected to write appropriate code that would constitute a SQL statement.
The most fundamental keyword used by SQL is SELECT. In order to process a request, you must specify what to select. To perform data analysis, the SELECT keyword uses the following syntax: SELECT What FROM WhatObject;
To select everything from a table, you can use the asterisk as the range of values. For example, to display all records from a table called Students, you can type: SELECT * FROM Students; After writing the expression in a Query window, you must execute the SQL statement to see its result. Here is an example:
You can also qualify the * selector by preceding it with the name of the table followed by the period operator. The above statement is equivalent to: SELECT Students.* FROM Students; In Lesson 8, we saw that you could create an alias for a table by preceding a column with a letter or a word and a period operator, and then entering the name of the table followed by that letter or word. Using this feature, the above statement can be written as: SELECT std.* FROM Students std; As opposed to viewing all data, you can also select one particular column whose fields you want to view. To do this, you can replace the What in our syntax with the name of the desired column. For example, to get a list of last names of students, you would execute the following statement: SELECT LastName FROM Students; GO You can also qualify a column by preceding it with the name of the table followed by the period operator. The above statement is equivalent to: SELECT Students.LastName FROM Students; When you execute the statement, it would display only the column that contains the last names. To consider more than one column in a statement, you can list them in the What factor of our syntax, separating them with a comma except for the last column. The syntax you would use is: SELECT Column1, Column2, Column_n FROM WhatObject; For example, to display a list that includes only the names, gender, Email address and home phone of records from a table called Students, you would type: SELECT FirstName, LastName, Gender, EmailAddress, HomePhone FROM Students; Once again, you can qualify each column by preceding it with the name of the table followed by the period operator. The above statement is equivalent to: SELECT Students.FirstName,
Students.LastName,
Students.Gender,
Students.EmailAddress,
Students.HomePhone
FROM Students;
You don't have to qualify all columns, you can qualify some and not qualify some others. The above statement is equivalent to: SELECT Students.FirstName,
LastName,
Students.Gender,
EmailAddress,
HomePhone
FROM Students;
When executed in a Query window, this expression would produce:
Once again, remember that you can use an alias name for a table by preceding each column with a letter or a word and a period operator, and then entering the name of the table followed by that letter or word. Here is an example: SELECT std.FirstName, std.LastName, std.Gender, std.EmailAddress, std.HomePhone FROM Students std;
If you specify a column to select from a table, every record would come up. This can cause the same value to repeat over and over again. Sometimes you want to show each value only once. To get such a result, you can use the DISTINCT keyword before the name of the column in the SELECT statement. In most cases, you would get a better result if you select only one column. Still, you can use as many columns as you want.
|
|
|||||||||||||||
|
|
|
|
||
| Previous | Copyright © 2007-2009 FunctionX, Inc. | Next |
|
|
||