Database

SQL: Calculated Fields

When working with SQL you will inevitably be asked to return data from your database in a way it's not stored. You'll be asked to return city, state and zip as a single string, yet they are stored separately. You'll be asked to return grand totals, but you only have line item totals. You'll be asked to provide counts, totals, or averages, and none of that is in the database. The answer to all these scenarios is to calculate those values for the user.

Concatenating Fields

Let's say we have an address table, and the columns city, state, and zip are each stored in there as VARCHAR. It's important that all three are varchar, otherwise you'll get errors when you try to combine them all into one string. Given this table, you want to present a list of "cityName, state zipcode". Note the spacing and the comma in the string. How would we do this?

SELECT
city + ', ' + state + ' ' + zip AS [output]
FROM address

output
--------
Charlotte, NC 28222
Kannapolis, NC 28081
Beverly Hills, CA 90210

Concatenating VARCHAR values is simple. Just remember all the fields have to be VARCHARs before concatenation. If one or more fields aren't, then you can CONVERT them to VARCHAR first.  

Mathematics

Let's say you have a table of sales, and in that table it lists, productName, quantitySold, and price. How could you query this table to add a subtotal column that shows the price paid for that quantity sold?

SELECT
productName
, quantitySold
, price
, quantitySold * price AS subTotal
FROM sales

productName quantitySold price subtotal
----------------- ---------------- -------- -----------
pen 1 .99 .99
paper 25 1.00 25.00

 

 

 

 

You can use any mathematical operator in the place of *. Basically any time you compute a column, the query will look like a formula, keep that in mind, and this will be a breeze!

Conclusion

This is only the beginning of creating calculated fields in SQL. In letter posts I'l show you extra functions that can do far more than simple math. After that I'll show you aggregate functions that can sum several rows of information into one row. If you have any questions, please send them in, I'm working hard to help explain the fundamentals of SQL so you can become better equipped to work through the many questions you'll be asked one day. I can only help you, if you "help me, help you."

Other Recommended Articles:

SQL - The Advanced LIKE Clauses

SQL 101 - The WHERE clause

About the Author: 

Look no further for expertise in: Business Analysis to gather the business requirements for the database; Database Architecting to design the logical design of the database; Database Development to actually build the objects needed by the business logic; finally, Database Administration to keep the database running in top form, and making sure there is a disaster recovery plan. Connect with Shannon Lowder.

Posted in: 
Development
Bookmark and Share

SQL - Select Filtering (Part 2)

In the previous post, I covered the WHERE clause. You should now feel pretty comfortable limiting the number of rows you get to return based on the values in a column. But I'm sure you've already asked "How can I limit based on two different columns?" I'm glad you asked!

You can chain together your WHERE clause predicates by using AND or OR. If you've had any programming experience before you should be pretty familiar with these two. If you choose AND, then both criteria have to be true. If you choose OR, then only one has to be true.

Ok, let’s look at my Person.Contact table again, I want to look at all the Skywalkers in that table.

SELECT
FirstName, LastName
FROM person.Contact
WHERE
LastName = 'Skywalker'

OUTPUT

FirstName  LastName
--------- ---------
Luke Skywalker
Marvin Skywalker
Michael Skywalker

 

Looks like I have 81 Skywalkers in my Person.Contact table. Who knew, they were so prolific? Anyway, the problem with getting all 81, is I only wanted to look at Luke’s record. Since we know the FirstName and the LastName, we can combine them into one WHERE clause using the AND operator.

The AND operator

SELECT
FirstName, LastName
FROM person.Contact
WHERE
LastName = 'Skywalker'
AND FirstName = 'Luke'

OUTPUT

FirstName  LastName
--------- ---------
Luke Skywalker

Now we can see the one record we want. You can chain together as many criteria as you need with the AND clause.

The OR Operator

Like I was saying before, when you use the OR operator, if either of your two tests are true, then the result will be returned. Let’s take our last query and change the AND to an OR, and look at how the results have changed.

SELECT
FirstName, LastName
FROM person.Contact
WHERE
LastName = 'Skywalker'
OR FirstName = 'Luke'

OUTPUT

FirstName  LastName
--------- ---------
Luke Skywalker
Luke Foster
Marvin Skywalker

On my server, I got 133 results. This query returns every record in Person.Contact where the first name is Luke, no matter what the last name. It also shows you every record with Skywalker as the last name, no matter what the first name is. That's what OR does. It returns results that match either criterion.

Combining AND With OR

