The Basics of SQL With Google Cloud
By: Cyber Dessy
Tags: Google Cloud Platform, Gcp, Bigquery Sql, Sql, Bigquery

Databases and Tables

SQL allows you to get information from “structured datasets”. Structured datasets have clear rules and formatting and often times are organized into tables, or data that’s formatted in rows and columns.

An example of unstructured data would be an image file. Unstructured data is inoperable with SQL and cannot be stored in BigQuery datasets or tables (at least natively.) To work with image data (for instance), you would use a service like Cloud Vision, perhaps through its API directly.

The following is an example of a structured dataset — a simple table:

If you’ve had experience with Google Sheets, then the above should look quite similar. As we see, the table has columns for User, Price, and Shipped and two rows that are composed of filled in column values.

A Database is essentially a collection of one or more tables. SQL is a structured database management tool, but quite often you will be running queries on one or a few tables joined together — not on whole databases.

SELECT and FROM

SQL is phonetic by nature and before running a query, it’s always helpful to first figure out what question you want to ask your data (unless you’re just exploring for fun.)

SQL has predefined keywords which you use to translate your question into the pseudo-english SQL syntax so you can get the database engine to return the answer you want.

The most essential keywords are SELECT and FROM:

· Use SELECT to specify what fields you want to pull from your dataset.

· Use FROM to specify what table or tables we want to pull our data from.

An example may help understanding. Assume that we have the following table example_table, which has columns USER, PRICE, and SHIPPED:

And let’s say that we want to just pull the data that’s found in the USER column. We can do this by running the following query that uses SELECT and FROM:

SELECT USER FROM example_table

If we executed the above command, we would select all the names from the USER column that are found in example_table.

You can also select multiple columns with the SQL SELECT keyword. Say that you want to pull the data that’s found in the USER and SHIPPED columns. To do this, modify the previous query by adding another column value to our SELECT query (making sure it’s separated by a comma!):

SELECT USER, SHIPPED FROM example_table

Running the above retrieves the USER and the SHIPPED data from memory:

And just like that you’ve covered two fundamental SQL keywords! Now to make things a bit more interesting.

WHERE

The WHERE keyword is another SQL command that filters tables for specific column values. Say that you want to pull the names from example_table whose packages were shipped. You can supplement the query with a WHERE, like the following:

SELECT USER FROM example_table WHERE SHIPPED=’YES’

Running the above returns all USERs whose packages have been SHIPPED from memory:

Now that you have a baseline understanding of SQL’s core keywords, apply what you’ve learned by running these types of queries in the BigQuery console on your Google Cloud Platform or Qwiklabs.