Introduction to Data Analysis
Fundamentals of Data Analysis
After creating a table and populating it with records, you may want to see what you can do with data stored in it. One of the most commonly performed operations by the users of a database is to look for data or to isolate data that responds to a particular criterion. Looking for data that is conform to a criterion is referred to as querying. The result of retrieving data based on a criterion is called a query.
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 SQL Server Management Studio or in a query window.
To visually analyze data, in the Object Explorer, you can right-click a table and click Open Table. Here is an example:
By default, when you open a table, the Query Designer toolbar comes up also:
Once the table is opened, on the main menu, you can click Query Designer. Alternatively, you can right-click anywhere on the table. In both cases
Alternatively, on the Query Designer toolbar, you can click the Show Diagram Pane button , the Show Criteria Pane button , and the Show SQL Pane button .
The Table window is divided in 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. To hide any section, you can right-click anywhere in the window, position the mouse on Show Panes and click one of the selections:
When a section is displaying, it menu option is surrounded.
A SQL statement is primarily built by selecting one or more columns whose data you want to view. To select a column, in the Diagram section of the Table window, 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 arrow of the combo box to display the list and select a column from that list:
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 version.
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 creating a SQL statement, you can view its result, which you can get by executing the statement. To do this, you can right-click anywhere in the Table window and click Execute SQL. Alternatively, on the toolbar, you can click the Execute SQL button .
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 Analysis is actually performed using SQL code that contains one or more criteria. To prepare for data analysis, you have various options:
After entering the SQL statement, you can execute it to see the result. This would display the Table window. 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 display the SQL Editor toolbar:
To specify how you want to show the results of your SQL statement, you have two options:
In either the Table window or 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, 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, 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;
In your SELECT statement, after specifying the column(s) as we have done so far, when you execute the SQL statement, the name of each column would appear as the column header. Fortunately, you can display any string of your choice for a column header.
To specify a column header other than the name of the column, if you are using the Table window, type the desired string in the Alias column corresponding to the column. Here is an example:
If you are using a query window or if you are writing your SELECT statement, on the right side of the column name, type AS followed by the desired name of the column header. If the desired column header is in one word, you can simply type it. Here is an example:
SELECT FirstName, LastName, HomePhone AS PhoneNumber, ParentsNames AS NamesOfParents FROM Students; GO
If you want the column header to appear with more than one word, you can provide the words as a string in single-quotes or between the square brackets: [ and ] . Here is an example:
SELECT FirstName AS [First Name], LastName AS [Last Name], HomePhone AS [Phone Number], ParentsNames AS [Names of Parents] FROM Students; GO
This would produce:
By qualifying each column, the above statement can also be written as follows:
SELECT Students.FirstName AS [First Name], Students.LastName AS [Last Name], Students.HomePhone AS [Phone Number], Students.ParentsNames AS [Names of Parents] FROM Students; GO
It can also be written as follows:
SELECT dbo.Students.FirstName AS [First Name], dbo.Students.LastName AS [Last Name], dbo.Students.HomePhone AS [Phone Number], dbo.Students.ParentsNames AS [Names of Parents] FROM Students; GO
It can also be written as follows:
SELECT std.FirstName AS [First Name], std.LastName AS [Last Name], std.HomePhone AS [Phone Number], std.ParentsNames AS [Names of Parents] FROM Students std; GO
Using the SELECT keyword, we have learned to create a list of isolated columns. These columns were rendered separate of each other. Instead of having separate columns, you can combine them to create a string or a value that is in fact an expression. For example, you can combine a first name and a last name to produce a full name as an expression. Another expression can use a date on the table, add a number to it to get a date on another day. An expression can also be used to perform a calculation on two or more columns such as employees weekly hours multiplied by their hourly salary to get their weekly salary.
The most common operator used is the addition. It can be used to combine two or more strings to get a new one. Here is an example:
SELECT FirstName + ' ' + LastName FROM Students; GO
This would produce:
The addition can also be used on numeric values. All other arithmetic operators can be used. For example, you can multiply a weekly hours value to an hourly salary to get a weekly salary. The statement of such an expression can be written as follows:
SELECT WeeklyHours * HourlySalary FROM Payroll
You can also create an alias for an expression to give it the desired name. To do this, on the right side of the expression, type AS followed by the name. AS we learned earlier, if the alias is in more than one word, include it in either single quotes or square brackets. Here is an example:
SELECT FirstName + ' ' + LastName AS 'Full Name', EmrgName + ' ' + EmrgPhone AS [Emergency Contact] FROM Students; GO
This would produce:
If you just create a regular expression using arithmetic operators, the new column would not have a name. The SQL allows you to specify a different name for any column during data analysis or a name for an expression. This is done using the assignment operator "=".
To change the name of a column during data analysis, on the right side of SELECT, type the desired name, followed by the assignment operator, followed by the actual name of the column. Here is an example:
SELECT EmergencyName = EmrgName FROM Students; GO
If you want to use more than one column, type each and assign it the desired name, separate them with commas. Here is an example:
SELECT LastName, EmergencyName = EmrgName, EmergencyPhone = EmrgPhone FROM Students; GO
This would produce:
You can also include the name between single-quotes or the square brackets. Here are examples:
SELECT LastName + ', ' + FirstName AS [Full Name], [Emergency Name] = EmrgName, 'Emergency Phone' = EmrgPhone FROM Students; GO
This would produce:
|Previous||Copyright © 2007 Yevol||Next|