Details on Data Analysis |
|
The lists of records we get with a SELECT statement are presented in the order they have in the table. SQL allows you to arrange records in alphabetical order, in chronological order or in numeric incremental order. After selecting a series of columns, you may want to list the records following an alphabetical order from one specific field. To get an alphabetical or an incremental order of records, you must let the database know what field would be used as reference. To specify the order, if you are using a Table window:
If you select Ascending or Sort Ascending, the list of records would be re-arranged based on the type of the selected column:
If you select Descending or Sort Descending, the list of records would be re-arranged based on the type of the selected column:
After selecting the desired Sort Type, you can execute the SQL statement.
In SQL, to specify the sorting order, use the ORDER BY expression. The syntax used would be: SELECT What FROM WhatObject ORDER BY WhatField; The column used as the basis must be recognized as part of the selected columns. For example, to get a list of students in alphabetical order based on the LastName column, you can use the following statement: SELECT FirstName, LastName, Gender, ParentsNames, SPHome FROM Students ORDER BY LastName; GO This would produce:
In the same way, you can get the list of girls followed by the list of boys by ordering the list in alphabetical order based on the Gender. The statement to produce this can be written as follows: SELECT FirstName, LastName, Gender, EmailAddress FROM Students ORDER BY Gender; GO As another example, to list all students arranged in alphabetical order by their last name, you can change the statement as follows: SELECT * FROM Students ORDER BY LastName; GO By default, records are ordered in ascending order. Nevertheless, the ascending order is controlled using the ASC keyword specified after the based field. For example, to sort the last names in ascending order including the first and last names, you would use a statement as follows: SELECT * FROM Students ORDER BY LastName ASC; GO On the other hand, if you want to sort records in reverse order, you can use the DESC keyword instead. It produces the opposite result to the ASC effect. Here is an example: SELECT FirstName, LastName, Gender, ParentsNames, SPHome FROM Students ORDER BY LastName DESC; GO This would produce:
In the previous lesson, we learned that we could analyze data using the Table window. Here is an example:
We also learned how to analyze data by creating and executing a SQL statement in a query window. Instead of selecting all data as we have done so far using the SELECT keyword, you can present a condition that the database would follow to isolate specific records. When analyzing data or if you are creating a query using the Table window, you can type an expression that uses one or more logical operators we reviewed in Lesson 5. Here is an example of an expression > '12/31/1993' This means that the dates that occur after 1993 would be selected.
If you are writing your SELECT statement, to formulate a condition, you use the WHERE keyword with a basic formula as follows: SELECT What FROM WhatObject WHERE Expression; The expressions used in conditions are built using algebraic, logical, and string operators. The Expression factor is called a criterion. Although a group of expressions, making it plural is called criteria, the word criteria is sometimes used for a singular expression also. The expression is written using the formula: ColumnName=Value The ColumnName factor must be an existing column of a table. It is followed by the assignment operator. The Value factor is the value that would set the condition. If the value is a word or a group of words (also called a string), you must include it in single-quotes. If it is a number, you can type its numeric value. Here is an example from a database of students, from a table named Students, to get a list of female students: SELECT DateOfBirth, LastName, FirstName, Gender, State, ParentsNames FROM Students WHERE Gender='Female'; GO This would produce: In a WHERE statement, you can also use the ORDER BY expression to sort a list of records based on a column of your choice. Here is an example: SELECT DateOfBirth, LastName, FirstName, Gender, State, ParentsNames FROM Students WHERE State='MD' ORDER BY LastName; GO This would produce:
In our SELECT statements so far, we were selecting the columns we needed to display. When formulating such a statement, you can apply a condition to a column without including that column in the result. For example, consider the above query. It is used to display a list of female students. Since we know that the result would show only the girls, it becomes redundant to include the Gender column in the statement. In this case, you can hide that column in the result. To hide a column from a query, omit that column in the SELECT statement but involve it in the WHERE condition. Here is an example: SELECT DateOfBirth, LastName, FirstName, State, ParentsNames FROM Students WHERE Gender='Female'; GO This would produce: Notice that the SELECT statement doesn't have the Gender column and the resulting query doesn't show the Gender column.
In Lesson 5, we saw that you could use the NOT operator to negate the validity of a Boolean expression. Consider the following statement: SELECT DateOfBirth, LastName, FirstName, State, ParentsNames FROM Students WHERE Gender = 'Female'; GO When this statement is executed, a list of female students would display. Instead of girls, to get a list of male students, you can negate this condition. To do this, type NOT before the condition. This would be done as follows: SELECT DateOfBirth, LastName, FirstName, Gender, State, ParentsNames FROM Students WHERE NOT Gender = 'Female'; GO To make this condition easier to read, you should include the positive expression in parentheses. This would be done as follows: SELECT DateOfBirth, LastName, FirstName, Gender, State, ParentsNames FROM Students WHERE NOT (Gender = 'Female'); GO This clearly indicates that it is the expression in the parentheses that is being negated. In the same way, you can use the IS NOT NULL to find the records that are not null. For example, you can create a list of only records that don't have a null value on a certain column. Here is an example: SELECT DateOfBirth, LastName, FirstName, State, ParentsNames FROM Students WHERE State IS NOT NULL; GO When this statement is executed, the table would display only the records that include a state for each student.
|
|
||
Previous | Copyright © 2007-2013, FunctionX | Next |
|