Including or Excluding Columns

Visual Database Tools

Visual Database Tools

Including or Excluding Columns

You can choose which columns appear in a query result.  When choosing which columns to include, there are several things to keep in mind:

  • You can include all of a table's columns   For example, you can include everything about each employee.  The resulting SQL looks like this:
    SELECT *
    FROM employee
    
  • You can include exactly the columns you want   For example, you can list the name of all the employees.  The resulting SQL looks like this:
    SELECT fname, minit, lname
    FROM employee
    

    The list of columns you include might not even include a column from every table in the query.  This does not mean that the table does not contribute to the query.  For an example of a query that uses a table without including any of that table's columns, see Using a Table Twice in One Query.

  • You can include all columns from all tables   For example, when you combine data from the sales and stores tables, you can include every column from either table in the result.  The resulting SQL might look like this:
    SELECT     *
    FROM         sales INNER JOIN
    stores ON sales.stor_id = stores.stor_id
    
  • You can include derived columns   That is, you can include columns that are not part of any database table of the query.  For example, you can create a result set containing the job description and the average job level for each job.  The resulting SQL might look like this:
    SELECT    job_desc, (max_lvl + min_lvl) / 2 
    FROM         jobs
    

    You can use SQL syntax to define the derived column (as in the preceding sample query) or you can employ a user-defined function that returns a scalar value. 

For more information on including columns in a query result, see Adding Columns.  For more information on user-defined functions, see User-Defined Functions.

See Also

Structure of Retrieval Queries