I'd like to issue a word of caution. When you need to combine AND with OR, please be aware of the order in which the comparisons will be made. This is where I introduce parentheses into my queries. Anyone remember "Please Excuse My Dear Aunt Sally"?

That can help you remember this: Anything in parentheses will be tested first.

When you start chaining together ANDs with ORs, you're going to see results that you don't expect to see. In those cases really study the logic you're sending to the SQL Parser. Should two of them really be considered at the same time?

Let's look at a contrived example. Show me all the products that are yellow or green and cost less than a dollar. You have to really consider that logic. Do you want to see all items that are yellow and less than a dollar and all the items green and less than a dollar? Or do you wish to see all items less than a dollar that are yellow or green?

SELECT
Name, color, ListPrice
FROM Production.product
WHERE
color = 'yellow'
OR color = 'green'
and ListPrice < 1.00

OUTPUT

Name                    color   listprice
--------------------- ------ ---------
Road-550-W Yellow, 38 Yellow 1120.49
Road-550-W Yellow, 40 Yellow 1120.49
Road-550-W Yellow, 42 Yellow 1120.49

The lesson I want you to pick up here is that you have to look at your results and compare them to the logic you intend the server to follow. If your results aren’t matching your intentions, look at the logic you’ve written. You may need to wrap some of your criteron together, so the server understand you.

The server after all is just a machine, and it will do exactly what you tell it, even if you’re not entirely sure what you’re telling it to do.

Conclusion

Logical operators are a fundamental part of developing queries. You'll have to define your instructions to the server is ways the server thinks are unambiguous. This can be a challenge, but with the proper training and patience, you can get the server to return the exact results you want every time. If not, you can always update your query and hit F5 again!

As always, if you have any questions send them in! I'm here to help.

About the Author: 

Look no further for expertise in: Business Analysis to gather the business requirements for the database; Database Architecting to design the logical design of the database; Database Development to actually build the objects needed by the business logic; finally, Database Administration to keep the database running in top form, and making sure there is a disaster recovery plan. Connect with Shannon Lowder.

Posted in: 
Development
Bookmark and Share

SQL 101 - The WHERE clause

SQL 101 - The WHERE clause

See only what you want to see.

After you learn how to get data out of a table with the SELECT command, you’ll soon ask the question, how do I limit the number of results I get back. Well, If you don’t ask the question, your DBA will. I don’t think he or she would like it if you only ever used SELECT without learning the WHERE clause. Adding this clause to your query will let you limit the number of rows the server will return. You’ll be able to identify certain characteristics the results will have in common.

Let’s go back to our copy of AdventureWorks.

This time, we’re going to look at contacts stored in our database. We’re going to use the Person.Contact table. We’re going to look for contacts with the last name Lowder.

SELECT
FirstName, LastName
FROM Person.Contact
WHERE
LastName = 'Lowder'

output

  FirstName  LastName
--------- --------
Shannon Lowder

The WHERE clause will let you limit the number of rows based on any column in the table you’re querying. Rather than showing every first and last name in the table, this query only returns the first and last names for contacts who have the last name lowder. Currently in my database, I only have one record. My record. If there had been any other Lowder in this table, say my parents, or sister, those records would have been returned by this query.

For now, you’ll only be able to limit your results based on the values in the columns. You have many comparison operators you can use. In my above example you saw the equals operator. Let’s show you a few more

Comparison Operators

  • =
  • >, >=
  • <, <=
  • <>, !=
  • LIKE
  • IN (…)
  • BETWEEN

The first four should look familiar if you’ve been through a few math classes, but the last three are specific to SQL. Let’s walk through how to use these.

LIKE

LIKE allows you to do partial matching. There are going to be times where you know only part of the value you’re searching for. Let’s say you know you have a contact whose last name starts with a “s”, but you can’t remember the rest. You can do that look up by using the LIKE operator and a wild card.

SELECT
FirstName, LastName
FROM Person.Contact
WHERE
LastName LIKE 's%'

output

  FirstName  LastName
--------- --------
Margaret Smith
Deanna Sabella
Lane Sacksteder
Peter Saddow
What a Joker!

Wild...like the Joker

There are other records, but I stopped after the 4th, you get the idea. If not… Run this query for yourself.

I mentioned a wild card. If you’ve worked in DOS, you may remember DIR *.txt. The * is the wild card in dos. In Microsoft SQL, the wild card is %. We wanted to see everything where the last name started with “s” and had anything after that.

The LIKE operator can do some pretty advanced things. If you are familiar with regular expressions, you should know that the LIKE comparator works with regular expressions. I really need to cover this in a blog post.

