Skip to main content

Unions

Again using the sample database available via the docker images from the first post in this series, lets cover unions. When using a union, you can combine results from two result sets into a single result set. This might be useful during reporting, or having your application make one call to your database instead of multiple, avoiding what might be an unnecessary round trip over the network to your database.

Subqueries

Continuing with the sample database available via the docker images from the first post in this series, let’s look at some examples demonstrating the syntax of subqueries. A subquery is where you have a select statement nested inside of another query. You might utilize this approach based on performance tuning, the readability of your queries, or out of necessity based on your database schemas. A discussion of performance tuning is out of scope of this post.

Aggregate Queries

Next in the SQL for the uninterested series, we’ll take a dive into aggregating your data, using some group by clauses. We’re still using the sample database available via the docker images from the first post in this series. The simplest aggregate query would be to get a count of the number of rows in a given table. We’ll start with a query to count the number of rows in the sales order table.

Join Now!

Continuing with the “SQL for the Uninterested” posts, let’s talk about joining two or more tables together to produce a single result set. There are 4 kinds of joins. To discuss these, let’s use the customer and address tables from our sample database. Join (aka Inner Join) Let’s start with our sales order data, writing a simple query. select * from sales_order; Data from the sales_order table Now what if we wanted to view the customer or address details?

Oh, Crud!

CRUD is an acronym for create, read, update, and delete. Given you already have a sample database populated with data, let’s start with some simple reads using the select statement. A simple select statement has the basic format of: SELECT column1, column2, … FROM some_table; Or to select all columns, use simply: SELECT * FROM some_table; Note the keywords are not case sensitive. Now let’s get our hands on some data.

SQL for the Uninterested

Ok, so maybe you’re not totally uninterested in SQL if you’re reading this. But your true passion lies in something else, like working on your hot new javascript project or whatever, and you really just need to brush up on a little SQL to knock out a quick task and then get back to the work you really love. If this describes you, the SQL for the Uninterested series was written for you.