FROM: Getting Data FROM a Table

In our last module, we got data from thin air, but now we need to get something a little more useful.

StackOverflow Example of FROM

Check out the Tags page at https://StackOverflow.com/tags:

StackOverflow's Tags page
StackOverflow’s Tags page

Tags can be applied to any question to help people filter for just the questions they’re the most interested in. This query lists the contents of the Tags table:

In the last lesson, we specifically named the fields we wanted like FirstName and LastName. When we just want all of the columns from a table, we use the wildcard *.

Run this query at Data.StackExchange.com, and you get:

StackOverflow Tags Results
StackOverflow Tags Results

There’s a couple of interesting concepts we need to cover here.

The “dbo.” part of the query is the schema name – in this case, database owner. When databases are first created, dbo is the default owner of everything. It’s good practice for performance to fully qualify our object names. We could have just said:

And it would have worked here, since our database only has one Tags table and it’s in our default schema, but we’re going to train you the right way from the get-go.

Notice that the rows aren’t necessarily in order. In my screenshot above, they’re sure not organized alphabetically by TagName, nor by Id (assuming that there may be some missing Ids in that list.) If we want them in order, we’re going to have to tell SQL Server that. Order isn’t guaranteed unless we specifically ask for an order.

SQL Server automatically used the column names for us. This was designed by whoever first designed the table – they picked the column names. By using SELECT *, we just got the column names as-is. This works fine for now – but you might want to override some of the column names. More on that later.

Now, you try.

Click Users at the top of StackOverflow, and check out the list of users:

StackOverflow users list
StackOverflow users list

Write a query to show the users list.

When you’re done with that, move on to the next lesson.