Join Bill Weinman for an in-depth discussion in this video Selecting columns, part of SQL Essential Training.
A given table may have a lot of columns, and you'll often only be interested in some of them. Fortunately, selecting particular columns is easy. Here's how it's done. Here I'm going to select the world.db database here in the SID application in my browser. And I'm going to type in some SQL here. Select all the columns from the country table. And I'm going to order it by code. And I'm going to limit our result to five rows.
And press Go. And there, we have five rows of data. And you see, they're ordered by code. And it includes all of the columns from the country table. The asterisk is in the place where you list the columns you want returned in the query. The asterisk is simply a wild card that means all of the columns. You can select particular columns from the table by listing them in the SELECT statement in place of the asterisk, like this. I'll select the name, code, region, and population.
And you see that my list is separated by commas, and there's no comma at the end of the list. And when I press Go, now I get those same rows, and I just get the columns that I've listed there, in the SELECT statement. If you'd like to return different names in your column headers, you may do so with the AS clause, after the column name, like this. I can say Name, AS Country. And I can say Code AS ISO. Because this is the ISO 3166 three letter country code that we're returning there.
And I can say population as Pop because population is big word. And I maybe just want something short there. And now when I press Go, you'll see that my column names have changed. I have Country, ISO, Region, and Pop, so that might be easier to read. The column headers are actually the names returned by the database interface. So they're commonly used in your code. They're also used in SQL to specify intermediate results, so the AS clause can be important in preventing main space collisions.
You'll see more examples of this later in the course. You'll also notice that the word AS is actually optional. If I take it out here, you see that it still works the same, and you'll often see code like this without the word AS. I like to include the word AS, because it makes it more clear. This can sometimes be hard to read, and you're not really sure if that's a typo, there's suppose to be a comma there, and it's another column name, or something like that. So I like to include the word AS, like that.
And I'll press Go, and you see that that works exactly the same. It's easy to select particular columns from a table in SQL. This is a feature you'll use a lot and you'll see a lot in this course.
- 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.