The SQL SELECT Statement

Most tasks you will execute against a database are done with SQL statements. Think of statements as a query, an insert, a change, or a delete operating. For example, to see all of your data, you would select all of the records from a database using the SELECT statement. Then we’ll ask for all, or *, and tell the command to show us where the data is coming from, which is the Customers table. Finally, we’ll be nice and tidy and put a semi-colon at the end; although if you forget, you can always do so after you hit return: SELECT * FROM Customers; As can be seen above, the SELECT statement is used to select data from a database. Results are stored in a result table that is simply called the result-set. The syntax to run a select is to run SELECT followed by a list of columns separated by commas and then a FROM statement to indicate which table of a database you’ll query followed by the name of the table and then a semi-colon (;). SELECT column name,column name FROM table name; The initial SELECT that we ran used an * instead of any column names. This worked because the * is a wild card that pulls all data. As indicated in the above syntax though, we can constrain our output to only the columns we care about. Below is the “Customers” table from our first article: ID Site Contact Address City Zip Country 1 Krypted Charles Edge my house Minneapolis 55418 US 2 Apple Tim Cook spaceship Cupertino 95014 US 3 Microsoft Satya Nadella campus Redmond 98053 US 4 Facebook Mark Zuckerberg foodhall Menlo Park 94025 US 5 JAMF Dean Hager Grain Exchange Minneapolis 55418 US This SQL statement selects the “ID” and “Site” columns from the “Customers” table: SELECT ID,Site FROM Customers; The result would be as follows: ID Site 1 Krypted 2 Apple 3 Microsoft 4 Facebook 5 JAMF In a table, each column can contain duplicate values (for example, multiple parties could be in the same city or multiple items for sale can have the same price. The DISTINCT keyword can be used to return only unique values, similar to the uniq command in bash. The syntax for the DISTINCT statement is as follows: SELECT DISTINCT column,column FROM table; The following SQL statement selects only the distinct values from the “City” columns from the “Customers” table in our example database: SELECT DISTINCT City FROM Customers; You can also search for data The WHERE clause is used to extract only those records that fulfill a specified criterion. SQL WHERE Syntax SELECT column_name,column_name FROM table_name WHERE column_name operator value; SELECT * FROM Customers WHERE Country=’US’; As you can see in the above, we used single quotes around text. We could have also used double-quotes. You do not need to quote numbers, as seen below: SELECT * FROM Customers WHERE ID=1; We used an = operator, but it’s worth noting that there are a number of others that can be super-helpful. The following operators are available when using a WHERE clause:
  • = Equal
  • <>  or != Not equal to
  • > Greater than
  • IN Indicates multiple potential values for a column
  • < Less than
  • >= Greater than or equal
  • <= Less than or equal
  • BETWEEN Between an inclusive range
  • LIKE Looks for a provided pattern

Websites & Hairdressers

When driving by the Aveda Institute one day, my wife pointed out to me that you can always tell how far into the program that the students are. The new hairdressers have long locks of hair, but those further along in the program have their hair get shorter and shorter and shorter. This is seemingly because they keep tweaking their hair through the program until there’s practically nothing left. Websites can turn out to be almost the exact opposite. The more you tweak it the bigger it gets, the longer it takes to load and the less time you have to focus on content. I’ve added a bunch of new stuff to over the past week. I’ve now gone and taken much of it back out so that the site will load quickly. I redesigned each of the graphics. But now I’ve also gone through and trimmed those same graphics. Like a hairdresser, I’m feeling practically bald, having tweaked the site to no end and in the process pulled out most of my hair. Ergo, the new design is done. I may nuke and pave it, starting from scratch, but for now, the new theme is done. Finally, if anyone is interested in the theme then I’ll post it here if/when I do nuke and pave the site.

More Additions

At the bottom of the sidebar, you will now find badges for my pages on the main social networks. I also added Flickr & Facebook into a new Pictures page (and added pictures to the actual Flickr account since I mostly used Facebook for that)… Also changed the title and post-info/comments sections to the dark red color scheme, since I didn’t really think they looked right as black any longer. Finally, made the navbar a little slimmer. Hope ya’ll like the changes.