Now Is the Time to Learn SQL

Photo by Domenico Loia on Unsplash

I have created a brand new online course on SQL, specifically designed for people who are not database experts. It will give you the knowledge and tools you need to be able to operate on any relational database that allows access to its underlying SQL. Practically all database management systems provide such access. The course is a highly interactive hands-on course. You download a free database management system and solve problems by running SQL queries against a sample database. The course is personally administered by Allen Taylor, the course developer and best-selling author of multiple books on database and SQL. You can discuss each concept and challenge with Allen and with your fellow students.

The course, SQL for Business Analysts is ready to go, but will not be “officially” released until August 1. If you register now, before the official roll-out, you can do so for half the regular price, a 50% discount. Click here to find out more.

Refining Database Retrievals with Comparison Predicates

Photo by Domenico Loia on Unsplash

In a previous post, we saw that, using SQL, it’s easy to retrieve all the data contained in a database table with a simple SELECT statement, such as:

SELECT * FROM customers ;

We also saw that it’s almost as easy to retrieve only what we want from a table, leaving behind all the rest:

SELECT * FROM customers

                WHERE state = ‘CA’ ;

That returns only the customers located in California.

The equals operator (=) is an example of a comparison operator and when two operands are compared with the equals operator, it is called a comparison predicate. A comparison predicate either evaluates to a true value or a false value. A customer is either located in California or she is not.

In addition to the ‘is equal to’ operator, there are five additional comparison operators. They are:

Is not equal to (<>)

Is less than (<)

Is greater than (>)

Is less than or equal to (<=)

Is greater than or equal to (>=)

All six of these operators may be used with numerical operands. It makes sense to say that:

One number is equal to another number

One number is unequal to another number

One number is less than another number

One number is greater than another number

One number is less than or equal to another number

One number is greater than or equal to another number

Similarly, all six operators make sense when applied to dates and times. However, all six do not make sense when applied to text strings. One string may be equal to another string, or unequal to it. However if you try to apply any of the others, such as less than, you will probably not receive the result you are looking for.

Comparison predicates enable you to zero in on the exact information that you want to extract from a database.

Four Myths about SQL

Some people may decide not to learn SQL because of something they may have heard, or just assumed, that is completely untrue. Myths such as these can stand in the way of people moving ahead in their careers. There are number of these myths that hold people back, but I would like to discuss just four of them.

  1. SQL is a programming language and I am not a programmer. Although SQL is a language, it is not a programming language in the way you are probably thinking. Most computer languages are procedural languages. To use them, a programmer creates a procedure in which a series of instructions are written in a step-by-step manner to cause a computer to perform some action. The programmer must understand what is going on at a deep level in order to generate the correct sequence of instructions.

 SQL is not like that. It is not a procedural language. It is called a non-procedural language because there is no need to write a procedure. With SQL all you need to do is write a statement that tells the computer what action you want it to perform. The DBMS figures out the details of how to do that, then goes ahead and does it.

  1. Knowing SQL would be of no value to me since I don’t have a programming job. Although it is true that information technology professionals have the most to do with databases, these days practically everyone in an organization has some exposure to them and may need information contained in them in order to do their jobs. You may not work with a database every day, but occasions will arise when you will need a fact contained in a database and there is no IT professional available to obtain it for you. Anyway, you should be able to perform basic database retrieval operations for yourself. It makes you a more valuable employee.
  2. You need to be some kind of brainiac to understand SQL. There is a mystique surrounding computers in general and SQL in particular that they are beyond the comprehension of ordinary people. This is particularly untrue about SQL, which consists of simple statements that are very similar to ordinary English-language sentences. If you can compose and write down a sentence, you could just as easily write an SQL statement that would perform a query.
  3. Knowing SQL won’t be of any value to me. This is the biggest myth of all. Our world today is totally dependent upon computers and the data stored within them. A lot of jobs will become obsolete and disappear within the next ten years, but jobs associated with information technology will not be among them. Learning SQL could be one of the most effective things that you could do to guarantee your future employability.

Retrieve Data with Simple SELECT Statements

 

Some things that you may want to retrieve from a database can be very easy to obtain with an SQL statement. Other questions might be more involved, and correspondingly require a rather complex SQL statement. Much like English-language sentences, SQL statements can contain multiple clauses that serve to precisely filter out all the data you don’t want, leaving only the information you do want, in the form that you want to see it.

Let’s look at a very simple retrieval operation to answer a simple question, and proceed from there to add clauses to SQL SELECT statements to home in on more tightly targeted questions.

For anyone working for a business or even a non-profit entity, it is probably important that you know as much about your existing customers as possible, so that you can find new customers with similar needs. You can delve into your database’s tables to find out more about your customers and what they have bought in the past. Let’s look at how we could do this with SQL..

After launching your DBMS, connect to a database that contains the information of interest. Take a look at the list of tables included in the database. Suppose one of them is named ‘customer.’

Perhaps the first thing you want to know is the number of customers you have in your customers table. You can answer this question with a simple SELECT statement. Go to the blank window that your DBMS provides, into which you can type SQL statements.

In the window, type:

SELECT * FROM customers ;

The asterisk is a wildcard character that means “all columns.” This will cause the data in all the columns of the customers table to be returned. The semi-colon denotes the end of the statement.

To execute the statement, There should be an icon or button that you can click. Once you click it, your statement will be executed and the result of the operation will be displayed. Along with a list of all the fields in all the rows of your database, there will probably also be a message telling you how many rows have been returned.

Since all you wanted was the number of customers, not a display of all the data for all those customers, there is another way to answer your question, with the statement:

SELECT COUNT (*) FROM customers ;

That returns the number of rows in the customers table.

It’s nice to know how many customers you have, but more useful from a marketing perspective might be to know how many customers you have in a particular region that you could target with advertising. Suppose you want to know how many customers you have in California, for example. A small addition to your original SELECT statement will do the trick. Type:

SELECT * FROM customers

WHERE state = ‘CA’ ;

The WHERE clause returns only those rows where the value in the state column is CA. Text strings such as CA must be enclosed in quote marks for the database engine to understand that it is looking at a text string.

Earlier, we used COUNT to count the number of customers in the customers table COUNT is an example of a Set function. Other set functions are: MAX, MIN, SUM, and AVG. As you would expect, MAX will return the maximum value that exists in the specified column, MIN will return the minimum, SUM will add up all the values, and AVG will return the average value.

Suppose we want to know the total of all the sales recorded in the invoices table of the database. I bet you could figure out what the SQL for that would be. It would be:

SELECT SUM(Total) FROM invoices ;

Execute that statement and you receive the total amount of sales that have been made.

More likely, you are interested in the total sales during an interval of time, for example a month. We can obtain this information by adding a WHERE clause to our statement.

SELECT SUM(Total) FROM invoices

WHERE InvoiceDate > ‘2017-01-31’ AND InvoiceDate < ‘2017-03-01’ ;

This gives us the total sales for the month of February 2017. The AND keyword is a logical connective that enables us to express a compound condition. It only returns rows where both the predicate before the AND keyword and the predicate after the AND keyword are true. A predicate is a statement that may either be logically True or logically False. A date is either greater than February first 2017 or it is not.

Other logical connectives are OR and NOT, although NOT does not connect two predicates. A predicate preceded by a NOT keyword evaluates to True if the predicate itself evaluates to False. A clause with an OR connective is considered to be true if either of the two predicates in contains, evaluates to a true value.

 

 

5 Reasons Why You Need SQL


Some people may decide not to learn SQL because of something they may have heard, or just assumed, that is completely untrue. Myths such as these can stand in the way of people moving ahead in their careers. There are number of these myths that hold people back, but I would like to discuss just four of them.

1. SQL is a programming language and I am not a programmer. Although SQL is a language, it is not a programming language in the way you are probably thinking. Most computer languages are procedural languages. To use them, a programmer creates a procedure in which a series of instructions are written in a step-by-step manner to cause a computer to perform some action. The programmer must understand what is going on at a deep level in order to generate the correct sequence of instructions.

SQL is not like that. It is not a procedural language. It is called a non-procedural language because there is no need to write a procedure. With SQL all you need to do is write a statement that tells the computer what action you want it to perform. The DBMS figures out the details of how to do that, then goes ahead and does it.

2. SQL is only for people who have a programming job. Although it is true that information technology professionals have the most to do with databases, these days practically everyone in an organization has some exposure to them and may need information contained in them in order to do their jobs. You may not work with a database every day, but occasions will arise when you will need a fact contained in a database and there is no IT professional available to obtain it for you. Anyway, you should be able to perform basic database retrieval operations for yourself. It makes you a more valuable employee.

3. You need to be some kind of brainiac to understand SQL. There is a mystique surrounding computers in general and SQL in particular that they are beyond the comprehension of ordinary people. This is particularly untrue about SQL, which consists of simple statements that are very similar to ordinary English-language sentences. If you can compose and write down a sentence, you could just as easily write an SQL statement that would perform a query.

4. Knowing SQL won’t be of any value to me. This is the biggest myth of all. Our world today is totally dependent upon computers and the data stored within them. A lot of jobs will become obsolete and disappear within the next ten years, but jobs associated with information technology will not be among them. Learning SQL could be one of the most effective things that you could do to guarantee your future employability.

What SQL Is and What It Isn’t

SQL is not a procedural language.

Procedural languages such as C++ or Python operate on data items one item at a time. People who program in languages such as C++ and Python write procedures that perform a sequence of operations one after another until an entire task is completed.

SQL is considered a non-procedural language because when you use it, you do not code instructions to perform a sequence of operations. In other words, you do not specify a sequence of operations to perform a task. Instead, you merely tell the DBMS you are using what you want done, and it would proceed to do it without any further instructions from you. If what you want is to retrieve specific information from a set of database records, just specify exactly what you want, and the DBMS will decide how best to satisfy your request, then return the result to you.

Application programmers embed SQL statements in their programs to interact with databases, and use procedural code to construct user interfaces, screens, reports, and program logic. If all you want to do is retrieve some data from a database to answer a question, you don’t need to go to all the trouble of writing a program. You can just feed an SQL statement directly to the DBMS and read out the result when it is returned to you.

For people who are not programmers, and who do not intend to become one, a working knowledge of how to craft SQL queries can be very useful. Even if an application program with embedded SQL exists for a database, questions are bound to arise that were not anticipated when that application program was written. It can be quite valuable to be able to answer such questions quickly and easily with a simple SQL query.

If you think that a basic knowledge of SQL might be helpful to you, click here to find out about a short course that will bring you up to speed.

Lobster Newburg

When you were a kid, did you ever hear anything like this: “Eat everything on your plate! Millions of people are starving in China while you sit there wasting food!” That is something I heard often as a child. My mother grew up during the Depression, and had been taught not to waste anything. She came from good working-class Irish stock, and she learned how to cook from her no-nonsense, meat and potatoes, Irish mother.

I don’t know what experience you might have with Irish cooking, but I’ll tell you what mine was. My grandmother was a master at preparing dinners consisting of roast beef, string beans, and mashed potatoes and gravy.

This was a good thing, because my grandfather LOVED roast beef, string beans, and mashed potatoes and gravy. I figure this must be the official national dinner of Ireland. It seems like every time I visited my grandparents they had roast beef, string beans, and mashed potatoes and gravy for dinner.

So when my mother left home and married my Dad, she knew how to cook one meal: roast beef, string beans, and mashed potatoes and gravy. I have to give my Dad a lot of credit for surviving those early years before she learned how to make meatloaf to serve along with the string beans and mashed potatoes and gravy. At last he had some variety.

After spending my entire youth

eating Gerber’s baby food spinach and Gerber’s baby food squash out of those little jars,

I was really happy the first day I was finally allowed to eat big people’s food.

I thought the string beans and mashed potatoes and gravy were great. The roast beef, however, I chewed and chewed and chewed and chewed into a big wad and then I spit it out.

Mom finally got the idea that a little variety might be good. This was after about five years of nothing but roast beef, string beans, and mashed potatoes and gravy, with an occasional meatloaf thrown in. She talked to one of our Italian neighbors and picked up a new recipe.

She learned how to heat up canned Chef BOYARDEE ravioli. I really liked the ravioli, and it went pretty well with the string beans, and mashed potatoes and gravy. Then she learned how to make spaghetti and meatballs.

They were terrific. Even better, once you filled your plate with spaghetti and meatballs, there was no room left for the string beans and mashed potatoes and gravy.

Flushed with success, she decided to try out even more new meals on her growing family. By this time I had brothers. We all appreciated the spaghetti and meatballs,

the ravioli,

and even the roast beef, string beans and mashed potatoes and gravy,

since we now had it only six nights a week instead of seven like before.

The first new dinner she tried after her spaghetti success was liver and onions with a side of canned lima beans, and of course mashed potatoes and gravy.

This did not go over quite as well with the troops as spaghetti did. However, thanks to Mom and Dad’s Depression-era upbringing, we boys had our plates filled by a parent. Then we were commanded to eat everything on our plates.

It was about this time that my brother Tyson figured out how to upchuck on demand. He would eat a few spoonfuls of mashed potatoes and gravy, look at that liver and onions in front of him, think about how disgusting it was, then put a bite of it in his mouth. Yup. It really was disgusting. Before you knew it, a queasy look would come over his face and BLORRP! He would throw up onto his plate. This of course ruined the rest of his dinner and he would be sent from the table.

The “punishment” of being sent from the table was actually a victory for him. He didn’t have to eat the liver and onions. No such luck for me or for my other brothers. We had to stay at the table until we had eaten every last bite of that liver.

Somehow my mother got the idea that maybe liver and onions might not be the best alternative to roast beef, string beans, and mashed potatoes and gravy. She decided to try something else. Maybe seafood would be more popular. That was when she surprised us all with—Lobster Newburg.

I was surprised all right. The first surprise was the nauseating smell that started wafting from the kitchen about a half hour before dinnertime. The next surprise was the way it looked when I came to the table. I had never been a big fan of casseroles anyway, but this one reached a new low.

A curdled cream sauce, shot through with red speckles, covered lumps of slimy canned white lobster meat. The combination of smells coming from the lobster and the sauce was overpowering. I pinched my nose shut and started breathing through my mouth. Lobster Newburg was the most horrible thing I had ever seen or smelled in my life, let alone put into my mouth.

Not only did I have to see it and smell it—I had to eat it–eat every last bite on my plate. I took a bite and felt the slimy meat quiver in my mouth. I started feeling woooozy. The smell of it and the feel of it in my mouth made my skin crawl. I started to feel faint. At that moment, I knew what Hell must be like. Without even trying, I suddenly understood how Tyson could upchuck on demand. This time I beat him to it.

Mom never served us Lobster Newburg again. And I never complained again about dinners of roast beef, string beans, and mashed potatoes and gravy.