IN

Let’s say you had a list of records you were looking for. For example, you want to find people with a last name of Smith, Jones, or Adams. Pretty common names, right? If you wanted to do this kind of look up you will want to use the IN (…) comparator, you can then list the values you want returned.

SELECT
FirstName, LastName
FROM Person.Contact
WHERE
LastName IN ('Smith','Jones','Adams')

output

  FirstName  LastName
--------- --------
Frances Adams
Margaret Smith
Carla Adams
Jay Adams
Robert Jones

I’ve truncated the results here. But feel free to run this query yourself to see the full results.

When you use the IN clause, you’re not going to have the same flexibility you have with LIKE. So don’t be too disappointed when you switch from LIKE to IN. They both have their uses. Learning when yo use each… that’s the key.

BETWEEN

When you want to search for items that occur in a range of values, you’ll use BETWEEN. I use this a lot when doing date based searches. Such as who was paid BETWEEN ’1/1/2005′ AND ’12/31/2005′. I also use it for value ranges like, show me all the products that have a list price between $2,500 and $5,000.

SELECT
Name, ListPrice
FROM Production.Product
WHERE
ListPrice BETWEEN 2500 and 5000

output

Name                ListPrice
----------------    ---------
Road-150 Red, 62 3578.27
Road-150 Red, 44 3578.27
Road-150 Red, 48 3578.27

Play around with the WHERE clause a bit. If you have any questions, please, feel free to comment below! I’m here to help you grow stronger in the ways of The Force, err… SQL.

About the Author: 

Look no further for expertise in: Business Analysis to gather the business requirements for the database; Database Architecting to design the logical design of the database; Database Development to actually build the objects needed by the business logic; finally, Database Administration to keep the database running in top form, and making sure there is a disaster recovery plan. Connect with Shannon Lowder.

Posted in: 
Development
Bookmark and Share

SQL 101 - Select

The first thing you need to know when learning SQL is how to get data out of a database. This means learning the SELECT command. Using this command will get the SQL server to return data to you. You can use this command to do some simple math, or to do the common "Hello World!" application you all learn the first day of a programming course.

SELECT 1 + 1
> 2


SELECT 'Hello World'
>Hello World


Keeping this in mind can become useful later, when you need to have the server return debugging statements to you during particularly long code writing sessions. Usually you'll need the SELECT command to look up a certain piece of data from a table. The following examples are going to work from the AdventureWorks database. SQL 101You can install this database on your own instance of SQL, or you can contact me and I'll give you access to my test server. This is a new service I'm offering to anyone who wants to learn SQL!

In the AdventureWorks database, there's a table used to store products created/sold by this company. It's called Production.Product. (for now, let's ignore the fact the table name has two parts...I'll explain that later!) In this table there are several columns, one of which is product's name.

If you wanted to get a list of the name for every product in this table, you write the following command:

SELECT
     Name
FROM Production.Product

SQL Table 1

This is just the first 4 items that showed up when I ran the query. You may see different names appear in your list. My data has been updated by myself, and other students accessing this database.

I'd like to point out the data returned is not ordered. In later articles I'll show you how to put the results into any order you may find useful. But for now, Let's stick to learning about the SELECT statement.

What if you wanted to know the name and the price? In SQL you can list as many columns as you like, in a comma separated list.

SELECT
   Name, StandardCost
FROM Production.Products


SQl Table 2

This is the first 4 results I received.

Finally, if you want to return all the data of the table, you can do that too. But I would like to point out using the following command on a table with lots of rows (thousands, or millions) could be very time and processor consuming. Use this only if you know the number of records and columns will be small enough your server and connection can handle. There are also many reasons NOT to use this, most of which have to do with the fact SQL is a shared resource... asking for all the data causes your server to work harder for you, and as a result, there are fewer resources left for other users.

Use the SELECT * queries sparingly!

SELECT *
FROM Production.Products

SQL Table 3

There are more columns off to the right, but I've cleaned up the output to make it easier to read.

As always, if you have any questions, please let me know! I'm here to help you understand SQL better. Let me know how I can do that. Play around with multiple variations

About the Author: 

Look no further for expertise in: Business Analysis to gather the business requirements for the database; Database Architecting to design the logical design of the database; Database Development to actually build the objects needed by the business logic; finally, Database Administration to keep the database running in top form, and making sure there is a disaster recovery plan. Connect with Shannon Lowder.

Posted in: 
Development
Bookmark and Share