CIT 111: Introduction to Databases

Problem Solving

Case Study

CASE STUDY � Apply the principles of problem solving to help create effective queries.

SQL syntax and function syntax can always be looked up. What you cant look up is how exactly to create unique queries. No amount of Googling is going to help you put together a query to get the unique data that your company needs. What will help is good problem-solving skills.

This is the point in the class where query creation can start to get challenging. You can look up different functions and what they do and what parameters you need and in what order, but knowing how to use these functions inside a complex query, can be challenging.

MIT had a good page about problem solving which is where most of the information in this case study came from: http://web.mit.edu/be.400/www/problem.htm

This page states that "Your ability to solve problems will have a great impact on your success in this course as well as in your "real life" endeavors."

"The questions in this course will be different than the exercises to which you are accustomed. In exercise solving, you recall and apply routines that you have previously used to solve questions, but in problem solving you are usually unsure about how to proceed as well as about the connection between the given data and the goal."

Let's look at the steps we can take to become a better problem solver.

Problem solving steps

ENGAGE:

2DEFINE:

EXPLORE:

PLAN:

DO IT:

EVALUATE:

If you learn to use this strategy effectively, you will find it a valuable tool to use for solving new and complex problems.

Practice

Let do a problem together as we go through the problem-solving steps.

We want to know how long its been since our customers orders have shipped. The length of time will be rounded to the nearest year.

  1. So, you just read through it. Dont panic, realize you have all the skills you need to solve this. You just need to go through the problem-solving steps.
  2. Re-read the problem and look for clues or keywords that might help.
    • Youre looking for a length of time between one date and another date.
    • That same length of time will need to be rounded.
    • We know about a function that rounds and one that finds the difference between two dates. Can we use those somehow?
    • We know the customer ship date already from the database as well. We also know what todays date is. Can these be parameters in a function?
  3. Lets play around with one of the customer ship dates to see if what we are thinking might work.
    1. Lets get an estimate. We look at all the customer order dates in the table. The first customer order was way back in January 3rd 2016. So, we can see about how many years its been since then to get an estimate.
    2. Now what function would work for this? We learned a lot of date functions, one to format a date, one to add an interval of time to a date and one of get the difference between two dates. Which would work best?
    3. We dont know the interval of time we need yet, thats what we are trying to figure out. We want to get a number of years as a result not an actual date, so we wont be formatting a date (we will round the result number though). So DATEDIFF will probably be the right function to use.
  4. We know the parameters for DATEDIFF are two dates and it always results in a difference in the number of days between those two dates. We also know that in order to get a positive number of days, we put the later date first. We also know we can get todays date with the NOW function. Lets try that and we can compare our estimate we got earlier for comparison. Also, remember we need years not days. What math would we need to make a number of days into years? Divide by 365 right? Then once we get the year division result it will probably have a decimal place that will need to be rounded up or down. We can then use a ROUND function for that.
  5. Lets try some SQL:
    1. SELECT DATEDIFF(NOW(), shipped_date) FROM cust_order;
    2. The first number we get was 1709. If I divide that by 365 what do I get?
    3. SELECT DATEDIFF(NOW(), shipped_date)/365 FROM cust_order;
    4. Now to round it. We know that one function can be nested inside another function. And the nest function runs first and the outer function will then use the inner function as its parameter. The round function takes it parameter and rounds it.
    5. SELECT ROUND(DATEDIFF(NOW(), shipped_date)/365) FROM cust_order;
  6. Does that new number look like my estimate? Yes? Then we are on the right track. No? then lets rethink our solution and try something else and go through problem-solving steps 4 and 5 again. At this point I might want to think about adding any other columns to clarify which order goes with what rounded year and maybe add an alias to describe the functions we ran.
    SELECT cust_order_id, 
      ROUND(DATEDIFF(NOW(), shipped_date)/365) AS 'Years since product shipped'
    FROM cust_order;

Try a couple more, it will be good practice for your homework.

(using bike)

(using magazine)

Problem solving results

Don't look at the solutions later in this activity until you have gone through the problem-solving steps first. Note that you may have been able to get the right results with different solutions and that is just fine, as long as it worked properly. But also note which was a more efficient solution.

Useful Links: