Join Bill Weinman for an in-depth discussion in this video Creating a simple subselect, part of SQL Essential Training (2014).
Subselects are effectively nested select statements. In SQL the result of the select statement is effectively a table. It may or may not be temporarily written to fix storage, but it can always be used as you would use a table. Because of this, a select statement may be used as a data source in another select statement. This usage is a called a subselect. We'll be using the world.db for this lesson. And I'm going to go ahead and copy and paste some code here from the exercise file for chapter 10.
And I'll paste it in here in our sql box. We're creating a temporary table called T. It has two columns, both of them text columns. And we're inserting some rows into it and then selecting from it. So you can see what it is. And there's our two columns. Each of these columns actually has two pieces of data in it, which we're going to extract with a select statement. The first column has some state code names, some two letter state abbreviations from the United States, and the second column has two letter country code abbreviations, and then there's some random digits after that.
4 digits for each of them. So, I'm going to create a little select statement, actually I'm just going to copy and paste it here from the exercise file. And it looks like this. I'll just reformat it a little bit, so it's a little easier to read. It's a simple SELECT statement and it has four substring calls. A substring call is able to grab a couple of characters out of a string or a subset of characters out of a string. The first one grabs the first two characters from the A column. The second one grabs the rest of the characters from the A column.
The third one grabs the first two characters from the B column. And the fourth one grabs the rest of the characters from the B column and it names them state, S code, country and C code with these as aliases. And so when I press go, you see we now have 4 columns. It's a select statement, but what it gives us, the result of the select statement is effectively a table. So now we're going to use it as such in a query. And so this is going to be a joined query.
We're going to use a joined select with the sub-select on the left, and the country table on the right. And it's going to look like this, co.Name. And that's the name column from the country table. And ss.Ccode. And that's the C code column, this one here from our sub-select. And say from, and then open parentheses. And then the sub-select goes in the open parentheses. We get rid of the semicolon here. And give it a close parenthesis, and say, as ss.
And so now this sub select is being used as if it were a table. And then we say, join Country as co on co. Code 2 equals 'ss.country' and so that's this country column which is the first two characters from the 'b' column in the table and I will give it a semicolon here and when we press go we now have this joint query With a column from the country table and a column from our sub select.
And it's joined on yet a different column from our sub select, with a different column in the country table. So, it's just a regular select statement, where instead of one of the tables, it's actually a sub select. A select statement is being used as if it were a table. So now I'm just going to drop our temporary table to restore the database to its original condition. And we can see that subselects are a convenient way of making your data available in different forms, while keeping your database schema and well organized.
- Understanding SQL terminology and syntax
- Creating new tables and records
- Inserting and updating data
- Writing basic SQL queries
- Sorting and filtering
- Accessing related tables with JOIN
- Working with strings
- Finding the numeric type of a value
- Using aggregate functions and transactions
- Updating a table with triggers
- Creating views
Skill Level Beginner
Q: For Mac OS X: When I try to start the Apache Web Server from the XAMPP control panel, it doesn't start, and when I open "localhost" in my web browser, I see a white screen that says "It Works!" instead of the XAMPP page.
sudo apachectl stop
Q: I'm on a Mac, and I get an error in SID that says "attempt to write a read only database." How can I fix this?
A: This usually means that the database folder does not have sufficient permissions for writing by the web user. This can happen if you create the SQL folder new, rather than copying it from the Exercise Files. Here's how to fix this:
- Open a Finder window and Navigate to /Applications/XAMPP/htdocs/SQL
- Control-click on the SQL folder and select "Get Info" from the context menu.
- Under "Sharing and Permissions" (you may need to open the disclosure triangle), in the "everyone" row, select "Read & Write."Then you can close the Info window.
- Now repeat the process for the three *.db files inside the folder.