9+-+SQL+Part+I

=What is SQL?!?= If you are in this class as a management or finance major you may be worried about this section of the course. Don't be. It isn't like programming in IT class with billions of 1's and 0's. It's english!...with a tiny bit of algebra. SQL stands for Structured Query Language. This is simply the input format required to get the desired outputs. Before we can write a successful query we need to understand a thing or two about a database.

Databases are made of Tables that look very similar to spreadsheets. They are made of columns and rows. The columns act as descriptive categories and the rows act as entries or records.

=How do I begin writing in SQL?= Before writing your actual query you need to know what information you need and where in the database (table name and column name) it is located.

Essentially //**you only need 3 things to run a query**//. They are called statements. You need a 1. **//select//** statement 2. a //**from**// statement and 3. a **//where//** statement.

Step 1. The **//select//** statment. This is your grocery list of information you would like. For example: if you were working in a store and needed a products item number, description, cost, usage, current balance (*and all information was in the item table of your database). Your grocery list is seperated by commas and a single space between each field. Your **//select//** statement would look like:

Select item_itemnum, item_description, item_cost, item_usuage, item_curbal

Step 2. The **//from//** statment. This is the easiest thing in the world. Look at your list of required data. Your **//from//** statement just includes the names of the tables the information comes from. For our previous example our from statement would be:

From item

Clearly the more data you need the more tables you will use and the real world examples of one table queries are slim to none however, this serves our learning purpose.

3. The **//where//** statement is the most complicated part of our SQL query. This is how we refine our search to give us exactly what we want. If you have more than two tables we have to link a common column in each table. For example, say we have our store again and we need information that appears on our "item" table and our "inventory" table. We would have to find a common column in each table and set those equal. Where item_itemnum = inventory_itemnum

Further refinement using AND, OR, LIKE and NOT LIKE are discussed in further chapters and will be useful in time.

The **//where//** statement allows you to use many mathematical symbols to further refine data. See chart below:


 * Operator || Description ||
 * = || Equality ||
 * <> || Nonequality ||
 * != || Nonequality ||
 * < || Less than ||
 * <= || Less than or equal to ||
 * !< || not less than ||
 * > || greater than ||
 * >= || greater than or equal to ||
 * !> || not greater than ||
 * BETWEEN || between two specified values ||
 * IS NULL || is a null value (blank) ||

These symbols will help you refine dates, prices, and many other important fields, depending on your desired data.

=What else can I do to increase effectivness of my query?= The possiblities with SQL are virtually limitless if you have the time and patience. Many people use an //**Order**// statement in between the **//from//** and //**where**// statements. This sorts the data while running the query. (Queries usually only take a matter of seconds to run). Many people opt to not use this function and save their results in an xls spreadsheet for manipulation and increased ease of analyzing. To use an **//order//** statement simply enter the column you want it sorted by. Example: Select item_itemnum, item.description, item.cost From item
 * Order by item_itemnum**

This will display your results in ASCENDING order by the item number. If you wish the results to be sorted by cost descending your order statement would appear as follows. Order by item_cost DESC

**More Information** For an example on how to use SQL or for more information on SQL these are two websites that will be useful. http://www.w3schools.com/sql/default.asp http://www.sql.org/