Retrieve specific columns from a table

Question

You want to print out a list of all of the facilities and their cost to members. How would you retrieve a list of only facility names and costs?

Expected Results

namemembercost
Tennis Court 15
Tennis Court 25
Badminton Court0
Table Tennis0
Massage Room 135
Massage Room 235
Squash Court3.5
Snooker Table0
Pool Table0

Your Answer

Your Results

Loading database...

Answers and Discussion

select name, membercost from cd.facilities;

For this question, we need to specify the columns that we want. We can do that with a simple comma-delimited list of column names specified to the select statement. All the database does is look at the columns available in the FROM clause, and return the ones we asked for, as illustrated below

Generally speaking, for non-throwaway queries it's considered desirable to specify the names of the columns you want in your queries rather than using *. This is because your application might not be able to cope if more columns get added into the table.